Skip to content

Commit 22097aa

Browse files
authored
bpo-32329: Fix sys.flags.hash_randomization (#4875)
sys.flags.hash_randomization is now properly set to 0 when hash randomization is turned off by PYTHONHASHSEED=0.
1 parent d1cb106 commit 22097aa

File tree

5 files changed

+12
-7
lines changed

5 files changed

+12
-7
lines changed

‎Lib/test/test_cmd_line.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,15 @@ def test_hash_randomization(self):
426426

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

432+
rc, out, err = assert_python_ok('-c', code, PYTHONHASHSEED='random')
433+
self.assertIn(b'random is 1', out)
434+
435+
rc, out, err = assert_python_ok('-c', code, PYTHONHASHSEED='0')
436+
self.assertIn(b'random is 0', out)
437+
433438
def test_del___main__(self):
434439
# Issue #15001: PyRun_SimpleFileExFlags() did crash because it kept a
435440
# borrowed reference to the dict of __main__ module and later modify
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``sys.flags.hash_randomization`` is now properly set to 0 when hash
2+
randomization is turned off by ``PYTHONHASHSEED=0``.

‎Modules/main.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ Py_Main(int argc, wchar_t **argv)
394394
exit(1);
395395
}
396396

397-
Py_HashRandomizationFlag = 1;
398397
_PyRandom_Init();
399398

400399
PySys_ResetWarnOptions();

‎Python/pylifecycle.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,6 @@ _Py_InitializeEx_Private(int install_sigs, int install_importlib)
330330
Py_OptimizeFlag = add_flag(Py_OptimizeFlag, p);
331331
if ((p = Py_GETENV("PYTHONDONTWRITEBYTECODE")) && *p != '\0')
332332
Py_DontWriteBytecodeFlag = add_flag(Py_DontWriteBytecodeFlag, p);
333-
/* The variable is only tested for existence here; _PyRandom_Init will
334-
check its value further. */
335-
if ((p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
336-
Py_HashRandomizationFlag = add_flag(Py_HashRandomizationFlag, p);
337333
#ifdef MS_WINDOWS
338334
if ((p = Py_GETENV("PYTHONLEGACYWINDOWSFSENCODING")) && *p != '\0')
339335
Py_LegacyWindowsFSEncodingFlag = add_flag(Py_LegacyWindowsFSEncodingFlag, p);

‎Python/random.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,9 +565,11 @@ _PyRandom_Init(void)
565565
if (seed == 0) {
566566
/* disable the randomized hash */
567567
memset(secret, 0, secret_size);
568+
Py_HashRandomizationFlag = 0;
568569
}
569570
else {
570571
lcg_urandom(seed, secret, secret_size);
572+
Py_HashRandomizationFlag = 1;
571573
}
572574
}
573575
else {
@@ -582,6 +584,7 @@ _PyRandom_Init(void)
582584
if (res < 0) {
583585
Py_FatalError("failed to get random numbers to initialize Python");
584586
}
587+
Py_HashRandomizationFlag = 1;
585588
}
586589
}
587590

0 commit comments

Comments
 (0)