[pypy-svn] r61681 - in pypy/trunk/pypy/lib: _ctypes app_test/ctypes_tests

afa at codespeak.net afa at codespeak.net
Tue Feb 10 01:41:02 CET 2009


Author: afa
Date: Tue Feb 10 01:40:59 2009
New Revision: 61681

Modified:
   pypy/trunk/pypy/lib/_ctypes/function.py
   pypy/trunk/pypy/lib/app_test/ctypes_tests/test_guess_argtypes.py
Log:
When guessing argument types, a unicode string should be converted to a c_wchar_p buffer.
This matches CPython behavior.


Modified: pypy/trunk/pypy/lib/_ctypes/function.py
==============================================================================
--- pypy/trunk/pypy/lib/_ctypes/function.py	(original)
+++ pypy/trunk/pypy/lib/_ctypes/function.py	Tue Feb 10 01:40:59 2009
@@ -171,13 +171,16 @@
     @staticmethod
     def _guess_argtypes(args):
         from _ctypes import _CData
-        from ctypes import c_char_p, c_void_p, c_int, Array, Structure
+        from ctypes import c_char_p, c_wchar_p, c_void_p, c_int
+        from ctypes import Array, Structure
         res = []
         for arg in args:
             if hasattr(arg, '_as_parameter_'):
                 arg = arg._as_parameter_
-            if isinstance(arg, basestring):
+            if isinstance(arg, str):
                 res.append(c_char_p)
+            elif isinstance(arg, unicode):
+                res.append(c_wchar_p)
             elif isinstance(arg, _CData):
                 res.append(type(arg))
             elif arg is None:

Modified: pypy/trunk/pypy/lib/app_test/ctypes_tests/test_guess_argtypes.py
==============================================================================
--- pypy/trunk/pypy/lib/app_test/ctypes_tests/test_guess_argtypes.py	(original)
+++ pypy/trunk/pypy/lib/app_test/ctypes_tests/test_guess_argtypes.py	Tue Feb 10 01:40:59 2009
@@ -18,7 +18,7 @@
     assert guess(['xca']) == [c_char_p]
     assert guess([None]) == [c_void_p]
     assert guess([c_int(3)]) == [c_int]
-    assert guess([u'xca']) == [c_char_p]
+    assert guess([u'xca']) == [c_wchar_p]
 
     class Stuff:
         pass
@@ -27,4 +27,8 @@
     
     assert guess([s]) == [c_void_p]
 
-    # not sure what else....
+def test_guess_unicode():
+    dll = ctypes.CDLL(str(conftest.sofile))
+    wcslen = dll.my_wcslen
+    text = u"Some long unicode string"
+    assert ctypes.cdll.msvcrt.wcslen(text) == len(text)



More information about the Pypy-commit mailing list