1import unittest 2 3from threading import Thread 4from unittest import TestCase 5 6from test.support import threading_helper 7 8@threading_helper.requires_working_threading() 9class TestCode(TestCase): 10 def test_code_attrs(self): 11 """Test concurrent accesses to lazily initialized code attributes""" 12 code_objects = [] 13 for _ in range(1000): 14 code_objects.append(compile("a + b", "<string>", "eval")) 15 16 def run_in_thread(): 17 for code in code_objects: 18 self.assertIsInstance(code.co_code, bytes) 19 self.assertIsInstance(code.co_freevars, tuple) 20 self.assertIsInstance(code.co_varnames, tuple) 21 22 threads = [Thread(target=run_in_thread) for _ in range(2)] 23 for thread in threads: 24 thread.start() 25 for thread in threads: 26 thread.join() 27 28 29if __name__ == "__main__": 30 unittest.main() 31