(original) (raw)

diff -r 0d2c364c7e5d Lib/test/test_cmd_line.py --- a/Lib/test/test_cmd_line.py Sat Apr 13 20:12:53 2013 +0300 +++ b/Lib/test/test_cmd_line.py Sat Apr 13 14:14:41 2013 -0400 @@ -400,6 +400,20 @@ self.assertEqual(rc, 0) self.assertIn(b'random is 1', out) + def test_PYTHONHASHSEED(self): + # Verify that setting PYTHONHASHSEED sets sys.hash_randomization + # appropriately + + code0 = 'import sys; print(sys.flags.hash_randomization)' + rc1, out1, err1 = assert_python_ok('-c', code0, PYTHONHASHSEED="random") + self.assertEqual(rc1, 0) + self.assertIn(b'1', out1) + + code1 = 'import sys; print(sys.flags.hash_randomization)' + rc2, out2, err2 = assert_python_ok('-c', code1, PYTHONHASHSEED="0") + self.assertEqual(rc2, 0) + self.assertIn(b'0', out2) + 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 diff -r 0d2c364c7e5d Modules/main.c --- a/Modules/main.c Sat Apr 13 20:12:53 2013 +0300 +++ b/Modules/main.c Sat Apr 13 14:14:41 2013 -0400 @@ -351,7 +351,7 @@ } } - Py_HashRandomizationFlag = 1; + /* _PyRandom_Init() sets Py_HashRandomizationFlag */ _PyRandom_Init(); PySys_ResetWarnOptions(); diff -r 0d2c364c7e5d Python/pythonrun.c --- a/Python/pythonrun.c Sat Apr 13 20:12:53 2013 +0300 +++ b/Python/pythonrun.c Sat Apr 13 14:14:41 2013 -0400 @@ -287,11 +287,8 @@ 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); + /* _PyRandom_Init() sets Py_HashRandomizationFlag */ _PyRandom_Init(); interp = PyInterpreterState_New(); diff -r 0d2c364c7e5d Python/random.c --- a/Python/random.c Sat Apr 13 20:12:53 2013 +0300 +++ b/Python/random.c Sat Apr 13 14:14:41 2013 -0400 @@ -254,6 +254,7 @@ else { lcg_urandom(seed, (unsigned char*)secret, secret_size); } + Py_HashRandomizationFlag = 0; } else { #ifdef MS_WINDOWS @@ -265,5 +266,6 @@ dev_urandom_noraise((char*)secret, secret_size); # endif #endif + Py_HashRandomizationFlag = 1; } }