Author: Armin Rigo <[email protected]>
Branch: 
Changeset: r1017:f5ee4b029afc
Date: 2012-10-29 09:17 +0100
http://bitbucket.org/cffi/cffi/changeset/f5ee4b029afc/

Log:    Fix the tests and kill support in the ctypes backend.

diff --git a/cffi/backend_ctypes.py b/cffi/backend_ctypes.py
--- a/cffi/backend_ctypes.py
+++ b/cffi/backend_ctypes.py
@@ -980,48 +980,7 @@
         return b._to_string(maxlen)
 
     def buffer(self, bptr, size=-1):
-        if sys.version_info >= (2, 7):
-            # buf = bptr._as_ctype_ptr
-            # return memoryview(buf.contents)
-            if isinstance(bptr, CTypesGenericPtr):
-                buf = bptr._as_ctype_ptr
-                val = buf.contents
-            elif isinstance(bptr, CTypesGenericArray):
-                buf = bptr._blob
-                val = bptr._blob
-            else:
-                raise TypeError(bptr)
-            class Hack(ctypes.Union):
-                _fields_ = [('stupid', type(val))]
-            ptr = ctypes.cast(buf, ctypes.POINTER(Hack))
-            view = memoryview(ptr.contents)
-            try:
-                view = view.cast('B')
-            except AttributeError:
-                raise NotImplementedError("buffer() with ctypes backend "
-                                          "in Python < 3.3")
-            if size >= 0:
-                view = view[:size]
-            return view
-
-        # haaaaaaaaaaaack
-        if '__pypy__' in sys.builtin_module_names:
-            raise NotImplementedError("PyPy: ffi.buffer() with ctypes backend")
-        call = ctypes.pythonapi.PyBuffer_FromReadWriteMemory
-        call.argtypes = (ctypes.c_void_p, ctypes.c_size_t)
-        call.restype = ctypes.py_object
-        #
-        if isinstance(bptr, CTypesGenericPtr):
-            if size < 0:
-                size = bptr._bitem_size
-            return call(bptr._as_ctype_ptr, size)
-        elif isinstance(bptr, CTypesGenericArray):
-            if size < 0:
-                size = ctypes.sizeof(bptr._blob)
-            return call(ctypes.pointer(bptr._blob), size)
-        else:
-            raise TypeError("pointer or array argument expected, got %r" %
-                            (type(bptr).__name__,))
+        raise NotImplementedError("buffer() with ctypes backend")
 
     def sizeof(self, cdata_or_BType):
         if isinstance(cdata_or_BType, CTypesData):
diff --git a/testing/backend_tests.py b/testing/backend_tests.py
--- a/testing/backend_tests.py
+++ b/testing/backend_tests.py
@@ -1074,13 +1074,8 @@
             b = ffi.buffer(a)
         except NotImplementedError as e:
             py.test.skip(str(e))
-        if sys.version_info < (2, 7):
-            assert type(b) is buffer
-            content = str(b)
-        else:
-            assert type(b) is memoryview
-            content = b.tobytes()
-        assert len(content) == 2
+        content = b[:]
+        assert len(content) == len(b) == 2
         if sys.byteorder == 'little':
             assert content == b'\x64\x00'
             assert b[0] == bufitem(b'\x64')
@@ -1098,12 +1093,7 @@
             b = ffi.buffer(a)
         except NotImplementedError as e:
             py.test.skip(str(e))
-        if sys.version_info < (2, 7):
-            assert type(b) is buffer
-            content = str(b)
-        else:
-            assert type(b) is memoryview
-            content = b.tobytes()
+        content = b[:]
         if sys.byteorder == 'little':
             assert content.startswith(b'\x64\x00\x00\x00\x65\x00\x00\x00')
             b[4] = bufitem(b'\x45')
@@ -1120,12 +1110,7 @@
             b = ffi.buffer(a, 1)
         except NotImplementedError as e:
             py.test.skip(str(e))
-        if sys.version_info < (2, 7):
-            assert type(b) is buffer
-            content = str(b)
-        else:
-            assert type(b) is memoryview
-            content = b.tobytes()
+        content = b[:]
         assert len(content) == 1
         if sys.byteorder == 'little':
             assert content == b'\x43'
@@ -1144,10 +1129,7 @@
             ffi.buffer(a1)
         except NotImplementedError as e:
             py.test.skip(str(e))
-        if sys.version_info < (3,):
-            assert ffi.buffer(a1)[:] == ffi.buffer(a2, 4*10)[:]
-        else:
-            assert ffi.buffer(a1).tobytes() == ffi.buffer(a2, 4*10).tobytes()
+        assert ffi.buffer(a1)[:] == ffi.buffer(a2, 4*10)[:]
 
     def test_ffi_buffer_with_file(self):
         ffi = FFI(backend=self.Backend())
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to