[Python-checkins] CVS: python/dist/src/Lib/test regrtest.py,1.34,1.35 test_threaded_import.py,1.1,1.2
Tim Peters
tim_one@users.sourceforge.net
Tue, 22 May 2001 11:28:27 -0700
Update of /cvsroot/python/python/dist/src/Lib/test
In directory usw-pr-cvs1:/tmp/cvs-serv28920/python/dist/src/Lib/test
Modified Files:
regrtest.py test_threaded_import.py
Log Message:
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.
Index: regrtest.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/regrtest.py,v
retrieving revision 1.34
retrieving revision 1.35
diff -C2 -r1.34 -r1.35
*** regrtest.py 2001/05/21 21:08:12 1.34
--- regrtest.py 2001/05/22 18:28:25 1.35
***************
*** 245,249 ****
sys.stdout = cfp
print test # Output file starts with test name
! __import__(test, globals(), locals(), [])
if cfp and not (generate or verbose):
cfp.close()
--- 245,256 ----
sys.stdout = cfp
print test # Output file starts with test name
! 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()
Index: test_threaded_import.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_threaded_import.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** test_threaded_import.py 2001/05/22 09:34:27 1.1
--- test_threaded_import.py 2001/05/22 18:28:25 1.2
***************
*** 7,10 ****
--- 7,11 ----
import thread
+ from test_support import verbose
critical_section = thread.allocate_lock()
***************
*** 21,52 ****
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.
! def run_the_test():
global N, done
done.acquire()
! for N in [1, 2, 3, 4, 20, 4, 3, 2]:
for i in range(N):
thread.start_new_thread(task, ())
done.acquire()
! thread.start_new_thread(run_the_test, ())
--- 22,46 ----
critical_section.release()
! # 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 test_main(): # magic name! see above
global N, done
done.acquire()
! 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."
! if __name__ == "__main__":
! test_main()