[Python-checkins] cpython (3.2): Issues #10017 and #14998: Fix TypeError using pprint on dictionaries with

florent.xicluna python-checkins at python.org
Sat Jul 21 11:23:14 CEST 2012


http://hg.python.org/cpython/rev/03cda5360dc6
changeset:   78212:03cda5360dc6
branch:      3.2
parent:      78206:034ff986019d
user:        Florent Xicluna <florent.xicluna at gmail.com>
date:        Sat Jul 21 11:17:38 2012 +0200
summary:
  Issues #10017 and #14998: Fix TypeError using pprint on dictionaries with unorderable key.

files:
  Lib/pprint.py           |  6 +++++-
  Lib/test/test_pprint.py |  9 +++++++++
  Misc/NEWS               |  3 +++
  3 files changed, 17 insertions(+), 1 deletions(-)


diff --git a/Lib/pprint.py b/Lib/pprint.py
--- a/Lib/pprint.py
+++ b/Lib/pprint.py
@@ -86,7 +86,11 @@
         self.obj = obj
 
     def __lt__(self, other):
-        rv = self.obj.__lt__(other.obj)
+        try:
+            rv = self.obj.__lt__(other.obj)
+        except TypeError:
+            rv = NotImplemented
+
         if rv is NotImplemented:
             rv = (str(type(self.obj)), id(self.obj)) < \
                  (str(type(other.obj)), id(other.obj))
diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py
--- a/Lib/test/test_pprint.py
+++ b/Lib/test/test_pprint.py
@@ -462,6 +462,15 @@
         self.assertEqual(clean(pprint.pformat(dict.fromkeys(keys))),
             '{' + ','.join('%r:None' % k for k in skeys) + '}')
 
+        # Issue 10017: TypeError on user-defined types as dict keys.
+        self.assertEqual(pprint.pformat({Unorderable: 0, 1: 0}),
+                         '{1: 0, ' + repr(Unorderable) +': 0}')
+
+        # Issue 14998: TypeError on tuples with NoneTypes as dict keys.
+        self.assertEqual(pprint.pformat({(1,): 0, (None,): 0}),
+                         '{(1,): 0, (None,): 0}')
+
+
 class DottedPrettyPrinter(pprint.PrettyPrinter):
 
     def format(self, object, context, maxlevels, level):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -98,6 +98,9 @@
 Library
 -------
 
+- Issues #10017 and #14998: Fix TypeError using pprint on dictionaries with
+  user-defined types as keys or other unorderable keys.
+
 - Issue #14635: telnetlib will use poll() rather than select() when possible
   to avoid failing due to the select() file descriptor limit.
 

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


More information about the Python-checkins mailing list