[pypy-svn] r78768 - in pypy/branch/fast-forward: lib_pypy/_ctypes pypy/module/_rawffi pypy/module/_rawffi/test

afa at codespeak.net afa at codespeak.net
Fri Nov 5 21:57:57 CET 2010


Author: afa
Date: Fri Nov  5 21:57:52 2010
New Revision: 78768

Modified:
   pypy/branch/fast-forward/lib_pypy/_ctypes/array.py
   pypy/branch/fast-forward/pypy/module/_rawffi/__init__.py
   pypy/branch/fast-forward/pypy/module/_rawffi/interp_rawffi.py
   pypy/branch/fast-forward/pypy/module/_rawffi/test/test__rawffi.py
Log:
Implement _rawffi.wcharp2unicode(), and use it to replace two XXX in ctypes.


Modified: pypy/branch/fast-forward/lib_pypy/_ctypes/array.py
==============================================================================
--- pypy/branch/fast-forward/lib_pypy/_ctypes/array.py	(original)
+++ pypy/branch/fast-forward/lib_pypy/_ctypes/array.py	Fri Nov  5 21:57:52 2010
@@ -5,14 +5,6 @@
 from _ctypes.basics import keepalive_key, store_reference, ensure_objects
 from _ctypes.basics import CArgObject
 
-def _create_unicode(buffer, maxlength):
-    res = []
-    for i in range(maxlength):
-        if buffer[i] == '\x00':
-            break
-        res.append(buffer[i])
-    return u''.join(res)
-
 class ArrayMeta(_CDataMeta):
     def __new__(self, name, cls, typedict):
         res = type.__new__(self, name, cls, typedict)
@@ -44,8 +36,8 @@
                 res.raw = property(getraw, setraw)
             elif subletter == 'u':
                 def getvalue(self):
-                    # rawffi support anyone?
-                    return _create_unicode(self._buffer, self._length_)
+                    return _rawffi.wcharp2unicode(self._buffer.buffer,
+                                                  self._length_)
 
                 def setvalue(self, val):
                     # we don't want to have buffers here
@@ -79,7 +71,7 @@
         if self._type_ is c_char:
             return _rawffi.charp2string(resarray.buffer, self._length_)
         if self._type_ is c_wchar:
-            xxx
+            return _rawffi.wcharp2unicode(resarray.buffer, self._length_)
         res = self.__new__(self)
         ffiarray = self._ffiarray.fromaddress(resarray.buffer, self._length_)
         res._buffer = ffiarray

Modified: pypy/branch/fast-forward/pypy/module/_rawffi/__init__.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/_rawffi/__init__.py	(original)
+++ pypy/branch/fast-forward/pypy/module/_rawffi/__init__.py	Fri Nov  5 21:57:52 2010
@@ -22,6 +22,7 @@
         'sizeof'             : 'interp_rawffi.sizeof',
         'alignment'          : 'interp_rawffi.alignment',
         'charp2string'       : 'interp_rawffi.charp2string',
+        'wcharp2unicode'     : 'interp_rawffi.wcharp2unicode',
         'charp2rawstring'    : 'interp_rawffi.charp2rawstring',
         'CallbackPtr'        : 'callback.W_CallbackPtr',
         '_num_of_allocated_objects' : 'tracker.num_of_allocated_objects',

Modified: pypy/branch/fast-forward/pypy/module/_rawffi/interp_rawffi.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/_rawffi/interp_rawffi.py	(original)
+++ pypy/branch/fast-forward/pypy/module/_rawffi/interp_rawffi.py	Fri Nov  5 21:57:52 2010
@@ -487,6 +487,13 @@
     return space.wrap(s)
 charp2string.unwrap_spec = [ObjSpace, r_uint, int]
 
+def wcharp2unicode(space, address, maxlength=sys.maxint):
+    if address == 0:
+        return space.w_None
+    s = rffi.wcharp2unicoden(rffi.cast(rffi.CWCHARP, address), maxlength)
+    return space.wrap(s)
+wcharp2unicode.unwrap_spec = [ObjSpace, r_uint, int]
+
 def charp2rawstring(space, address, maxlength=-1):
     if maxlength == -1:
         return charp2string(space, address)

Modified: pypy/branch/fast-forward/pypy/module/_rawffi/test/test__rawffi.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/_rawffi/test/test__rawffi.py	(original)
+++ pypy/branch/fast-forward/pypy/module/_rawffi/test/test__rawffi.py	Fri Nov  5 21:57:52 2010
@@ -298,6 +298,14 @@
         arg1.free()
         arg2.free()
 
+    def test_returning_unicode(self):
+        import _rawffi
+        A = _rawffi.Array('u')
+        a = A(6, u'xx\x00\x00xx')
+        res = _rawffi.wcharp2unicode(a.buffer)
+        assert isinstance(res, unicode)
+        assert res == u'xx'
+
     def test_raw_callable(self):
         import _rawffi
         lib = _rawffi.CDLL(self.lib_name)



More information about the Pypy-commit mailing list