[pypy-svn] r62017 - in pypy/trunk/pypy: module/_winreg module/_winreg/test rlib

afa at codespeak.net afa at codespeak.net
Thu Feb 19 01:39:08 CET 2009


Author: afa
Date: Thu Feb 19 01:39:06 2009
New Revision: 62017

Added:
   pypy/trunk/pypy/module/_winreg/
   pypy/trunk/pypy/module/_winreg/__init__.py
   pypy/trunk/pypy/module/_winreg/interp_winreg.py
   pypy/trunk/pypy/module/_winreg/test/
   pypy/trunk/pypy/module/_winreg/test/test_winreg.py
   pypy/trunk/pypy/rlib/rwinreg.py
Log:
Start a _winreg module


Added: pypy/trunk/pypy/module/_winreg/__init__.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/module/_winreg/__init__.py	Thu Feb 19 01:39:06 2009
@@ -0,0 +1,15 @@
+from pypy.interpreter.mixedmodule import MixedModule
+from pypy.module._winreg import interp_winreg
+from pypy.rlib.rwinreg import constants
+
+class Module(MixedModule):
+    appleveldefs = {
+    }
+    interpleveldefs = {
+        'SetValue': 'interp_winreg.SetValue',
+        'QueryValue': 'interp_winreg.QueryValue',
+        'HKEYType': 'interp_winreg.W_HKEY',
+    }
+
+    for name, value in constants.iteritems():
+        interpleveldefs[name] = "space.wrap(%s)" % (value,)

Added: pypy/trunk/pypy/module/_winreg/interp_winreg.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/module/_winreg/interp_winreg.py	Thu Feb 19 01:39:06 2009
@@ -0,0 +1,86 @@
+from pypy.interpreter.baseobjspace import Wrappable
+from pypy.interpreter.baseobjspace import ObjSpace, W_Root
+from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.typedef import TypeDef
+from pypy.interpreter.error import OperationError
+from pypy.rpython.lltypesystem import rffi, lltype
+from pypy.rlib import rwinreg, rwin32
+
+class W_HKEY(Wrappable):
+    def __init__(self, hkey):
+        self.hkey = hkey
+
+    def __nonzero__(self):
+        return self.hkey != 0
+
+    def descr_repr(self, space):
+        return space.wrap("<PyHKEY:%d>" % (self.hkey,))
+    descr_repr.unwrap_spec = ['self', ObjSpace]
+
+def new_HKEY(space, w_subtype, hkey):
+    return space.wrap(W_HKEY(hkey))
+descr_HKEY_new = interp2app(new_HKEY,
+                            unwrap_spec=[ObjSpace, W_Root, int])
+
+W_HKEY.typedef = TypeDef(
+    "_winreg.HKEYType",
+    __new__ = descr_HKEY_new,
+    __repr__ = interp2app(W_HKEY.descr_repr),
+    )
+
+def hkey_w(w_key, space):
+    if space.is_w(w_key, space.w_None):
+        errstring = space.wrap("None is not a valid HKEY in this context")
+        raise OperationError(space.w_TypeError, errstring)
+    elif isinstance(w_key, W_HKEY):
+        return w_key.hkey
+    elif space.is_true(space.isinstance(w_key, space.w_int)):
+        return space.int_w(w_key)
+    elif space.is_true(space.isinstance(w_key, space.w_long)):
+        return space.uint_w(w_key)
+    else:
+        errstring = space.wrap("The object is not a PyHKEY object")
+        raise OperationError(space.w_TypeError, errstring)
+
+def SetValue(space, w_key, w_subkey, typ, value):
+    if typ != rwinreg.REG_SZ:
+        errstring = space.wrap("Type must be _winreg.REG_SZ")
+        raise OperationError(space.w_ValueError, errstring)
+    key = hkey_w(w_key, space)
+    if space.is_w(w_subkey, space.w_None):
+        subkey = None
+    else:
+        subkey = space.str_w(w_subkey)
+    dataptr = rffi.str2charp(value)
+    try:
+        ret = rwinreg.RegSetValue(key, subkey, rwinreg.REG_SZ, dataptr, len(value))
+    finally:
+        rffi.free_charp(dataptr)
+    if ret != 0:
+        rwin32.raiseWindowError(ret, 'RegSetValue')
+SetValue.unwrap_spec = [ObjSpace, W_Root, W_Root, int, str]
+
+def QueryValue(space, w_key, w_subkey):
+    key = hkey_w(w_key, space)
+    if space.is_w(w_subkey, space.w_None):
+        subkey = None
+    else:
+        subkey = space.str_w(w_subkey)
+    bufsize_p = lltype.malloc(rwin32.PLONG.TO, 1, flavor='raw')
+    try:
+        ret = rwinreg.RegQueryValue(key, subkey, None, bufsize_p)
+        if ret != 0:
+            rwin32.raiseWindowError(ret, 'RegQueryValue')
+        buf = lltype.malloc(rffi.CCHARP.TO, bufsize_p[0], flavor='raw')
+        try:
+            ret = rwinreg.RegQueryValue(key, subkey, buf, bufsize_p)
+            if ret != 0:
+                rwin32.raiseWindowError(ret, 'RegQueryValue')
+            return space.wrap(rffi.charp2strn(buf, bufsize_p[0] - 1))
+        finally:
+            lltype.free(buf, flavor='raw')
+    finally:
+        lltype.free(bufsize_p, flavor='raw')
+    if ret != 0:
+        rwin32.raiseWindowError(ret, 'RegQueryValue')
+SetValue.unwrap_spec = [ObjSpace, W_Root, W_Root, int, str]

Added: pypy/trunk/pypy/module/_winreg/test/test_winreg.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/module/_winreg/test/test_winreg.py	Thu Feb 19 01:39:06 2009
@@ -0,0 +1,40 @@
+from pypy.conftest import gettestobjspace
+
+import os, sys, py
+
+if sys.platform != 'win32':
+    py.test.skip("_winreg is a win32 module")
+
+class AppTestHKey:
+    def setup_class(cls):
+        space = gettestobjspace(usemodules=('_winreg',))
+        cls.space = space
+
+    def test_repr(self):
+        import _winreg
+        k = _winreg.HKEYType(123)
+        assert str(k) == "<PyHKEY:123>"
+
+class AppTestFfi:
+    def setup_class(cls):
+        import _winreg
+        space = gettestobjspace(usemodules=('_winreg',))
+        cls.space = space
+        cls.root_key = _winreg.HKEY_CURRENT_USER
+        cls.test_key_name = "SOFTWARE\\Pypy Registry Test Key - Delete Me"
+        cls.w_root_key = space.wrap(cls.root_key)
+        cls.w_test_key_name = space.wrap(cls.test_key_name)
+
+    def teardown_class(cls):
+        import _winreg
+        return
+        try:
+            _winreg.DeleteKey(cls.root_key, cls.test_key_name)
+        except WindowsError:
+            pass
+
+    def test_simple_write(self):
+        from _winreg import SetValue, QueryValue, REG_SZ
+        value = "Some Default value"
+        SetValue(self.root_key, self.test_key_name, REG_SZ, value)
+        assert QueryValue(self.root_key, self.test_key_name) == value

Added: pypy/trunk/pypy/rlib/rwinreg.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/rlib/rwinreg.py	Thu Feb 19 01:39:06 2009
@@ -0,0 +1,53 @@
+from pypy.rpython.lltypesystem import lltype, rffi
+from pypy.rpython.tool import rffi_platform as platform
+from pypy.translator.tool.cbuild import ExternalCompilationInfo
+from pypy.rlib import rwin32
+
+eci = ExternalCompilationInfo(
+    includes = ['windows.h',
+                ],
+    libraries = ('Advapi32',)
+    )
+class CConfig:
+    _compilation_info_ = eci
+
+
+constant_names = '''
+KEY_QUERY_VALUE KEY_SET_VALUE KEY_CREATE_SUB_KEY KEY_ENUMERATE_SUB_KEYS
+KEY_NOTIFY KEY_CREATE_LINK KEY_READ KEY_WRITE KEY_EXECUTE KEY_ALL_ACCESS
+KEY_WOW64_64KEY KEY_WOW64_32KEY REG_OPTION_RESERVED REG_OPTION_NON_VOLATILE
+REG_OPTION_VOLATILE REG_OPTION_CREATE_LINK REG_OPTION_BACKUP_RESTORE
+REG_OPTION_OPEN_LINK REG_LEGAL_OPTION REG_CREATED_NEW_KEY
+REG_OPENED_EXISTING_KEY REG_WHOLE_HIVE_VOLATILE REG_REFRESH_HIVE
+REG_NO_LAZY_FLUSH REG_NOTIFY_CHANGE_NAME REG_NOTIFY_CHANGE_ATTRIBUTES
+REG_NOTIFY_CHANGE_LAST_SET REG_NOTIFY_CHANGE_SECURITY REG_LEGAL_CHANGE_FILTER
+REG_NONE REG_SZ REG_EXPAND_SZ REG_BINARY REG_DWORD REG_DWORD_LITTLE_ENDIAN
+REG_DWORD_BIG_ENDIAN REG_LINK REG_MULTI_SZ REG_RESOURCE_LIST
+REG_FULL_RESOURCE_DESCRIPTOR REG_RESOURCE_REQUIREMENTS_LIST
+
+HKEY_LOCAL_MACHINE HKEY_CLASSES_ROOT HKEY_CURRENT_CONFIG HKEY_CURRENT_USER
+HKEY_DYN_DATA HKEY_LOCAL_MACHINE HKEY_PERFORMANCE_DATATA HKEY_USERS
+'''.split()
+for name in constant_names:
+    setattr(CConfig, name, platform.DefinedConstantInteger(name))
+
+constants = {}
+cConfig = platform.configure(CConfig)
+constants.update(cConfig)
+globals().update(cConfig)
+
+def external(name, args, result):
+    return rffi.llexternal(name, args, result, compilation_info=eci,
+                           calling_conv='win')
+
+HKEY = rwin32.HANDLE
+
+RegSetValue = external('RegSetValueA',
+                       [HKEY, rffi.CCHARP, rwin32.DWORD,
+                        rffi.CCHARP, rwin32.DWORD],
+                       rffi.LONG)
+
+RegQueryValue = external('RegQueryValueA',
+                         [HKEY, rffi.CCHARP,
+                          rffi.CCHARP, rwin32.PLONG],
+                         rffi.LONG)



More information about the Pypy-commit mailing list