Skip to content

Commit 3f2f4fe

Browse files
authored
bpo-39947: Move get_recursion_depth() to _testinternalcapi (GH-18974)
Move get_recursion_depth() function from _testcapi to _testinternalcapi to avoid accessing PyThreadState attributes directly in _testcapi.
1 parent 224481a commit 3f2f4fe

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

Lib/test/test_exceptions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ def test_recursion_normalizing_exception(self):
994994
# finalization of these locals.
995995
code = """if 1:
996996
import sys
997-
from _testcapi import get_recursion_depth
997+
from _testinternalcapi import get_recursion_depth
998998
999999
class MyException(Exception): pass
10001000

Lib/test/test_sys.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def test_setrecursionlimit_recursion_depth(self):
241241
# mark". Otherwise, it may not be possible anymore to
242242
# reset the overflowed flag to 0.
243243

244-
from _testcapi import get_recursion_depth
244+
from _testinternalcapi import get_recursion_depth
245245

246246
def set_recursion_limit_at_depth(depth, limit):
247247
recursion_depth = get_recursion_depth()

Modules/_testcapimodule.c

+5-13
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
* standard Python regression test, via Lib/test/test_capi.py.
66
*/
77

8-
/* The Visual Studio projects builds _testcapi with Py_BUILD_CORE_MODULE
9-
define, but we only want to test the public C API, not the internal
10-
C API. */
8+
/* This module tests the public (Include/ and Include/cpython/) C API.
9+
The internal C API must not be used here: use _testinternalcapi for that.
10+
11+
The Visual Studio projects builds _testcapi with Py_BUILD_CORE_MODULE
12+
macro defined, but only the public C API must be tested here. */
1113
#undef Py_BUILD_CORE_MODULE
1214

1315
#define PY_SSIZE_T_CLEAN
@@ -4523,15 +4525,6 @@ test_PyTime_AsMicroseconds(PyObject *self, PyObject *args)
45234525
return _PyTime_AsNanosecondsObject(ms);
45244526
}
45254527

4526-
static PyObject*
4527-
get_recursion_depth(PyObject *self, PyObject *args)
4528-
{
4529-
PyThreadState *tstate = PyThreadState_Get();
4530-
4531-
/* subtract one to ignore the frame of the get_recursion_depth() call */
4532-
return PyLong_FromLong(tstate->recursion_depth - 1);
4533-
}
4534-
45354528
static PyObject*
45364529
pymem_buffer_overflow(PyObject *self, PyObject *args)
45374530
{
@@ -5486,7 +5479,6 @@ static PyMethodDef TestMethods[] = {
54865479
#endif
54875480
{"PyTime_AsMilliseconds", test_PyTime_AsMilliseconds, METH_VARARGS},
54885481
{"PyTime_AsMicroseconds", test_PyTime_AsMicroseconds, METH_VARARGS},
5489-
{"get_recursion_depth", get_recursion_depth, METH_NOARGS},
54905482
{"pymem_buffer_overflow", pymem_buffer_overflow, METH_NOARGS},
54915483
{"pymem_api_misuse", pymem_api_misuse, METH_NOARGS},
54925484
{"pymem_malloc_without_gil", pymem_malloc_without_gil, METH_NOARGS},

Modules/_testinternalcapi.c

+12-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#define PY_SSIZE_T_CLEAN
1010

1111
#include "Python.h"
12-
#include "pycore_initconfig.h"
12+
#include "pycore_initconfig.h" // _Py_GetConfigsAsDict()
1313

1414

1515
static PyObject *
@@ -19,8 +19,19 @@ get_configs(PyObject *self, PyObject *Py_UNUSED(args))
1919
}
2020

2121

22+
static PyObject*
23+
get_recursion_depth(PyObject *self, PyObject *args)
24+
{
25+
PyThreadState *tstate = PyThreadState_Get();
26+
27+
/* subtract one to ignore the frame of the get_recursion_depth() call */
28+
return PyLong_FromLong(tstate->recursion_depth - 1);
29+
}
30+
31+
2232
static PyMethodDef TestMethods[] = {
2333
{"get_configs", get_configs, METH_NOARGS},
34+
{"get_recursion_depth", get_recursion_depth, METH_NOARGS},
2435
{NULL, NULL} /* sentinel */
2536
};
2637

0 commit comments

Comments
 (0)