Skip to content

Commit f00828a

Browse files
authored
bpo-36851: Clean the frame stack if the execution ends with a return and the stack is not empty (GH-13191)
1 parent 33e067d commit f00828a

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

Lib/test/test_code.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
import threading
131131
import unittest
132132
import weakref
133+
import opcode
133134
try:
134135
import ctypes
135136
except ImportError:
@@ -379,6 +380,43 @@ def run(self):
379380
tt.join()
380381
self.assertEqual(LAST_FREED, 500)
381382

383+
@cpython_only
384+
def test_clean_stack_on_return(self):
385+
386+
def f(x):
387+
return x
388+
389+
code = f.__code__
390+
ct = type(f.__code__)
391+
392+
# Insert an extra LOAD_FAST, this duplicates the value of
393+
# 'x' in the stack, leaking it if the frame is not properly
394+
# cleaned up upon exit.
395+
396+
bytecode = list(code.co_code)
397+
bytecode.insert(-2, opcode.opmap['LOAD_FAST'])
398+
bytecode.insert(-2, 0)
399+
400+
c = ct(code.co_argcount, code.co_posonlyargcount,
401+
code.co_kwonlyargcount, code.co_nlocals, code.co_stacksize+1,
402+
code.co_flags, bytes(bytecode),
403+
code.co_consts, code.co_names, code.co_varnames,
404+
code.co_filename, code.co_name, code.co_firstlineno,
405+
code.co_lnotab, code.co_freevars, code.co_cellvars)
406+
new_function = type(f)(c, f.__globals__, 'nf', f.__defaults__, f.__closure__)
407+
408+
class Var:
409+
pass
410+
the_object = Var()
411+
var = weakref.ref(the_object)
412+
413+
new_function(the_object)
414+
415+
# Check if the_object is leaked
416+
del the_object
417+
assert var() is None
418+
419+
382420
def test_main(verbose=None):
383421
from test import test_code
384422
run_doctest(test_code, verbose)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The ``FrameType`` stack is now correctly cleaned up if the execution ends
2+
with a return and the stack is not empty.

Python/ceval.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,7 +1755,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
17551755
case TARGET(RETURN_VALUE): {
17561756
retval = POP();
17571757
assert(f->f_iblock == 0);
1758-
goto return_or_yield;
1758+
goto exit_returning;
17591759
}
17601760

17611761
case TARGET(GET_AITER): {
@@ -1924,7 +1924,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
19241924
/* and repeat... */
19251925
assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
19261926
f->f_lasti -= sizeof(_Py_CODEUNIT);
1927-
goto return_or_yield;
1927+
goto exit_yielding;
19281928
}
19291929

19301930
case TARGET(YIELD_VALUE): {
@@ -1941,7 +1941,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
19411941
}
19421942

19431943
f->f_stacktop = stack_pointer;
1944-
goto return_or_yield;
1944+
goto exit_yielding;
19451945
}
19461946

19471947
case TARGET(POP_EXCEPT): {
@@ -3581,16 +3581,18 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
35813581
break;
35823582
} /* main loop */
35833583

3584+
assert(retval == NULL);
3585+
assert(PyErr_Occurred());
3586+
3587+
exit_returning:
3588+
35843589
/* Pop remaining stack entries. */
35853590
while (!EMPTY()) {
35863591
PyObject *o = POP();
35873592
Py_XDECREF(o);
35883593
}
35893594

3590-
assert(retval == NULL);
3591-
assert(PyErr_Occurred());
3592-
3593-
return_or_yield:
3595+
exit_yielding:
35943596
if (tstate->use_tracing) {
35953597
if (tstate->c_tracefunc) {
35963598
if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,

0 commit comments

Comments
 (0)