Skip to content

Instantly share code, notes, and snippets.

@domenkozar
Last active December 10, 2015 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domenkozar/4409304 to your computer and use it in GitHub Desktop.
Save domenkozar/4409304 to your computer and use it in GitHub Desktop.
import collections
def nest_variables(variables):
nested = dict()
for key, value in variables.items():
segments = key.split('.')
location = nested
for segment in segments[:-1]:
if segment not in location:
location[segment] = dict()
location = location[segment]
print("location: %s" % location)
print("nested: %s" % nested)
if not isinstance(location, dict):
print('good')
return
location[segments[-1]] = value
print('bad')
# use ordereddict, just to be sure order is irrelevant
nest_variables(collections.OrderedDict({'foo.bar': '1', 'foo': '2'}))
@domenkozar
Copy link
Author

Run about 5 times in terminal (should return different output):

  • $ PYTHONHASHSEED="random" python2.7 hashprob.py

and then about 5 times as (works as expected):

  • $ python2.7 hashprob.py

Same problem with python3.3 that turns random hashseed by default

@reebalazs
Copy link

I an reproduce it. It misses about half of the time.

python 2.7.3 (default, Nov 17 2012, 19:54:34)
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin

@domenkozar
Copy link
Author

Thanks!

Seems to be the case of two bugs. One being introduction of hash randomization (or better say dict really not keeping the order) and the other of test case not covering a bug, which I was hunting.

Bottom of line, this fails due to careless passing of dict instead of ordereddict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment