summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Heller <theller@ctypes.org>2008-05-29 19:42:34 (GMT)
committerThomas Heller <theller@ctypes.org>2008-05-29 19:42:34 (GMT)
commit9287acf83df56a11aaa001657df652ed6804b105 (patch)
tree616f535ee369132b23cd26fad0b16a31d1257f5e
parenta52b244cc10a7644d86c4f09b761b3611a65378d (diff)
downloadcpython-9287acf83df56a11aaa001657df652ed6804b105.zip
cpython-9287acf83df56a11aaa001657df652ed6804b105.tar.gz
cpython-9287acf83df56a11aaa001657df652ed6804b105.tar.bz2
ctypes NULL function pointers have a boolean False value now.
-rw-r--r--Lib/ctypes/test/test_pointers.py8
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/_ctypes/_ctypes.c42
3 files changed, 32 insertions, 21 deletions
diff --git a/Lib/ctypes/test/test_pointers.py b/Lib/ctypes/test/test_pointers.py
index 586655a..fd42c80 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 2838887..90f7d61 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 (``<i8>``) in XMLRPC
transfers.
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 5b2a054..740b7f6 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",