From: Thomas Heller Date: Thu, 29 May 2008 19:42:34 +0000 (+0000) Subject: ctypes NULL function pointers have a boolean False value now. X-Git-Tag: v2.6b1~204 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9287acf83df56a11aaa001657df652ed6804b105;p=python ctypes NULL function pointers have a boolean False value now. --- diff --git a/Lib/ctypes/test/test_pointers.py b/Lib/ctypes/test/test_pointers.py index 586655af7d..fd42c8029b 100644 --- a/Lib/ctypes/test/test_pointers.py +++ b/Lib/ctypes/test/test_pointers.py @@ -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() diff --git a/Misc/NEWS b/Misc/NEWS index 2838887217..90f7d61141 100644 --- 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 (````) in XMLRPC transfers. diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 5b2a0546e4..740b7f6e15 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -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",