]> granicus.if.org Git - python/commitdiff
bpo-30030: Revert f50354ad (tempfile) (#1187)
authorVictor Stinner <victor.stinner@gmail.com>
Wed, 19 Apr 2017 20:59:51 +0000 (22:59 +0200)
committerGitHub <noreply@github.com>
Wed, 19 Apr 2017 20:59:51 +0000 (22:59 +0200)
Revert f50354adaaafebe95ad09d09b825804a686ea843: it introduced a
regression in test_threadedtempfile.

Lib/tempfile.py
Lib/test/test_tempfile.py

index 6112208382871a42c64b6393d6e9f2ccc3d238fb..61462357c7283e13716b6a3fe431b7ca64b11a3d 100644 (file)
@@ -133,21 +133,32 @@ def _sanitize_params(prefix, suffix, dir):
     return prefix, suffix, dir, output_type
 
 
-def _RandomNameSequence():
-    """Generate an endless sequence of unpredictable strings which
-    can safely be incorporated into file names.  Each string is 8
-    characters long.  Multiple threads and forked processes can
-    safely use the same instance at the same time."""
+class _RandomNameSequence:
+    """An instance of _RandomNameSequence generates an endless
+    sequence of unpredictable strings which can safely be incorporated
+    into file names.  Each string is six characters long.  Multiple
+    threads can safely use the same instance at the same time.
+
+    _RandomNameSequence is an iterator."""
 
     characters = "abcdefghijklmnopqrstuvwxyz0123456789_"
-    rng_pid = None
-    while True:
+
+    @property
+    def rng(self):
         cur_pid = _os.getpid()
-        if cur_pid != rng_pid:
-            choose = _Random().choice
-            rng_pid = cur_pid
-        letters = [choose(characters) for dummy in range(8)]
-        yield ''.join(letters)
+        if cur_pid != getattr(self, '_rng_pid', None):
+            self._rng = _Random()
+            self._rng_pid = cur_pid
+        return self._rng
+
+    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():
     """Generate a list of candidate temporary directories which
index 367e48dbe93d26ef529e2c6b79b469c1147f7f2a..51df1ecd7d18e63e0f927cde322087f007d3eee5 100644 (file)
@@ -1,5 +1,4 @@
 # tempfile.py unit tests.
-import collections.abc
 import tempfile
 import errno
 import io
@@ -291,9 +290,9 @@ class TestGetCandidateNames(BaseTestCase):
     """Test the internal function _get_candidate_names."""
 
     def test_retval(self):
-        # _get_candidate_names returns an iterator
+        # _get_candidate_names returns a _RandomNameSequence object
         obj = tempfile._get_candidate_names()
-        self.assertIsInstance(obj, collections.abc.Iterator)
+        self.assertIsInstance(obj, tempfile._RandomNameSequence)
 
     def test_same_thing(self):
         # _get_candidate_names always returns the same object