]> granicus.if.org Git - python/commitdiff
bpo-32329: Fix sys.flags.hash_randomization (#4875)
authorVictor Stinner <victor.stinner@gmail.com>
Fri, 15 Dec 2017 00:39:48 +0000 (01:39 +0100)
committerGitHub <noreply@github.com>
Fri, 15 Dec 2017 00:39:48 +0000 (01:39 +0100)
sys.flags.hash_randomization is now properly set to 0 when hash
randomization is turned off by PYTHONHASHSEED=0.

Lib/test/test_cmd_line.py
Misc/NEWS.d/next/Core and Builtins/2017-12-15-00-55-35.bpo-32329.XL1O99.rst [new file with mode: 0644]
Modules/main.c
Python/pylifecycle.c
Python/random.c

index ae2bcd43754ab0fd097f6ed828973f35eb54e4ae..98441b0f4a5d4d162f5955ef2de2648897bf1516 100644 (file)
@@ -426,10 +426,15 @@ class CmdLineTest(unittest.TestCase):
 
         # 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
diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-12-15-00-55-35.bpo-32329.XL1O99.rst b/Misc/NEWS.d/next/Core and Builtins/2017-12-15-00-55-35.bpo-32329.XL1O99.rst
new file mode 100644 (file)
index 0000000..36ca84e
--- /dev/null
@@ -0,0 +1,2 @@
+``sys.flags.hash_randomization`` is now properly set to 0 when hash
+randomization is turned off by ``PYTHONHASHSEED=0``.
index b0fb78f887a58016f544c98af05e4da7b62fdc86..3ac405c0bd91d92097fc54cbf2616ea1135de737 100644 (file)
@@ -394,7 +394,6 @@ Py_Main(int argc, wchar_t **argv)
         exit(1);
     }
 
-    Py_HashRandomizationFlag = 1;
     _PyRandom_Init();
 
     PySys_ResetWarnOptions();
index 640271fd20b7090d2041c150a2d2d7dc7ad5d4cf..ecfdfee218dcc4a705ce7ddd9df8709adcdfa05e 100644 (file)
@@ -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);
index c97d5e71002a240d3a0c88cd2ff377fd3c8de1f0..e0ee153ec4a2631a618330063776752faa7eb4a6 100644 (file)
@@ -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 {
@@ -582,6 +584,7 @@ _PyRandom_Init(void)
         if (res < 0) {
             Py_FatalError("failed to get random numbers to initialize Python");
         }
+        Py_HashRandomizationFlag = 1;
     }
 }