diff options
author | Florent Xicluna <florent.xicluna@gmail.com> | 2012-07-21 09:17:38 (GMT) |
---|---|---|
committer | Florent Xicluna <florent.xicluna@gmail.com> | 2012-07-21 09:17:38 (GMT) |
commit | d6da90f93d6d44365f5c9f6a2290be90ccfc8d60 (patch) | |
tree | 1354a065046686913cb29c1c8c8e0a6ea9e9dc79 /Lib | |
parent | b4bbee25b1e3f4bccac222f806b3138fb72439d6 (diff) | |
download | cpython-d6da90f93d6d44365f5c9f6a2290be90ccfc8d60.zip cpython-d6da90f93d6d44365f5c9f6a2290be90ccfc8d60.tar.gz cpython-d6da90f93d6d44365f5c9f6a2290be90ccfc8d60.tar.bz2 |
Issues #10017 and #14998: Fix TypeError using pprint on dictionaries with unorderable key.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/pprint.py | 6 | ||||
-rw-r--r-- | Lib/test/test_pprint.py | 9 |
2 files changed, 14 insertions, 1 deletions
diff --git a/Lib/pprint.py b/Lib/pprint.py index b8417f5..ae96dde 100644 --- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -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)) diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index 4e53cd8..7daf4a7 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -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): |