From a65c977552507dd19d2cc073fc91ae22cc66bbff Mon Sep 17 00:00:00 2001 From: Kyle Stanley Date: Fri, 12 Jul 2019 17:22:05 -0400 Subject: bpo-19696: Move threaded_import_hangers (GH-14655) Move `threaded_import_hangers`, a dependency of `test_threaded_import`, to the directory `test_importlib/`. Also update the import references for `threaded_import_hangers` in `test_threaded_import`. https://bugs.python.org/issue19696 --- Lib/test/test_importlib/test_threaded_import.py | 6 +-- Lib/test/test_importlib/threaded_import_hangers.py | 45 ++++++++++++++++++++++ Lib/test/threaded_import_hangers.py | 45 ---------------------- 3 files changed, 48 insertions(+), 48 deletions(-) create mode 100644 Lib/test/test_importlib/threaded_import_hangers.py delete mode 100644 Lib/test/threaded_import_hangers.py diff --git a/Lib/test/test_importlib/test_threaded_import.py b/Lib/test/test_importlib/test_threaded_import.py index 8607f36..d1f64c7 100644 --- a/Lib/test/test_importlib/test_threaded_import.py +++ b/Lib/test/test_importlib/test_threaded_import.py @@ -178,11 +178,11 @@ class ThreadedImportTests(unittest.TestCase): # In case this test is run again, make sure the helper module # gets loaded from scratch again. try: - del sys.modules['test.threaded_import_hangers'] + del sys.modules['test.test_importlib.threaded_import_hangers'] except KeyError: pass - import test.threaded_import_hangers - self.assertFalse(test.threaded_import_hangers.errors) + import test.test_importlib.threaded_import_hangers + self.assertFalse(test.test_importlib.threaded_import_hangers.errors) def test_circular_imports(self): # The goal of this test is to exercise implementations of the import diff --git a/Lib/test/test_importlib/threaded_import_hangers.py b/Lib/test/test_importlib/threaded_import_hangers.py new file mode 100644 index 0000000..5484e60 --- /dev/null +++ b/Lib/test/test_importlib/threaded_import_hangers.py @@ -0,0 +1,45 @@ +# This is a helper module for test_threaded_import. The test imports this +# module, and this module tries to run various Python library functions in +# their own thread, as a side effect of being imported. If the spawned +# thread doesn't complete in TIMEOUT seconds, an "appeared to hang" message +# is appended to the module-global `errors` list. That list remains empty +# if (and only if) all functions tested complete. + +TIMEOUT = 10 + +import threading + +import tempfile +import os.path + +errors = [] + +# This class merely runs a function in its own thread T. The thread importing +# this module holds the import lock, so if the function called by T tries +# to do its own imports it will block waiting for this module's import +# to complete. +class Worker(threading.Thread): + def __init__(self, function, args): + threading.Thread.__init__(self) + self.function = function + self.args = args + + def run(self): + self.function(*self.args) + +for name, func, args in [ + # Bug 147376: TemporaryFile hung on Windows, starting in Python 2.4. + ("tempfile.TemporaryFile", lambda: tempfile.TemporaryFile().close(), ()), + + # The real cause for bug 147376: ntpath.abspath() caused the hang. + ("os.path.abspath", os.path.abspath, ('.',)), + ]: + + try: + t = Worker(func, args) + t.start() + t.join(TIMEOUT) + if t.is_alive(): + errors.append("%s appeared to hang" % name) + finally: + del t diff --git a/Lib/test/threaded_import_hangers.py b/Lib/test/threaded_import_hangers.py deleted file mode 100644 index 5484e60..0000000 --- a/Lib/test/threaded_import_hangers.py +++ /dev/null @@ -1,45 +0,0 @@ -# This is a helper module for test_threaded_import. The test imports this -# module, and this module tries to run various Python library functions in -# their own thread, as a side effect of being imported. If the spawned -# thread doesn't complete in TIMEOUT seconds, an "appeared to hang" message -# is appended to the module-global `errors` list. That list remains empty -# if (and only if) all functions tested complete. - -TIMEOUT = 10 - -import threading - -import tempfile -import os.path - -errors = [] - -# This class merely runs a function in its own thread T. The thread importing -# this module holds the import lock, so if the function called by T tries -# to do its own imports it will block waiting for this module's import -# to complete. -class Worker(threading.Thread): - def __init__(self, function, args): - threading.Thread.__init__(self) - self.function = function - self.args = args - - def run(self): - self.function(*self.args) - -for name, func, args in [ - # Bug 147376: TemporaryFile hung on Windows, starting in Python 2.4. - ("tempfile.TemporaryFile", lambda: tempfile.TemporaryFile().close(), ()), - - # The real cause for bug 147376: ntpath.abspath() caused the hang. - ("os.path.abspath", os.path.abspath, ('.',)), - ]: - - try: - t = Worker(func, args) - t.start() - t.join(TIMEOUT) - if t.is_alive(): - errors.append("%s appeared to hang" % name) - finally: - del t -- cgit v0.12