[Python-checkins] cpython (2.7): Fixup repr for dict_proxy objects.

raymond.hettinger python-checkins at python.org
Thu Jun 30 01:51:37 CEST 2011


http://hg.python.org/cpython/rev/9a0b6c07b488
changeset:   71086:9a0b6c07b488
branch:      2.7
parent:      71055:cdfcd00873cd
user:        Raymond Hettinger <python at rcn.com>
date:        Thu Jun 30 00:44:36 2011 +0100
summary:
  Fixup repr for dict_proxy objects.

files:
  Lib/test/test_descr.py |   4 ++++
  Misc/NEWS              |   2 ++
  Objects/descrobject.c  |  16 +++++++++++++++-
  3 files changed, 21 insertions(+), 1 deletions(-)


diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py
--- a/Lib/test/test_descr.py
+++ b/Lib/test/test_descr.py
@@ -4589,6 +4589,10 @@
                 pass
         self.C = C
 
+    def test_repr(self):
+        self.assertIn('dict_proxy({', repr(vars(self.C)))
+        self.assertIn("'meth':", repr(vars(self.C)))
+
     def test_iter_keys(self):
         # Testing dict-proxy iterkeys...
         keys = [ key for key in self.C.__dict__.iterkeys() ]
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,8 @@
   the following case: sys.stdin.read() stopped with CTRL+d (end of file),
   raw_input() interrupted by CTRL+c.
 
+- dict_proxy objects now display their contents rather than just the class name.
+
 Library
 -------
 
diff --git a/Objects/descrobject.c b/Objects/descrobject.c
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -801,6 +801,20 @@
     return PyObject_Str(pp->dict);
 }
 
+static PyObject *
+proxy_repr(proxyobject *pp)
+{
+    PyObject *dictrepr;
+    PyObject *result;
+
+    dictrepr = PyObject_Repr(pp->dict);
+    if (dictrepr == NULL)
+        return NULL;
+    result = PyString_FromFormat("dict_proxy(%s)", PyString_AS_STRING(dictrepr));
+    Py_DECREF(dictrepr);
+    return result;
+}
+
 static int
 proxy_traverse(PyObject *self, visitproc visit, void *arg)
 {
@@ -832,7 +846,7 @@
     0,                                          /* tp_getattr */
     0,                                          /* tp_setattr */
     (cmpfunc)proxy_compare,                     /* tp_compare */
-    0,                                          /* tp_repr */
+    (reprfunc)proxy_repr,                       /* tp_repr */
     0,                                          /* tp_as_number */
     &proxy_as_sequence,                         /* tp_as_sequence */
     &proxy_as_mapping,                          /* tp_as_mapping */

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list