classification
Title: Bug in hash randomization
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, iElectric, neologix
Priority: normal Keywords:

Created on 2012-12-29 21:37 by iElectric, last changed 2012-12-30 12:20 by iElectric. This issue is now closed.

Messages (8)
msg178539 - (view) Author: Domen Kožar (iElectric) Date: 2012-12-29 21:37
Script to reproduce the issue https://gist.github.com/4409304
msg178554 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2012-12-30 06:03
You're relying on the iteration order of dictionaries being consistent. That's a bug in your program.
msg178567 - (view) Author: Domen Kožar (iElectric) Date: 2012-12-30 11:33
I believe this is not the case, I have updated example to use ordereddict, same effect:

https://gist.github.com/4409304
msg178568 - (view) Author: Charles-François Natali (neologix) * (Python committer) Date: 2012-12-30 11:42
What exactly are you trying to demonstrate?
As explained by Benjamin, the output can differ from one invokation to another because the iteration order depends on the hash value (position in the buckets). Running your script on Python 2.7 or curent outputs "good" and "bad" randomly, which is expected when randomization is enabled.
With randomization off, the output is consistent.
msg178569 - (view) Author: Domen Kožar (iElectric) Date: 2012-12-30 12:01
That would mean there is a bug in OrderedDict, since iterator of item in OrderedDict should keep the order?
msg178570 - (view) Author: Charles-François Natali (neologix) * (Python committer) Date: 2012-12-30 12:15
No, there's a bug in your code:
"""
nest_variables(collections.OrderedDict({'foo.bar': '1', 'foo': '2'}))
"""

You pass the OrderedDict *and already constructed dict*, so entries are inserted in a random order.
Just use this and it'll work properly:
"""
nest_variables(collections.OrderedDict(('foo.bar', '1'), ('foo', '2')))
"""
msg178571 - (view) Author: Charles-François Natali (neologix) * (Python committer) Date: 2012-12-30 12:17
There's actually a parenthesis missing:
"""
nest_variables(collections.OrderedDict((('foo.bar', '1'), ('foo', '2'))))
"""
msg178572 - (view) Author: Domen Kožar (iElectric) Date: 2012-12-30 12:20
Ah, works much better if you pass tuple to ordereddict. Seems like a bug in my program indeed (I was using ordereddict, but not correctly). 

Sorry for the noise!
History
Date User Action Args
2012-12-30 12:20:25iElectricsetmessages: + msg178572
2012-12-30 12:17:52neologixsetstatus: open -> closed
resolution: not a bug
messages: + msg178571

stage: resolved
2012-12-30 12:15:31neologixsetmessages: + msg178570
2012-12-30 12:01:33iElectricsetmessages: + msg178569
2012-12-30 11:42:11neologixsetnosy: + neologix
messages: + msg178568
2012-12-30 11:33:51iElectricsetstatus: closed -> open
resolution: not a bug -> (no value)
messages: + msg178567
2012-12-30 06:03:09benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg178554

resolution: not a bug
2012-12-29 21:37:29iElectriccreate