]> granicus.if.org Git - python/commitdiff
The __repr__ method of a NULL py_object does no longer raise an
authorThomas Heller <theller@ctypes.org>
Wed, 16 Aug 2006 14:07:44 +0000 (14:07 +0000)
committerThomas Heller <theller@ctypes.org>
Wed, 16 Aug 2006 14:07:44 +0000 (14:07 +0000)
exception.  Remove a stray '?' character from the exception text
when the value is retrieved of such an object.

Includes tests.

Lib/ctypes/__init__.py
Lib/ctypes/test/test_python_api.py
Misc/NEWS
Modules/_ctypes/cfield.c

index 7690796e4ab957d88fb351a2deba6f36665df85c..61923b61e53de03d59a4b49e0883222aea7abb31 100644 (file)
@@ -135,6 +135,11 @@ from _ctypes import _SimpleCData
 
 class py_object(_SimpleCData):
     _type_ = "O"
+    def __repr__(self):
+        try:
+            return super(py_object, self).__repr__()
+        except ValueError:
+            return "%s(<NULL>)" % type(self).__name__
 
 class c_short(_SimpleCData):
     _type_ = "h"
index 78e02317cf09469579325067fb7fce689d43b7dd..9d134740f5afb34988e6cf7add17ce473fec4c68 100644 (file)
@@ -78,5 +78,10 @@ class PythonAPITestCase(unittest.TestCase):
         # not enough arguments
         self.failUnlessRaises(TypeError, PyOS_snprintf, buf)
 
+    def test_pyobject_repr(self):
+        self.failUnlessEqual(repr(py_object()), "py_object(<NULL>)")
+        self.failUnlessEqual(repr(py_object(42)), "py_object(42)")
+        self.failUnlessEqual(repr(py_object(object)), "py_object(%r)" % object)
+
 if __name__ == "__main__":
     unittest.main()
index 587ed1e128f37783bc0be7fdd201e9f73335d714..712a99781682c4b59ae9d7fbf64ff51f7cc5b7fd 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -64,6 +64,9 @@ Core and builtins
 Library
 -------
 
+- The __repr__ method a NULL ctypes.py_object() does no longer raise
+  an exception.
+
 - uuid.UUID now has a bytes_le attribute. This returns the UUID in 
   little-endian byte order for Windows. In addition, uuid.py had some
   workarounds for clocks with low resolution, to stop the code yielding
index 62a5fe6eb314afefb5b4623697d174802daacba7..c16a387464f9c928a0dc89daf526a6e2fbbddca0 100644 (file)
@@ -1100,7 +1100,7 @@ O_get(void *ptr, unsigned size)
                if (!PyErr_Occurred())
                        /* Set an error if not yet set */
                        PyErr_SetString(PyExc_ValueError,
-                                       "PyObject is NULL?");
+                                       "PyObject is NULL");
                return NULL;
        }
        Py_INCREF(ob);