Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/core/IronPython/Compiler/Ast/AstMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ internal static class AstMethods {
public static readonly MethodInfo ForLoopDispose = GetMethod((Action<KeyValuePair<IEnumerator, IDisposable>>)PythonOps.ForLoopDispose);
public static readonly MethodInfo GetClosureTupleFromContext = GetMethod((Func<CodeContext, MutableTuple>)PythonOps.GetClosureTupleFromContext);
public static readonly MethodInfo PushFrame = GetMethod((Func<CodeContext, FunctionCode, List<FunctionStack>>)PythonOps.PushFrame);
#if DEBUG
public static readonly MethodInfo PopFrame = GetMethod((Action<CodeContext, FunctionCode, List<FunctionStack>>)PythonOps.PopFrame);
#endif
public static readonly MethodInfo PushGeneratorFrame = GetMethod((Action<CodeContext, PythonGenerator, FunctionCode>)PythonOps.PushGeneratorFrame);
public static readonly MethodInfo FormatString = GetMethod((Func<CodeContext, string, object, string>)PythonOps.FormatString);
public static readonly MethodInfo GeneratorCheckThrowableAndReturnSendValue = GetMethod((Func<object, object>)PythonOps.GeneratorCheckThrowableAndReturnSendValue);
Expand Down
27 changes: 27 additions & 0 deletions src/core/IronPython/Compiler/Ast/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -428,6 +454,7 @@ public override MSAst.Expression Reduce() {
)
)
);
#endif
}

public override Type Type => Body.Type;
Expand Down
6 changes: 3 additions & 3 deletions src/core/IronPython/Compiler/OnDiskScriptCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/IronPython/Compiler/PythonScriptCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private object RunWorker(CodeContext ctx) {
return target(ctx, _code);
} finally {
PythonOps.RestoreCurrentExceptionState(e);
PopFrame();
PopFrame(ctx, _code);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/core/IronPython/Compiler/RunnableScriptCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FunctionStack> stack = PythonOps.GetFunctionStack();
PythonOps.AssertOwnFrameOnTop(context, code, stack);
stack.RemoveAt(stack.Count - 1);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/IronPython/Compiler/RuntimeScriptCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private object InvokeTarget(Scope scope) {
return _optimizedTarget(funcCode);
} finally {
PythonOps.RestoreCurrentExceptionState(e);
PopFrame();
PopFrame(_optimizedContext, funcCode);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/core/IronPython/Modules/Builtin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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 {
Expand Down
27 changes: 27 additions & 0 deletions src/core/IronPython/Runtime/Operations/PythonOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4538,6 +4538,33 @@ public static List<FunctionStack> PushFrame(CodeContext/*!*/ context, FunctionCo
return stack;
}

/// <summary>
/// Removes the entry that <see cref="PushFrame"/> pushed for code that can't suspend, after checking it in debug builds.
/// </summary>
/// <remarks>
/// Generated code calls this only in debug builds. In release builds it removes the top entry itself, without checking.
/// </remarks>
public static void PopFrame(CodeContext/*!*/ context, FunctionCode function, List<FunctionStack> stack) {
AssertOwnFrameOnTop(context, function, stack);
stack.RemoveAt(stack.Count - 1);
}

/// <summary>
/// Asserts that the entry on top of <paramref name="stack"/> is the one that <paramref name="function"/>, running in
/// <paramref name="context"/>, pushed and is about to remove.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
[Conditional("DEBUG")]
internal static void AssertOwnFrameOnTop(CodeContext/*!*/ context, FunctionCode function, List<FunctionStack> 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");
}

/// <summary>
/// Pushes the frame of a generator body on the current thread, and hands it over to <paramref name="generator"/>, which removes
/// it at each suspension, adds it back at each resumption, and removes it when the body finishes.
Expand Down
83 changes: 62 additions & 21 deletions tests/suite/modules/system_related/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<module>'
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 == '<traced>' 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 == '<traced>':
# 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, '<traced>', 'exec')
try:
sys.settrace(f)
exec(code, {'out': out})
finally:
sys.settrace(orig_sys_trace_func)
out = []
code = compile(source, '<traced>', '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):
Expand Down
Loading