diff -r bba20ce29d26 Lib/pprint.py --- a/Lib/pprint.py Wed Jul 18 15:12:47 2012 -0700 +++ b/Lib/pprint.py Thu Jul 19 02:17:03 2012 +0300 @@ -86,7 +86,7 @@ self.obj = obj def __lt__(self, other): - rv = self.obj.__lt__(other.obj) + rv = type(self.obj).__lt__(self.obj, other.obj) if rv is NotImplemented: rv = (str(type(self.obj)), id(self.obj)) < \ (str(type(other.obj)), id(other.obj)) diff -r bba20ce29d26 Lib/test/test_pprint.py --- a/Lib/test/test_pprint.py Wed Jul 18 15:12:47 2012 -0700 +++ b/Lib/test/test_pprint.py Thu Jul 19 02:17:03 2012 +0300 @@ -32,6 +32,13 @@ def __repr__(self): return str(id(self)) +class A: + pass + +class B: + pass + + class QueryTestCase(unittest.TestCase): def setUp(self): @@ -451,6 +458,19 @@ self.assertEqual(pprint.pformat(nested_dict, depth=1), lv1_dict) self.assertEqual(pprint.pformat(nested_list, depth=1), lv1_list) + def test_class_keys(self): + # Issue 10017 + dict_with_classes = {A: 1, B: 2} + # Class objects fall back to sorting by id + classes = map(repr, sorted([A, B], key=id)) + classes_expected = '{{{0}: 1, {1}: 2}}'.format(*classes) + + dict_with_class = {'key1': 1, A: 2, 'key2': 3} + class_expected = "{{'key1': 1, 'key2': 3, {A}: 2}}".format(A=repr(A)) + + self.assertEqual(pprint.pformat(dict_with_classes), classes_expected) + self.assertEqual(pprint.pformat(dict_with_class), class_expected) + def test_sort_unorderable_values(self): # Issue 3976: sorted pprints fail for unorderable values. n = 20