cpython: 4d0dcfbdf45b (original) (raw)
Mercurial > cpython
changeset 78213:4d0dcfbdf45b
Issues #10017 and #14998: Fix TypeError using pprint on dictionaries with unorderable key. [#10017]
Florent Xicluna florent.xicluna@gmail.com | |
---|---|
date | Sat, 21 Jul 2012 11:22:33 +0200 |
parents | f39895c55699(current diff)03cda5360dc6(diff) |
children | 79d44f4920d9 |
files | Lib/test/test_pprint.py Misc/NEWS |
diffstat | 3 files changed, 18 insertions(+), 2 deletions(-)[+] [-] Lib/pprint.py 6 Lib/test/test_pprint.py 9 Misc/NEWS 5 |
line wrap: on
line diff
--- 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)[](#l1.7)
try:[](#l1.8)
rv = self.obj.__lt__(other.obj)[](#l1.9)
except TypeError:[](#l1.10)
rv = NotImplemented[](#l1.11)
+ if rv is NotImplemented: rv = (str(type(self.obj)), id(self.obj)) < [](#l1.14) (str(type(other.obj)), id(other.obj))
--- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -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.[](#l2.7)
self.assertEqual(pprint.pformat({Unorderable: 0, 1: 0}),[](#l2.8)
'{1: 0, ' + repr(Unorderable) +': 0}')[](#l2.9)
# Issue 14998: TypeError on tuples with NoneTypes as dict keys.[](#l2.11)
self.assertEqual(pprint.pformat({(1,): 0, (None,): 0}),[](#l2.12)
'{(1,): 0, (None,): 0}')[](#l2.13)
+ + class DottedPrettyPrinter(pprint.PrettyPrinter): def format(self, object, context, maxlevels, level):
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -48,10 +48,13 @@ Core and Builtins