diff --git a/src/core/IronPython/Compiler/Ast/AstMethods.cs b/src/core/IronPython/Compiler/Ast/AstMethods.cs index 3c69393e2..a5fa305dc 100644 --- a/src/core/IronPython/Compiler/Ast/AstMethods.cs +++ b/src/core/IronPython/Compiler/Ast/AstMethods.cs @@ -76,6 +76,9 @@ internal static class AstMethods { public static readonly MethodInfo ForLoopDispose = GetMethod((Action>)PythonOps.ForLoopDispose); public static readonly MethodInfo GetClosureTupleFromContext = GetMethod((Func)PythonOps.GetClosureTupleFromContext); public static readonly MethodInfo PushFrame = GetMethod((Func>)PythonOps.PushFrame); +#if DEBUG + public static readonly MethodInfo PopFrame = GetMethod((Action>)PythonOps.PopFrame); +#endif public static readonly MethodInfo PushGeneratorFrame = GetMethod((Action)PythonOps.PushGeneratorFrame); public static readonly MethodInfo FormatString = GetMethod((Func)PythonOps.FormatString); public static readonly MethodInfo GeneratorCheckThrowableAndReturnSendValue = GetMethod((Func)PythonOps.GeneratorCheckThrowableAndReturnSendValue); diff --git a/src/core/IronPython/Compiler/Ast/Node.cs b/src/core/IronPython/Compiler/Ast/Node.cs index ee7b6dce0..da0a67b33 100644 --- a/src/core/IronPython/Compiler/Ast/Node.cs +++ b/src/core/IronPython/Compiler/Ast/Node.cs @@ -405,6 +405,32 @@ public override MSAst.Expression Reduce() { } #endif +#if DEBUG + // Debug builds check that the entry removed is the one pushed here. The check compares the code object pushed, since the + // function's __code__ can be reassigned while it runs. Release builds remove the top entry without checking. + var code = Ast.Variable(typeof(FunctionCode), "$frameCode"); + return Ast.Block( + new[] { code }, + AstUtils.Try( + Ast.Assign( + FunctionStackVariable, + Ast.Call( + AstMethods.PushFrame, + _localContext, + Ast.Assign(code, _codeObject) + ) + ), + Body + ).Finally( + Ast.Call( + AstMethods.PopFrame, + _localContext, + code, + FunctionStackVariable + ) + ) + ); +#else return AstUtils.Try( Ast.Assign( FunctionStackVariable, @@ -428,6 +454,7 @@ public override MSAst.Expression Reduce() { ) ) ); +#endif } public override Type Type => Body.Type; diff --git a/src/core/IronPython/Compiler/OnDiskScriptCode.cs b/src/core/IronPython/Compiler/OnDiskScriptCode.cs index 046574d30..7bb4cf95f 100644 --- a/src/core/IronPython/Compiler/OnDiskScriptCode.cs +++ b/src/core/IronPython/Compiler/OnDiskScriptCode.cs @@ -40,12 +40,12 @@ private static Ast.PythonAst MakeAstFromSourceUnit(SourceUnit sourceUnit) { public override object Run() { CodeContext ctx = CreateContext(); + var funcCode = EnsureFunctionCode(_target, false, true); + PushFrame(ctx, funcCode); try { - var funcCode = EnsureFunctionCode(_target, false, true); - PushFrame(ctx, funcCode); return _target(ctx, funcCode); } finally { - PopFrame(); + PopFrame(ctx, funcCode); } } diff --git a/src/core/IronPython/Compiler/PythonScriptCode.cs b/src/core/IronPython/Compiler/PythonScriptCode.cs index dfa64632d..5b2e71252 100644 --- a/src/core/IronPython/Compiler/PythonScriptCode.cs +++ b/src/core/IronPython/Compiler/PythonScriptCode.cs @@ -65,7 +65,7 @@ private object RunWorker(CodeContext ctx) { return target(ctx, _code); } finally { PythonOps.RestoreCurrentExceptionState(e); - PopFrame(); + PopFrame(ctx, _code); } } diff --git a/src/core/IronPython/Compiler/RunnableScriptCode.cs b/src/core/IronPython/Compiler/RunnableScriptCode.cs index 6865bfdc0..5a883e3f6 100644 --- a/src/core/IronPython/Compiler/RunnableScriptCode.cs +++ b/src/core/IronPython/Compiler/RunnableScriptCode.cs @@ -90,9 +90,10 @@ protected void PushFrame(CodeContext context, FunctionCode code) { } } - protected void PopFrame() { + protected void PopFrame(CodeContext context, FunctionCode code) { if (((PythonContext)SourceUnit.LanguageContext).PythonOptions.Frames) { List stack = PythonOps.GetFunctionStack(); + PythonOps.AssertOwnFrameOnTop(context, code, stack); stack.RemoveAt(stack.Count - 1); } } diff --git a/src/core/IronPython/Compiler/RuntimeScriptCode.cs b/src/core/IronPython/Compiler/RuntimeScriptCode.cs index 4a583c94c..781e219b1 100644 --- a/src/core/IronPython/Compiler/RuntimeScriptCode.cs +++ b/src/core/IronPython/Compiler/RuntimeScriptCode.cs @@ -64,7 +64,7 @@ private object InvokeTarget(Scope scope) { return _optimizedTarget(funcCode); } finally { PythonOps.RestoreCurrentExceptionState(e); - PopFrame(); + PopFrame(_optimizedContext, funcCode); } } diff --git a/src/core/IronPython/Modules/Builtin.cs b/src/core/IronPython/Modules/Builtin.cs index 89b8d68e3..b49cba90c 100644 --- a/src/core/IronPython/Modules/Builtin.cs +++ b/src/core/IronPython/Modules/Builtin.cs @@ -323,6 +323,7 @@ public static object eval(CodeContext/*!*/ context, [NotNone] FunctionCode code, try { return code.Call(evalContext); } finally { + PythonOps.AssertOwnFrameOnTop(evalContext, code, stack); stack.RemoveAt(stack.Count - 1); } } @@ -392,6 +393,7 @@ public static void exec(CodeContext/*!*/ context, [NotNone] FunctionCode code, P try { code.Call(execContext); } finally { + PythonOps.AssertOwnFrameOnTop(execContext, code, stack); stack.RemoveAt(stack.Count - 1); } } else { diff --git a/src/core/IronPython/Runtime/Operations/PythonOps.cs b/src/core/IronPython/Runtime/Operations/PythonOps.cs index efa2fc59f..454287223 100644 --- a/src/core/IronPython/Runtime/Operations/PythonOps.cs +++ b/src/core/IronPython/Runtime/Operations/PythonOps.cs @@ -4538,6 +4538,33 @@ public static List PushFrame(CodeContext/*!*/ context, FunctionCo return stack; } + /// + /// Removes the entry that pushed for code that can't suspend, after checking it in debug builds. + /// + /// + /// Generated code calls this only in debug builds. In release builds it removes the top entry itself, without checking. + /// + public static void PopFrame(CodeContext/*!*/ context, FunctionCode function, List stack) { + AssertOwnFrameOnTop(context, function, stack); + stack.RemoveAt(stack.Count - 1); + } + + /// + /// Asserts that the entry on top of is the one that , running in + /// , pushed and is about to remove. + /// + /// + /// Code that can't suspend pushes its entry and removes it on the same thread, so the entry is on top when it is removed, unless + /// other code has left an entry behind or removed one that wasn't its own. The assertion makes that fail where it happens, + /// instead of showing up later as wrong frames. Calls to this method are compiled only in debug builds. + /// + [Conditional("DEBUG")] + internal static void AssertOwnFrameOnTop(CodeContext/*!*/ context, FunctionCode function, List stack) { + // sys._getframe replaces entries by copies with frame objects, so compare the parts. + Debug.Assert(stack.Count > 0 && stack[stack.Count - 1].Code == function && stack[stack.Count - 1].Context == context, + "the entry of code that can't suspend should be on top of the frame stack when the code removes it"); + } + /// /// Pushes the frame of a generator body on the current thread, and hands it over to , which removes /// it at each suspension, adds it back at each resumption, and removes it when the body finishes. diff --git a/tests/suite/modules/system_related/test_sys.py b/tests/suite/modules/system_related/test_sys.py index b22bbe87f..5d8f9d1ea 100644 --- a/tests/suite/modules/system_related/test_sys.py +++ b/tests/suite/modules/system_related/test_sys.py @@ -246,35 +246,76 @@ def f(frame, event, arg): def test_settrace_jump(self): # A trace function can set f_lineno to any line of the code, in module code as well as in a function. # All the code is compiled under the same file name, since a jump must work in code compiled again under a file name too. - sources = [ - "\n\nout.append(3)\nout.append(4)\n\nout.append(6)\n", - "def g(out):\n\n out.append(3)\n out.append(4)\n\n out.append(6)\ng(out)\n", - ] + # The line jumped to gets no 'line' event. The code that jumped still reports its return, with the value returned or + # None when an exception leaves it, and leaves the frame stack as it found it. + module = "\n\nout.append(3)\nout.append(4)\n\nout.append(6)\n" + function = "def g(out):\n\n out.append(3)\n out.append(4)\n\n out.append(6)\n return 7\nout.append(g(out))\n" + raising = "def g(out):\n\n out.append(3)\n out.append(4)\n\n raise KeyError(6)\ng(out)\n" + M = '' cases = [ - (6, 4, [3, 4, 4, 6]), # backward - (3, 6, [6]), # forward + # backward + (module, 6, 4, [3, 4, 4, 6], [ + (M, 'call'), (M, 'line', 3), (M, 'line', 4), (M, 'line', 6), (M, 'line', 6), (M, 'return', 6, None), + ]), + (function, 6, 4, [3, 4, 4, 6, 7], [ + (M, 'call'), (M, 'line', 1), (M, 'line', 8), + ('g', 'call'), ('g', 'line', 3), ('g', 'line', 4), ('g', 'line', 6), ('g', 'line', 6), ('g', 'line', 7), ('g', 'return', 7, 7), + (M, 'return', 8, None), + ]), + (raising, 6, 4, [3, 4, 4, KeyError], [ + (M, 'call'), (M, 'line', 1), (M, 'line', 7), + ('g', 'call'), ('g', 'line', 3), ('g', 'line', 4), ('g', 'line', 6), ('g', 'line', 6), ('g', 'exception', 6, KeyError), ('g', 'return', 6, None), + (M, 'exception', 7, KeyError), (M, 'return', 7, None), + ]), + # forward + (module, 3, 6, [6], [ + (M, 'call'), (M, 'line', 3), (M, 'return', 6, None), + ]), + (function, 3, 6, [6, 7], [ + (M, 'call'), (M, 'line', 1), (M, 'line', 8), + ('g', 'call'), ('g', 'line', 3), ('g', 'line', 7), ('g', 'return', 7, 7), + (M, 'return', 8, None), + ]), + (raising, 3, 6, [KeyError], [ + (M, 'call'), (M, 'line', 1), (M, 'line', 7), + ('g', 'call'), ('g', 'line', 3), ('g', 'exception', 6, KeyError), ('g', 'return', 6, None), + (M, 'exception', 7, KeyError), (M, 'return', 7, None), + ]), ] orig_sys_trace_func = sys.gettrace() - for source in sources: - for jump_from, jump_to, expected in cases: - jumped = False - def f(frame, event, arg): - nonlocal jumped - if frame.f_code.co_filename == '' and event == 'line' and frame.f_lineno == jump_from and not jumped: + for source, jump_from, jump_to, expected_out, expected_events in cases: + jumped = False + events = [] + def f(frame, event, arg): + nonlocal jumped + if frame.f_code.co_filename == '': + # test_settrace_first_line checks the line of 'call' events, which differs between versions for module code + event_info = (frame.f_code.co_name, event) + if event != 'call': + event_info += (frame.f_lineno,) + if event in ('return', 'exception'): + event_info += (arg[0] if event == 'exception' else arg,) + events.append(event_info) + + if event == 'line' and frame.f_lineno == jump_from and not jumped: jumped = True frame.f_lineno = jump_to - return f + return f - out = [] - code = compile(source, '', 'exec') - try: - sys.settrace(f) - exec(code, {'out': out}) - finally: - sys.settrace(orig_sys_trace_func) + out = [] + code = compile(source, '', 'exec') + try: + sys.settrace(f) + exec(code, {'out': out}) + except KeyError: + out.append(KeyError) + finally: + sys.settrace(orig_sys_trace_func) - self.assertEqual(out, expected, (source, jump_from, jump_to)) + self.assertEqual(out, expected_out, (source, jump_from, jump_to)) + self.assertEqual(events, expected_events, (source, jump_from, jump_to)) + self.assertEqual(sys._getframe(0).f_code.co_name, 'test_settrace_jump', (source, jump_from, jump_to)) def test_cp30130(self): def f(frame, event, arg):