Issue 32329: PYTHONHASHSEED=0 python3 -R should enable hash randomization (original) (raw)

Python pretends that hash randomization is always enabled, but it's not:

$ PYTHONHASHSEED=0 python3 -c 'import sys; print(sys.flags.hash_randomization)' 1 vstinner@apu$ PYTHONHASHSEED=0 python3 -c 'import sys; print(hash("abc"))' 4596069200710135518 vstinner@apu$ PYTHONHASHSEED=0 python3 -c 'import sys; print(hash("abc"))' 4596069200710135518 vstinner@apu$ PYTHONHASHSEED=0 python3 -c 'import sys; print(hash("abc"))' 4596069200710135518

=> sys.flags.hash_randomization must be zero

Moreover, the -R flag is always ignored, it's not possible to override the PYTHONHASHSEED environment variable:

vstinner@apu$ PYTHONHASHSEED=0 python3 -R -c 'import sys; print(sys.flags.hash_randomization)' 1 vstinner@apu$ PYTHONHASHSEED=0 python3 -R -c 'import sys; print(hash("abc"))' 4596069200710135518 vstinner@apu$ PYTHONHASHSEED=0 python3 -R -c 'import sys; print(hash("abc"))' 4596069200710135518 vstinner@apu$ PYTHONHASHSEED=0 python3 -R -c 'import sys; print(hash("abc"))' 4596069200710135518

I expect that -R enables hash randomization and overrides PYTHONHASHSEED environment variable.

Attached PR fixes these issues and adds an unit test.

"PYTHONHASHSEED=0 python3 -R" now enables hash randomization on master (Python 3.7), I also fixed the sys.flags.hash_randomization value when using PYTHONHASHSEED=0.

I chose to not modify the behaviour of the -R option in Python 3.6. I dislike having to document a behaviour change in a minor Python version (3.6.x). I only fixed the value of sys.flags.hash_randomization in Python 3.6.