diff options
author | Inada Naoki <songofacandy@gmail.com> | 2020-10-31 02:15:38 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-31 02:15:38 (GMT) |
commit | 43ca084c88d1e46a44199f347c72e26db84903c9 (patch) | |
tree | e2353d496893ee141bf5b3d2a0f523d3fe6d9db1 /Lib/tempfile.py | |
parent | b62bdf71ea0cd52041d49691d8ae3dc645bd48e1 (diff) | |
download | cpython-43ca084c88d1e46a44199f347c72e26db84903c9.zip cpython-43ca084c88d1e46a44199f347c72e26db84903c9.tar.gz cpython-43ca084c88d1e46a44199f347c72e26db84903c9.tar.bz2 |
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.
Diffstat (limited to 'Lib/tempfile.py')
-rw-r--r-- | Lib/tempfile.py | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 1bc5c71..770f72c 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 |