]> granicus.if.org Git - python/commitdiff
ctypes NULL function pointers have a boolean False value now.
authorThomas Heller <theller@ctypes.org>
Thu, 29 May 2008 19:42:34 +0000 (19:42 +0000)
committerThomas Heller <theller@ctypes.org>
Thu, 29 May 2008 19:42:34 +0000 (19:42 +0000)
Lib/ctypes/test/test_pointers.py
Misc/NEWS
Modules/_ctypes/_ctypes.c

index 586655af7df13f5a57ca69d863bd068d6a64e7c6..fd42c8029b421ea8564ce287b6cd27a7150ca7de 100644 (file)
@@ -175,5 +175,13 @@ class PointersTestCase(unittest.TestCase):
         self.assertRaises(TypeError, c_void_p, 3.14) # make sure floats are NOT accepted
         self.assertRaises(TypeError, c_void_p, object()) # nor other objects
 
+    def test_pointers_bool(self):
+        # NULL pointers have a boolean False value, non-NULL pointers True.
+        self.failUnlessEqual(bool(POINTER(c_int)()), False)
+        self.failUnlessEqual(bool(pointer(c_int())), True)
+
+        self.failUnlessEqual(bool(CFUNCTYPE(None)(0)), False)
+        self.failUnlessEqual(bool(CFUNCTYPE(None)(42)), True)
+
 if __name__ == '__main__':
     unittest.main()
index 28388872173fd53ed9ff19806cb10b5466232d15..90f7d611417400cee5d7ddb328bc745a89cc81c3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -63,6 +63,9 @@ Extension Modules
 Library
 -------
 
+- Issue #1797 (partial fix): ctypes NULL function pointers have a
+  False boolean value now.
+
 - Issue #2985: Allow 64-bit integer responses (``<i8>``) in XMLRPC
   transfers.
 
index 5b2a0546e45961157235b729123f770e999c7b1e..740b7f6e15f1865a3012a84da3bf0eb9f83b2c39 100644 (file)
@@ -3784,6 +3784,26 @@ CFuncPtr_repr(CFuncPtrObject *self)
                                   self);
 }
 
+static int
+Pointer_nonzero(CDataObject *self)
+{
+       return *(void **)self->b_ptr != NULL;
+}
+
+static PyNumberMethods Pointer_as_number = {
+       0, /* nb_add */
+       0, /* nb_subtract */
+       0, /* nb_multiply */
+       0, /* nb_divide */
+       0, /* nb_remainder */
+       0, /* nb_divmod */
+       0, /* nb_power */
+       0, /* nb_negative */
+       0, /* nb_positive */
+       0, /* nb_absolute */
+       (inquiry)Pointer_nonzero, /* nb_nonzero */
+};
+
 PyTypeObject CFuncPtr_Type = {
        PyVarObject_HEAD_INIT(NULL, 0)
        "_ctypes.CFuncPtr",
@@ -3795,7 +3815,7 @@ PyTypeObject CFuncPtr_Type = {
        0,                                      /* tp_setattr */
        0,                                      /* tp_compare */
        (reprfunc)CFuncPtr_repr,                /* tp_repr */
-       0,                                      /* tp_as_number */
+       &Pointer_as_number,                     /* tp_as_number */
        0,                                      /* tp_as_sequence */
        0,                                      /* tp_as_mapping */
        0,                                      /* tp_hash */
@@ -5003,26 +5023,6 @@ static PyMappingMethods Pointer_as_mapping = {
        Pointer_subscript,
 };
 
-static int
-Pointer_nonzero(CDataObject *self)
-{
-       return *(void **)self->b_ptr != NULL;
-}
-
-static PyNumberMethods Pointer_as_number = {
-       0, /* nb_add */
-       0, /* nb_subtract */
-       0, /* nb_multiply */
-       0, /* nb_divide */
-       0, /* nb_remainder */
-       0, /* nb_divmod */
-       0, /* nb_power */
-       0, /* nb_negative */
-       0, /* nb_positive */
-       0, /* nb_absolute */
-       (inquiry)Pointer_nonzero, /* nb_nonzero */
-};
-
 PyTypeObject Pointer_Type = {
        PyVarObject_HEAD_INIT(NULL, 0)
        "_ctypes._Pointer",