From 43ca084c88d1e46a44199f347c72e26db84903c9 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Sat, 31 Oct 2020 11:15:38 +0900 Subject: [PATCH] Revert "bpo-42160: tempfile: Reduce overhead of pid check. (GH-22997)" `_RandomNameSequence` is not true singleton so using `os.register_at_fork` doesn't make sense unlike `random._inst`. This reverts commit 8e409cebad42032bb7d0f2cadd8b1e36081d98af. --- Lib/tempfile.py | 22 ++++++++++--------- Lib/test/test_tempfile.py | 4 ++-- .../2020-10-27-00-42-09.bpo-42160.eiLOCi.rst | 1 - 3 files changed, 14 insertions(+), 13 deletions(-) delete mode 100644 Misc/NEWS.d/next/Library/2020-10-27-00-42-09.bpo-42160.eiLOCi.rst diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 1bc5c71fd03..770f72c2529 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -129,22 +129,24 @@ class _RandomNameSequence: _RandomNameSequence is an iterator.""" - def __init__(self, characters="abcdefghijklmnopqrstuvwxyz0123456789_", length=8, rng=None): - if rng is None: - rng = _Random() - if hasattr(_os, "fork"): - # prevent same state after fork - _os.register_at_fork(after_in_child=rng.seed) - self.rng = rng - self.characters = characters - self.length = length + characters = "abcdefghijklmnopqrstuvwxyz0123456789_" + + @property + 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): return self def __next__(self): c = self.characters - return ''.join(self.rng.choices(c, k=self.length)) + 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 diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index 77d710efaf1..8ace883d74b 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -153,8 +153,8 @@ class TestRandomNameSequence(BaseTestCase): self.r = tempfile._RandomNameSequence() super().setUp() - def test_get_eight_char_str(self): - # _RandomNameSequence returns a eight-character string + def test_get_six_char_str(self): + # _RandomNameSequence returns a six-character string s = next(self.r) self.nameCheck(s, '', '', '') diff --git a/Misc/NEWS.d/next/Library/2020-10-27-00-42-09.bpo-42160.eiLOCi.rst b/Misc/NEWS.d/next/Library/2020-10-27-00-42-09.bpo-42160.eiLOCi.rst deleted file mode 100644 index c5f3091283a..00000000000 --- a/Misc/NEWS.d/next/Library/2020-10-27-00-42-09.bpo-42160.eiLOCi.rst +++ /dev/null @@ -1 +0,0 @@ -Replaced pid check in ``tempfile._RandomNameSequence`` with ``os.register_at_fork`` to reduce overhead.