@@ -193,18 +193,12 @@ extern void _PyEval_DeactivateOpCache(void);
193193
194194/* --- _Py_EnterRecursiveCall() ----------------------------------------- */
195195
196- #ifdef USE_STACKCHECK
197- /* With USE_STACKCHECK macro defined, trigger stack checks in
198- _Py_CheckRecursiveCall() on every 64th call to _Py_EnterRecursiveCall. */
199196static inline int _Py_MakeRecCheck (PyThreadState * tstate ) {
200- return (tstate -> c_recursion_remaining -- < 0
201- || (tstate -> c_recursion_remaining & 63 ) == 0 );
197+ char here ;
198+ uintptr_t here_addr = (uintptr_t )& here ;
199+ _PyThreadStateImpl * _tstate = (_PyThreadStateImpl * )tstate ;
200+ return here_addr < _tstate -> c_stack_soft_limit ;
202201}
203- #else
204- static inline int _Py_MakeRecCheck (PyThreadState * tstate ) {
205- return tstate -> c_recursion_remaining -- < 0 ;
206- }
207- #endif
208202
209203// Export for '_json' shared extension, used via _Py_EnterRecursiveCall()
210204// static inline function.
@@ -220,23 +214,31 @@ static inline int _Py_EnterRecursiveCallTstate(PyThreadState *tstate,
220214 return (_Py_MakeRecCheck (tstate ) && _Py_CheckRecursiveCall (tstate , where ));
221215}
222216
223- static inline void _Py_EnterRecursiveCallTstateUnchecked (PyThreadState * tstate ) {
224- assert (tstate -> c_recursion_remaining > 0 );
225- tstate -> c_recursion_remaining -- ;
226- }
227-
228217static inline int _Py_EnterRecursiveCall (const char * where ) {
229218 PyThreadState * tstate = _PyThreadState_GET ();
230219 return _Py_EnterRecursiveCallTstate (tstate , where );
231220}
232221
233- static inline void _Py_LeaveRecursiveCallTstate (PyThreadState * tstate ) {
234- tstate -> c_recursion_remaining ++ ;
222+ static inline void _Py_LeaveRecursiveCallTstate (PyThreadState * tstate ) {
223+ (void )tstate ;
224+ }
225+
226+ PyAPI_FUNC (void ) _Py_InitializeRecursionLimits (PyThreadState * tstate );
227+
228+ static inline int _Py_ReachedRecursionLimit (PyThreadState * tstate ) {
229+ char here ;
230+ uintptr_t here_addr = (uintptr_t )& here ;
231+ _PyThreadStateImpl * _tstate = (_PyThreadStateImpl * )tstate ;
232+ if (here_addr > _tstate -> c_stack_soft_limit ) {
233+ return 0 ;
234+ }
235+ if (_tstate -> c_stack_hard_limit == 0 ) {
236+ _Py_InitializeRecursionLimits (tstate );
237+ }
238+ return here_addr <= _tstate -> c_stack_soft_limit ;
235239}
236240
237241static inline void _Py_LeaveRecursiveCall (void ) {
238- PyThreadState * tstate = _PyThreadState_GET ();
239- _Py_LeaveRecursiveCallTstate (tstate );
240242}
241243
242244extern struct _PyInterpreterFrame * _PyEval_GetFrame (void );
@@ -327,7 +329,6 @@ void _Py_unset_eval_breaker_bit_all(PyInterpreterState *interp, uintptr_t bit);
327329
328330PyAPI_FUNC (PyObject * ) _PyFloat_FromDouble_ConsumeInputs (_PyStackRef left , _PyStackRef right , double value );
329331
330-
331332#ifdef __cplusplus
332333}
333334#endif
0 commit comments