| 1 |
import ctypes |
| 2 |
import sys |
| 3 |
import unittest |
| 4 |
|
| 5 |
from test.support import threading_helper |
| 6 |
from test.support.threading_helper import run_concurrently |
| 7 |
|
| 8 |
|
| 9 |
_PyImport_AddModuleRef = ctypes.pythonapi.PyImport_AddModuleRef |
| 10 |
_PyImport_AddModuleRef.argtypes = (ctypes.c_char_p,) |
| 11 |
_PyImport_AddModuleRef.restype = ctypes.py_object |
| 12 |
|
| 13 |
|
| 14 |
@threading_helper.requires_working_threading() |
| 15 |
class TestImportCAPI(unittest.TestCase): |
| 16 |
def test_pyimport_addmoduleref_thread_safe(self): |
| 17 |
# gh-137422: Concurrent calls to PyImport_AddModuleRef with the same |
| 18 |
# module name must return the same module object. |
| 19 |
|
| 20 |
NUM_ITERS = 10 |
| 21 |
NTHREADS = 4 |
| 22 |
|
| 23 |
module_name = f"test_free_threading_addmoduleref_{id(self)}" |
| 24 |
module_name_bytes = module_name.encode() |
| 25 |
sys.modules.pop(module_name, None) |
| 26 |
results = [] |
| 27 |
|
| 28 |
def worker(): |
| 29 |
module = _PyImport_AddModuleRef(module_name_bytes) |
| 30 |
results.append(module) |
| 31 |
|
| 32 |
for _ in range(NUM_ITERS): |
| 33 |
try: |
| 34 |
run_concurrently(worker_func=worker, nthreads=NTHREADS) |
| 35 |
self.assertEqual(len(results), NTHREADS) |
| 36 |
reference = results[0] |
| 37 |
for module in results[1:]: |
| 38 |
self.assertIs(module, reference) |
| 39 |
self.assertIn(module_name, sys.modules) |
| 40 |
self.assertIs(sys.modules[module_name], reference) |
| 41 |
finally: |
| 42 |
results.clear() |
| 43 |
sys.modules.pop(module_name, None) |
| 44 |
|
| 45 |
|
| 46 |
if __name__ == "__main__": |
| 47 |
unittest.main() |