| OLD | NEW |
| 1 # Tests invocation of the interpreter with various command line arguments | 1 # Tests invocation of the interpreter with various command line arguments |
| 2 # All tests are executed with environment variables ignored | 2 # All tests are executed with environment variables ignored |
| 3 # See test_cmd_line_script.py for testing of script execution | 3 # See test_cmd_line_script.py for testing of script execution |
| 4 | 4 |
| 5 import os | 5 import os |
| 6 import test.support, unittest | 6 import test.support, unittest |
| 7 import os | |
| 8 import sys | 7 import sys |
| 9 import subprocess | 8 import subprocess |
| 10 | 9 |
| 11 def _spawn_python(*args): | 10 def _spawn_python(*args): |
| 12 cmd_line = [sys.executable] | 11 cmd_line = [sys.executable] |
| 13 # When testing -S, we need PYTHONPATH to work (see test_site_flag()) | 12 # When testing -S, we need PYTHONPATH to work (see test_site_flag()) |
| 14 if '-S' not in args: | 13 if '-S' not in args: |
| 15 cmd_line.append('-E') | 14 cmd_line.append('-E') |
| 16 cmd_line.extend(args) | 15 cmd_line.extend(args) |
| 17 return subprocess.Popen(cmd_line, stdin=subprocess.PIPE, | 16 return subprocess.Popen(cmd_line, stdin=subprocess.PIPE, |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 path = path.encode("ascii", "backslashreplace") | 182 path = path.encode("ascii", "backslashreplace") |
| 184 sys.stdout.buffer.write(path)""" | 183 sys.stdout.buffer.write(path)""" |
| 185 code = code.strip().splitlines() | 184 code = code.strip().splitlines() |
| 186 code = '; '.join(code) | 185 code = '; '.join(code) |
| 187 p = _spawn_python('-S', '-c', code) | 186 p = _spawn_python('-S', '-c', code) |
| 188 stdout, _ = p.communicate() | 187 stdout, _ = p.communicate() |
| 189 p.stdout.close() | 188 p.stdout.close() |
| 190 self.assertTrue(path1.encode('ascii') in stdout) | 189 self.assertTrue(path1.encode('ascii') in stdout) |
| 191 self.assertTrue(path2.encode('ascii') in stdout) | 190 self.assertTrue(path2.encode('ascii') in stdout) |
| 192 | 191 |
| 192 def test_hash_randomization(self): |
| 193 # Verify that -R enables hash randomization: |
| 194 self.verify_valid_flag('-R') |
| 195 hashes = [] |
| 196 for i in range(2): |
| 197 code = 'print(hash("spam"))' |
| 198 data, rc = self.start_python_and_exit_code('-R', '-c', code) |
| 199 self.assertEqual(rc, 0) |
| 200 hashes.append(data) |
| 201 self.assertNotEqual(hashes[0], hashes[1]) |
| 202 |
| 203 # Verify that sys.flags contains hash_randomization |
| 204 code = 'import sys; print("random is", sys.flags.hash_randomization)' |
| 205 data, rc = self.start_python_and_exit_code('-R', '-c', code) |
| 206 self.assertEqual(rc, 0) |
| 207 self.assertIn(b'random is 1', data) |
| 193 | 208 |
| 194 def test_main(): | 209 def test_main(): |
| 195 test.support.run_unittest(CmdLineTest) | 210 test.support.run_unittest(CmdLineTest) |
| 196 test.support.reap_children() | 211 test.support.reap_children() |
| 197 | 212 |
| 198 if __name__ == "__main__": | 213 if __name__ == "__main__": |
| 199 test_main() | 214 test_main() |
| OLD | NEW |