From b868e63443d86d9016679b6451fd143717ab6de7 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Thu, 19 Nov 2009 01:02:56 +0000 Subject: Issue 3976: fix pprint for sets, frozensets, and dicts containing unorderable types. --- Lib/test/test_pprint.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'Lib/test/test_pprint.py') diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index 3d13b6064bf..43709356ca2 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): -- cgit v1.2.3