Armin Rigo avatar Armin Rigo committed db89e70

Use None as a NULL pointer.

Comments (0)

Files changed (2)

ffi/backend_ctypes.py

                 return self._address
 
             def __eq__(self, other):
-                return (isinstance(other, CTypesPtr) and
-                        self._address == other._address)
+                return ((isinstance(other, CTypesPtr) and
+                         self._address == other._address)
+                        or (self._address == 0 and other is None))
 
             def __ne__(self, other):
-                return not (isinstance(other, CTypesPtr) and
-                            self._address == other._address)
+                return not self.__eq__(other)
 
             def __getitem__(self, index):
                 return BItem._from_ctypes(self._as_ctype_ptr[index])
 
             @staticmethod
             def _to_ctypes(value):
-                return ctypes.cast(value._convert_to_address_of(BItem),
-                                   CTypesPtr._ctype)
+                if value is None:
+                    address = 0
+                else:
+                    address = value._convert_to_address_of(BItem)
+                return ctypes.cast(address, CTypesPtr._ctype)
 
             @staticmethod
             def _from_ctypes(ctypes_ptr):
+                if not ctypes_ptr:
+                    return None
                 self = CTypesPtr.__new__(CTypesPtr)
                 self._address = ctypes.addressof(ctypes_ptr.contents)
                 self._as_ctype_ptr = ctypes_ptr

testing/backend_tests.py

         assert p[0] == '\xff'
         p = ffi.new("char[]", "abcd")
         assert len(p) == 4
+
+    def test_none_as_null(self):
+        ffi = FFI(backend=self.Backend())
+        p = ffi.new("int*[1]")
+        assert p[0] is None
+        #
+        n = ffi.new("int[1]", [99])
+        p = ffi.new("int*[1]", [n])
+        assert p[0][0] == 99
+        p[0] = None
+        assert ffi.new("int*") == None
+        assert ffi.new("int*") is not None
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.