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

In test_threading_local, test both the default _thread._local implementation

and the pure Python implementation in Lib/_threading_local.py
üst ee55df5c
import unittest import unittest
from doctest import DocTestSuite from doctest import DocTestSuite
from test import support from test import support
threading = support.import_module('threading')
import weakref import weakref
import gc import gc
# Modules under test
_thread = support.import_module('_thread')
threading = support.import_module('threading')
import _threading_local
class Weak(object): class Weak(object):
pass pass
...@@ -13,7 +18,8 @@ def target(local, weaklist): ...@@ -13,7 +18,8 @@ def target(local, weaklist):
local.weak = weak local.weak = weak
weaklist.append(weakref.ref(weak)) weaklist.append(weakref.ref(weak))
class ThreadingLocalTest(unittest.TestCase):
class BaseLocalTest:
def test_local_refs(self): def test_local_refs(self):
self._local_refs(20) self._local_refs(20)
...@@ -21,7 +27,7 @@ class ThreadingLocalTest(unittest.TestCase): ...@@ -21,7 +27,7 @@ class ThreadingLocalTest(unittest.TestCase):
self._local_refs(100) self._local_refs(100)
def _local_refs(self, n): def _local_refs(self, n):
local = threading.local() local = self._local()
weaklist = [] weaklist = []
for i in range(n): for i in range(n):
t = threading.Thread(target=target, args=(local, weaklist)) t = threading.Thread(target=target, args=(local, weaklist))
...@@ -48,7 +54,7 @@ class ThreadingLocalTest(unittest.TestCase): ...@@ -48,7 +54,7 @@ class ThreadingLocalTest(unittest.TestCase):
# is created but not correctly set on the object. # is created but not correctly set on the object.
# The first member set may be bogus. # The first member set may be bogus.
import time import time
class Local(threading.local): class Local(self._local):
def __init__(self): def __init__(self):
time.sleep(0.01) time.sleep(0.01)
local = Local() local = Local()
...@@ -69,7 +75,7 @@ class ThreadingLocalTest(unittest.TestCase): ...@@ -69,7 +75,7 @@ class ThreadingLocalTest(unittest.TestCase):
def test_derived_cycle_dealloc(self): def test_derived_cycle_dealloc(self):
# http://bugs.python.org/issue6990 # http://bugs.python.org/issue6990
class Local(threading.local): class Local(self._local):
pass pass
locals = None locals = None
passed = False passed = False
...@@ -108,24 +114,28 @@ class ThreadingLocalTest(unittest.TestCase): ...@@ -108,24 +114,28 @@ class ThreadingLocalTest(unittest.TestCase):
def test_arguments(self): def test_arguments(self):
# Issue 1522237 # Issue 1522237
from _thread import _local as local class MyLocal(self._local):
from _threading_local import local as py_local def __init__(self, *args, **kwargs):
pass
MyLocal(a=1)
MyLocal(1)
self.assertRaises(TypeError, self._local, a=1)
self.assertRaises(TypeError, self._local, 1)
for cls in (local, py_local): class ThreadLocalTest(unittest.TestCase, BaseLocalTest):
class MyLocal(cls): _local = _thread._local
def __init__(self, *args, **kwargs):
pass
MyLocal(a=1) class PyThreadingLocalTest(unittest.TestCase, BaseLocalTest):
MyLocal(1) _local = _threading_local.local
self.assertRaises(TypeError, cls, a=1)
self.assertRaises(TypeError, cls, 1)
def test_main(): def test_main():
suite = unittest.TestSuite() suite = unittest.TestSuite()
suite.addTest(DocTestSuite('_threading_local')) suite.addTest(DocTestSuite('_threading_local'))
suite.addTest(unittest.makeSuite(ThreadingLocalTest)) suite.addTest(unittest.makeSuite(ThreadLocalTest))
suite.addTest(unittest.makeSuite(PyThreadingLocalTest))
try: try:
from thread import _local from thread import _local
......
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