summaryrefslogtreecommitdiff
path: root/Python/sysmodule.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-10-13 00:11:21 +0200
committerVictor Stinner <victor.stinner@gmail.com>2015-10-13 00:11:21 +0200
commit74bc8ad30176e4759485b13c3ca09b007ff82576 (patch)
treee1ae5ad4f84cc8d0720be0f4d6c831ce6178dd77 /Python/sysmodule.c
parent406f9934c2e498c6ecdd3175fef32e333dabb1dc (diff)
downloadcpython-74bc8ad30176e4759485b13c3ca09b007ff82576.tar.gz
sys.setrecursionlimit() now raises RecursionError
Issue #25274: sys.setrecursionlimit() now raises a RecursionError if the new recursion limit is too low depending at the current recursion depth. Modify also the "lower-water mark" formula to make it monotonic. This mark is used to decide when the overflowed flag of the thread state is reset.
Diffstat (limited to 'Python/sysmodule.c')
-rw-r--r--Python/sysmodule.c29
1 files changed, 26 insertions, 3 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index f600baf10e..334f5d0c8e 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -632,14 +632,37 @@ processor's time-stamp counter."
static PyObject *
sys_setrecursionlimit(PyObject *self, PyObject *args)
{
- int new_limit;
+ int new_limit, mark;
+ PyThreadState *tstate;
+
if (!PyArg_ParseTuple(args, "i:setrecursionlimit", &new_limit))
return NULL;
- if (new_limit <= 0) {
+
+ if (new_limit < 1) {
PyErr_SetString(PyExc_ValueError,
- "recursion limit must be positive");
+ "recursion limit must be greater or equal than 1");
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. */
+ mark = _Py_RecursionLimitLowerWaterMark(new_limit);
+ tstate = PyThreadState_GET();
+ if (tstate->recursion_depth >= mark) {
+ PyErr_Format(PyExc_RecursionError,
+ "cannot set the recursion limit to %i at "
+ "the recursion depth %i: the limit is too low",
+ new_limit, tstate->recursion_depth);
+ return NULL;
+ }
+
Py_SetRecursionLimit(new_limit);
Py_INCREF(Py_None);
return Py_None;