homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author erickt
Recipients erickt
Date 2008年09月26日.16:14:57
SpamBayes Score 0.0
Marked as misclassified No
Message-id <1222445700.05.0.107288672735.issue3976@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2008年09月26日 16:15:00ericktsetrecipients: + erickt
2008年09月26日 16:15:00ericktsetmessageid: <1222445700.05.0.107288672735.issue3976@psf.upfronthosting.co.za>
2008年09月26日 16:14:59ericktlinkissue3976 messages
2008年09月26日 16:14:58ericktcreate

AltStyle によって変換されたページ (->オリジナル) /