Skip to content

Commit e7c9f4a

Browse files
authored
Cleanup exit code for interpreter. (GH-17756)
1 parent 97f1267 commit e7c9f4a

File tree

2 files changed

+5
-42
lines changed

2 files changed

+5
-42
lines changed

Lib/test/test_code.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -434,42 +434,6 @@ def run(self):
434434
tt.join()
435435
self.assertEqual(LAST_FREED, 500)
436436

437-
@cpython_only
438-
def test_clean_stack_on_return(self):
439-
440-
def f(x):
441-
return x
442-
443-
code = f.__code__
444-
ct = type(f.__code__)
445-
446-
# Insert an extra LOAD_FAST, this duplicates the value of
447-
# 'x' in the stack, leaking it if the frame is not properly
448-
# cleaned up upon exit.
449-
450-
bytecode = list(code.co_code)
451-
bytecode.insert(-2, opcode.opmap['LOAD_FAST'])
452-
bytecode.insert(-2, 0)
453-
454-
c = ct(code.co_argcount, code.co_posonlyargcount,
455-
code.co_kwonlyargcount, code.co_nlocals, code.co_stacksize+1,
456-
code.co_flags, bytes(bytecode),
457-
code.co_consts, code.co_names, code.co_varnames,
458-
code.co_filename, code.co_name, code.co_firstlineno,
459-
code.co_lnotab, code.co_freevars, code.co_cellvars)
460-
new_function = type(f)(c, f.__globals__, 'nf', f.__defaults__, f.__closure__)
461-
462-
class Var:
463-
pass
464-
the_object = Var()
465-
var = weakref.ref(the_object)
466-
467-
new_function(the_object)
468-
469-
# Check if the_object is leaked
470-
del the_object
471-
assert var() is None
472-
473437

474438
def test_main(verbose=None):
475439
from test import test_code

Python/ceval.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,7 +1913,8 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
19131913
case TARGET(RETURN_VALUE): {
19141914
retval = POP();
19151915
assert(f->f_iblock == 0);
1916-
goto exit_returning;
1916+
assert(EMPTY());
1917+
goto exiting;
19171918
}
19181919

19191920
case TARGET(GET_AITER): {
@@ -2083,7 +2084,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
20832084
/* and repeat... */
20842085
assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
20852086
f->f_lasti -= sizeof(_Py_CODEUNIT);
2086-
goto exit_yielding;
2087+
goto exiting;
20872088
}
20882089

20892090
case TARGET(YIELD_VALUE): {
@@ -2100,7 +2101,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
21002101
}
21012102

21022103
f->f_stacktop = stack_pointer;
2103-
goto exit_yielding;
2104+
goto exiting;
21042105
}
21052106

21062107
case TARGET(POP_EXCEPT): {
@@ -3632,15 +3633,13 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
36323633
assert(retval == NULL);
36333634
assert(_PyErr_Occurred(tstate));
36343635

3635-
exit_returning:
3636-
36373636
/* Pop remaining stack entries. */
36383637
while (!EMPTY()) {
36393638
PyObject *o = POP();
36403639
Py_XDECREF(o);
36413640
}
36423641

3643-
exit_yielding:
3642+
exiting:
36443643
if (tstate->use_tracing) {
36453644
if (tstate->c_tracefunc) {
36463645
if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,

0 commit comments

Comments
 (0)