comparison mercurial/win32.py @ 16807:80142f385af9

win32: move lookupreg() to windows.py lookupreg() doesn't use the win32 API directly any more, it uses the Python standard library module _winreg.
author Adrian Buehlmann <adrian@cadifra.com>
date Sun, 27 May 2012 11:29:52 +0200
parents 186049f70026
children 6fc7fd72ba3e
comparison
equal deleted inserted replaced
16806:186049f70026 16807:80142f385af9
3 # Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others 3 # Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others
4 # 4 #
5 # This software may be used and distributed according to the terms of the 5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version. 6 # GNU General Public License version 2 or any later version.
7 7
8 import encoding 8 import ctypes, errno, os, subprocess, random
9 import ctypes, errno, os, subprocess, random, _winreg
10 9
11 _kernel32 = ctypes.windll.kernel32 10 _kernel32 = ctypes.windll.kernel32
12 _advapi32 = ctypes.windll.advapi32 11 _advapi32 = ctypes.windll.advapi32
13 _user32 = ctypes.windll.user32 12 _user32 = ctypes.windll.user32
14 13
242 return status.value == _STILL_ACTIVE 241 return status.value == _STILL_ACTIVE
243 finally: 242 finally:
244 _kernel32.CloseHandle(h) 243 _kernel32.CloseHandle(h)
245 return _kernel32.GetLastError() != _ERROR_INVALID_PARAMETER 244 return _kernel32.GetLastError() != _ERROR_INVALID_PARAMETER
246 245
247 def lookupreg(key, valname=None, scope=None):
248 ''' Look up a key/value name in the Windows registry.
249
250 valname: value name. If unspecified, the default value for the key
251 is used.
252 scope: optionally specify scope for registry lookup, this can be
253 a sequence of scopes to look up in order. Default (CURRENT_USER,
254 LOCAL_MACHINE).
255 '''
256 if scope is None:
257 scope = (_winreg.HKEY_CURRENT_USER, _winreg.HKEY_LOCAL_MACHINE)
258 elif not isinstance(scope, (list, tuple)):
259 scope = (scope,)
260 for s in scope:
261 try:
262 val = _winreg.QueryValueEx(_winreg.OpenKey(s, key), valname)[0]
263 # never let a Unicode string escape into the wild
264 return encoding.tolocal(val.encode('UTF-8'))
265 except EnvironmentError:
266 pass
267
268 def executablepath(): 246 def executablepath():
269 '''return full path of hg.exe''' 247 '''return full path of hg.exe'''
270 size = 600 248 size = 600
271 buf = ctypes.create_string_buffer(size + 1) 249 buf = ctypes.create_string_buffer(size + 1)
272 len = _kernel32.GetModuleFileNameA(None, ctypes.byref(buf), size) 250 len = _kernel32.GetModuleFileNameA(None, ctypes.byref(buf), size)