Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 513acb3

Browse files
authored
Update tests for Python 3.12 (#60)
* Install explicitly setuptools for Python 3.12 * Fix tests for Python 3.12 beta1 immortal objects PEP 683 * Fix refleak check for Python 3.12
1 parent b4f2bb6 commit 513acb3

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

.github/workflows/build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ jobs:
5858
uses: actions/setup-python@v4
5959
with:
6060
python-version: ${{ matrix.python }}
61+
- name: Install setuptools
62+
run: python -m pip install setuptools
6163
- name: Display the Python version
6264
run: python -VV
6365
- name: Run tests

tests/test_pythoncapi_compat.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""
1010
from __future__ import absolute_import
1111
from __future__ import print_function
12+
import gc
1213
import os.path
1314
import shutil
1415
import subprocess
@@ -82,6 +83,14 @@ def _run_tests(tests, verbose):
8283
test_func()
8384

8485

86+
_HAS_CLEAR_TYPE_CACHE = hasattr(sys, '_clear_type_cache')
87+
88+
def _refleak_cleanup():
89+
if _HAS_CLEAR_TYPE_CACHE:
90+
sys._clear_type_cache()
91+
gc.collect()
92+
93+
8594
def _check_refleak(test_func, verbose):
8695
nrun = 6
8796
for i in range(1, nrun + 1):
@@ -93,6 +102,7 @@ def _check_refleak(test_func, verbose):
93102

94103
init_refcnt = sys.gettotalrefcount()
95104
test_func()
105+
_refleak_cleanup()
96106
diff = sys.gettotalrefcount() - init_refcnt
97107

98108
if i > 3 and diff:

tests/test_pythoncapi_compat_cext.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
# define CHECK_REFCNT
3838
#endif
3939

40+
// CPython 3.12 beta 1 implements immortal objects (PEP 683)
41+
#if 0x030C00B1 <= PY_VERSION_HEX && !defined(PYPY_VERSION)
42+
// Don't check reference count of Python 3.12 immortal objects (ex: bool
43+
// and str types)
44+
# define IMMORTAL_OBJS
45+
#endif
46+
4047
#ifdef CHECK_REFCNT
4148
# define ASSERT_REFCNT(expr) assert(expr)
4249
#else
@@ -381,7 +388,9 @@ test_module_add_type(PyObject *module)
381388
if (PyModule_AddType(module, type) < 0) {
382389
return -1;
383390
}
391+
#ifndef IMMORTAL_OBJS
384392
ASSERT_REFCNT(Py_REFCNT(type) == refcnt + 1);
393+
#endif
385394

386395
PyObject *attr = PyObject_GetAttrString(module, type_name);
387396
if (attr == _Py_NULL) {
@@ -412,7 +421,9 @@ test_module_addobjectref(PyObject *module)
412421
ASSERT_REFCNT(Py_REFCNT(obj) == refcnt);
413422
return -1;
414423
}
424+
#ifndef IMMORTAL_OBJS
415425
ASSERT_REFCNT(Py_REFCNT(obj) == refcnt + 1);
426+
#endif
416427

417428
if (PyObject_DelAttrString(module, name) < 0) {
418429
return -1;

0 commit comments

Comments
 (0)