@@ -1350,8 +1350,12 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
1350
1350
next_instr += f -> f_lasti / sizeof (_Py_CODEUNIT ) + 1 ;
1351
1351
}
1352
1352
stack_pointer = f -> f_valuestack + f -> f_stackdepth ;
1353
- /* To help with debugging, set f->f_stackdepth to -1.
1354
- * Update when returning or calling trace function */
1353
+ /* Set f->f_stackdepth to -1.
1354
+ * Update when returning or calling trace function.
1355
+ Having f_stackdepth <= 0 ensures that invalid
1356
+ values are not visible to the cycle GC.
1357
+ We choose -1 rather than 0 to assist debugging.
1358
+ */
1355
1359
f -> f_stackdepth = -1 ;
1356
1360
f -> f_state = FRAME_EXECUTING ;
1357
1361
@@ -1450,6 +1454,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
1450
1454
/* Reload possibly changed frame fields */
1451
1455
JUMPTO (f -> f_lasti );
1452
1456
stack_pointer = f -> f_valuestack + f -> f_stackdepth ;
1457
+ f -> f_stackdepth = -1 ;
1453
1458
if (err )
1454
1459
/* trace function raised an exception */
1455
1460
goto error ;
@@ -2075,6 +2080,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
2075
2080
assert (f -> f_iblock == 0 );
2076
2081
assert (EMPTY ());
2077
2082
f -> f_state = FRAME_RETURNED ;
2083
+ f -> f_stackdepth = 0 ;
2078
2084
goto exiting ;
2079
2085
}
2080
2086
@@ -2245,6 +2251,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
2245
2251
assert (f -> f_lasti >= (int )sizeof (_Py_CODEUNIT ));
2246
2252
f -> f_lasti -= sizeof (_Py_CODEUNIT );
2247
2253
f -> f_state = FRAME_SUSPENDED ;
2254
+ f -> f_stackdepth = stack_pointer - f -> f_valuestack ;
2248
2255
goto exiting ;
2249
2256
}
2250
2257
@@ -2261,6 +2268,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
2261
2268
retval = w ;
2262
2269
}
2263
2270
f -> f_state = FRAME_SUSPENDED ;
2271
+ f -> f_stackdepth = stack_pointer - f -> f_valuestack ;
2264
2272
goto exiting ;
2265
2273
}
2266
2274
@@ -3843,10 +3851,9 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
3843
3851
PyObject * o = POP ();
3844
3852
Py_XDECREF (o );
3845
3853
}
3846
-
3854
+ f -> f_stackdepth = 0 ;
3847
3855
f -> f_state = FRAME_RAISED ;
3848
3856
exiting :
3849
- f -> f_stackdepth = stack_pointer - f -> f_valuestack ;
3850
3857
if (tstate -> use_tracing ) {
3851
3858
if (tstate -> c_tracefunc ) {
3852
3859
if (call_trace_protected (tstate -> c_tracefunc , tstate -> c_traceobj ,
0 commit comments