diff -r 03b5f75ddac7 Lib/pprint.py --- a/Lib/pprint.py Thu Jun 07 20:04:17 2012 -0400 +++ b/Lib/pprint.py Thu Jun 07 23:28:44 2012 -0400 @@ -86,7 +86,11 @@ 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 -r 03b5f75ddac7 Lib/test/test_pprint.py --- a/Lib/test/test_pprint.py Thu Jun 07 20:04:17 2012 -0400 +++ b/Lib/test/test_pprint.py Thu Jun 07 23:28:44 2012 -0400 @@ -466,6 +466,15 @@ 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):