Skip to content

Instantly share code, notes, and snippets.

@akaptur
Created September 29, 2014 19:17
Show Gist options
  • Save akaptur/be988fc5cc3ff41f66c3 to your computer and use it in GitHub Desktop.
Save akaptur/be988fc5cc3ff41f66c3 to your computer and use it in GitHub Desktop.
full recursion limit checker
int
_Py_CheckRecursiveCall(char *where)
{
PyThreadState *tstate = PyThreadState_GET();
#ifdef USE_STACKCHECK
if (PyOS_CheckStack()) {
--tstate->recursion_depth;
PyErr_SetString(PyExc_MemoryError, "Stack overflow");
return -1;
}
#endif
_Py_CheckRecursionLimit = recursion_limit;
if (tstate->recursion_critical)
/* Somebody asked that we don't check for recursion. */
return 0;
if (tstate->overflowed) {
if (tstate->recursion_depth > recursion_limit + 50) {
/* Overflowing while handling an overflow. Give up. */
Py_FatalError("Cannot recover from stack overflow.");
}
return 0;
}
if (tstate->recursion_depth > recursion_limit) {
--tstate->recursion_depth;
tstate->overflowed = 1;
PyErr_Format(PyExc_RuntimeError,
"maximum recursion depth exceeded%s",
where);
return -1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment