Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Lib/test/test_cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,15 @@ def test_hash_randomization(self):

# Verify that sys.flags contains hash_randomization
code = 'import sys; print("random is", sys.flags.hash_randomization)'
rc, out, err = assert_python_ok('-c', code)
self.assertEqual(rc, 0)
rc, out, err = assert_python_ok('-c', code, PYTHONHASHSEED='')
self.assertIn(b'random is 1', out)

rc, out, err = assert_python_ok('-c', code, PYTHONHASHSEED='random')
self.assertIn(b'random is 1', out)

rc, out, err = assert_python_ok('-c', code, PYTHONHASHSEED='0')
self.assertIn(b'random is 0', out)

def test_del___main__(self):
# Issue #15001: PyRun_SimpleFileExFlags() did crash because it kept a
# borrowed reference to the dict of __main__ module and later modify
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``sys.flags.hash_randomization`` is now properly set to 0 when hash
randomization is turned off by ``PYTHONHASHSEED=0``.
1 change: 0 additions & 1 deletion Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ Py_Main(int argc, wchar_t **argv)
exit(1);
}

Py_HashRandomizationFlag = 1;
_PyRandom_Init();

PySys_ResetWarnOptions();
Expand Down
4 changes: 0 additions & 4 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,6 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib)
Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
/* The variable is only tested for existence here; _PyRandom_Init will
check its value further. */
if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p);
#ifdef MS_WINDOWS
if ((p = Py_GETENV("PYTHONLEGACYWINDOWSFSENCODING")) && *p != '\0')
Py_LegacyWindowsFSEncodingFlag = add_flag(Py_LegacyWindowsFSEncodingFlag, p);
Expand Down
3 changes: 3 additions & 0 deletions Python/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -565,9 +565,11 @@ _PyRandom_Init(void)
if (seed == 0) {
/* disable the randomized hash */
memset(secret, 0, secret_size);
Py_HashRandomizationFlag = 0;
}
else {
lcg_urandom(seed, secret, secret_size);
Py_HashRandomizationFlag = 1;
}
}
else {
Expand All @@ -582,6 +584,7 @@ _PyRandom_Init(void)
if (res < 0) {
Py_FatalError("failed to get random numbers to initialize Python");
}
Py_HashRandomizationFlag = 1;
}
}

Expand Down