aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_threading_local.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_threading_local.py')
-rw-r--r--Lib/test/test_threading_local.py67
1 files changed, 29 insertions, 38 deletions
diff --git a/Lib/test/test_threading_local.py b/Lib/test/test_threading_local.py
index 4c9f2961ae8..c886a25d8ab 100644
--- a/Lib/test/test_threading_local.py
+++ b/Lib/test/test_threading_local.py
@@ -1,12 +1,12 @@
import unittest
from doctest import DocTestSuite
-from test import test_support
+from test import support
import weakref
import gc
# Modules under test
-_thread = test_support.import_module('thread')
-threading = test_support.import_module('threading')
+_thread = support.import_module('_thread')
+threading = support.import_module('threading')
import _threading_local
@@ -18,6 +18,7 @@ def target(local, weaklist):
local.weak = weak
weaklist.append(weakref.ref(weak))
+
class BaseLocalTest:
def test_local_refs(self):
@@ -77,11 +78,12 @@ class BaseLocalTest:
class Local(self._local):
pass
locals = None
- passed = [False]
+ passed = False
e1 = threading.Event()
e2 = threading.Event()
def f():
+ nonlocal passed
# 1) Involve Local in a cycle
cycle = [Local()]
cycle.append(cycle)
@@ -95,7 +97,7 @@ class BaseLocalTest:
e2.wait()
# 4) New Locals should be empty
- passed[0] = all(not hasattr(local, 'foo') for local in locals)
+ passed = all(not hasattr(local, 'foo') for local in locals)
t = threading.Thread(target=f)
t.start()
@@ -108,22 +110,18 @@ class BaseLocalTest:
e2.set()
t.join()
- self.assertTrue(passed[0])
+ self.assertTrue(passed)
def test_arguments(self):
# Issue 1522237
- from thread import _local as local
- from _threading_local import local as py_local
-
- for cls in (local, py_local):
- class MyLocal(cls):
- def __init__(self, *args, **kwargs):
- pass
+ class MyLocal(self._local):
+ def __init__(self, *args, **kwargs):
+ pass
- MyLocal(a=1)
- MyLocal(1)
- self.assertRaises(TypeError, cls, a=1)
- self.assertRaises(TypeError, cls, 1)
+ MyLocal(a=1)
+ MyLocal(1)
+ self.assertRaises(TypeError, self._local, a=1)
+ self.assertRaises(TypeError, self._local, 1)
def _test_one_class(self, c):
self._failed = "No error message set or cleared."
@@ -186,11 +184,6 @@ class BaseLocalTest:
"""To test that subclasses behave properly."""
self._test_dict_attribute(LocalSubclass)
-
-class ThreadLocalTest(unittest.TestCase, BaseLocalTest):
- _local = _thread._local
-
- # Fails for the pure Python implementation
def test_cycle_collection(self):
class X:
pass
@@ -203,6 +196,10 @@ class ThreadLocalTest(unittest.TestCase, BaseLocalTest):
gc.collect()
self.assertIs(wr(), None)
+
+class ThreadLocalTest(unittest.TestCase, BaseLocalTest):
+ _local = _thread._local
+
class PyThreadingLocalTest(unittest.TestCase, BaseLocalTest):
_local = _threading_local.local
@@ -213,22 +210,16 @@ def test_main():
suite.addTest(unittest.makeSuite(ThreadLocalTest))
suite.addTest(unittest.makeSuite(PyThreadingLocalTest))
- try:
- from thread import _local
- except ImportError:
- pass
- else:
- import _threading_local
- local_orig = _threading_local.local
- def setUp(test):
- _threading_local.local = _local
- def tearDown(test):
- _threading_local.local = local_orig
- suite.addTest(DocTestSuite('_threading_local',
- setUp=setUp, tearDown=tearDown)
- )
-
- test_support.run_unittest(suite)
+ local_orig = _threading_local.local
+ def setUp(test):
+ _threading_local.local = _thread._local
+ def tearDown(test):
+ _threading_local.local = local_orig
+ suite.addTest(DocTestSuite('_threading_local',
+ setUp=setUp, tearDown=tearDown)
+ )
+
+ support.run_unittest(suite)
if __name__ == '__main__':
test_main()