[pypy-svn] r61816 - in pypy/trunk/pypy: lib/_ctypes module/_rawffi

afa at codespeak.net afa at codespeak.net
Fri Feb 13 01:33:33 CET 2009


Author: afa
Date: Fri Feb 13 01:33:31 2009
New Revision: 61816

Modified:
   pypy/trunk/pypy/lib/_ctypes/primitive.py
   pypy/trunk/pypy/module/_rawffi/interp_rawffi.py
Log:
Add support for the "X" primitive type: BSTR
which is a unicode string managed by the Windows API.


Modified: pypy/trunk/pypy/lib/_ctypes/primitive.py
==============================================================================
--- pypy/trunk/pypy/lib/_ctypes/primitive.py	(original)
+++ pypy/trunk/pypy/lib/_ctypes/primitive.py	Fri Feb 13 01:33:31 2009
@@ -211,6 +211,34 @@
                 return pyobj_container.get(self._buffer[0])
             result.value = property(_getvalue, _setvalue)
 
+        elif tp == 'X':
+            from ctypes import windll
+            SysAllocStringLen = windll.oleaut32.SysAllocStringLen
+            SysStringLen = windll.oleaut32.SysStringLen
+            SysFreeString = windll.oleaut32.SysFreeString
+            from _ctypes import _wstring_at_addr
+            def _getvalue(self):
+                addr = self._buffer[0]
+                if addr == 0:
+                    return None
+                else:
+                    size = SysStringLen(addr)
+                    return _wstring_at_addr(addr, size)
+
+            def _setvalue(self, value):
+                if isinstance(value, basestring):
+                    if isinstance(value, str):
+                        value = value.decode(ConvMode.encoding,
+                                             ConvMode.errors)
+                    array = _rawffi.Array('u')(len(value)+1, value)
+                    value = SysAllocStringLen(array.buffer, len(value))
+                elif value is None:
+                    value = 0
+                if self._buffer[0]:
+                    SysFreeString(self._buffer[0])
+                self._buffer[0] = value
+            result.value = property(_getvalue, _setvalue)
+
         return result
 
     from_address = cdata_from_address

Modified: pypy/trunk/pypy/module/_rawffi/interp_rawffi.py
==============================================================================
--- pypy/trunk/pypy/module/_rawffi/interp_rawffi.py	(original)
+++ pypy/trunk/pypy/module/_rawffi/interp_rawffi.py	Fri Feb 13 01:33:31 2009
@@ -75,6 +75,9 @@
     'P' : rffi.VOIDP,
 }
 
+if _MS_WINDOWS:
+    LL_TYPEMAP['X'] = rffi.CCHARP
+
 def letter2tp(space, key):
     try:
         return UNPACKED_TYPECODES[key]



More information about the Pypy-commit mailing list