[pypy-svn] r51405 - pypy/dist/pypy/lib/_ctypes

fijal at codespeak.net fijal at codespeak.net
Tue Feb 12 11:53:03 CET 2008


Author: fijal
Date: Tue Feb 12 11:53:02 2008
New Revision: 51405

Modified:
   pypy/dist/pypy/lib/_ctypes/builtin.py
   pypy/dist/pypy/lib/_ctypes/function.py
   pypy/dist/pypy/lib/_ctypes/primitive.py
Log:
unicode support.


Modified: pypy/dist/pypy/lib/_ctypes/builtin.py
==============================================================================
--- pypy/dist/pypy/lib/_ctypes/builtin.py	(original)
+++ pypy/dist/pypy/lib/_ctypes/builtin.py	Tue Feb 12 11:53:02 2008
@@ -1,6 +1,10 @@
 
 import _rawffi, sys
 
+class ConvMode:
+    encoding = 'ascii'
+    errors = 'strict'
+
 _memmove_addr = ('memmove', 'libc.so.6')
 _memset_addr = ('memset', 'libc.so.6')
 
@@ -10,8 +14,11 @@
     obj = ctypes.c_char_p._CData_input(addr)[0]
     return _rawffi.charp2rawstring(obj, lgt)
 
-def set_conversion_mode(one, two):
-    pass
+def set_conversion_mode(encoding, errors):
+    old_cm = ConvMode.encoding, ConvMode.errors
+    ConvMode.errors = errors
+    ConvMode.encoding = encoding
+    return old_cm
 
 def _wstring_at_addr(addr, lgt):
     import ctypes

Modified: pypy/dist/pypy/lib/_ctypes/function.py
==============================================================================
--- pypy/dist/pypy/lib/_ctypes/function.py	(original)
+++ pypy/dist/pypy/lib/_ctypes/function.py	Tue Feb 12 11:53:02 2008
@@ -112,5 +112,5 @@
         try:
             return [argtype._CData_input(arg) for argtype, arg in
                     zip(argtypes, args)]
-        except TypeError, e:
-            raise ArgumentError(e.args[0])
+        except (UnicodeError, TypeError), e:
+            raise ArgumentError(str(e))

Modified: pypy/dist/pypy/lib/_ctypes/primitive.py
==============================================================================
--- pypy/dist/pypy/lib/_ctypes/primitive.py	(original)
+++ pypy/dist/pypy/lib/_ctypes/primitive.py	Tue Feb 12 11:53:02 2008
@@ -3,6 +3,7 @@
 SIMPLE_TYPE_CHARS = "cbBhHiIlLdfuzZqQPXOv"
 
 from _ctypes.basics import _CData, _CDataMeta, cdata_from_address
+from _ctypes.builtin import ConvMode
 
 class NULL(object):
     pass
@@ -68,7 +69,10 @@
                     return _rawffi.charp2string(addr)
 
             def _setvalue(self, value):
-                if isinstance(value, str):
+                if isinstance(value, basestring):
+                    if isinstance(value, unicode):
+                        value = value.encode(ConvMode.encoding,
+                                             ConvMode.errors)
                     array = _rawffi.Array('c')(len(value)+1, value)
                     value = array.buffer
                     # XXX free 'array' later
@@ -87,7 +91,10 @@
                     return _wstring_at_addr(addr, -1)
 
             def _setvalue(self, value):
-                if isinstance(value, str):
+                if isinstance(value, basestring):
+                    if isinstance(value, str):
+                        value = value.decode(ConvMode.encoding,
+                                             ConvMode.errors)
                     array = _rawffi.Array('u')(len(value)+1, value)
                     value = array.buffer
                     # XXX free 'array' later
@@ -142,6 +149,27 @@
                 return SimpleType.from_param(self, value)
             result.from_param = classmethod(from_param)
 
+        elif tp == 'u':
+            def _setvalue(self, val):
+                if isinstance(val, str):
+                    val = val.decode(ConvMode.encoding, ConvMode.errors)
+                # possible if we use 'ignore'
+                if val:
+                    self._buffer[0] = val
+            def _getvalue(self):
+                return self._buffer[0]
+            result.value = property(_getvalue, _setvalue)
+
+        elif tp == 'c':
+            def _setvalue(self, val):
+                if isinstance(val, unicode):
+                    val = val.encode(ConvMode.encoding, ConvMode.errors)
+                if val:
+                    self._buffer[0] = val
+            def _getvalue(self):
+                return self._buffer[0]
+            result.value = property(_getvalue, _setvalue)
+
         return result
 
     from_address = cdata_from_address



More information about the Pypy-commit mailing list