cpython: 3c9ddd93c983 (original) (raw)
Mercurial > cpython
changeset 73743:3c9ddd93c983 3.2
Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module. Patch by Brian Harring. [#12856]
Antoine Pitrou solipsis@pitrou.net | |
---|---|
date | Fri, 25 Nov 2011 21:28:15 +0100 |
parents | 2d6f0e2fe034 |
children | 588087429809 fa59b3758b14 |
files | Lib/tempfile.py Lib/test/test_tempfile.py Misc/NEWS |
diffstat | 3 files changed, 43 insertions(+), 2 deletions(-)[+] [-] Lib/tempfile.py 9 Lib/test/test_tempfile.py 32 Misc/NEWS 4 |
line wrap: on
line diff
--- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -112,8 +112,13 @@ class RandomNameSequence: characters = "abcdefghijklmnopqrstuvwxyz0123456789"
- @property
- def rng(self):
cur_pid = _os.getpid()[](#l1.11)
if cur_pid != getattr(self, '_rng_pid', None):[](#l1.12)
self._rng = _Random()[](#l1.13)
self._rng_pid = cur_pid[](#l1.14)
return self._rng[](#l1.15)
--- 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 @@ -135,6 +136,37 @@ class test__RandomNameSequence(TC): except: self.failOnException("iteration")
- @unittest.skipUnless(hasattr(os, 'fork'),
"os.fork is required for this test")[](#l2.16)
- def test_process_awareness(self):
# ensure that the random source differs between[](#l2.18)
# child and parent.[](#l2.19)
read_fd, write_fd = os.pipe()[](#l2.20)
pid = None[](#l2.21)
try:[](#l2.22)
pid = os.fork()[](#l2.23)
if not pid:[](#l2.24)
os.close(read_fd)[](#l2.25)
os.write(write_fd, next(self.r).encode("ascii"))[](#l2.26)
os.close(write_fd)[](#l2.27)
# bypass the normal exit handlers- leave those to[](#l2.28)
# the parent.[](#l2.29)
os._exit(0)[](#l2.30)
parent_value = next(self.r)[](#l2.31)
child_value = os.read(read_fd, len(parent_value)).decode("ascii")[](#l2.32)
finally:[](#l2.33)
if pid:[](#l2.34)
# best effort to ensure the process can't bleed out[](#l2.35)
# via any bugs above[](#l2.36)
try:[](#l2.37)
os.kill(pid, signal.SIGKILL)[](#l2.38)
except EnvironmentError:[](#l2.39)
pass[](#l2.40)
os.close(read_fd)[](#l2.41)
os.close(write_fd)[](#l2.42)
self.assertNotEqual(child_value, parent_value)[](#l2.43)
+ + test_classes.append(test__RandomNameSequence)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -83,6 +83,10 @@ Core and Builtins Library ------- +- Issue #12856: Ensure child processes do not inherit the parent's random