diff options
| author | Antoine Pitrou <solipsis@pitrou.net> | 2011-11-25 20:28:15 (GMT) |
|---|---|---|
| committer | Antoine Pitrou <solipsis@pitrou.net> | 2011-11-25 20:28:15 (GMT) |
| commit | a5d5bb997b1cb8b5d8a33d67c505ceba3bc3d9e9 (patch) | |
| tree | 9e88e41369c4d6ebb7dedf6781de00d90d79651f /Lib/test/test_tempfile.py | |
| parent | da75dd2a9bc56d8a68e106fa10f8dd0052fff869 (diff) | |
| download | cpython-a5d5bb997b1cb8b5d8a33d67c505ceba3bc3d9e9.zip cpython-a5d5bb997b1cb8b5d8a33d67c505ceba3bc3d9e9.tar.gz cpython-a5d5bb997b1cb8b5d8a33d67c505ceba3bc3d9e9.tar.bz2 | |
Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Patch by Brian Harring.
Diffstat (limited to 'Lib/test/test_tempfile.py')
| -rw-r--r-- | Lib/test/test_tempfile.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index b50214a..2ddc04c 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -1,6 +1,7 @@ # tempfile.py unit tests. import tempfile import os +import signal import sys import re import warnings @@ -126,6 +127,37 @@ class test__RandomNameSequence(TC): except: self.failOnException("iteration") + @unittest.skipUnless(hasattr(os, 'fork'), + "os.fork is required for this test") + def test_process_awareness(self): + # ensure that the random source differs between + # child and parent. + read_fd, write_fd = os.pipe() + pid = None + try: + pid = os.fork() + if not pid: + os.close(read_fd) + os.write(write_fd, next(self.r).encode("ascii")) + os.close(write_fd) + # bypass the normal exit handlers- leave those to + # the parent. + os._exit(0) + parent_value = next(self.r) + child_value = os.read(read_fd, len(parent_value)).decode("ascii") + finally: + if pid: + # best effort to ensure the process can't bleed out + # via any bugs above + try: + os.kill(pid, signal.SIGKILL) + except EnvironmentError: + pass + os.close(read_fd) + os.close(write_fd) + self.assertNotEqual(child_value, parent_value) + + test_classes.append(test__RandomNameSequence) |
