New submission from Erick Tryzelaar <[EMAIL PROTECTED]>: I've run into a case where pprint isn't able to print out a particular data structure, and have distilled it down to a simple example:
import pprint class A: pass pprint.pprint({A(): 1, A(): 2}) Which throws this exception: Traceback (most recent call last): File "/opt/local/Library/Frameworks/Python.framework/Versions/3.0/lib/python3 .0/pprint.py", line 272, in _safe_repr items = sorted(items) TypeError: unorderable types: A() < A() During handling of the above exception, another exception occurred: Traceback (most recent call last): File "foo.py", line 6, in <module> pprint.pprint({A(): 1, A(): 2}) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.0/lib/python3 .0/pprint.py", line 55, in pprint printer.pprint(object) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.0/lib/python3 .0/pprint.py", line 106, in pprint self._format(object, self._stream, 0, 0, {}, 0) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.0/lib/python3 .0/pprint.py", line 129, in _format rep = self._repr(object, context, level - 1) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.0/lib/python3 .0/pprint.py", line 216, in _repr self._depth, level) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.0/lib/python3 .0/pprint.py", line 228, in format return _safe_repr(object, context, maxlevels, level) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.0/lib/python3 .0/pprint.py", line 277, in _safe_repr items = sorted(items, key=sortkey) TypeError: unorderable types: A() < A() This is happening because of this block of code: try: items = sorted(items) except TypeError: def sortkey(item): key, value = item return str(type(key)), key, value items = sorted(items, key=sortkey) The exception block is trying to sort the items again, but in this instance, it's still not orderable. Could we get _safe_repr to at least give up on sorting at this point? Or, we could try just falling back to sorting according to the class name, with: try: items = sorted(items) except TypeError: def sortkey(item): key, value = item return str(type(key)), key, value try: items = sorted(items, key=sortkey) except TypeError: def sortkey(item): key, value = item return str(type(key)) That would at least give some ordering to the output. Unfortunately, in this case it's a shame that we don't have the cmp function any more, because then we could just fall back to giving up on the ordering for just certain unorderable keys, but still have sorted output for orderable keys. I thought maybe we could test if the key and value have __lt__, but it looks like all classes now have that function, even if the user didn't implement it. In the long run though, I suppose the case where you have mixed types in a dict there's no sensible ordering anyway. ---------- components: Library (Lib) messages: 73858 nosy: erickt severity: normal status: open title: pprint._safe_repr is not general enough in one instance versions: Python 3.0 _______________________________________ Python tracker <[EMAIL PROTECTED]> <http://bugs.python.org/issue3976> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com