summaryrefslogtreecommitdiffstats
path: root/Include
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-05-05 14:52:52 (GMT)
committerGitHub <noreply@github.com>2020-05-05 14:52:52 (GMT)
commit4e30ed3af06ae655f4cb8aad8cba21f341384250 (patch)
treebb32e34e7c6a587446b41fea6a8f7ffac8a0c6d9 /Include
parent627f7012353411590434a7d5777ddcbcc8d97fcd (diff)
downloadcpython-4e30ed3af06ae655f4cb8aad8cba21f341384250.zip
cpython-4e30ed3af06ae655f4cb8aad8cba21f341384250.tar.gz
cpython-4e30ed3af06ae655f4cb8aad8cba21f341384250.tar.bz2
bpo-40513: Per-interpreter recursion_limit (GH-19929)
Move recursion_limit member from _PyRuntimeState.ceval to PyInterpreterState.ceval. * Py_SetRecursionLimit() now only sets _Py_CheckRecursionLimit of ceval.c if the current Python thread is part of the main interpreter. * Inline _Py_MakeEndRecCheck() into _Py_LeaveRecursiveCall(). * Convert _Py_RecursionLimitLowerWaterMark() macro into a static inline function.
Diffstat (limited to 'Include')
-rw-r--r--Include/internal/pycore_ceval.h24
-rw-r--r--Include/internal/pycore_interp.h1
-rw-r--r--Include/internal/pycore_runtime.h1
3 files changed, 14 insertions, 12 deletions
diff --git a/Include/internal/pycore_ceval.h b/Include/internal/pycore_ceval.h
index 2df796d..18c8f02 100644
--- a/Include/internal/pycore_ceval.h
+++ b/Include/internal/pycore_ceval.h
@@ -65,12 +65,12 @@ PyAPI_DATA(int) _Py_CheckRecursionLimit;
/* With USE_STACKCHECK macro defined, trigger stack checks in
_Py_CheckRecursiveCall() on every 64th call to Py_EnterRecursiveCall. */
static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
- return (++tstate->recursion_depth > _Py_CheckRecursionLimit
+ return (++tstate->recursion_depth > tstate->interp->ceval.recursion_limit
|| ++tstate->stackcheck_counter > 64);
}
#else
static inline int _Py_MakeRecCheck(PyThreadState *tstate) {
- return (++tstate->recursion_depth > _Py_CheckRecursionLimit);
+ return (++tstate->recursion_depth > tstate->interp->ceval.recursion_limit);
}
#endif
@@ -90,20 +90,22 @@ static inline int _Py_EnterRecursiveCall_inline(const char *where) {
#define Py_EnterRecursiveCall(where) _Py_EnterRecursiveCall_inline(where)
-
/* Compute the "lower-water mark" for a recursion limit. When
* Py_LeaveRecursiveCall() is called with a recursion depth below this mark,
* the overflowed flag is reset to 0. */
-#define _Py_RecursionLimitLowerWaterMark(limit) \
- (((limit) > 200) \
- ? ((limit) - 50) \
- : (3 * ((limit) >> 2)))
-
-#define _Py_MakeEndRecCheck(x) \
- (--(x) < _Py_RecursionLimitLowerWaterMark(_Py_CheckRecursionLimit))
+static inline int _Py_RecursionLimitLowerWaterMark(int limit) {
+ if (limit > 200) {
+ return (limit - 50);
+ }
+ else {
+ return (3 * (limit >> 2));
+ }
+}
static inline void _Py_LeaveRecursiveCall(PyThreadState *tstate) {
- if (_Py_MakeEndRecCheck(tstate->recursion_depth)) {
+ tstate->recursion_depth--;
+ int limit = tstate->interp->ceval.recursion_limit;
+ if (tstate->recursion_depth < _Py_RecursionLimitLowerWaterMark(limit)) {
tstate->overflowed = 0;
}
}
diff --git a/Include/internal/pycore_interp.h b/Include/internal/pycore_interp.h
index fafc72e..0829101 100644
--- a/Include/internal/pycore_interp.h
+++ b/Include/internal/pycore_interp.h
@@ -33,6 +33,7 @@ struct _pending_calls {
};
struct _ceval_state {
+ int recursion_limit;
/* Records whether tracing is on for any thread. Counts the number
of threads for which tstate->c_tracefunc is non-NULL, so if the
value is 0, we know we don't have to check this thread's
diff --git a/Include/internal/pycore_runtime.h b/Include/internal/pycore_runtime.h
index c597335..8ca1dfb 100644
--- a/Include/internal/pycore_runtime.h
+++ b/Include/internal/pycore_runtime.h
@@ -14,7 +14,6 @@ extern "C" {
/* ceval state */
struct _ceval_runtime_state {
- int recursion_limit;
struct _gil_runtime_state gil;
};