def run(self): print("inside ID", Local._get_context_id()) # Make sure the attribute is not there try: test_local.foo self.failed = "leak inside" return except AttributeError: pass # Check the value is good self.failed = "set inside" test_local.foo = 123 assert test_local.foo == 123 # Binary signal that these tests passed to the outer thread self.failed = ""
def test_local_thread_nested(): """ Tests that local does not leak across threads """ test_local = Local() # Unassigned should be an error with pytest.raises(AttributeError): test_local.foo print("outside ID", Local._get_context_id()) # Assign and check it does not persist inside the thread class TestThread(threading.Thread): # Failure reason failed = "unknown" def run(self): print("inside ID", Local._get_context_id()) # Make sure the attribute is not there try: test_local.foo self.failed = "leak inside" return except AttributeError: pass # Check the value is good self.failed = "set inside" test_local.foo = 123 assert test_local.foo == 123 # Binary signal that these tests passed to the outer thread self.failed = "" test_local.foo = 8 thread = TestThread() thread.start() thread.join() assert thread.failed == "" # Check it didn't leak back out assert test_local.foo == 8