diff options
author | Raymond Hettinger <python@rcn.com> | 2009-11-19 01:07:05 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-11-19 01:07:05 (GMT) |
commit | a7da1663ecf9a881970b096a6dd0494584d0f795 (patch) | |
tree | b3239a5f8dfc4f4de103b6dfddcdd057b6040b8a /Lib/test/test_pprint.py | |
parent | df961cfbfa1d52c6fdd138444e621ed7ffcd76bb (diff) | |
download | cpython-a7da1663ecf9a881970b096a6dd0494584d0f795.zip cpython-a7da1663ecf9a881970b096a6dd0494584d0f795.tar.gz cpython-a7da1663ecf9a881970b096a6dd0494584d0f795.tar.bz2 |
Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types.
Diffstat (limited to 'Lib/test/test_pprint.py')
-rw-r--r-- | Lib/test/test_pprint.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index 3d13b60..4370935 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -2,6 +2,7 @@ import pprint import test.support import unittest import test.test_set +import random # list, tuple and dict subclasses that do or don't overwrite __repr__ class list2(list): @@ -25,6 +26,10 @@ class dict3(dict): def __repr__(self): return dict.__repr__(self) +class Unorderable: + def __repr__(self): + return str(id(self)) + class QueryTestCase(unittest.TestCase): def setUp(self): @@ -407,6 +412,20 @@ class QueryTestCase(unittest.TestCase): self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict) self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list) + def test_sort_unorderable_values(self): + # Issue 3976: sorted pprints fail for unorderable values. + n = 20 + keys = [Unorderable() for i in range(n)] + random.shuffle(keys) + skeys = sorted(keys, key=id) + clean = lambda s: s.replace(' ', '').replace('\n','') + + self.assertEqual(clean(pprint.pformat(set(keys))), + '{' + ','.join(map(repr, skeys)) + '}') + self.assertEqual(clean(pprint.pformat(frozenset(keys))), + 'frozenset({' + ','.join(map(repr, skeys)) + '})') + self.assertEqual(clean(pprint.pformat(dict.fromkeys(keys))), + '{' + ','.join('%r:None' % k for k in skeys) + '}') class DottedPrettyPrinter(pprint.PrettyPrinter): |