diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2012-05-17 16:55:59 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2012-05-17 16:55:59 (GMT) |
commit | ea3eb88bcaef775c8f2bbe310053f9990a8c9ea7 (patch) | |
tree | 9461ae28e833a95a79cc4811df68435bf0e00fc3 /Lib/test | |
parent | 5cec9d2ae57da6fb5c424bb297ddb67902393b2d (diff) | |
download | cpython-ea3eb88bcaef775c8f2bbe310053f9990a8c9ea7.zip cpython-ea3eb88bcaef775c8f2bbe310053f9990a8c9ea7.tar.gz cpython-ea3eb88bcaef775c8f2bbe310053f9990a8c9ea7.tar.bz2 |
Issue #9260: A finer-grained import lock.
Most of the import sequence now uses per-module locks rather than the
global import lock, eliminating well-known issues with threads and imports.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/lock_tests.py | 12 | ||||
-rw-r--r-- | Lib/test/test_pkg.py | 2 | ||||
-rw-r--r-- | Lib/test/test_threaded_import.py | 19 |
3 files changed, 30 insertions, 3 deletions
diff --git a/Lib/test/lock_tests.py b/Lib/test/lock_tests.py index d88f364..bfbf44e 100644 --- a/Lib/test/lock_tests.py +++ b/Lib/test/lock_tests.py @@ -247,7 +247,6 @@ class RLockTests(BaseLockTests): # Cannot release an unacquired lock lock = self.locktype() self.assertRaises(RuntimeError, lock.release) - self.assertRaises(RuntimeError, lock._release_save) lock.acquire() lock.acquire() lock.release() @@ -255,6 +254,17 @@ class RLockTests(BaseLockTests): lock.release() lock.release() self.assertRaises(RuntimeError, lock.release) + + def test_release_save_unacquired(self): + # Cannot _release_save an unacquired lock + lock = self.locktype() + self.assertRaises(RuntimeError, lock._release_save) + lock.acquire() + lock.acquire() + lock.release() + lock.acquire() + lock.release() + lock.release() self.assertRaises(RuntimeError, lock._release_save) def test_different_thread(self): diff --git a/Lib/test/test_pkg.py b/Lib/test/test_pkg.py index 4d0eee8..355efe7 100644 --- a/Lib/test/test_pkg.py +++ b/Lib/test/test_pkg.py @@ -23,6 +23,8 @@ def cleanout(root): def fixdir(lst): if "__builtins__" in lst: lst.remove("__builtins__") + if "__initializing__" in lst: + lst.remove("__initializing__") return lst diff --git a/Lib/test/test_threaded_import.py b/Lib/test/test_threaded_import.py index bb72ad4..d67c904 100644 --- a/Lib/test/test_threaded_import.py +++ b/Lib/test/test_threaded_import.py @@ -12,7 +12,7 @@ import time import shutil import unittest from test.support import ( - verbose, import_module, run_unittest, TESTFN, reap_threads) + verbose, import_module, run_unittest, TESTFN, reap_threads, forget) threading = import_module('threading') def task(N, done, done_tasks, errors): @@ -187,7 +187,7 @@ class ThreadedImportTests(unittest.TestCase): contents = contents % {'delay': delay} with open(os.path.join(TESTFN, name + ".py"), "wb") as f: f.write(contents.encode('utf-8')) - self.addCleanup(sys.modules.pop, name, None) + self.addCleanup(forget, name) results = [] def import_ab(): @@ -204,6 +204,21 @@ class ThreadedImportTests(unittest.TestCase): t2.join() self.assertEqual(set(results), {'a', 'b'}) + def test_side_effect_import(self): + code = """if 1: + import threading + def target(): + import random + t = threading.Thread(target=target) + t.start() + t.join()""" + sys.path.insert(0, os.curdir) + self.addCleanup(sys.path.remove, os.curdir) + with open(TESTFN + ".py", "wb") as f: + f.write(code.encode('utf-8')) + self.addCleanup(forget, TESTFN) + __import__(TESTFN) + @reap_threads def test_main(): |