]> granicus.if.org Git - python/commitdiff
#5587: add a repr to dict_proxy objects. Patch by David Stanek and Daniel Urban.
authorEzio Melotti <ezio.melotti@gmail.com>
Sat, 18 Dec 2010 14:59:43 +0000 (14:59 +0000)
committerEzio Melotti <ezio.melotti@gmail.com>
Sat, 18 Dec 2010 14:59:43 +0000 (14:59 +0000)
Lib/test/test_descr.py
Misc/NEWS
Objects/descrobject.c

index 233b7cb0ac7a17f51a751b6bdb99cbbc51f70967..115d1e860ae6eb06437175278b798b8ab1b0f5a4 100644 (file)
@@ -4268,6 +4268,11 @@ class DictProxyTests(unittest.TestCase):
             pass
         self.assertEqual(type(C.__dict__), type(B.__dict__))
 
+    def test_repr(self):
+        # Testing dict_proxy.__repr__
+        dict_ = {k: v for k, v in self.C.__dict__.items()}
+        self.assertEqual(repr(self.C.__dict__), 'dict_proxy({!r})'.format(dict_))
+
 
 class PTypesLongInitTest(unittest.TestCase):
     # This is in its own TestCase so that it can be run before any other tests.
index fdda4826167c3c999c761e895fdccb22918c7230..020ddd1000eb0d31869be06131f2057aff8ec4a9 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -17,6 +17,9 @@ Core and Builtins
   rather than the Py_IsInitialized flag, avoiding a Fatal Python
   error in certain circumstances when an import is done in __del__.
 
+- Issue #5587: add a repr to dict_proxy objects.  Patch by David Stanek and
+  Daniel Urban.
+
 Library
 -------
 
index 967110291d5c4df1901af32f241b23c2f8d16eb1..11c68d336c0fe9defb74e00df4e321038cb9190c 100644 (file)
@@ -766,6 +766,12 @@ proxy_str(proxyobject *pp)
     return PyObject_Str(pp->dict);
 }
 
+static PyObject *
+proxy_repr(proxyobject *pp)
+{
+    return PyUnicode_FromFormat("dict_proxy(%R)", pp->dict);
+}
+
 static int
 proxy_traverse(PyObject *self, visitproc visit, void *arg)
 {
@@ -791,7 +797,7 @@ PyTypeObject PyDictProxy_Type = {
     0,                                          /* tp_getattr */
     0,                                          /* tp_setattr */
     0,                                          /* tp_reserved */
-    0,                                          /* tp_repr */
+    (reprfunc)proxy_repr,                       /* tp_repr */
     0,                                          /* tp_as_number */
     &proxy_as_sequence,                         /* tp_as_sequence */
     &proxy_as_mapping,                          /* tp_as_mapping */