Commit 0882c6be authored by Florent Xicluna's avatar Florent Xicluna

Issues #10017 and #14998: Fix TypeError using pprint on dictionaries with unorderable key.

parents 3d9f257c e4a9093a
......@@ -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))
......
......@@ -466,6 +466,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):
......
......@@ -48,10 +48,13 @@ Core and Builtins
- Issue 15307: Virtual environments now use symlinks with framework builds
on Mac OS X, like other POSIX builds.
Library
-------
- Issues #10017 and #14998: Fix TypeError using pprint on dictionaries with
user-defined types as keys or other unorderable keys.
- Issue #15397: inspect.getmodulename() is now based directly on importlib
via a new importlib.machinery.all_suffixes() API.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment