comparison mercurial/util_win32.py @ 6880:892806b3fc0f

util: disinfect lookup_reg strings (issue1126) lookup_reg could return Unicode strings, which would infect other strings and generate unexpected tracebacks. Spotted by R?my Roy. Fold in silly nested function while we're at it.
author Matt Mackall <mpm@selenic.com>
date Sun, 10 Aug 2008 22:55:00 -0500
parents a88259018f79
children d2375bbee6d4
comparison
equal deleted inserted replaced
6879:24fd94ed1cc0 6880:892806b3fc0f
199 from _winreg import HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, \ 199 from _winreg import HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, \
200 QueryValueEx, OpenKey 200 QueryValueEx, OpenKey
201 except ImportError: 201 except ImportError:
202 return None 202 return None
203 203
204 def query_val(scope, key, valname):
205 try:
206 keyhandle = OpenKey(scope, key)
207 return QueryValueEx(keyhandle, valname)[0]
208 except EnvironmentError:
209 return None
210
211 if scope is None: 204 if scope is None:
212 scope = (HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE) 205 scope = (HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE)
213 elif not isinstance(scope, (list, tuple)): 206 elif not isinstance(scope, (list, tuple)):
214 scope = (scope,) 207 scope = (scope,)
215 for s in scope: 208 for s in scope:
216 val = query_val(s, key, valname) 209 try:
217 if val is not None: 210 val = QueryValueEx(OpenKey(scope, key), valname)[0]
218 return val 211 # never let a Unicode string escape into the wild
212 return util.tolocal(val.encode('UTF-8'))
213 except EnvironmentError:
214 pass
219 215
220 def system_rcpath_win32(): 216 def system_rcpath_win32():
221 '''return default os-specific hgrc search path''' 217 '''return default os-specific hgrc search path'''
222 proc = win32api.GetCurrentProcess() 218 proc = win32api.GetCurrentProcess()
223 try: 219 try: