From 44c9b4fec29e2cd9c9ace88d062d46db1f305e1d Mon Sep 17 00:00:00 2001 From: Michael Hanselmann Date: Tue, 11 Jan 2011 12:12:46 +0100 Subject: [PATCH] utils: Move ResetTempfileModule to wrappers module It's not exactly a wrapper, but this seemed like the best place. Signed-off-by: Michael Hanselmann Reviewed-by: Iustin Pop --- lib/utils/__init__.py | 23 ----------------------- lib/utils/wrapper.py | 24 ++++++++++++++++++++++++ 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/lib/utils/__init__.py b/lib/utils/__init__.py index 171130a..810924e 100644 --- a/lib/utils/__init__.py +++ b/lib/utils/__init__.py @@ -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. diff --git a/lib/utils/wrapper.py b/lib/utils/wrapper.py index b87f69c..6cf1654 100644 --- a/lib/utils/wrapper.py +++ b/lib/utils/wrapper.py @@ -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") -- 1.7.10.4