I would like to get the same dictionary string for both Python 2.7 and Python 3.5 versions. I hardcoded PYTHONHASHSEED
as below:
$ source ~/virtualenv/bin/activate
$ python --version
Python 2.7.12
$ PYTHONHASHSEED=0 python -c "print({'one':2,'two':3,'three':4})"
{'three': 4, 'two': 3, 'one': 2}
$ source ~/virtualenv-python3/bin/activate
$ python --version
Python 3.5.2
$ PYTHONHASHSEED=0 python -c "print({'one':2,'two':3,'three':4})"
{'two': 3, 'three': 4, 'one': 2}
Why these two invocations result with different output? Is there any way to find PYTHONHASHSEED
s values giving the same results for both Python 2.7 and Python 3.5?
UPDATE
My real case:
encoding_case.py
import base64
import json
def sth_to_call(b64_of_dict):
raise NotImplementedError(
'This is simulation of independent resources '
'and this should be mocked. That is why I am raising otherwise.'
)
def f(**kwargs):
sth_to_call(base64.b64encode(json.dumps(kwargs).encode()))
test_encoding_case.py
import mock
import unittest
import encoding_case
class EncodingCaseTest(unittest.TestCase):
@mock.patch('encoding_case.sth_to_call')
def test_f(self, sth_to_call_mock):
encoding_case.f(**{'one': 2, 'two': 3, 'three': 4})
sth_to_call_mock\
.assert_called_with(b'eyJ0d28iOiAzLCAidGhyZWUiOiA0LCAib25lIjogMn0=')
Fails after invocation of PYTHONHASHSEED=0 nosetests test_encoding_case
under Python 2.7 with error:
AssertionError: Expected call: sth_to_call('eyJ0d28iOiAzLCAidGhyZWUiOiA0LCAib25lIjogMn0=')
Actual call: sth_to_call('eyJvbmUiOiAyLCAidGhyZWUiOiA0LCAidHdvIjogM30=')
The same invocation under Python 3.5 results with test passing.