Kaydet (Commit) 76e12088 authored tarafından Antoine Pitrou's avatar Antoine Pitrou

Merged revisions 82885 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r82885 | antoine.pitrou | 2010-07-14 13:52:38 +0200 (mer., 14 juil. 2010) | 4 lines

  Issue #9251: test_threaded_import didn't fail when run through regrtest
  if the import lock was disabled.
........
üst a6c03197
...@@ -5,71 +5,67 @@ ...@@ -5,71 +5,67 @@
# complains several times about module random having no attribute # complains several times about module random having no attribute
# randrange, and then Python hangs. # randrange, and then Python hangs.
import _thread as thread import imp
from test.support import verbose, TestFailed import sys
import unittest
from test.support import verbose, TestFailed, import_module, run_unittest
thread = import_module('_thread')
critical_section = thread.allocate_lock() def task(N, done, done_tasks, errors):
done = thread.allocate_lock() try:
def task():
global N, critical_section, done
import random import random
# This will fail if random is not completely initialized
x = random.randrange(1, 3) x = random.randrange(1, 3)
critical_section.acquire() except Exception as e:
N -= 1 errors.append(e.with_traceback(None))
# Must release critical_section before releasing done, else the main finally:
# thread can exit and set critical_section to None as part of global done_tasks.append(thread.get_ident())
# teardown; then critical_section.release() raises AttributeError. finished = len(done_tasks) == N
finished = N == 0
critical_section.release()
if finished: if finished:
done.release() done.release()
def test_import_hangers():
import sys
if verbose:
print("testing import hangers ...", end=' ')
import test.threaded_import_hangers
try:
if test.threaded_import_hangers.errors:
raise TestFailed(test.threaded_import_hangers.errors)
elif verbose:
print("OK.")
finally:
# In case this test is run again, make sure the helper module
# gets loaded from scratch again.
del sys.modules['test.threaded_import_hangers']
# 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 class ThreadedImportTests(unittest.TestCase):
global N, done
import imp def test_parallel_module_init(self):
if imp.lock_held(): if imp.lock_held():
# This triggers on, e.g., from test import autotest. # This triggers on, e.g., from test import autotest.
raise unittest.SkipTest("can't run when import lock is held") raise unittest.SkipTest("can't run when import lock is held")
done = thread.allocate_lock()
done.acquire() done.acquire()
for N in (20, 50) * 3: for N in (20, 50) * 3:
if verbose: if verbose:
print("Trying", N, "threads ...", end=' ') print("Trying", N, "threads ...", end=' ')
# Make sure that random gets reimported freshly
try:
del sys.modules['random']
except KeyError:
pass
errors = []
done_tasks = []
for i in range(N): for i in range(N):
thread.start_new_thread(task, ()) thread.start_new_thread(task, (N, done, done_tasks, errors,))
done.acquire() done.acquire()
self.assertFalse(errors)
if verbose: if verbose:
print("OK.") print("OK.")
done.release() done.release()
test_import_hangers() def test_import_hangers(self):
# 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']
except KeyError:
pass
import test.threaded_import_hangers
self.assertFalse(test.threaded_import_hangers.errors)
def test_main():
run_unittest(ThreadedImportTests)
if __name__ == "__main__": if __name__ == "__main__":
test_main() test_main()
...@@ -333,6 +333,9 @@ Build ...@@ -333,6 +333,9 @@ Build
Tests Tests
----- -----
- Issue #9251: test_threaded_import didn't fail when run through regrtest
if the import lock was disabled.
- Issue #7449: Skip test_socketserver if threading support is disabled - Issue #7449: Skip test_socketserver if threading support is disabled
- Issue #8672: Add a zlib test ensuring that an incomplete stream can be - Issue #8672: Add a zlib test ensuring that an incomplete stream can be
......
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