[Python-checkins] r51317 - in python/trunk: Lib/ctypes/__init__.py Lib/ctypes/test/test_python_api.py Misc/NEWS Modules/_ctypes/cfield.c

thomas.heller python-checkins at python.org
Wed Aug 16 16:07:51 CEST 2006


Author: thomas.heller
Date: Wed Aug 16 16:07:44 2006
New Revision: 51317

Modified:
   python/trunk/Lib/ctypes/__init__.py
   python/trunk/Lib/ctypes/test/test_python_api.py
   python/trunk/Misc/NEWS
   python/trunk/Modules/_ctypes/cfield.c
Log:
The __repr__ method of a NULL py_object does no longer raise an
exception.  Remove a stray '?' character from the exception text
when the value is retrieved of such an object.

Includes tests.

Modified: python/trunk/Lib/ctypes/__init__.py
==============================================================================
--- python/trunk/Lib/ctypes/__init__.py	(original)
+++ python/trunk/Lib/ctypes/__init__.py	Wed Aug 16 16:07:44 2006
@@ -135,6 +135,11 @@
 
 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"

Modified: python/trunk/Lib/ctypes/test/test_python_api.py
==============================================================================
--- python/trunk/Lib/ctypes/test/test_python_api.py	(original)
+++ python/trunk/Lib/ctypes/test/test_python_api.py	Wed Aug 16 16:07:44 2006
@@ -78,5 +78,10 @@
         # 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()

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Wed Aug 16 16:07:44 2006
@@ -64,6 +64,9 @@
 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

Modified: python/trunk/Modules/_ctypes/cfield.c
==============================================================================
--- python/trunk/Modules/_ctypes/cfield.c	(original)
+++ python/trunk/Modules/_ctypes/cfield.c	Wed Aug 16 16:07:44 2006
@@ -1100,7 +1100,7 @@
 		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);


More information about the Python-checkins mailing list