This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: sys.flags.hash_randomization doesn't return correct value
Type: Stage: resolved
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: Stephen.Tu, christian.heimes, dmalcolm, iritkatriel, msmhrt
Priority: normal Keywords: patch

Created on 2013-04-13 05:46 by msmhrt, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
hashrandomization.patch Stephen.Tu, 2013-04-13 18:16 review
Messages (6)
msg186693 - (view) Author: Masami HIRATA (msmhrt) Date: 2013-04-13 05:46
OS: Windows 7 Starter Edition SP1 (32-bit)
Python: 3.3.1 (python-3.3.1.msi)

It seems that sys.flags.hash_randomization doesn't return correct value

Output:
C:\>set PYTHONHASHSEED=random

C:\>C:\Python33\python.exe
Python 3.3.1 (v3.3.1:d9893d13c628, Apr  6 2013, 20:25:12) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.flags.hash_randomization
1
>>> ^Z


C:\>set PYTHONHASHSEED=1

C:\>C:\Python33\python.exe
Python 3.3.1 (v3.3.1:d9893d13c628, Apr  6 2013, 20:25:12) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.flags.hash_randomization
1
>>> ^Z


C:\>set PYTHONHASHSEED=12345

C:\>C:\Python33\python.exe
...
>>> import sys
>>> sys.flags.hash_randomization
12345
>>> ^Z

Output I Expected:
C:\>set PYTHONHASHSEED=random

C:\>C:\Python33\python.exe
...
>>> import sys
>>> sys.flags.hash_randomization
True
>>> ^Z


C:\>set PYTHONHASHSEED=1

C:\>C:\Python33\python.exe
...
>>> import sys
>>> sys.flags.hash_randomization
False
>>> ^Z


C:\>set PYTHONHASHSEED=12345

C:\>C:\Python33\python.exe
...
>>> import sys
>>> sys.flags.hash_randomization
False
>>> ^Z
msg186777 - (view) Author: Stephen Tu (Stephen.Tu) * Date: 2013-04-13 18:16
Py_HashRandomizationFlag was not getting properly set anywhere. This patch fixes this. Also a test case

Behavior is now:
$ cat ~/hr.py 
import sys
print(sys.flags.hash_randomization)
$ PYTHONHASHSEED=random ./python.exe ~/hr.py 
1
$ PYTHONHASHSEED=0 ./python.exe ~/hr.py 
0
$ PYTHONHASHSEED=1 ./python.exe ~/hr.py 
0
msg186870 - (view) Author: Masami HIRATA (msmhrt) Date: 2013-04-14 00:37
It seems there is a misunderstanding on my part.

Python v3.3.1 documentation says,
"sys.flags The struct sequence flags exposes the status of command line flags. The attributes are read only."

I think that sys.flags.hash_randomization should be always 1 on Python v3.3.1

because the documentation says,
"-R 
Kept for compatibility. On Python 3.3 and greater, hash randomization is turned on by default."
msg186871 - (view) Author: Stephen Tu (Stephen.Tu) * Date: 2013-04-14 00:56
But it would seem that setting PYTHONHASHSEED != "random" does disable hash randomization. also, not sure what the semantics of the following is:

$ PYTHONHASHSEED=1 python -R ...

right now, python3 basically ignores -R.
msg186872 - (view) Author: Masami HIRATA (msmhrt) Date: 2013-04-14 01:14
We should add 'randomization' and 'seed' attribute to sys.hash_info, I think.
msg407152 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-11-27 15:33
This is working now (note that since 3.10 hash randomisation is enabled by default):

cpython % export PYTHONHASHSEED=random
cpython % ./python.exe -c "import sys; print(sys.flags.hash_randomization)"
1
cpython % export PYTHONHASHSEED=0                                          
cpython % ./python.exe -c "import sys; print(sys.flags.hash_randomization)"
0
cpython % export PYTHONHASHSEED=1                                          
cpython % ./python.exe -c "import sys; print(sys.flags.hash_randomization)"
1
History
Date User Action Args
2022-04-11 14:57:44adminsetgithub: 61908
2021-11-27 15:33:08iritkatrielsetstatus: open -> closed

nosy: + iritkatriel
messages: + msg407152

resolution: out of date
stage: resolved
2013-04-15 13:41:49benjamin.petersonsetnosy: + dmalcolm
2013-04-15 12:15:12christian.heimessetnosy: + christian.heimes
2013-04-14 01:14:22msmhrtsetmessages: + msg186872
2013-04-14 00:56:57Stephen.Tusetmessages: + msg186871
2013-04-14 00:37:10msmhrtsetmessages: + msg186870
2013-04-13 18:16:44Stephen.Tusetfiles: + hashrandomization.patch

nosy: + Stephen.Tu
messages: + msg186777

keywords: + patch
2013-04-13 05:46:48msmhrtcreate