[pypy-svn] pypy jitypes2: make more types _ffi compatible

antocuni commits-noreply at bitbucket.org
Mon Jan 31 18:27:41 CET 2011


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: jitypes2
Changeset: r41502:5829c18f253a
Date: 2011-01-28 16:40 +0100
http://bitbucket.org/pypy/pypy/changeset/5829c18f253a/

Log:	make more types _ffi compatible

diff --git a/lib_pypy/_ctypes/primitive.py b/lib_pypy/_ctypes/primitive.py
--- a/lib_pypy/_ctypes/primitive.py
+++ b/lib_pypy/_ctypes/primitive.py
@@ -140,6 +140,7 @@
                     value = 0
                 self._buffer[0] = value
             result.value = property(_getvalue, _setvalue)
+
         elif tp == 'Z':
             # c_wchar_p
             def _getvalue(self):
@@ -247,6 +248,12 @@
                 else:
                     self._buffer[0] = 0  # VARIANT_FALSE
             result.value = property(_getvalue, _setvalue)
+
+        # make pointer-types compatible with the _ffi fast path
+        if result._is_pointer_like():
+            def _as_ffi_pointer_(self):
+                return self._get_buffer_value()
+            result._as_ffi_pointer_ = _as_ffi_pointer_
             
         return result
 

diff --git a/pypy/module/test_lib_pypy/ctypes_tests/test_fastpath.py b/pypy/module/test_lib_pypy/ctypes_tests/test_fastpath.py
--- a/pypy/module/test_lib_pypy/ctypes_tests/test_fastpath.py
+++ b/pypy/module/test_lib_pypy/ctypes_tests/test_fastpath.py
@@ -1,4 +1,4 @@
-from ctypes import CDLL, POINTER, pointer, c_byte, c_int
+from ctypes import CDLL, POINTER, pointer, c_byte, c_int, c_char_p
 import sys
 import py
 from support import BaseCTypesTestChecker
@@ -44,3 +44,21 @@
         result = f(pointer(v))
         assert type(result) == POINTER(c_int)
         assert result.contents.value == 42
+
+    def test_simple_pointer_args(self):
+        f = dll.my_strchr
+        f.argtypes = [c_char_p, c_int]
+        f.restype = c_char_p
+        mystr = c_char_p("abcd")
+        result = f(mystr, ord("b"))
+        assert result == "bcd"
+
+    @py.test.mark.xfail
+    def test_strings(self):
+        f = dll.my_strchr
+        f.argtypes = [c_char_p, c_int]
+        f.restype = c_char_p
+        # python strings need to be converted to c_char_p, but this is
+        # supported only in the slow path so far
+        result = f("abcd", ord("b"))
+        assert result == "bcd"


More information about the Pypy-commit mailing list