Commit 44c9b4fe authored by Michael Hanselmann's avatar Michael Hanselmann

utils: Move ResetTempfileModule to wrappers module

It's not exactly a wrapper, but this seemed like the best place.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 7d444d59
......@@ -689,29 +689,6 @@ def RunParts(dir_name, env=None, reset_env=False):
return rr
def ResetTempfileModule():
"""Resets the random name generator of the tempfile module.
This function should be called after C{os.fork} in the child process to
ensure it creates a newly seeded random generator. Otherwise it would
generate the same random parts as the parent process. If several processes
race for the creation of a temporary file, this could lead to one not getting
a temporary name.
"""
# pylint: disable-msg=W0212
if hasattr(tempfile, "_once_lock") and hasattr(tempfile, "_name_sequence"):
tempfile._once_lock.acquire()
try:
# Reset random name generator
tempfile._name_sequence = None
finally:
tempfile._once_lock.release()
else:
logging.critical("The tempfile module misses at least one of the"
" '_once_lock' and '_name_sequence' attributes")
def ForceDictType(target, key_types, allowed_values=None):
"""Force the values of a dict to have certain types.
......
......@@ -29,6 +29,7 @@ import tempfile
import fcntl
import os
import select
import logging
def TestDelay(duration):
......@@ -168,3 +169,26 @@ def GetClosedTempfile(*args, **kwargs):
(fd, path) = tempfile.mkstemp(*args, **kwargs)
CloseFdNoError(fd)
return path
def ResetTempfileModule():
"""Resets the random name generator of the tempfile module.
This function should be called after C{os.fork} in the child process to
ensure it creates a newly seeded random generator. Otherwise it would
generate the same random parts as the parent process. If several processes
race for the creation of a temporary file, this could lead to one not getting
a temporary name.
"""
# pylint: disable-msg=W0212
if hasattr(tempfile, "_once_lock") and hasattr(tempfile, "_name_sequence"):
tempfile._once_lock.acquire()
try:
# Reset random name generator
tempfile._name_sequence = None
finally:
tempfile._once_lock.release()
else:
logging.critical("The tempfile module misses at least one of the"
" '_once_lock' and '_name_sequence' attributes")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment