diff options
author | Tim Peters <tim.peters@gmail.com> | 2001-05-22 18:28:25 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2001-05-22 18:28:25 (GMT) |
commit | d97422115e9ed6498bc7a6f792a0bf8f278f9097 (patch) | |
tree | a976c7881907ac437d497527a06861fb5fc83693 /Lib | |
parent | bc5619826e6c84d68f73df02d712302b9f25a924 (diff) | |
download | cpython-d97422115e9ed6498bc7a6f792a0bf8f278f9097.zip cpython-d97422115e9ed6498bc7a6f792a0bf8f278f9097.tar.gz cpython-d97422115e9ed6498bc7a6f792a0bf8f278f9097.tar.bz2 |
Implementing an idea from Guido on the checkins list:
When regrtest.py finds an attribute "test_main" in a test it imports,
regrtest runs the test's test_main after the import. test_threaded_import
needs this else the cross-thread import lock prevents it from making
progress. Other tests can use this hack too, but I doubt it will ever be
popular.
Diffstat (limited to 'Lib')
-rwxr-xr-x | Lib/test/regrtest.py | 9 | ||||
-rw-r--r-- | Lib/test/test_threaded_import.py | 40 |
2 files changed, 25 insertions, 24 deletions
diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py index c77abc3..9c83221 100755 --- a/Lib/test/regrtest.py +++ b/Lib/test/regrtest.py @@ -244,7 +244,14 @@ def runtest(test, generate, verbose, quiet, testdir = None): if cfp: sys.stdout = cfp print test # Output file starts with test name - __import__(test, globals(), locals(), []) + the_module = __import__(test, globals(), locals(), []) + # Most tests run to completion simply as a side-effect of + # being imported. For the benefit of tests that can't run + # that way (like test_threaded_import), explicitly invoke + # their test_main() function (if it exists). + indirect_test = getattr(the_module, "test_main", None) + if indirect_test is not None: + indirect_test() if cfp and not (generate or verbose): cfp.close() finally: diff --git a/Lib/test/test_threaded_import.py b/Lib/test/test_threaded_import.py index 17fe4c3..fafb873 100644 --- a/Lib/test/test_threaded_import.py +++ b/Lib/test/test_threaded_import.py @@ -6,6 +6,7 @@ # randrange, and then Python hangs. import thread +from test_support import verbose critical_section = thread.allocate_lock() done = thread.allocate_lock() @@ -20,33 +21,26 @@ def task(): done.release() critical_section.release() -# Tricky, tricky, tricky. -# When regrtest imports this module, the thread running regrtest grabs the -# import lock and won't let go of it until this module returns. All other -# threads attempting an import hang for the duration. So we have to spawn -# a thread to run the test and return to regrtest.py right away, else the -# test can't make progress. -# -# One miserable consequence: This test can't wait to make sure all the -# threads complete! -# -# Another: If this test fails, the output may show up while running -# some other test. -# -# Another: If you run this test directly, the OS will probably kill -# all the threads right away, because the program exits immediately -# after spawning a thread to run the real test. -# -# Another: If this test ever does fail and you attempt to run it by -# itself via regrtest, the same applies: regrtest will get out so fast -# the OS will kill all the threads here. +# Tricky: When regrtest imports this module, the thread running regrtest +# grabs the import lock and won't let go of it until this module returns. +# All other threads attempting an import hang for the duration. Since +# this test spawns threads that do little *but* import, we can't do that +# successfully until after this module finishes importing and regrtest +# regains control. To make this work, a special case was added to +# regrtest to invoke a module's "test_main" function (if any) after +# importing it. -def run_the_test(): +def test_main(): # magic name! see above global N, done done.acquire() - for N in [1, 2, 3, 4, 20, 4, 3, 2]: + for N in (20, 50) * 3: + if verbose: + print "Trying", N, "threads ...", for i in range(N): thread.start_new_thread(task, ()) done.acquire() + if verbose: + print "OK." -thread.start_new_thread(run_the_test, ()) +if __name__ == "__main__": + test_main() |