@@ -510,6 +510,29 @@ Py_SetRecursionLimit(int new_limit)
510510 recursion_limit = new_limit ;
511511}
512512
513+ int
514+ _Py_CheckRecursiveCall (char * where )
515+ {
516+ PyThreadState * tstate = PyThreadState_GET ();
517+
518+ #ifdef USE_STACKCHECK
519+ if (PyOS_CheckStack ()) {
520+ -- tstate -> recursion_depth ;
521+ PyErr_SetString (PyExc_MemoryError , "Stack overflow" );
522+ return -1 ;
523+ }
524+ #endif
525+ if (tstate -> recursion_depth > recursion_limit ) {
526+ -- tstate -> recursion_depth ;
527+ PyErr_Format (PyExc_RuntimeError ,
528+ "maximum recursion depth exceeded%s" ,
529+ where );
530+ return -1 ;
531+ }
532+ return 0 ;
533+ }
534+
535+
513536/* Status code for main loop (reason for stack unwind) */
514537
515538enum why_code {
@@ -674,21 +697,9 @@ eval_frame(PyFrameObject *f)
674697 if (f == NULL )
675698 return NULL ;
676699
677- #ifdef USE_STACKCHECK
678- if (tstate -> recursion_depth %10 == 0 && PyOS_CheckStack ()) {
679- PyErr_SetString (PyExc_MemoryError , "Stack overflow" );
680- return NULL ;
681- }
682- #endif
683-
684700 /* push frame */
685- if (++ tstate -> recursion_depth > recursion_limit ) {
686- -- tstate -> recursion_depth ;
687- PyErr_SetString (PyExc_RuntimeError ,
688- "maximum recursion depth exceeded" );
689- tstate -> frame = f -> f_back ;
701+ if (Py_EnterRecursiveCall ("" ))
690702 return NULL ;
691- }
692703
693704 tstate -> frame = f ;
694705
@@ -710,9 +721,7 @@ eval_frame(PyFrameObject *f)
710721 if (call_trace (tstate -> c_tracefunc , tstate -> c_traceobj ,
711722 f , PyTrace_CALL , Py_None )) {
712723 /* Trace function raised an error */
713- -- tstate -> recursion_depth ;
714- tstate -> frame = f -> f_back ;
715- return NULL ;
724+ goto exit_eval_frame ;
716725 }
717726 }
718727 if (tstate -> c_profilefunc != NULL ) {
@@ -722,9 +731,7 @@ eval_frame(PyFrameObject *f)
722731 tstate -> c_profileobj ,
723732 f , PyTrace_CALL , Py_None )) {
724733 /* Profile function raised an error */
725- -- tstate -> recursion_depth ;
726- tstate -> frame = f -> f_back ;
727- return NULL ;
734+ goto exit_eval_frame ;
728735 }
729736 }
730737 }
@@ -2428,7 +2435,8 @@ eval_frame(PyFrameObject *f)
24282435 reset_exc_info (tstate );
24292436
24302437 /* pop frame */
2431- -- tstate -> recursion_depth ;
2438+ exit_eval_frame :
2439+ Py_LeaveRecursiveCall ();
24322440 tstate -> frame = f -> f_back ;
24332441
24342442 return retval ;
0 commit comments