mirror of
https://github.com/python/cpython.git
synced 2025-07-09 20:35:26 +00:00
Reimplement tempfile._RandomNameSequence using a generator function. (#1075)
This commit is contained in:
parent
e8a6bb4f39
commit
f50354adaa
2 changed files with 15 additions and 25 deletions
|
@ -133,32 +133,21 @@ def _sanitize_params(prefix, suffix, dir):
|
||||||
return prefix, suffix, dir, output_type
|
return prefix, suffix, dir, output_type
|
||||||
|
|
||||||
|
|
||||||
class _RandomNameSequence:
|
def _RandomNameSequence():
|
||||||
"""An instance of _RandomNameSequence generates an endless
|
"""Generate an endless sequence of unpredictable strings which
|
||||||
sequence of unpredictable strings which can safely be incorporated
|
can safely be incorporated into file names. Each string is 8
|
||||||
into file names. Each string is six characters long. Multiple
|
characters long. Multiple threads and forked processes can
|
||||||
threads can safely use the same instance at the same time.
|
safely use the same instance at the same time."""
|
||||||
|
|
||||||
_RandomNameSequence is an iterator."""
|
|
||||||
|
|
||||||
characters = "abcdefghijklmnopqrstuvwxyz0123456789_"
|
characters = "abcdefghijklmnopqrstuvwxyz0123456789_"
|
||||||
|
rng_pid = None
|
||||||
@property
|
while True:
|
||||||
def rng(self):
|
|
||||||
cur_pid = _os.getpid()
|
cur_pid = _os.getpid()
|
||||||
if cur_pid != getattr(self, '_rng_pid', None):
|
if cur_pid != rng_pid:
|
||||||
self._rng = _Random()
|
choose = _Random().choice
|
||||||
self._rng_pid = cur_pid
|
rng_pid = cur_pid
|
||||||
return self._rng
|
letters = [choose(characters) for dummy in range(8)]
|
||||||
|
yield ''.join(letters)
|
||||||
def __iter__(self):
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __next__(self):
|
|
||||||
c = self.characters
|
|
||||||
choose = self.rng.choice
|
|
||||||
letters = [choose(c) for dummy in range(8)]
|
|
||||||
return ''.join(letters)
|
|
||||||
|
|
||||||
def _candidate_tempdir_list():
|
def _candidate_tempdir_list():
|
||||||
"""Generate a list of candidate temporary directories which
|
"""Generate a list of candidate temporary directories which
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
# tempfile.py unit tests.
|
# tempfile.py unit tests.
|
||||||
|
import collections.abc
|
||||||
import tempfile
|
import tempfile
|
||||||
import errno
|
import errno
|
||||||
import io
|
import io
|
||||||
|
@ -290,9 +291,9 @@ class TestGetCandidateNames(BaseTestCase):
|
||||||
"""Test the internal function _get_candidate_names."""
|
"""Test the internal function _get_candidate_names."""
|
||||||
|
|
||||||
def test_retval(self):
|
def test_retval(self):
|
||||||
# _get_candidate_names returns a _RandomNameSequence object
|
# _get_candidate_names returns an iterator
|
||||||
obj = tempfile._get_candidate_names()
|
obj = tempfile._get_candidate_names()
|
||||||
self.assertIsInstance(obj, tempfile._RandomNameSequence)
|
self.assertIsInstance(obj, collections.abc.Iterator)
|
||||||
|
|
||||||
def test_same_thing(self):
|
def test_same_thing(self):
|
||||||
# _get_candidate_names always returns the same object
|
# _get_candidate_names always returns the same object
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue