mirror of
https://github.com/python/cpython.git
synced 2025-09-18 14:40:43 +00:00
Issue #12856: Ensure child processes do not inherit the parent's random seed for filename generation in the tempfile module.
Patch by Brian Harring.
This commit is contained in:
commit
c24847658f
3 changed files with 43 additions and 2 deletions
|
@ -108,8 +108,13 @@ class _RandomNameSequence:
|
||||||
|
|
||||||
characters = "abcdefghijklmnopqrstuvwxyz0123456789_"
|
characters = "abcdefghijklmnopqrstuvwxyz0123456789_"
|
||||||
|
|
||||||
def __init__(self):
|
@property
|
||||||
self.rng = _Random()
|
def rng(self):
|
||||||
|
cur_pid = _os.getpid()
|
||||||
|
if cur_pid != getattr(self, '_rng_pid', None):
|
||||||
|
self._rng = _Random()
|
||||||
|
self._rng_pid = cur_pid
|
||||||
|
return self._rng
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return self
|
return self
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
# tempfile.py unit tests.
|
# tempfile.py unit tests.
|
||||||
import tempfile
|
import tempfile
|
||||||
import os
|
import os
|
||||||
|
import signal
|
||||||
import sys
|
import sys
|
||||||
import re
|
import re
|
||||||
import warnings
|
import warnings
|
||||||
|
@ -135,6 +136,37 @@ class test__RandomNameSequence(TC):
|
||||||
except:
|
except:
|
||||||
self.failOnException("iteration")
|
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)
|
test_classes.append(test__RandomNameSequence)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -395,6 +395,10 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #12856: Ensure child processes do not inherit the parent's random
|
||||||
|
seed for filename generation in the tempfile module. Patch by Brian
|
||||||
|
Harring.
|
||||||
|
|
||||||
- Issue #9957: SpooledTemporaryFile.truncate() now accepts an optional size
|
- Issue #9957: SpooledTemporaryFile.truncate() now accepts an optional size
|
||||||
parameter, as other file-like objects. Patch by Ryan Kelly.
|
parameter, as other file-like objects. Patch by Ryan Kelly.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue