[pypy-commit] cffi default: Test for 'ffi.cast("foo*", None) == None'. Documentation.

arigo noreply at buildbot.pypy.org
Sun Jun 17 11:21:56 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r398:b7a980b11462
Date: 2012-06-17 11:21 +0200
http://bitbucket.org/cffi/cffi/changeset/b7a980b11462/

Log:	Test for 'ffi.cast("foo*", None) == None'. Documentation.

diff --git a/c/_ffi_backend.c b/c/_ffi_backend.c
--- a/c/_ffi_backend.c
+++ b/c/_ffi_backend.c
@@ -1097,12 +1097,18 @@
         goto Unimplemented;
 
     assert(CData_Check(v));
-    if (!CData_Check(w))
+    obv = (CDataObject *)v;
+
+    if (w == Py_None) {
+        equal = (obv->c_data == NULL);
+    }
+    else if (CData_Check(w)) {
+        obw = (CDataObject *)w;
+        equal = (obv->c_type == obw->c_type) && (obv->c_data == obw->c_data);
+    }
+    else
         goto Unimplemented;
 
-    obv = (CDataObject *)v;
-    obw = (CDataObject *)w;
-    equal = (obv->c_type == obw->c_type) && (obv->c_data == obw->c_data);
     return (equal ^ (op == Py_NE)) ? Py_True : Py_False;
 
  Unimplemented:
diff --git a/cffi/backend_ctypes.py b/cffi/backend_ctypes.py
--- a/cffi/backend_ctypes.py
+++ b/cffi/backend_ctypes.py
@@ -112,6 +112,8 @@
         return bool(self._address)
 
     def __eq__(self, other):
+        if other is None:
+            return not bool(self._address)
         return (type(self) is type(other) and
                 self._address == other._address)
 
diff --git a/doc/source/index.rst b/doc/source/index.rst
--- a/doc/source/index.rst
+++ b/doc/source/index.rst
@@ -391,6 +391,9 @@
     >>> int(x)
     42
 
+Similarly, this is the only way to get cdata objects for ``NULL``
+pointers, which are normally returned as None.
+
 The initializer given as the optional second argument to ``ffi.new()``
 can be mostly anything that you would use as an initializer for C code,
 with lists or tuples instead of using the C syntax ``{ .., .., .. }``.
diff --git a/testing/backend_tests.py b/testing/backend_tests.py
--- a/testing/backend_tests.py
+++ b/testing/backend_tests.py
@@ -174,8 +174,11 @@
     def test_pointer_direct(self):
         ffi = FFI(backend=self.Backend())
         p = ffi.cast("int*", 0)
+        assert p is not None
         assert bool(p) is False
         assert p == ffi.cast("int*", 0)
+        assert p == ffi.cast("int*", None)
+        assert p == None == p
         a = ffi.new("int[]", [123, 456])
         p = ffi.cast("int*", a)
         assert bool(p) is True


More information about the pypy-commit mailing list