[Zope-dev] Re: Unit Test Failures
Tim Peters
tim.peters at gmail.com
Mon Dec 19 11:28:15 EST 2005
...
[Philipp]
>> Well, if you look closer you find that it uses pprint.pformat which
always outputs the
>> same on all machines (because it provides output sorted by the
dictionary key).
[Tres Seaver]
> I see that in the implementation; it isn't documented as part of
> pprint's contract, however.
It's not part of the implementation either. For example,
>>> d = {"z": 1, "m": 2}
>>> pprint.pprint(d)
{'z': 1, 'm': 2}
That is, it's not true that pprint always sorts a dict for display.
Looks like Jim's suggested
from zope.testing.doctestunit import pprint
inherits this insecurity.
In the example above, two things conspire to give "unsorted" output:
1. pprint(dict) doesn't try to sort at all if _repr(dict) is "short".
2. On my 32-bit box, hash('z') < hash('m'), which leads to platform accidents
in dict insertion putting 'z' before 'm' in the "natural" dict
iteration order:
>>> d
{'z': 1, 'm': 2}
Because of #1, pprint passes on that order.
On a 64-bit box, the order may differ (or across Python versions on
one box if the string hash, or dict insertion, algorithms change).
More information about the Zope-Dev
mailing list