[pypy-commit] cffi default: Cast from None (as a NULL pointer) to an integer.

arigo noreply at buildbot.pypy.org
Sun Jun 17 16:17:01 CEST 2012


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r405:80faa502d66d
Date: 2012-06-17 15:43 +0200
http://bitbucket.org/cffi/cffi/changeset/80faa502d66d/

Log:	Cast from None (as a NULL pointer) to an integer.

diff --git a/c/_ffi_backend.c b/c/_ffi_backend.c
--- a/c/_ffi_backend.c
+++ b/c/_ffi_backend.c
@@ -1833,6 +1833,9 @@
             value = (unsigned char)PyString_AS_STRING(ob)[0];
         }
     }
+    else if (ob == Py_None) {
+        value = 0;
+    }
     else {
         value = _my_PyLong_AsUnsignedLongLong(ob, 0);
         if (value == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
diff --git a/cffi/backend_ctypes.py b/cffi/backend_ctypes.py
--- a/cffi/backend_ctypes.py
+++ b/cffi/backend_ctypes.py
@@ -260,6 +260,8 @@
                 source = source._cast_to_integer()
             elif isinstance(source, str):
                 source = ord(source)
+            elif source is None:
+                source = 0
             else:
                 raise TypeError("bad type for cast to %r: %r" %
                                 (CTypesPrimitive, type(source).__name__))
diff --git a/testing/backend_tests.py b/testing/backend_tests.py
--- a/testing/backend_tests.py
+++ b/testing/backend_tests.py
@@ -594,6 +594,7 @@
         q = ffi.cast("short*", l1)
         assert q == ffi.cast("short*", int(l1))
         assert q[0] == 0x1234
+        assert int(ffi.cast("intptr_t", None)) == 0
 
     def test_cast_functionptr_and_int(self):
         ffi = FFI(backend=self.Backend())


More information about the pypy-commit mailing list