]> granicus.if.org Git - python/commitdiff
Issues #10017 and #14998: Fix TypeError using pprint on dictionaries with unorderable...
authorFlorent Xicluna <florent.xicluna@gmail.com>
Sat, 21 Jul 2012 09:17:38 +0000 (11:17 +0200)
committerFlorent Xicluna <florent.xicluna@gmail.com>
Sat, 21 Jul 2012 09:17:38 +0000 (11:17 +0200)
Lib/pprint.py
Lib/test/test_pprint.py
Misc/NEWS

index b8417f592255db70fe33df97c6fe5c9b8d9e3c59..ae96dde1e0266c65d0e8f4cc7ce96f5d930ab4ed 100644 (file)
@@ -86,7 +86,11 @@ class _safe_key:
         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))
index 4e53cd8a015e5c78ad8306137668457ccf9e7cf3..7daf4a7b60c157a183ef2261eca58aebc2d8864a 100644 (file)
@@ -462,6 +462,15 @@ class QueryTestCase(unittest.TestCase):
         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):
index 2a407233982a8daae4263e07cd234039ffbefa63..3d4d049852b3818c4b660f94d5eb8b8a6008c27e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -98,6 +98,9 @@ Core and Builtins
 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.