| 1 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 2 |
from rpython.rlib import rwinreg, rwin32, rstring |
| 3 |
from rpython.rlib.rarithmetic import r_uint, r_ulonglong, intmask |
| 4 |
from rpython.rlib.buffer import ByteBuffer |
| 5 |
from rpython.rlib.rutf8 import check_utf8 |
| 6 |
|
| 7 |
from pypy.interpreter.baseobjspace import W_Root, BufferInterfaceNotFound |
| 8 |
from pypy.interpreter.gateway import interp2app, unwrap_spec |
| 9 |
from pypy.interpreter.typedef import TypeDef, GetSetProperty |
| 10 |
from pypy.interpreter.error import OperationError, oefmt, wrap_oserror |
| 11 |
from pypy.interpreter.unicodehelper import ( |
| 12 |
str_decode_utf_16, utf8_encode_utf_16) |
| 13 |
from pypy.module._codecs.interp_codecs import CodecState |
| 14 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
eci = ExternalCompilationInfo( |
| 20 |
includes=['windows.h'], |
| 21 |
post_include_bits=[ |
| 22 |
"RPY_EXTERN LONG\n" |
| 23 |
"pypy_RegChangeReflectionKey(FARPROC address, HKEY key);\n" |
| 24 |
"RPY_EXTERN LONG\n" |
| 25 |
"pypy_RegQueryReflectionKey(FARPROC address, HKEY key, LPBOOL isDisabled);\n" |
| 26 |
"RPY_EXTERN LONG\n" |
| 27 |
"pypy_RegDeleteKeyExW(FARPROC address, HKEY key, LPCWSTR subkey,\n" |
| 28 |
" REGSAM sam, DWORD reserved);\n" |
| 29 |
], |
| 30 |
separate_module_sources=[''' |
| 31 |
LONG |
| 32 |
pypy_RegChangeReflectionKey(FARPROC address, HKEY key) { |
| 33 |
LONG (WINAPI *func)(HKEY); |
| 34 |
*(FARPROC*)&func = address; |
| 35 |
return func(key); |
| 36 |
} |
| 37 |
|
| 38 |
LONG |
| 39 |
pypy_RegQueryReflectionKey(FARPROC address, HKEY key, LPBOOL isDisabled) { |
| 40 |
LONG (WINAPI *func)(HKEY, LPBOOL); |
| 41 |
*(FARPROC*)&func = address; |
| 42 |
return func(key, isDisabled); |
| 43 |
} |
| 44 |
|
| 45 |
LONG |
| 46 |
pypy_RegDeleteKeyExW(FARPROC address, HKEY key, LPCWSTR subkey, |
| 47 |
REGSAM sam, DWORD reserved) { |
| 48 |
LONG (WINAPI *func)(HKEY, LPCWSTR, REGSAM, DWORD); |
| 49 |
*(FARPROC*)&func = address; |
| 50 |
return func(key, subkey, sam, reserved); |
| 51 |
} |
| 52 |
'''], |
| 53 |
) |
| 54 |
pypy_RegChangeReflectionKey = rffi.llexternal( |
| 55 |
'pypy_RegChangeReflectionKey', |
| 56 |
[rffi.VOIDP, rwinreg.HKEY], |
| 57 |
rffi.LONG, compilation_info=eci) |
| 58 |
|
| 59 |
pypy_RegQueryReflectionKey = rffi.llexternal( |
| 60 |
'pypy_RegQueryReflectionKey', |
| 61 |
[rffi.VOIDP, rwinreg.HKEY, rwin32.LPBOOL], |
| 62 |
rffi.LONG, compilation_info=eci) |
| 63 |
|
| 64 |
pypy_RegDeleteKeyExW = rffi.llexternal( |
| 65 |
'pypy_RegDeleteKeyExW', |
| 66 |
[rffi.VOIDP, rwinreg.HKEY, rffi.CWCHARP, rwinreg.REGSAM, rwin32.DWORD], |
| 67 |
rffi.LONG, compilation_info=eci) |
| 68 |
|
| 69 |
|
| 70 |
def raiseWindowsError(space, errcode, context): |
| 71 |
message = rwin32.FormatErrorW(errcode) |
| 72 |
w_errcode = space.newint(errcode) |
| 73 |
w_t = space.newtuple([w_errcode, space.newtext(*message), |
| 74 |
space.w_None, w_errcode]) |
| 75 |
raise OperationError(space.w_WindowsError, w_t) |
| 76 |
|
| 77 |
|
| 78 |
class W_HKEY(W_Root): |
| 79 |
def __init__(self, space, hkey): |
| 80 |
self.hkey = hkey |
| 81 |
self.space = space |
| 82 |
self.register_finalizer(space) |
| 83 |
|
| 84 |
def _finalize_(self): |
| 85 |
|
| 86 |
try: |
| 87 |
self.Close(self.space) |
| 88 |
except: |
| 89 |
pass |
| 90 |
|
| 91 |
def as_int(self): |
| 92 |
return rffi.cast(rffi.SIZE_T, self.hkey) |
| 93 |
|
| 94 |
def descr_bool(self, space): |
| 95 |
return space.newbool(self.as_int() != 0) |
| 96 |
|
| 97 |
def descr_handle_get(self, space): |
| 98 |
return space.newint(self.as_int()) |
| 99 |
|
| 100 |
def descr_repr(self, space): |
| 101 |
return space.newtext("<PyHKEY:0x%x>" % (self.as_int(),)) |
| 102 |
|
| 103 |
def descr_int(self, space): |
| 104 |
return space.newint(self.as_int()) |
| 105 |
|
| 106 |
def descr__enter__(self, space): |
| 107 |
return self |
| 108 |
|
| 109 |
def descr__exit__(self, space, __args__): |
| 110 |
CloseKey(space, self) |
| 111 |
|
| 112 |
def Close(self, space): |
| 113 |
"""key.Close() - Closes the underlying Windows handle. |
| 114 |
If the handle is already closed, no error is raised.""" |
| 115 |
CloseKey(space, self) |
| 116 |
|
| 117 |
def Detach(self, space): |
| 118 |
"""int = key.Detach() - Detaches the Windows handle from the handle object. |
| 119 |
|
| 120 |
The result is the value of the handle before it is detached. If the |
| 121 |
handle is already detached, this will return zero. |
| 122 |
|
| 123 |
After calling this function, the handle is effectively invalidated, |
| 124 |
but the handle is not closed. You would call this function when you |
| 125 |
need the underlying win32 handle to exist beyond the lifetime of the |
| 126 |
handle object.""" |
| 127 |
key = self.as_int() |
| 128 |
self.hkey = rwin32.NULL_HANDLE |
| 129 |
w_ret = space.newint(key) |
| 130 |
space.audit("winreg.PyHKEY.Detach", [w_ret]) |
| 131 |
return w_ret |
| 132 |
|
| 133 |
|
| 134 |
@unwrap_spec(key=int) |
| 135 |
def new_HKEY(space, w_subtype, key): |
| 136 |
hkey = rffi.cast(rwinreg.HKEY, key) |
| 137 |
return W_HKEY(space, hkey) |
| 138 |
|
| 139 |
|
| 140 |
descr_HKEY_new = interp2app(new_HKEY) |
| 141 |
|
| 142 |
|
| 143 |
W_HKEY.typedef = TypeDef( |
| 144 |
"winreg.HKEYType", |
| 145 |
__doc__="""\ |
| 146 |
PyHKEY Object - A Python object, representing a win32 registry key. |
| 147 |
|
| 148 |
This object wraps a Windows HKEY object, automatically closing it when |
| 149 |
the object is destroyed. To guarantee cleanup, you can call either |
| 150 |
the Close() method on the PyHKEY, or the CloseKey() method. |
| 151 |
|
| 152 |
All functions which accept a handle object also accept an integer - |
| 153 |
however, use of the handle object is encouraged. |
| 154 |
|
| 155 |
Functions: |
| 156 |
Close() - Closes the underlying handle. |
| 157 |
Detach() - Returns the integer Win32 handle, detaching it from the object |
| 158 |
|
| 159 |
Properties: |
| 160 |
handle - The integer Win32 handle. |
| 161 |
|
| 162 |
Operations: |
| 163 |
__bool__ - Handles with an open object return true, otherwise false. |
| 164 |
__int__ - Converting a handle to an integer returns the Win32 handle. |
| 165 |
__cmp__ - Handle objects are compared using the handle value.""", |
| 166 |
__new__=descr_HKEY_new, |
| 167 |
__repr__=interp2app(W_HKEY.descr_repr), |
| 168 |
__int__=interp2app(W_HKEY.descr_int), |
| 169 |
__bool__=interp2app(W_HKEY.descr_bool), |
| 170 |
__enter__=interp2app(W_HKEY.descr__enter__), |
| 171 |
__exit__=interp2app(W_HKEY.descr__exit__), |
| 172 |
handle=GetSetProperty(W_HKEY.descr_handle_get), |
| 173 |
Close=interp2app(W_HKEY.Close), |
| 174 |
Detach=interp2app(W_HKEY.Detach), |
| 175 |
) |
| 176 |
|
| 177 |
|
| 178 |
def hkey_w(w_hkey, space): |
| 179 |
if space.is_w(w_hkey, space.w_None): |
| 180 |
raise oefmt(space.w_TypeError, |
| 181 |
"None is not a valid HKEY in this context") |
| 182 |
elif isinstance(w_hkey, W_HKEY): |
| 183 |
return w_hkey.hkey |
| 184 |
elif space.isinstance_w(w_hkey, space.w_int): |
| 185 |
if space.is_true(space.lt(w_hkey, space.newint(0))): |
| 186 |
return rffi.cast(rwinreg.HKEY, space.int_w(w_hkey)) |
| 187 |
return rffi.cast(rwinreg.HKEY, space.uint_w(w_hkey)) |
| 188 |
else: |
| 189 |
raise oefmt(space.w_TypeError, "The object is not a PyHKEY object") |
| 190 |
|
| 191 |
|
| 192 |
def CloseKey(space, w_hkey): |
| 193 |
"""CloseKey(hkey) - Closes a previously opened registry key. |
| 194 |
|
| 195 |
The hkey argument specifies a previously opened key. |
| 196 |
|
| 197 |
Note that if the key is not closed using this method, it will be |
| 198 |
closed when the hkey object is destroyed by Python.""" |
| 199 |
hkey = hkey_w(w_hkey, space) |
| 200 |
if hkey: |
| 201 |
ret = rwinreg.RegCloseKey(hkey) |
| 202 |
if ret != 0: |
| 203 |
raiseWindowsError(space, ret, 'RegCloseKey') |
| 204 |
if isinstance(w_hkey, W_HKEY): |
| 205 |
space.interp_w(W_HKEY, w_hkey).hkey = rwin32.NULL_HANDLE |
| 206 |
|
| 207 |
|
| 208 |
def FlushKey(space, w_hkey): |
| 209 |
"""FlushKey(key) - Writes all the attributes of a key to the registry. |
| 210 |
|
| 211 |
key is an already open key, or any one of the predefined HKEY_* constants. |
| 212 |
|
| 213 |
It is not necessary to call RegFlushKey to change a key. |
| 214 |
Registry changes are flushed to disk by the registry using its lazy flusher. |
| 215 |
Registry changes are also flushed to disk at system shutdown. |
| 216 |
Unlike CloseKey(), the FlushKey() method returns only when all the data has |
| 217 |
been written to the registry. |
| 218 |
An application should only call FlushKey() if it requires absolute certainty |
| 219 |
that registry changes are on disk. |
| 220 |
If you don't know whether a FlushKey() call is required, it probably isn't.""" |
| 221 |
hkey = hkey_w(w_hkey, space) |
| 222 |
if hkey: |
| 223 |
ret = rwinreg.RegFlushKey(hkey) |
| 224 |
if ret != 0: |
| 225 |
raiseWindowsError(space, ret, 'RegFlushKey') |
| 226 |
|
| 227 |
|
| 228 |
def LoadKey(space, w_hkey, w_subkey, w_filename): |
| 229 |
"""LoadKey(key, sub_key, file_name) - Creates a subkey under the specified key |
| 230 |
and stores registration information from a specified file into that subkey. |
| 231 |
|
| 232 |
key is an already open key, or any one of the predefined HKEY_* constants. |
| 233 |
sub_key is a string that identifies the sub_key to load |
| 234 |
file_name is the name of the file to load registry data from. |
| 235 |
This file must have been created with the SaveKey() function. |
| 236 |
Under the file allocation table (FAT) file system, the filename may not |
| 237 |
have an extension. |
| 238 |
|
| 239 |
A call to LoadKey() fails if the calling process does not have the |
| 240 |
SE_RESTORE_PRIVILEGE privilege. |
| 241 |
|
| 242 |
If key is a handle returned by ConnectRegistry(), then the path specified |
| 243 |
in fileName is relative to the remote computer. |
| 244 |
|
| 245 |
The docs imply key must be in the HKEY_USER or HKEY_LOCAL_MACHINE tree""" |
| 246 |
|
| 247 |
hkey = hkey_w(w_hkey, space) |
| 248 |
space.audit("winreg.LoadKey", [ |
| 249 |
space.newint(rffi.cast(rffi.SIZE_T, hkey)), |
| 250 |
w_subkey, w_filename]) |
| 251 |
utf8 = space.utf8_w(w_subkey) |
| 252 |
uni_len = space.len_w(w_subkey) |
| 253 |
with rffi.scoped_utf82wcharp(utf8, uni_len) as wide_subkey: |
| 254 |
c_subkey = rffi.cast(rffi.CWCHARP, wide_subkey) |
| 255 |
utf8 = space.utf8_w(w_filename) |
| 256 |
uni_len = space.len_w(w_filename) |
| 257 |
with rffi.scoped_utf82wcharp(utf8, uni_len) as wide_filename: |
| 258 |
c_filename = rffi.cast(rffi.CWCHARP, wide_filename) |
| 259 |
ret = rwinreg.RegLoadKeyW(hkey, c_subkey, c_filename) |
| 260 |
if ret != 0: |
| 261 |
raiseWindowsError(space, ret, 'RegLoadKey') |
| 262 |
|
| 263 |
|
| 264 |
def SaveKey(space, w_hkey, w_filename): |
| 265 |
""" |
| 266 |
SaveKey(key, file_name) - Saves the specified key, and all its subkeys to the |
| 267 |
specified file. |
| 268 |
|
| 269 |
key is an already open key, or any one of the predefined HKEY_* constants. |
| 270 |
file_name is the name of the file to save registry data to. |
| 271 |
This file cannot already exist. If this filename includes an extension, |
| 272 |
it cannot be used on file allocation table (FAT) file systems by the |
| 273 |
LoadKey(), ReplaceKey() or RestoreKey() methods. |
| 274 |
|
| 275 |
If key represents a key on a remote computer, the path described by |
| 276 |
file_name is relative to the remote computer. |
| 277 |
The caller of this method must possess the SeBackupPrivilege security |
| 278 |
privilege. This function passes NULL for security_attributes to the API.""" |
| 279 |
hkey = hkey_w(w_hkey, space) |
| 280 |
space.audit("winreg.SaveKey", [ |
| 281 |
space.newint(rffi.cast(rffi.SIZE_T, hkey)), |
| 282 |
w_filename]) |
| 283 |
utf8 = space.utf8_w(w_filename) |
| 284 |
uni_len = space.len_w(w_filename) |
| 285 |
with rffi.scoped_utf82wcharp(utf8, uni_len) as wide_filename: |
| 286 |
c_filename = rffi.cast(rffi.CWCHARP, wide_filename) |
| 287 |
ret = rwinreg.RegSaveKeyW(hkey, c_filename, None) |
| 288 |
if ret != 0: |
| 289 |
raiseWindowsError(space, ret, 'RegSaveKey') |
| 290 |
|
| 291 |
|
| 292 |
@unwrap_spec(typ=int) |
| 293 |
def SetValue(space, w_hkey, w_subkey, typ, w_value): |
| 294 |
""" |
| 295 |
SetValue(key, sub_key, type, value) - Associates a value with a specified key. |
| 296 |
|
| 297 |
key is an already open key, or any one of the predefined HKEY_* constants. |
| 298 |
sub_key is a string that names the subkey with which the value is associated. |
| 299 |
type is an integer that specifies the type of the data. Currently this |
| 300 |
must be REG_SZ, meaning only strings are supported. |
| 301 |
value is a string that specifies the new value. |
| 302 |
|
| 303 |
If the key specified by the sub_key parameter does not exist, the SetValue |
| 304 |
function creates it. |
| 305 |
|
| 306 |
Value lengths are limited by available memory. Long values (more than |
| 307 |
2048 bytes) should be stored as files with the filenames stored in |
| 308 |
the configuration registry. This helps the registry perform efficiently. |
| 309 |
|
| 310 |
The key identified by the key parameter must have been opened with |
| 311 |
KEY_SET_VALUE access.""" |
| 312 |
if typ != rwinreg.REG_SZ: |
| 313 |
raise oefmt(space.w_ValueError, "Type must be winreg.REG_SZ") |
| 314 |
hkey = hkey_w(w_hkey, space) |
| 315 |
state = space.fromcache(CodecState) |
| 316 |
errh = state.encode_error_handler |
| 317 |
utf8 = space.utf8_w(w_subkey) |
| 318 |
space.audit("winreg.SetValue", [ |
| 319 |
space.newint(rffi.cast(rffi.SIZE_T, hkey)), |
| 320 |
w_subkey, space.newint(typ), w_value]) |
| 321 |
s = utf8 + '\x00' |
| 322 |
w_s = space.newtext(s) |
| 323 |
subkeyW = utf8_encode_utf_16(space, s, w_s, 'strict', errh, allow_surrogates=True) |
| 324 |
utf8 = space.utf8_w(w_value) |
| 325 |
valueW = utf8_encode_utf_16(space, s, w_s, 'strict', errh, allow_surrogates=True) |
| 326 |
valueL = space.len_w(w_value) |
| 327 |
|
| 328 |
|
| 329 |
with rffi.scoped_nonmovingbuffer(subkeyW) as subkeyP0: |
| 330 |
subkeyP = rffi.cast(rffi.CWCHARP, rffi.ptradd(subkeyP0, 2)) |
| 331 |
with rffi.scoped_nonmovingbuffer(valueW) as valueP0: |
| 332 |
valueP = rffi.cast(rffi.CWCHARP, rffi.ptradd(valueP0, 2)) |
| 333 |
ret = rwinreg.RegSetValueW(hkey, subkeyP, rwinreg.REG_SZ, |
| 334 |
valueP, valueL) |
| 335 |
if ret != 0: |
| 336 |
raiseWindowsError(space, ret, 'RegSetValue') |
| 337 |
|
| 338 |
|
| 339 |
def QueryValue(space, w_hkey, w_subkey): |
| 340 |
""" |
| 341 |
string = QueryValue(key, sub_key) - retrieves the unnamed value for a key. |
| 342 |
|
| 343 |
key is an already open key, or any one of the predefined HKEY_* constants. |
| 344 |
sub_key is a string that holds the name of the subkey with which the value |
| 345 |
is associated. If this parameter is None or empty, the function retrieves |
| 346 |
the value set by the SetValue() method for the key identified by key. |
| 347 |
|
| 348 |
Values in the registry have name, type, and data components. This method |
| 349 |
retrieves the data for a key's first value that has a NULL name. |
| 350 |
But the underlying API call doesn't return the type: Lame, DONT USE THIS!!!""" |
| 351 |
hkey = hkey_w(w_hkey, space) |
| 352 |
if space.is_w(w_subkey, space.w_None): |
| 353 |
subkey = None |
| 354 |
else: |
| 355 |
subkey = space.utf8_w(w_subkey).decode('utf8') |
| 356 |
space.audit("winreg.QueryValue", [ |
| 357 |
space.newint(rffi.cast(rffi.SIZE_T, hkey)), |
| 358 |
w_subkey, space.newtext('')]) |
| 359 |
with rffi.scoped_unicode2wcharp(subkey) as wide_subkey: |
| 360 |
c_subkey = rffi.cast(rffi.CWCHARP, wide_subkey) |
| 361 |
with lltype.scoped_alloc(rwin32.PLONG.TO, 1) as bufsize_p: |
| 362 |
bufsize_p[0] = rffi.cast(rwin32.LONG, 0) |
| 363 |
ret = rwinreg.RegQueryValueW(hkey, c_subkey, None, bufsize_p) |
| 364 |
bufSize = intmask(bufsize_p[0]) |
| 365 |
if ret == rwinreg.ERROR_MORE_DATA: |
| 366 |
bufSize = 256 |
| 367 |
elif ret != 0: |
| 368 |
raiseWindowsError(space, ret, 'RegQueryValue') |
| 369 |
|
| 370 |
while True: |
| 371 |
buf = ByteBuffer(bufSize) |
| 372 |
bufP = rffi.cast(rffi.CWCHARP, buf.get_raw_address()) |
| 373 |
ret = rwinreg.RegQueryValueW(hkey, c_subkey, bufP, bufsize_p) |
| 374 |
if ret == rwinreg.ERROR_MORE_DATA: |
| 375 |
|
| 376 |
bufSize *= 2 |
| 377 |
bufsize_p[0] = rffi.cast(rwin32.LONG, bufSize) |
| 378 |
continue |
| 379 |
|
| 380 |
if ret != 0: |
| 381 |
raiseWindowsError(space, ret, 'RegQueryValue') |
| 382 |
utf8, lgt = wbuf_to_utf8(space, buf[0:intmask(bufsize_p[0])]) |
| 383 |
return space.newtext(utf8, lgt) |
| 384 |
|
| 385 |
|
| 386 |
def convert_to_regdata(space, w_value, typ): |
| 387 |
'''returns CCHARP, int''' |
| 388 |
buf = None |
| 389 |
|
| 390 |
if typ == rwinreg.REG_DWORD: |
| 391 |
if space.is_none(w_value) or space.isinstance_w(w_value, space.w_int): |
| 392 |
if space.is_none(w_value): |
| 393 |
value = r_uint(0) |
| 394 |
else: |
| 395 |
value = space.c_uint_w(w_value) |
| 396 |
buflen = rffi.sizeof(rwin32.DWORD) |
| 397 |
buf1 = lltype.malloc(rffi.CArray(rwin32.DWORD), 1, flavor='raw') |
| 398 |
buf1[0] = rffi.cast(rffi.UINT, value) |
| 399 |
buf = rffi.cast(rffi.CCHARP, buf1) |
| 400 |
|
| 401 |
elif typ == rwinreg.REG_QWORD: |
| 402 |
if space.is_none(w_value) or space.isinstance_w(w_value, space.w_int): |
| 403 |
if space.is_none(w_value): |
| 404 |
value = r_ulonglong(0) |
| 405 |
else: |
| 406 |
value = space.r_ulonglong_w(w_value) |
| 407 |
buflen = rffi.sizeof(rffi.ULONGLONG) |
| 408 |
buf1 = lltype.malloc(rffi.CArray(rffi.ULONGLONG), 1, flavor='raw') |
| 409 |
buf1[0] = rffi.cast(rffi.ULONGLONG, value) |
| 410 |
buf = rffi.cast(rffi.CCHARP, buf1) |
| 411 |
|
| 412 |
elif typ == rwinreg.REG_SZ or typ == rwinreg.REG_EXPAND_SZ: |
| 413 |
if space.is_w(w_value, space.w_None): |
| 414 |
buflen = 1 |
| 415 |
buf = lltype.malloc(rffi.CCHARP.TO, buflen, flavor='raw') |
| 416 |
buf[0] = '\0' |
| 417 |
else: |
| 418 |
buf = rffi.unicode2wcharp(space.utf8_w(w_value).decode('utf8')) |
| 419 |
buf = rffi.cast(rffi.CCHARP, buf) |
| 420 |
buflen = (space.len_w(w_value) * 2) + 1 |
| 421 |
|
| 422 |
elif typ == rwinreg.REG_MULTI_SZ: |
| 423 |
if space.is_w(w_value, space.w_None): |
| 424 |
buflen = 1 |
| 425 |
buf = lltype.malloc(rffi.CCHARP.TO, buflen, flavor='raw') |
| 426 |
buf[0] = '\0' |
| 427 |
elif space.isinstance_w(w_value, space.w_list): |
| 428 |
strings = [] |
| 429 |
buflen = 0 |
| 430 |
|
| 431 |
|
| 432 |
w_iter = space.iter(w_value) |
| 433 |
while True: |
| 434 |
try: |
| 435 |
w_item = space.next(w_iter) |
| 436 |
item = space.utf8_w(w_item).decode('utf8') |
| 437 |
strings.append(item) |
| 438 |
buflen += 2 * (len(item) + 1) |
| 439 |
except OperationError as e: |
| 440 |
if not e.match(space, space.w_StopIteration): |
| 441 |
raise |
| 442 |
break |
| 443 |
buflen += 2 |
| 444 |
buf = lltype.malloc(rffi.CCHARP.TO, buflen, flavor='raw') |
| 445 |
|
| 446 |
|
| 447 |
buflen = 0 |
| 448 |
for string in strings: |
| 449 |
with rffi.scoped_unicode2wcharp(string) as wchr: |
| 450 |
c_str = rffi.cast(rffi.CCHARP, wchr) |
| 451 |
for i in range(len(string) * 2): |
| 452 |
buf[buflen + i] = c_str[i] |
| 453 |
buflen += (len(string) + 1) * 2 |
| 454 |
buf[buflen - 1] = '\0' |
| 455 |
buf[buflen - 2] = '\0' |
| 456 |
buflen += 2 |
| 457 |
buf[buflen - 1] = '\0' |
| 458 |
buf[buflen - 2] = '\0' |
| 459 |
|
| 460 |
else: |
| 461 |
if space.is_w(w_value, space.w_None): |
| 462 |
buflen = 0 |
| 463 |
buf = lltype.nullptr(rffi.CCHARP.TO) |
| 464 |
else: |
| 465 |
try: |
| 466 |
value = w_value.buffer_w(space, space.BUF_SIMPLE) |
| 467 |
except BufferInterfaceNotFound: |
| 468 |
raise oefmt(space.w_TypeError, |
| 469 |
"Objects of type '%T' can not be used as binary " |
| 470 |
"registry values", w_value) |
| 471 |
else: |
| 472 |
value = value.as_str() |
| 473 |
buflen = len(value) |
| 474 |
buf = rffi.str2charp(value) |
| 475 |
|
| 476 |
if buf is not None: |
| 477 |
return rffi.cast(rffi.CWCHARP, buf), buflen |
| 478 |
|
| 479 |
raise oefmt(space.w_ValueError, |
| 480 |
"Could not convert the data to the specified type") |
| 481 |
|
| 482 |
|
| 483 |
def wbuf_to_utf8(space, wbuf): |
| 484 |
state = space.fromcache(CodecState) |
| 485 |
errh = state.decode_error_handler |
| 486 |
w_wbuf = space.newbytes(wbuf) |
| 487 |
utf8, lgt, pos = str_decode_utf_16(space, wbuf, w_wbuf, 'surrogatepass', final=True, |
| 488 |
errorhandler=errh) |
| 489 |
if len(utf8) > 1 and utf8[len(utf8) - 1] == '\x00': |
| 490 |
|
| 491 |
newlen = len(utf8) - 1 |
| 492 |
assert newlen >=0 |
| 493 |
utf8 = utf8[0:newlen] |
| 494 |
lgt -= 1 |
| 495 |
return utf8, lgt |
| 496 |
|
| 497 |
|
| 498 |
def convert_from_regdata(space, buf, buflen, typ): |
| 499 |
if typ == rwinreg.REG_DWORD: |
| 500 |
if not buflen: |
| 501 |
return space.newint(0) |
| 502 |
d = rffi.cast(rwin32.LPDWORD, buf.get_raw_address())[0] |
| 503 |
return space.newint(d) |
| 504 |
|
| 505 |
elif typ == rwinreg.REG_QWORD: |
| 506 |
if not buflen: |
| 507 |
return space.newint(0) |
| 508 |
d = rffi.cast(rffi.ULONGLONGP, buf.get_raw_address())[0] |
| 509 |
return space.newint(d) |
| 510 |
|
| 511 |
elif typ == rwinreg.REG_SZ or typ == rwinreg.REG_EXPAND_SZ: |
| 512 |
if not buflen: |
| 513 |
return space.newtext('', 0) |
| 514 |
even = (buflen // 2) * 2 |
| 515 |
utf8, lgt = wbuf_to_utf8(space, buf[0:even]) |
| 516 |
|
| 517 |
i = 0 |
| 518 |
utf8len = len(utf8) |
| 519 |
while i < utf8len: |
| 520 |
if utf8[i] == '\x00': |
| 521 |
utf8 = utf8[0:i] |
| 522 |
lgt = check_utf8(utf8, True) |
| 523 |
break |
| 524 |
i += 1 |
| 525 |
w_s = space.newtext(utf8, lgt) |
| 526 |
return w_s |
| 527 |
|
| 528 |
elif typ == rwinreg.REG_MULTI_SZ: |
| 529 |
if not buflen: |
| 530 |
return space.newlist([]) |
| 531 |
even = (buflen // 2) * 2 |
| 532 |
utf8, lgt = wbuf_to_utf8(space, buf[0:even]) |
| 533 |
parts = rstring.split(utf8, '\0') |
| 534 |
partslen = len(parts) |
| 535 |
if partslen > 0 and parts[partslen-1] == '': |
| 536 |
partslen -= 1 |
| 537 |
ret = [] |
| 538 |
i = 0 |
| 539 |
while i < partslen: |
| 540 |
lgt = check_utf8(parts[i], True) |
| 541 |
ret.append(space.newtext(parts[i], lgt)) |
| 542 |
i += 1 |
| 543 |
return space.newlist(ret) |
| 544 |
|
| 545 |
else: |
| 546 |
if buflen == 0: |
| 547 |
return space.w_None |
| 548 |
else: |
| 549 |
return space.newbytes(buf[0:buflen]) |
| 550 |
|
| 551 |
|
| 552 |
@unwrap_spec(typ=int) |
| 553 |
def SetValueEx(space, w_hkey, w_value_name, w_reserved, typ, w_value): |
| 554 |
""" |
| 555 |
SetValueEx(key, value_name, reserved, type, value) - Stores data in the value |
| 556 |
field of an open registry key. |
| 557 |
|
| 558 |
key is an already open key, or any one of the predefined HKEY_* constants. |
| 559 |
value_name is a string containing the name of the value to set, or None |
| 560 |
type is an integer that specifies the type of the data. This should be one of: |
| 561 |
REG_BINARY -- Binary data in any form. |
| 562 |
REG_DWORD -- A 32-bit number. |
| 563 |
REG_DWORD_LITTLE_ENDIAN -- A 32-bit number in little-endian format. |
| 564 |
REG_DWORD_BIG_ENDIAN -- A 32-bit number in big-endian format. |
| 565 |
REG_QWORD -- A 64-bit number. |
| 566 |
REG_QWORD_LITTLE_ENDIAN -- A 64-bit number in little-endian format. |
| 567 |
REG_EXPAND_SZ -- A null-terminated string that contains unexpanded references |
| 568 |
to environment variables (for example, %PATH%). |
| 569 |
REG_LINK -- A Unicode symbolic link. |
| 570 |
REG_MULTI_SZ -- An sequence of null-terminated strings, terminated by |
| 571 |
two null characters. Note that Python handles this |
| 572 |
termination automatically. |
| 573 |
REG_NONE -- No defined value type. |
| 574 |
REG_RESOURCE_LIST -- A device-driver resource list. |
| 575 |
REG_SZ -- A null-terminated string. |
| 576 |
reserved can be anything - zero is always passed to the API. |
| 577 |
value is a string that specifies the new value. |
| 578 |
|
| 579 |
This method can also set additional value and type information for the |
| 580 |
specified key. The key identified by the key parameter must have been |
| 581 |
opened with KEY_SET_VALUE access. |
| 582 |
|
| 583 |
To open the key, use the CreateKeyEx() or OpenKeyEx() methods. |
| 584 |
|
| 585 |
Value lengths are limited by available memory. Long values (more than |
| 586 |
2048 bytes) should be stored as files with the filenames stored in |
| 587 |
the configuration registry. This helps the registry perform efficiently.""" |
| 588 |
hkey = hkey_w(w_hkey, space) |
| 589 |
buf, buflen = convert_to_regdata(space, w_value, typ) |
| 590 |
space.audit("winreg.SetValue", [ |
| 591 |
space.newint(rffi.cast(rffi.SIZE_T, hkey)), |
| 592 |
w_value_name, space.newint(typ), w_value]) |
| 593 |
try: |
| 594 |
utf8 = space.utf8_w(w_value_name) |
| 595 |
uni_len = space.len_w(w_value_name) |
| 596 |
with rffi.scoped_utf82wcharp(utf8, uni_len) as wide_vn: |
| 597 |
c_vn = rffi.cast(rffi.CWCHARP, wide_vn) |
| 598 |
ret = rwinreg.RegSetValueExW(hkey, c_vn, 0, typ, buf, buflen) |
| 599 |
finally: |
| 600 |
if buf != lltype.nullptr(rffi.CWCHARP.TO): |
| 601 |
lltype.free(buf, flavor='raw') |
| 602 |
if ret != 0: |
| 603 |
raiseWindowsError(space, ret, 'RegSetValueEx') |
| 604 |
|
| 605 |
|
| 606 |
def QueryValueEx(space, w_hkey, w_subkey): |
| 607 |
""" |
| 608 |
value,type_id = QueryValueEx(key, value_name) - Retrieves the type and data for |
| 609 |
a specified value name associated with an open registry key. |
| 610 |
|
| 611 |
key is an already open key, or any one of the predefined HKEY_* constants. |
| 612 |
value_name is a string indicating the value to query""" |
| 613 |
hkey = hkey_w(w_hkey, space) |
| 614 |
if space.is_w(w_subkey, space.w_None): |
| 615 |
subkey = None |
| 616 |
else: |
| 617 |
subkey = space.utf8_w(w_subkey).decode('utf8') |
| 618 |
space.audit("winreg.QueryValue", [ |
| 619 |
space.newint(rffi.cast(rffi.SIZE_T, hkey)), |
| 620 |
space.newtext(''), w_subkey]) |
| 621 |
null_dword = lltype.nullptr(rwin32.LPDWORD.TO) |
| 622 |
with rffi.scoped_unicode2wcharp(subkey) as wide_subkey: |
| 623 |
c_subkey = rffi.cast(rffi.CWCHARP, wide_subkey) |
| 624 |
with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as dataSize: |
| 625 |
ret = rwinreg.RegQueryValueExW(hkey, c_subkey, null_dword, |
| 626 |
null_dword, None, dataSize) |
| 627 |
bufSize = intmask(dataSize[0]) |
| 628 |
if ret == rwinreg.ERROR_MORE_DATA: |
| 629 |
|
| 630 |
bufSize = 256 |
| 631 |
elif ret != 0: |
| 632 |
raiseWindowsError(space, ret, 'RegQueryValue') |
| 633 |
while True: |
| 634 |
dataBuf = ByteBuffer(bufSize) |
| 635 |
dataBufP = rffi.cast(rffi.CWCHARP, dataBuf.get_raw_address()) |
| 636 |
with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as retType: |
| 637 |
|
| 638 |
ret = rwinreg.RegQueryValueExW(hkey, c_subkey, null_dword, |
| 639 |
retType, dataBufP, dataSize) |
| 640 |
if ret == rwinreg.ERROR_MORE_DATA: |
| 641 |
|
| 642 |
bufSize *= 2 |
| 643 |
dataSize[0] = rffi.cast(rwin32.DWORD, bufSize) |
| 644 |
continue |
| 645 |
if ret != 0: |
| 646 |
raiseWindowsError(space, ret, 'RegQueryValueEx') |
| 647 |
length = intmask(dataSize[0]) |
| 648 |
ret_type = intmask(retType[0]) |
| 649 |
return space.newtuple2( |
| 650 |
convert_from_regdata(space, dataBuf, |
| 651 |
length, ret_type), |
| 652 |
space.newint(intmask(ret_type)), |
| 653 |
) |
| 654 |
|
| 655 |
|
| 656 |
def CreateKey(space, w_hkey, w_subkey): |
| 657 |
"""key = CreateKey(key, sub_key) - Creates or opens the specified key. |
| 658 |
|
| 659 |
key is an already open key, or one of the predefined HKEY_* constants |
| 660 |
sub_key is a string that names the key this method opens or creates. |
| 661 |
If key is one of the predefined keys, sub_key may be None. In that case, |
| 662 |
the handle returned is the same key handle passed in to the function. |
| 663 |
|
| 664 |
If the key already exists, this function opens the existing key |
| 665 |
|
| 666 |
The return value is the handle of the opened key. |
| 667 |
If the function fails, an exception is raised.""" |
| 668 |
hkey = hkey_w(w_hkey, space) |
| 669 |
space.audit("winreg.CreateKey", [ |
| 670 |
space.newint(rffi.cast(rffi.SIZE_T, hkey)), |
| 671 |
w_subkey, space.newint(rwinreg.KEY_WRITE)]) |
| 672 |
utf8 = space.utf8_w(w_subkey) |
| 673 |
uni_len = space.len_w(w_subkey) |
| 674 |
with rffi.scoped_utf82wcharp(utf8, uni_len) as wide_subkey: |
| 675 |
c_subkey = rffi.cast(rffi.CWCHARP, wide_subkey) |
| 676 |
with lltype.scoped_alloc(rwinreg.PHKEY.TO, 1) as rethkey: |
| 677 |
ret = rwinreg.RegCreateKeyW(hkey, c_subkey, rethkey) |
| 678 |
if ret != 0: |
| 679 |
raiseWindowsError(space, ret, 'CreateKey') |
| 680 |
w_ret = W_HKEY(space, rethkey[0]) |
| 681 |
space.audit("winreg.OpenKey/result", [space.newint(w_ret.as_int())]) |
| 682 |
return w_ret |
| 683 |
|
| 684 |
|
| 685 |
@unwrap_spec(reserved=int, access=r_uint) |
| 686 |
def CreateKeyEx(space, w_key, w_sub_key, reserved=0, access=rwinreg.KEY_WRITE): |
| 687 |
"""key = CreateKey(key, sub_key) - Creates or opens the specified key. |
| 688 |
|
| 689 |
key is an already open key, or one of the predefined HKEY_* constants |
| 690 |
sub_key is a string that names the key this method opens or creates. |
| 691 |
If key is one of the predefined keys, sub_key may be None. In that case, |
| 692 |
the handle returned is the same key handle passed in to the function. |
| 693 |
|
| 694 |
If the key already exists, this function opens the existing key |
| 695 |
|
| 696 |
The return value is the handle of the opened key. |
| 697 |
If the function fails, an exception is raised.""" |
| 698 |
hkey = hkey_w(w_key, space) |
| 699 |
space.audit("winreg.CreateKey", [ |
| 700 |
space.newint(rffi.cast(rffi.SIZE_T, hkey)), |
| 701 |
w_sub_key, space.newint(access)]) |
| 702 |
utf8 = space.utf8_w(w_sub_key) |
| 703 |
uni_len = space.len_w(w_sub_key) |
| 704 |
with rffi.scoped_utf82wcharp(utf8, uni_len) as wide_sub_key: |
| 705 |
c_subkey = rffi.cast(rffi.CWCHARP, wide_sub_key) |
| 706 |
with lltype.scoped_alloc(rwinreg.PHKEY.TO, 1) as rethkey: |
| 707 |
ret = rwinreg.RegCreateKeyExW(hkey, c_subkey, reserved, None, 0, |
| 708 |
access, None, rethkey, |
| 709 |
lltype.nullptr(rwin32.LPDWORD.TO)) |
| 710 |
if ret != 0: |
| 711 |
raiseWindowsError(space, ret, 'CreateKeyEx') |
| 712 |
w_ret = W_HKEY(space, rethkey[0]) |
| 713 |
space.audit("winreg.OpenKey/result", [space.newint(w_ret.as_int())]) |
| 714 |
return w_ret |
| 715 |
|
| 716 |
|
| 717 |
def DeleteKey(space, w_hkey, w_subkey): |
| 718 |
""" |
| 719 |
DeleteKey(key, subkey) - Deletes the specified key. |
| 720 |
|
| 721 |
key is an already open key, or any one of the predefined HKEY_* constants. |
| 722 |
sub_key is a string that must be a subkey of the key identified by the key |
| 723 |
parameter. This value must not be None, and the key may not have subkeys. |
| 724 |
|
| 725 |
This method can not delete keys with subkeys. |
| 726 |
|
| 727 |
If the method succeeds, the entire key, including all of its values, |
| 728 |
is removed. If the method fails, an EnvironmentError exception is raised.""" |
| 729 |
hkey = hkey_w(w_hkey, space) |
| 730 |
space.audit("winreg.DeleteKey", [ |
| 731 |
space.newint(rffi.cast(rffi.SIZE_T, hkey)), |
| 732 |
w_subkey, space.newint(0)]) |
| 733 |
utf8 = space.utf8_w(w_subkey) |
| 734 |
uni_len = space.len_w(w_subkey) |
| 735 |
with rffi.scoped_utf82wcharp(utf8, uni_len) as wide_subkey: |
| 736 |
c_subkey = rffi.cast(rffi.CWCHARP, wide_subkey) |
| 737 |
ret = rwinreg.RegDeleteKeyW(hkey, c_subkey) |
| 738 |
if ret != 0: |
| 739 |
raiseWindowsError(space, ret, 'RegDeleteKey') |
| 740 |
|
| 741 |
|
| 742 |
def DeleteValue(space, w_hkey, w_subkey): |
| 743 |
"""DeleteValue(key, value) - Removes a named value from a registry key. |
| 744 |
|
| 745 |
key is an already open key, or any one of the predefined HKEY_* constants. |
| 746 |
value is a string that identifies the value to remove.""" |
| 747 |
hkey = hkey_w(w_hkey, space) |
| 748 |
space.audit("winreg.DeleteValue", [ |
| 749 |
space.newint(rffi.cast(rffi.SIZE_T, hkey)), w_subkey]) |
| 750 |
utf8 = space.utf8_w(w_subkey) |
| 751 |
uni_len = space.len_w(w_subkey) |
| 752 |
with rffi.scoped_utf82wcharp(utf8, uni_len) as wide_subkey: |
| 753 |
c_subkey = rffi.cast(rffi.CWCHARP, wide_subkey) |
| 754 |
ret = rwinreg.RegDeleteValueW(hkey, c_subkey) |
| 755 |
if ret != 0: |
| 756 |
raiseWindowsError(space, ret, 'RegDeleteValue') |
| 757 |
|
| 758 |
|
| 759 |
@unwrap_spec(reserved=int, access=r_uint) |
| 760 |
def OpenKey(space, w_key, w_sub_key, reserved=0, access=rwinreg.KEY_READ): |
| 761 |
""" |
| 762 |
key = OpenKey(key, sub_key, res = 0, sam = KEY_READ) - Opens the specified key. |
| 763 |
|
| 764 |
key is an already open key, or any one of the predefined HKEY_* constants. |
| 765 |
sub_key is a string that identifies the sub_key to open |
| 766 |
res is a reserved integer, and must be zero. Default is zero. |
| 767 |
sam is an integer that specifies an access mask that describes the desired |
| 768 |
security access for the key. Default is KEY_READ |
| 769 |
|
| 770 |
The result is a new handle to the specified key |
| 771 |
If the function fails, an EnvironmentError exception is raised.""" |
| 772 |
hkey = hkey_w(w_key, space) |
| 773 |
utf8 = space.utf8_w(w_sub_key) |
| 774 |
state = space.fromcache(CodecState) |
| 775 |
errh = state.encode_error_handler |
| 776 |
space.audit("winreg.OpenKey", [ |
| 777 |
space.newint(rffi.cast(rffi.SIZE_T, hkey)), |
| 778 |
w_sub_key, space.newint(access)]) |
| 779 |
|
| 780 |
s = utf8 + '\x00' |
| 781 |
w_s = space.newtext(s) |
| 782 |
subkeyW = utf8_encode_utf_16( |
| 783 |
space, s, w_s, 'strict', errh, allow_surrogates=True) |
| 784 |
with rffi.scoped_nonmovingbuffer(subkeyW) as subkeyP0: |
| 785 |
subkeyP = rffi.cast(rffi.CWCHARP, rffi.ptradd(subkeyP0, 2)) |
| 786 |
with lltype.scoped_alloc(rwinreg.PHKEY.TO, 1) as rethkey: |
| 787 |
ret = rwinreg.RegOpenKeyExW(hkey, subkeyP, reserved, access, |
| 788 |
rethkey) |
| 789 |
if ret != 0: |
| 790 |
raiseWindowsError(space, ret, 'RegOpenKeyEx') |
| 791 |
w_ret = W_HKEY(space, rethkey[0]) |
| 792 |
space.audit("winreg.OpenKey/result", [space.newint(w_ret.as_int())]) |
| 793 |
return w_ret |
| 794 |
|
| 795 |
|
| 796 |
@unwrap_spec(index=int) |
| 797 |
def EnumValue(space, w_hkey, index): |
| 798 |
"""tuple = EnumValue(key, index) - Enumerates values of an open registry key. |
| 799 |
key is an already open key, or any one of the predefined HKEY_* constants. |
| 800 |
index is an integer that identifies the index of the value to retrieve. |
| 801 |
|
| 802 |
The function retrieves the name of one subkey each time it is called. |
| 803 |
It is typically called repeatedly, until an EnvironmentError exception |
| 804 |
is raised, indicating no more values. |
| 805 |
|
| 806 |
The result is a tuple of 3 items: |
| 807 |
value_name is a string that identifies the value. |
| 808 |
value_data is an object that holds the value data, and whose type depends |
| 809 |
on the underlying registry type. |
| 810 |
data_type is an integer that identifies the type of the value data.""" |
| 811 |
hkey = hkey_w(w_hkey, space) |
| 812 |
space.audit("winreg.EnumValue", [ |
| 813 |
space.newint(rffi.cast(rffi.SIZE_T, hkey)), |
| 814 |
space.newint(index)]) |
| 815 |
null_dword = lltype.nullptr(rwin32.LPDWORD.TO) |
| 816 |
|
| 817 |
with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as valueSize: |
| 818 |
with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as dataSize: |
| 819 |
ret = rwinreg.RegQueryInfoKeyW( |
| 820 |
hkey, None, null_dword, null_dword, |
| 821 |
null_dword, null_dword, null_dword, |
| 822 |
null_dword, valueSize, dataSize, |
| 823 |
null_dword, lltype.nullptr(rwin32.PFILETIME.TO)) |
| 824 |
if ret != 0: |
| 825 |
raiseWindowsError(space, ret, 'RegQueryInfoKey') |
| 826 |
|
| 827 |
valueSize[0] = rffi.cast(rwin32.DWORD, intmask(valueSize[0]) + 1) |
| 828 |
dataSize[0] = rffi.cast(rwin32.DWORD, intmask(dataSize[0]) + 1) |
| 829 |
bufDataSize = intmask(dataSize[0]) |
| 830 |
bufValueSize = intmask(valueSize[0]) * 2 |
| 831 |
|
| 832 |
valueBuf = ByteBuffer(bufValueSize) |
| 833 |
valueBufP = rffi.cast(rffi.CWCHARP, valueBuf.get_raw_address()) |
| 834 |
while True: |
| 835 |
dataBuf = ByteBuffer(bufDataSize) |
| 836 |
dataBufP = rffi.cast(rffi.CCHARP, dataBuf.get_raw_address()) |
| 837 |
with lltype.scoped_alloc(rwin32.LPDWORD.TO, |
| 838 |
1) as retType: |
| 839 |
ret = rwinreg.RegEnumValueW( |
| 840 |
hkey, index, valueBufP, valueSize, |
| 841 |
null_dword, retType, dataBufP, dataSize) |
| 842 |
if ret == rwinreg.ERROR_MORE_DATA: |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
bufDataSize *= 4 |
| 847 |
dataSize[0] = rffi.cast(rwin32.DWORD, |
| 848 |
bufDataSize) |
| 849 |
valueSize[0] = rffi.cast(rwin32.DWORD, |
| 850 |
bufValueSize) |
| 851 |
continue |
| 852 |
|
| 853 |
if ret != 0: |
| 854 |
raiseWindowsError(space, ret, 'RegEnumValue') |
| 855 |
|
| 856 |
length = intmask(dataSize[0]) |
| 857 |
vlen = (intmask(valueSize[0]) + 1) * 2 |
| 858 |
utf8v, lenv = wbuf_to_utf8(space, valueBuf[0:vlen]) |
| 859 |
ret_type = intmask(retType[0]) |
| 860 |
return space.newtuple([ |
| 861 |
space.newtext(utf8v, lenv), |
| 862 |
convert_from_regdata(space, dataBuf, |
| 863 |
length, ret_type), |
| 864 |
space.newint(ret_type), |
| 865 |
]) |
| 866 |
|
| 867 |
|
| 868 |
@unwrap_spec(index=int) |
| 869 |
def EnumKey(space, w_hkey, index): |
| 870 |
"""string = EnumKey(key, index) - Enumerates subkeys of an open registry key. |
| 871 |
|
| 872 |
key is an already open key, or any one of the predefined HKEY_* constants. |
| 873 |
index is an integer that identifies the index of the key to retrieve. |
| 874 |
|
| 875 |
The function retrieves the name of one subkey each time it is called. |
| 876 |
It is typically called repeatedly until an EnvironmentError exception is |
| 877 |
raised, indicating no more values are available.""" |
| 878 |
hkey = hkey_w(w_hkey, space) |
| 879 |
space.audit("winreg.EnumKey", [ |
| 880 |
space.newint(rffi.cast(rffi.SIZE_T, hkey)), |
| 881 |
space.newint(index)]) |
| 882 |
null_dword = lltype.nullptr(rwin32.LPDWORD.TO) |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
buf = ByteBuffer(257 * 2) |
| 891 |
bufP = rffi.cast(rwin32.LPWSTR, buf.get_raw_address()) |
| 892 |
with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as valueSize: |
| 893 |
valueSize[0] = rffi.cast(rwin32.DWORD, 257) |
| 894 |
ret = rwinreg.RegEnumKeyExW(hkey, index, bufP, valueSize, |
| 895 |
null_dword, None, null_dword, |
| 896 |
lltype.nullptr(rwin32.PFILETIME.TO)) |
| 897 |
if ret != 0: |
| 898 |
raiseWindowsError(space, ret, 'RegEnumKeyEx') |
| 899 |
vlen = intmask(valueSize[0]) * 2 |
| 900 |
utf8, lgt = wbuf_to_utf8(space, buf[0:vlen]) |
| 901 |
return space.newtext(utf8, lgt) |
| 902 |
|
| 903 |
|
| 904 |
def QueryInfoKey(space, w_hkey): |
| 905 |
"""tuple = QueryInfoKey(key) - Returns information about a key. |
| 906 |
|
| 907 |
key is an already open key, or any one of the predefined HKEY_* constants. |
| 908 |
|
| 909 |
The result is a tuple of 3 items: |
| 910 |
An integer that identifies the number of sub keys this key has. |
| 911 |
An integer that identifies the number of values this key has. |
| 912 |
A long integer that identifies when the key was last modified (if available) |
| 913 |
as 100's of nanoseconds since Jan 1, 1600.""" |
| 914 |
hkey = hkey_w(w_hkey, space) |
| 915 |
space.audit("winreg.QueryInfoKey", [ |
| 916 |
space.newint(rffi.cast(rffi.SIZE_T, hkey))]) |
| 917 |
with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as nSubKeys: |
| 918 |
with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as nValues: |
| 919 |
with lltype.scoped_alloc(rwin32.PFILETIME.TO, 1) as ft: |
| 920 |
null_dword = lltype.nullptr(rwin32.LPDWORD.TO) |
| 921 |
ret = rwinreg.RegQueryInfoKeyW( |
| 922 |
hkey, None, null_dword, null_dword, |
| 923 |
nSubKeys, null_dword, null_dword, |
| 924 |
nValues, null_dword, null_dword, |
| 925 |
null_dword, ft) |
| 926 |
if ret != 0: |
| 927 |
raiseWindowsError(space, ret, 'RegQueryInfoKey') |
| 928 |
lgt = ((lltype.r_longlong(ft[0].c_dwHighDateTime) << 32) + |
| 929 |
lltype.r_longlong(ft[0].c_dwLowDateTime)) |
| 930 |
return space.newtuple([space.newint(nSubKeys[0]), |
| 931 |
space.newint(nValues[0]), |
| 932 |
space.newint(lgt)]) |
| 933 |
|
| 934 |
|
| 935 |
def ConnectRegistry(space, w_machine, w_hkey): |
| 936 |
""" |
| 937 |
key = ConnectRegistry(computer_name, key) |
| 938 |
|
| 939 |
Establishes a connection to a predefined registry handle on another computer. |
| 940 |
|
| 941 |
computer_name is the name of the remote computer, of the form \\\\computername. |
| 942 |
If None, the local computer is used. |
| 943 |
key is the predefined handle to connect to. |
| 944 |
|
| 945 |
The return value is the handle of the opened key. |
| 946 |
If the function fails, an EnvironmentError exception is raised.""" |
| 947 |
hkey = hkey_w(w_hkey, space) |
| 948 |
space.audit("winreg.ConnectRegistry", [ |
| 949 |
w_machine, space.newint(rffi.cast(rffi.SIZE_T, hkey))]) |
| 950 |
if space.is_none(w_machine): |
| 951 |
with lltype.scoped_alloc(rwinreg.PHKEY.TO, 1) as rethkey: |
| 952 |
ret = rwinreg.RegConnectRegistryW(None, hkey, rethkey) |
| 953 |
if ret != 0: |
| 954 |
raiseWindowsError(space, ret, 'RegConnectRegistry') |
| 955 |
return W_HKEY(space, rethkey[0]) |
| 956 |
else: |
| 957 |
utf8 = space.utf8_w(w_machine) |
| 958 |
state = space.fromcache(CodecState) |
| 959 |
errh = state.encode_error_handler |
| 960 |
s = utf8 + '\x00' |
| 961 |
w_s = space.newtext(s) |
| 962 |
machineW = utf8_encode_utf_16(space, s, w_s, 'strict', errh, allow_surrogates=True) |
| 963 |
with rffi.scoped_nonmovingbuffer(machineW) as machineP0: |
| 964 |
machineP = rffi.cast(rwin32.LPWSTR, rffi.ptradd(machineP0, 2)) |
| 965 |
with lltype.scoped_alloc(rwinreg.PHKEY.TO, 1) as rethkey: |
| 966 |
ret = rwinreg.RegConnectRegistryW(machineP, hkey, rethkey) |
| 967 |
if ret != 0: |
| 968 |
raiseWindowsError(space, ret, 'RegConnectRegistry') |
| 969 |
return W_HKEY(space, rethkey[0]) |
| 970 |
|
| 971 |
|
| 972 |
def ExpandEnvironmentStrings(space, w_source): |
| 973 |
"string = ExpandEnvironmentStrings(string) - Expand environment vars." |
| 974 |
try: |
| 975 |
source, source_ulen = space.utf8_len_w(w_source) |
| 976 |
space.audit("winreg.ConnectRegistry", [w_source]) |
| 977 |
res, res_ulen = rwinreg.ExpandEnvironmentStrings(source, source_ulen) |
| 978 |
return space.newutf8(res, res_ulen) |
| 979 |
except WindowsError as e: |
| 980 |
raise wrap_oserror(space, e) |
| 981 |
|
| 982 |
|
| 983 |
class ReflectionFunction(object): |
| 984 |
def __init__(self, name, stdcall_wrapper): |
| 985 |
self.name = name |
| 986 |
self.handle = lltype.nullptr(rffi.VOIDP.TO) |
| 987 |
self.wrapper = stdcall_wrapper |
| 988 |
|
| 989 |
def check(self): |
| 990 |
if self.handle != lltype.nullptr(rffi.VOIDP.TO): |
| 991 |
return True |
| 992 |
from rpython.rlib.rdynload import GetModuleHandle, dlsym |
| 993 |
lib = GetModuleHandle("advapi32.dll") |
| 994 |
try: |
| 995 |
handle = dlsym(lib, self.name) |
| 996 |
except KeyError: |
| 997 |
return False |
| 998 |
self.handle = handle |
| 999 |
return True |
| 1000 |
|
| 1001 |
def call(self, *args): |
| 1002 |
assert self.handle != lltype.nullptr(rffi.VOIDP.TO) |
| 1003 |
return self.wrapper(self.handle, *args) |
| 1004 |
|
| 1005 |
|
| 1006 |
_RegDisableReflectionKey = ReflectionFunction( |
| 1007 |
"RegDisableReflectionKey", pypy_RegChangeReflectionKey) |
| 1008 |
_RegEnableReflectionKey = ReflectionFunction( |
| 1009 |
"RegEnableReflectionKey", pypy_RegChangeReflectionKey) |
| 1010 |
_RegQueryReflectionKey = ReflectionFunction( |
| 1011 |
"RegQueryReflectionKey", pypy_RegQueryReflectionKey) |
| 1012 |
_RegDeleteKeyExW = ReflectionFunction("RegDeleteKeyExW", pypy_RegDeleteKeyExW) |
| 1013 |
|
| 1014 |
|
| 1015 |
def DisableReflectionKey(space, w_key): |
| 1016 |
"""Disables registry reflection for 32-bit processes running on a 64-bit |
| 1017 |
Operating System. Will generally raise NotImplemented if executed on |
| 1018 |
a 32-bit Operating System. |
| 1019 |
If the key is not on the reflection list, the function succeeds but has no |
| 1020 |
effect. Disabling reflection for a key does not affect reflection of any |
| 1021 |
subkeys.""" |
| 1022 |
hkey = hkey_w(w_key, space) |
| 1023 |
space.audit("winreg.DisableReflectionKey", [ |
| 1024 |
space.newint(rffi.cast(rffi.SIZE_T, hkey))]) |
| 1025 |
if not _RegDisableReflectionKey.check(): |
| 1026 |
raise oefmt(space.w_NotImplementedError, |
| 1027 |
"not implemented on this platform") |
| 1028 |
else: |
| 1029 |
ret = _RegDisableReflectionKey.call(hkey) |
| 1030 |
if ret != 0: |
| 1031 |
raiseWindowsError(space, ret, 'RegDisableReflectionKey') |
| 1032 |
|
| 1033 |
|
| 1034 |
def EnableReflectionKey(space, w_key): |
| 1035 |
"""Restores registry reflection for the specified disabled key. |
| 1036 |
Will generally raise NotImplemented if executed on a 32-bit Operating |
| 1037 |
System. Restoring reflection for a key does not affect reflection of any |
| 1038 |
subkeys.""" |
| 1039 |
hkey = hkey_w(w_key, space) |
| 1040 |
space.audit("winreg.EnableReflectionKey", [ |
| 1041 |
space.newint(rffi.cast(rffi.SIZE_T, hkey))]) |
| 1042 |
if not _RegEnableReflectionKey.check(): |
| 1043 |
raise oefmt(space.w_NotImplementedError, |
| 1044 |
"not implemented on this platform") |
| 1045 |
else: |
| 1046 |
ret = _RegEnableReflectionKey.call(hkey) |
| 1047 |
if ret != 0: |
| 1048 |
raiseWindowsError(space, ret, 'RegEnableReflectionKey') |
| 1049 |
|
| 1050 |
|
| 1051 |
def QueryReflectionKey(space, w_key): |
| 1052 |
"""bool = QueryReflectionKey(hkey) - Determines the reflection state for |
| 1053 |
the specified key. Will generally raise NotImplemented if executed on a |
| 1054 |
32-bit Operating System.""" |
| 1055 |
hkey = hkey_w(w_key, space) |
| 1056 |
space.audit("winreg.QueryReflectionKey", [ |
| 1057 |
space.newint(rffi.cast(rffi.SIZE_T, hkey))]) |
| 1058 |
if not _RegQueryReflectionKey.check(): |
| 1059 |
raise oefmt(space.w_NotImplementedError, |
| 1060 |
"not implemented on this platform") |
| 1061 |
else: |
| 1062 |
with lltype.scoped_alloc(rwin32.LPBOOL.TO, 1) as isDisabled: |
| 1063 |
ret = _RegQueryReflectionKey.call(hkey, isDisabled) |
| 1064 |
if ret != 0: |
| 1065 |
raiseWindowsError(space, ret, 'RegQueryReflectionKey') |
| 1066 |
return space.newbool(intmask(isDisabled[0]) != 0) |
| 1067 |
|
| 1068 |
|
| 1069 |
@unwrap_spec(access=r_uint, reserved=int) |
| 1070 |
def DeleteKeyEx(space, w_key, w_sub_key, access=rwinreg.KEY_WOW64_64KEY, reserved=0): |
| 1071 |
"""DeleteKeyEx(key, sub_key, sam, res) - Deletes the specified key. |
| 1072 |
|
| 1073 |
key is an already open key, or any one of the predefined HKEY_* constants. |
| 1074 |
sub_key is a string that must be a subkey of the key identified by the key |
| 1075 |
parameter. |
| 1076 |
res is a reserved integer, and must be zero. Default is zero. |
| 1077 |
sam is an integer that specifies an access mask that describes the desired |
| 1078 |
This value must not be None, and the key may not have subkeys. |
| 1079 |
|
| 1080 |
This method can not delete keys with subkeys. |
| 1081 |
|
| 1082 |
If the method succeeds, the entire key, including all of its values, |
| 1083 |
is removed. If the method fails, a WindowsError exception is raised. |
| 1084 |
On unsupported Windows versions, NotImplementedError is raised.""" |
| 1085 |
hkey = hkey_w(w_key, space) |
| 1086 |
space.audit("winreg.DeleteKey", [ |
| 1087 |
space.newint(rffi.cast(rffi.SIZE_T, hkey)), |
| 1088 |
w_sub_key, space.newint(0)]) |
| 1089 |
if not _RegDeleteKeyExW.check(): |
| 1090 |
raise oefmt(space.w_NotImplementedError, |
| 1091 |
"not implemented on this platform") |
| 1092 |
utf8 = space.utf8_w(w_sub_key) |
| 1093 |
uni_len = space.len_w(w_sub_key) |
| 1094 |
with rffi.scoped_utf82wcharp(utf8, uni_len) as wide_subkey: |
| 1095 |
c_subkey = rffi.cast(rffi.CWCHARP, wide_subkey) |
| 1096 |
ret = _RegDeleteKeyExW.call(hkey, c_subkey, access, reserved) |
| 1097 |
if ret != 0: |
| 1098 |
raiseWindowsError(space, ret, 'RegDeleteKeyEx') |