mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 23:21:29 +00:00 
			
		
		
		
	bpo-36851: Clean the frame stack if the execution ends with a return and the stack is not empty (GH-13191)
This commit is contained in:
		
							parent
							
								
									33e067d6a2
								
							
						
					
					
						commit
						f00828a742
					
				
					 3 changed files with 49 additions and 7 deletions
				
			
		| 
						 | 
					@ -130,6 +130,7 @@
 | 
				
			||||||
import threading
 | 
					import threading
 | 
				
			||||||
import unittest
 | 
					import unittest
 | 
				
			||||||
import weakref
 | 
					import weakref
 | 
				
			||||||
 | 
					import opcode
 | 
				
			||||||
try:
 | 
					try:
 | 
				
			||||||
    import ctypes
 | 
					    import ctypes
 | 
				
			||||||
except ImportError:
 | 
					except ImportError:
 | 
				
			||||||
| 
						 | 
					@ -379,6 +380,43 @@ def run(self):
 | 
				
			||||||
            tt.join()
 | 
					            tt.join()
 | 
				
			||||||
            self.assertEqual(LAST_FREED, 500)
 | 
					            self.assertEqual(LAST_FREED, 500)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        @cpython_only
 | 
				
			||||||
 | 
					        def test_clean_stack_on_return(self):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            def f(x):
 | 
				
			||||||
 | 
					                return x
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            code = f.__code__
 | 
				
			||||||
 | 
					            ct = type(f.__code__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            # Insert an extra LOAD_FAST, this duplicates the value of
 | 
				
			||||||
 | 
					            # 'x' in the stack, leaking it if the frame is not properly
 | 
				
			||||||
 | 
					            # cleaned up upon exit.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            bytecode = list(code.co_code)
 | 
				
			||||||
 | 
					            bytecode.insert(-2, opcode.opmap['LOAD_FAST'])
 | 
				
			||||||
 | 
					            bytecode.insert(-2, 0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            c = ct(code.co_argcount, code.co_posonlyargcount,
 | 
				
			||||||
 | 
					                   code.co_kwonlyargcount, code.co_nlocals, code.co_stacksize+1,
 | 
				
			||||||
 | 
					                   code.co_flags, bytes(bytecode),
 | 
				
			||||||
 | 
					                   code.co_consts, code.co_names, code.co_varnames,
 | 
				
			||||||
 | 
					                   code.co_filename, code.co_name, code.co_firstlineno,
 | 
				
			||||||
 | 
					                   code.co_lnotab, code.co_freevars, code.co_cellvars)
 | 
				
			||||||
 | 
					            new_function = type(f)(c, f.__globals__, 'nf', f.__defaults__, f.__closure__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            class Var:
 | 
				
			||||||
 | 
					                pass
 | 
				
			||||||
 | 
					            the_object = Var()
 | 
				
			||||||
 | 
					            var = weakref.ref(the_object)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            new_function(the_object)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            # Check if the_object is leaked
 | 
				
			||||||
 | 
					            del the_object
 | 
				
			||||||
 | 
					            assert var() is None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_main(verbose=None):
 | 
					def test_main(verbose=None):
 | 
				
			||||||
    from test import test_code
 | 
					    from test import test_code
 | 
				
			||||||
    run_doctest(test_code, verbose)
 | 
					    run_doctest(test_code, verbose)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,2 @@
 | 
				
			||||||
 | 
					The ``FrameType`` stack is now correctly cleaned up if the execution ends
 | 
				
			||||||
 | 
					with a return and the stack is not empty.
 | 
				
			||||||
| 
						 | 
					@ -1755,7 +1755,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
 | 
				
			||||||
        case TARGET(RETURN_VALUE): {
 | 
					        case TARGET(RETURN_VALUE): {
 | 
				
			||||||
            retval = POP();
 | 
					            retval = POP();
 | 
				
			||||||
            assert(f->f_iblock == 0);
 | 
					            assert(f->f_iblock == 0);
 | 
				
			||||||
            goto return_or_yield;
 | 
					            goto exit_returning;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        case TARGET(GET_AITER): {
 | 
					        case TARGET(GET_AITER): {
 | 
				
			||||||
| 
						 | 
					@ -1924,7 +1924,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
 | 
				
			||||||
            /* and repeat... */
 | 
					            /* and repeat... */
 | 
				
			||||||
            assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
 | 
					            assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
 | 
				
			||||||
            f->f_lasti -= sizeof(_Py_CODEUNIT);
 | 
					            f->f_lasti -= sizeof(_Py_CODEUNIT);
 | 
				
			||||||
            goto return_or_yield;
 | 
					            goto exit_yielding;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        case TARGET(YIELD_VALUE): {
 | 
					        case TARGET(YIELD_VALUE): {
 | 
				
			||||||
| 
						 | 
					@ -1941,7 +1941,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            f->f_stacktop = stack_pointer;
 | 
					            f->f_stacktop = stack_pointer;
 | 
				
			||||||
            goto return_or_yield;
 | 
					            goto exit_yielding;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        case TARGET(POP_EXCEPT): {
 | 
					        case TARGET(POP_EXCEPT): {
 | 
				
			||||||
| 
						 | 
					@ -3581,16 +3581,18 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
    } /* main loop */
 | 
					    } /* main loop */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    assert(retval == NULL);
 | 
				
			||||||
 | 
					    assert(PyErr_Occurred());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					exit_returning:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* Pop remaining stack entries. */
 | 
					    /* Pop remaining stack entries. */
 | 
				
			||||||
    while (!EMPTY()) {
 | 
					    while (!EMPTY()) {
 | 
				
			||||||
        PyObject *o = POP();
 | 
					        PyObject *o = POP();
 | 
				
			||||||
        Py_XDECREF(o);
 | 
					        Py_XDECREF(o);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    assert(retval == NULL);
 | 
					exit_yielding:
 | 
				
			||||||
    assert(PyErr_Occurred());
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
return_or_yield:
 | 
					 | 
				
			||||||
    if (tstate->use_tracing) {
 | 
					    if (tstate->use_tracing) {
 | 
				
			||||||
        if (tstate->c_tracefunc) {
 | 
					        if (tstate->c_tracefunc) {
 | 
				
			||||||
            if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
 | 
					            if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue