diff options
author | Gregory P. Smith <greg@krypto.org> | 2015-12-14 04:01:44 (GMT) |
---|---|---|
committer | Gregory P. Smith <greg@krypto.org> | 2015-12-14 04:01:44 (GMT) |
commit | 220ba72c20869975ee9e900f78c7a724db13a3ee (patch) | |
tree | 8101b0989e8ee28c32c44c60ffc9877beeaaa569 /Lib | |
parent | 4cb6d37d1d9b2463a121f5f0c6ed9aed2d3f2f9a (diff) | |
download | cpython-220ba72c20869975ee9e900f78c7a724db13a3ee.zip cpython-220ba72c20869975ee9e900f78c7a724db13a3ee.tar.gz cpython-220ba72c20869975ee9e900f78c7a724db13a3ee.tar.bz2 |
Fix test_cmd_line not to fail if PYTHONHASHSEED is set to a fixed seed
due to test_hash_randomization expecting a different seed per process.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_cmd_line.py | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index cb9bbdd..5cad3ec 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -402,12 +402,24 @@ class CmdLineTest(unittest.TestCase): # Verify that -R enables hash randomization: self.verify_valid_flag('-R') hashes = [] - for i in range(2): + if os.environ.get('PYTHONHASHSEED', 'random') != 'random': + env = dict(os.environ) # copy + # We need to test that it is enabled by default without + # the environment variable enabling it for us. + del env['PYTHONHASHSEED'] + env['__cleanenv'] = '1' # consumed by assert_python_ok() + else: + env = {} + for i in range(3): code = 'print(hash("spam"))' - rc, out, err = assert_python_ok('-c', code) + rc, out, err = assert_python_ok('-c', code, **env) self.assertEqual(rc, 0) hashes.append(out) - self.assertNotEqual(hashes[0], hashes[1]) + hashes = sorted(set(hashes)) # uniq + # Rare chance of failure due to 3 random seeds honestly being equal. + self.assertGreater(len(hashes), 1, + msg='3 runs produced an identical random hash ' + ' for "spam": {}'.format(hashes)) # Verify that sys.flags contains hash_randomization code = 'import sys; print("random is", sys.flags.hash_randomization)' |