Skip to content

Commit 224481a

Browse files
authored
bpo-39947: Move Py_EnterRecursiveCall() to internal C API (GH-18972)
Move the static inline function flavor of Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() to the internal C API: they access PyThreadState attributes. The limited C API provides regular functions which hide implementation details.
1 parent 0b72b23 commit 224481a

File tree

7 files changed

+68
-56
lines changed

7 files changed

+68
-56
lines changed

Include/cpython/ceval.h

-56
Original file line numberDiff line numberDiff line change
@@ -31,62 +31,6 @@ PyAPI_FUNC(Py_ssize_t) _PyEval_RequestCodeExtraIndex(freefunc);
3131
PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *);
3232
PyAPI_FUNC(int) _PyEval_SliceIndexNotNone(PyObject *, Py_ssize_t *);
3333

34-
PyAPI_DATA(int) _Py_CheckRecursionLimit;
35-
36-
#ifdef USE_STACKCHECK
37-
/* With USE_STACKCHECK macro defined, trigger stack checks in
38-
_Py_CheckRecursiveCall() on every 64th call to Py_EnterRecursiveCall. */
39-
static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
40-
return (++tstate->recursion_depth > _Py_CheckRecursionLimit
41-
|| ++tstate->stackcheck_counter > 64);
42-
}
43-
#else
44-
static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
45-
return (++tstate->recursion_depth > _Py_CheckRecursionLimit);
46-
}
47-
#endif
48-
49-
PyAPI_FUNC(int) _Py_CheckRecursiveCall(
50-
PyThreadState *tstate,
51-
const char *where);
52-
53-
static inline int _Py_EnterRecursiveCall(PyThreadState *tstate,
54-
const char *where) {
55-
return (_Py_MakeRecCheck(tstate) && _Py_CheckRecursiveCall(tstate, where));
56-
}
57-
58-
static inline int _Py_EnterRecursiveCall_inline(const char *where) {
59-
PyThreadState *tstate = PyThreadState_GET();
60-
return _Py_EnterRecursiveCall(tstate, where);
61-
}
62-
63-
#define Py_EnterRecursiveCall(where) _Py_EnterRecursiveCall_inline(where)
64-
65-
66-
/* Compute the "lower-water mark" for a recursion limit. When
67-
* Py_LeaveRecursiveCall() is called with a recursion depth below this mark,
68-
* the overflowed flag is reset to 0. */
69-
#define _Py_RecursionLimitLowerWaterMark(limit) \
70-
(((limit) > 200) \
71-
? ((limit) - 50) \
72-
: (3 * ((limit) >> 2)))
73-
74-
#define _Py_MakeEndRecCheck(x) \
75-
(--(x) < _Py_RecursionLimitLowerWaterMark(_Py_CheckRecursionLimit))
76-
77-
static inline void _Py_LeaveRecursiveCall(PyThreadState *tstate) {
78-
if (_Py_MakeEndRecCheck(tstate->recursion_depth)) {
79-
tstate->overflowed = 0;
80-
}
81-
}
82-
83-
static inline void _Py_LeaveRecursiveCall_inline(void) {
84-
PyThreadState *tstate = PyThreadState_GET();
85-
_Py_LeaveRecursiveCall(tstate);
86-
}
87-
88-
#define Py_LeaveRecursiveCall() _Py_LeaveRecursiveCall_inline()
89-
9034
#ifdef __cplusplus
9135
}
9236
#endif

Include/internal/pycore_ceval.h

+60
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,66 @@ extern PyObject *_PyEval_EvalCode(
5656
extern int _PyEval_ThreadsInitialized(_PyRuntimeState *runtime);
5757
extern PyStatus _PyEval_InitThreads(PyThreadState *tstate);
5858

59+
60+
/* --- _Py_EnterRecursiveCall() ----------------------------------------- */
61+
62+
PyAPI_DATA(int) _Py_CheckRecursionLimit;
63+
64+
#ifdef USE_STACKCHECK
65+
/* With USE_STACKCHECK macro defined, trigger stack checks in
66+
_Py_CheckRecursiveCall() on every 64th call to Py_EnterRecursiveCall. */
67+
static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
68+
return (++tstate->recursion_depth > _Py_CheckRecursionLimit
69+
|| ++tstate->stackcheck_counter > 64);
70+
}
71+
#else
72+
static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
73+
return (++tstate->recursion_depth > _Py_CheckRecursionLimit);
74+
}
75+
#endif
76+
77+
PyAPI_FUNC(int) _Py_CheckRecursiveCall(
78+
PyThreadState *tstate,
79+
const char *where);
80+
81+
static inline int _Py_EnterRecursiveCall(PyThreadState *tstate,
82+
const char *where) {
83+
return (_Py_MakeRecCheck(tstate) && _Py_CheckRecursiveCall(tstate, where));
84+
}
85+
86+
static inline int _Py_EnterRecursiveCall_inline(const char *where) {
87+
PyThreadState *tstate = PyThreadState_GET();
88+
return _Py_EnterRecursiveCall(tstate, where);
89+
}
90+
91+
#define Py_EnterRecursiveCall(where) _Py_EnterRecursiveCall_inline(where)
92+
93+
94+
/* Compute the "lower-water mark" for a recursion limit. When
95+
* Py_LeaveRecursiveCall() is called with a recursion depth below this mark,
96+
* the overflowed flag is reset to 0. */
97+
#define _Py_RecursionLimitLowerWaterMark(limit) \
98+
(((limit) > 200) \
99+
? ((limit) - 50) \
100+
: (3 * ((limit) >> 2)))
101+
102+
#define _Py_MakeEndRecCheck(x) \
103+
(--(x) < _Py_RecursionLimitLowerWaterMark(_Py_CheckRecursionLimit))
104+
105+
static inline void _Py_LeaveRecursiveCall(PyThreadState *tstate) {
106+
if (_Py_MakeEndRecCheck(tstate->recursion_depth)) {
107+
tstate->overflowed = 0;
108+
}
109+
}
110+
111+
static inline void _Py_LeaveRecursiveCall_inline(void) {
112+
PyThreadState *tstate = PyThreadState_GET();
113+
_Py_LeaveRecursiveCall(tstate);
114+
}
115+
116+
#define Py_LeaveRecursiveCall() _Py_LeaveRecursiveCall_inline()
117+
118+
59119
#ifdef __cplusplus
60120
}
61121
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Move the static inline function flavor of Py_EnterRecursiveCall() and
2+
Py_LeaveRecursiveCall() to the internal C API: they access PyThreadState
3+
attributes. The limited C API provides regular functions which hide
4+
implementation details.

Objects/abstract.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Abstract Object Interface (many thanks to Jim Fulton) */
22

33
#include "Python.h"
4+
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
45
#include "pycore_pyerrors.h"
56
#include "pycore_pystate.h"
67
#include <ctype.h>

Objects/descrobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* Descriptors -- a new, flexible way to describe attributes */
22

33
#include "Python.h"
4+
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
45
#include "pycore_object.h"
56
#include "pycore_pystate.h"
67
#include "pycore_tupleobject.h"

Objects/methodobject.c

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* Method object implementation */
33

44
#include "Python.h"
5+
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
56
#include "pycore_object.h"
67
#include "pycore_pyerrors.h"
78
#include "pycore_pymem.h"

Objects/object.c

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* Generic object operations; and implementation of None */
33

44
#include "Python.h"
5+
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
56
#include "pycore_context.h"
67
#include "pycore_initconfig.h"
78
#include "pycore_object.h"

0 commit comments

Comments
 (0)