diff options
author | Mark Shannon <mark@hotpy.org> | 2021-11-16 11:01:57 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-16 11:01:57 (GMT) |
commit | b9310773756f40f77e075f221a90dd41e6964efc (patch) | |
tree | 43674ba9f42f2ff81dfd3025956b0ce8e12251f9 /Python/sysmodule.c | |
parent | 9bf2cbc4c498812e14f20d86acb61c53928a5a57 (diff) | |
download | cpython-b9310773756f40f77e075f221a90dd41e6964efc.zip cpython-b9310773756f40f77e075f221a90dd41e6964efc.tar.gz cpython-b9310773756f40f77e075f221a90dd41e6964efc.tar.bz2 |
bpo-45753: Make recursion checks more efficient. (GH-29524)
* Uses recursion remaining, instead of recursion depth to speed up check against recursion limit.
Diffstat (limited to 'Python/sysmodule.c')
-rw-r--r-- | Python/sysmodule.c | 16 |
1 files changed, 5 insertions, 11 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 27937a0..3e2091e 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -1187,20 +1187,14 @@ sys_setrecursionlimit_impl(PyObject *module, int new_limit) return NULL; } - /* Issue #25274: When the recursion depth hits the recursion limit in - _Py_CheckRecursiveCall(), the overflowed flag of the thread state is - set to 1 and a RecursionError is raised. The overflowed flag is reset - to 0 when the recursion depth goes below the low-water mark: see - Py_LeaveRecursiveCall(). - - Reject too low new limit if the current recursion depth is higher than - the new low-water mark. Otherwise it may not be possible anymore to - reset the overflowed flag to 0. */ - if (tstate->recursion_depth >= new_limit) { + /* Reject too low new limit if the current recursion depth is higher than + the new low-water mark. */ + int depth = tstate->recursion_limit - tstate->recursion_remaining; + if (depth >= new_limit) { _PyErr_Format(tstate, PyExc_RecursionError, "cannot set the recursion limit to %i at " "the recursion depth %i: the limit is too low", - new_limit, tstate->recursion_depth); + new_limit, depth); return NULL; } |