[pypy-commit] pypy cffi-1.0: ffi.NULL

arigo noreply at buildbot.pypy.org
Sat May 2 22:35:57 CEST 2015


Author: Armin Rigo <arigo at tunes.org>
Branch: cffi-1.0
Changeset: r76990:121470ee9062
Date: 2015-05-02 22:22 +0200
http://bitbucket.org/pypy/pypy/changeset/121470ee9062/

Log:	ffi.NULL

diff --git a/pypy/module/_cffi_backend/__init__.py b/pypy/module/_cffi_backend/__init__.py
--- a/pypy/module/_cffi_backend/__init__.py
+++ b/pypy/module/_cffi_backend/__init__.py
@@ -2,7 +2,7 @@
 from pypy.interpreter.mixedmodule import MixedModule
 from rpython.rlib import rdynload
 
-VERSION = "0.9.2"
+VERSION = "1.0.0"
 
 
 class Module(MixedModule):
@@ -53,6 +53,10 @@
     if sys.platform == 'win32':
         interpleveldefs['getwinerror'] = 'cerrno.getwinerror'
 
+    def startup(self, space):
+        from pypy.module._cffi_backend import ffi_obj
+        ffi_obj._startup(space)
+
 for _name in ["RTLD_LAZY", "RTLD_NOW", "RTLD_GLOBAL", "RTLD_LOCAL",
               "RTLD_NODELETE", "RTLD_NOLOAD", "RTLD_DEEPBIND"]:
     if getattr(rdynload.cConfig, _name) is not None:
diff --git a/pypy/module/_cffi_backend/ffi_obj.py b/pypy/module/_cffi_backend/ffi_obj.py
--- a/pypy/module/_cffi_backend/ffi_obj.py
+++ b/pypy/module/_cffi_backend/ffi_obj.py
@@ -4,6 +4,7 @@
 from rpython.rlib import jit, rgc
 
 from pypy.module._cffi_backend import parse_c_type, realize_c_type
+from pypy.module._cffi_backend import newtype
 
 
 ACCEPT_STRING   = 1
@@ -101,3 +102,9 @@
         new = interp2app(W_FFIObject.descr_new),
         typeof = interp2app(W_FFIObject.descr_typeof),
         )
+
+def _startup(space):
+    ctvoidp = newtype.new_pointer_type(space, newtype.new_void_type(space))
+    w_NULL = ctvoidp.cast(space.wrap(0))
+    w_ffitype = space.gettypefor(W_FFIObject)
+    w_ffitype.dict_w['NULL'] = w_NULL
diff --git a/pypy/module/_cffi_backend/test/test_ffi_obj.py b/pypy/module/_cffi_backend/test/test_ffi_obj.py
--- a/pypy/module/_cffi_backend/test/test_ffi_obj.py
+++ b/pypy/module/_cffi_backend/test/test_ffi_obj.py
@@ -38,6 +38,7 @@
         assert ffi.typeof("int(*)()") is ffi.typeof("int(*)()")
 
     def test_ffi_cache_type_globally(self):
+        import _cffi_backend as _cffi1_backend
         ffi1 = _cffi1_backend.FFI()
         ffi2 = _cffi1_backend.FFI()
         t1 = ffi1.typeof("int *")
@@ -45,11 +46,13 @@
         assert t1 is t2
 
     def test_ffi_invalid(self):
+        import _cffi_backend as _cffi1_backend
         ffi = _cffi1_backend.FFI()
         # array of 10 times an "int[]" is invalid
-        py.test.raises(ValueError, ffi.typeof, "int[10][]")
+        raises(ValueError, ffi.typeof, "int[10][]")
 
     def test_ffi_docstrings(self):
+        import _cffi_backend as _cffi1_backend
         # check that all methods of the FFI class have a docstring.
         check_type = type(_cffi1_backend.FFI.new)
         for methname in dir(_cffi1_backend.FFI):
@@ -60,21 +63,32 @@
                         methname,)
 
     def test_ffi_NULL(self):
+        import _cffi_backend as _cffi1_backend
         NULL = _cffi1_backend.FFI.NULL
         assert _cffi1_backend.FFI().typeof(NULL).cname == "void *"
 
+    def test_ffi_no_attr(self):
+        import _cffi_backend as _cffi1_backend
+        ffi = _cffi1_backend.FFI()
+        raises(AttributeError, "ffi.no_such_name")
+        raises(AttributeError, "ffi.no_such_name = 42")
+        raises(AttributeError, "del ffi.no_such_name")
+
     def test_ffi_string(self):
+        import _cffi_backend as _cffi1_backend
         ffi = _cffi1_backend.FFI()
         p = ffi.new("char[]", b"foobar\x00baz")
         assert ffi.string(p) == b"foobar"
 
     def test_ffi_errno(self):
+        import _cffi_backend as _cffi1_backend
         # xxx not really checking errno, just checking that we can read/write it
         ffi = _cffi1_backend.FFI()
         ffi.errno = 42
         assert ffi.errno == 42
 
     def test_ffi_alignof(self):
+        import _cffi_backend as _cffi1_backend
         ffi = _cffi1_backend.FFI()
         assert ffi.alignof("int") == 4
         assert ffi.alignof("int[]") == 4
@@ -84,14 +98,16 @@
         assert ffi.alignof(ffi.new("int[]", 41)) == 4
 
     def test_ffi_sizeof(self):
+        import _cffi_backend as _cffi1_backend
         ffi = _cffi1_backend.FFI()
         assert ffi.sizeof("int") == 4
-        py.test.raises(ffi.error, ffi.sizeof, "int[]")
+        raises(ffi.error, ffi.sizeof, "int[]")
         assert ffi.sizeof("int[41]") == 41 * 4
         assert ffi.sizeof(ffi.new("int[41]")) == 41 * 4
         assert ffi.sizeof(ffi.new("int[]", 41)) == 41 * 4
 
     def test_ffi_callback(self):
+        import _cffi_backend as _cffi1_backend
         ffi = _cffi1_backend.FFI()
         assert ffi.callback("int(int)", lambda x: x + 42)(10) == 52
         assert ffi.callback("int(*)(int)", lambda x: x + 42)(10) == 52
@@ -99,6 +115,7 @@
         assert ffi.callback("int(int)", lambda x: x + "", error=-66)(10) == -66
 
     def test_ffi_callback_decorator(self):
+        import _cffi_backend as _cffi1_backend
         ffi = _cffi1_backend.FFI()
         assert ffi.callback(ffi.typeof("int(*)(int)"))(lambda x: x + 42)(10) == 52
         deco = ffi.callback("int(int)", error=-66)
@@ -106,6 +123,7 @@
         assert deco(lambda x: x + 42)(10) == 52
 
     def test_ffi_getctype(self):
+        import _cffi_backend as _cffi1_backend
         ffi = _cffi1_backend.FFI()
         assert ffi.getctype("int") == "int"
         assert ffi.getctype("int", 'x') == "int x"
@@ -124,6 +142,7 @@
         assert ffi.getctype("int[5]", ' ** foo ') == "int(** foo)[5]"
 
     def test_addressof(self):
+        import _cffi_backend as _cffi1_backend
         ffi = _cffi1_backend.FFI()
         a = ffi.new("int[10]")
         b = ffi.addressof(a, 5)
@@ -131,6 +150,7 @@
         assert a[7] == -123
 
     def test_handle(self):
+        import _cffi_backend as _cffi1_backend
         ffi = _cffi1_backend.FFI()
         x = [2, 4, 6]
         xp = ffi.new_handle(x)


More information about the pypy-commit mailing list