Skip to content

Commit 5fc6653

Browse files
authored
Fix tests on python3.14t (#166)
* Add Py_MOD_GIL_NOT_USED to the C/C++ extension. * Add "t" suffix to the Python version on free-threaded build. * On Python 3.14 and newer, call sys._clear_internal_caches() instead of sys._clear_type_cache() to avoid a deprecation warning.
1 parent 442b482 commit 5fc6653

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

tests/test_pythoncapi_compat.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
except ImportError:
2020
# Python 2
2121
faulthandler = None
22+
try:
23+
import sysconfig
24+
except ImportError:
25+
# Python 3.1 and older
26+
sysconfig = None
2227

2328
# test.utils
2429
from utils import run_command, command_stdout
@@ -49,9 +54,7 @@ def display_title(title):
4954
if not VERBOSE:
5055
return
5156

52-
ver = sys.version_info
53-
title = "Python %s.%s: %s" % (ver.major, ver.minor, title)
54-
57+
title = "%s: %s" % (python_version(), title)
5558
print(title)
5659
print("=" * len(title))
5760
print()
@@ -94,10 +97,13 @@ def _run_tests(tests, verbose):
9497
test_func()
9598

9699

100+
_HAS_CLEAR_INTERNAL_CACHES = hasattr(sys, '_clear_internal_caches')
97101
_HAS_CLEAR_TYPE_CACHE = hasattr(sys, '_clear_type_cache')
98102

99103
def _refleak_cleanup():
100-
if _HAS_CLEAR_TYPE_CACHE:
104+
if _HAS_CLEAR_INTERNAL_CACHES:
105+
sys._clear_internal_caches()
106+
elif _HAS_CLEAR_TYPE_CACHE:
101107
sys._clear_type_cache()
102108
gc.collect()
103109

@@ -134,10 +140,18 @@ def python_version():
134140
python_impl = "PyPy"
135141
else:
136142
python_impl = 'Python'
137-
return "%s %s.%s (%s build)" % (python_impl, ver.major, ver.minor, build)
143+
pyver = "%s.%s" % (ver.major, ver.minor)
144+
if ver >= (3, 13):
145+
if sysconfig.get_config_var('Py_GIL_DISABLED'):
146+
# Free-threaded build
147+
pyver += "t"
148+
return "%s %s (%s build)" % (python_impl, pyver, build)
138149

139150

140151
def run_tests(module_name, lang):
152+
if VERBOSE:
153+
print("")
154+
141155
title = "Test %s (%s)" % (module_name, lang)
142156
display_title(title)
143157

tests/test_pythoncapi_compat_cext.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2624,6 +2624,9 @@ module_exec(PyObject *module)
26242624
#if PY_VERSION_HEX >= 0x03050000
26252625
static PyModuleDef_Slot module_slots[] = {
26262626
{Py_mod_exec, _Py_CAST(void*, module_exec)},
2627+
#if PY_VERSION_HEX >= 0x030D0000
2628+
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
2629+
#endif
26272630
{0, _Py_NULL}
26282631
};
26292632
#endif

0 commit comments

Comments
 (0)