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
5 changes: 2 additions & 3 deletions src/core/IronPython/Compiler/Ast/AstMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ internal static class AstMethods {
public static readonly MethodInfo ImportWithNames = GetMethod((Func<CodeContext, string, string[], int, object>)PythonOps.ImportWithNames);
public static readonly MethodInfo ImportFrom = GetMethod((Func<CodeContext, object, string, object>)PythonOps.ImportFrom);
public static readonly MethodInfo ImportStar = GetMethod((Action<CodeContext, string, int>)PythonOps.ImportStar);
public static readonly MethodInfo SaveCurrentException = GetMethod((Func<Exception>)PythonOps.SaveCurrentException);
public static readonly MethodInfo RestoreCurrentException = GetMethod((Action<Exception>)PythonOps.RestoreCurrentException);
public static readonly MethodInfo SaveCurrentExceptionState = GetMethod((Func<object>)PythonOps.SaveCurrentExceptionState);
public static readonly MethodInfo RestoreCurrentExceptionState = GetMethod((Action<object>)PythonOps.RestoreCurrentExceptionState);
public static readonly MethodInfo MakeGeneratorExpression = GetMethod((Func<object, object, object>)PythonOps.MakeGeneratorExpression);
public static readonly MethodInfo ListAddForComprehension = GetMethod((Action<PythonList, object>)PythonOps.ListAddForComprehension);
public static readonly MethodInfo SetAddForComprehension = GetMethod((Action<SetCollection, object>)PythonOps.SetAddForComprehension);
Expand All @@ -37,7 +37,6 @@ internal static class AstMethods {
public static readonly MethodInfo RemoveModule = GetMethod((Action<CodeContext, string, object>)PythonOps.RemoveModule);
public static readonly MethodInfo ModuleStarted = GetMethod((Action<CodeContext, ModuleOptions>)PythonOps.ModuleStarted);
public static readonly MethodInfo MakeRethrownException = GetMethod((Func<CodeContext, Exception>)PythonOps.MakeRethrownException);
public static readonly MethodInfo MakeRethrowExceptionWorker = GetMethod((Func<Exception, Exception>)PythonOps.MakeRethrowExceptionWorker);
public static readonly MethodInfo MakeException = GetMethod((Func<CodeContext, object, Exception>)PythonOps.MakeException);
public static readonly MethodInfo MakeExceptionWithCause = GetMethod((Func<CodeContext, object, object, Exception>)PythonOps.MakeExceptionWithCause);
public static readonly MethodInfo MakeSlice = GetMethod((Func<object, object, object, Slice>)PythonOps.MakeSlice);
Expand Down
31 changes: 15 additions & 16 deletions src/core/IronPython/Compiler/Ast/TryStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public override MSAst.Expression Reduce() {
MSAst.ParameterExpression? exception;

if (_handlers.Length > 0) {
previousExceptionContext = Ast.Variable(typeof(Exception), "$previousException");
previousExceptionContext = Ast.Variable(typeof(object), "$previousException");
exception = Ast.Variable(typeof(Exception), "$exception");
@catch = TransformHandlers(exception, previousExceptionContext);
} else if (Finally != null) {
Expand Down Expand Up @@ -117,7 +117,7 @@ public override MSAst.Expression Reduce() {
LightExceptions.RewriteExternal(
AstUtils.Try(
Parent.AddDebugInfo(AstUtils.Empty(), new SourceSpan(Span.Start, GlobalParent.IndexToLocation(HeaderIndex))),
Ast.Assign(previousExceptionContext, Ast.Call(AstMethods.SaveCurrentException)),
Ast.Assign(previousExceptionContext, Ast.Call(AstMethods.SaveCurrentExceptionState)),
body,
AstUtils.Constant(null)
).Catch(exception,
Expand Down Expand Up @@ -151,7 +151,7 @@ public override MSAst.Expression Reduce() {
GlobalParent.AddDebugInfo(AstUtils.Empty(), new SourceSpan(Span.Start, GlobalParent.IndexToLocation(HeaderIndex))),
// save existing line updated
PushLineUpdated(false, lineUpdated!),
Ast.Assign(previousExceptionContext, Ast.Call(AstMethods.SaveCurrentException)),
Ast.Assign(previousExceptionContext, Ast.Call(AstMethods.SaveCurrentExceptionState)),
body,
AstUtils.Constant(null)
).Catch(exception,
Expand Down Expand Up @@ -212,6 +212,8 @@ private MSAst.Expression AddFinally(MSAst.Expression/*!*/ body) {
).Catch(
locException,
Expression.Block(
// save the line # that was last executing during the try
Parent.GetSaveLineNumberExpression(locException, false),
// If there was no except block, or the except block threw, then the
// exception has not yet been properly set, so we need to set the
// currently handled exception when we catch it
Expand All @@ -221,12 +223,6 @@ private MSAst.Expression AddFinally(MSAst.Expression/*!*/ body) {
)
)
).FinallyWithJumps(
// if we had an exception save the line # that was last executing during the try
AstUtils.If(
Expression.NotEqual(tryThrows, Expression.Default(typeof(Exception))),
Parent.GetSaveLineNumberExpression(tryThrows, false)
),

// clear the frames incase thae finally throws, and allow line number
// updates to proceed
UpdateLineUpdated(false),
Expand Down Expand Up @@ -322,13 +318,13 @@ private MSAst.Expression TransformHandlers(MSAst.ParameterExpression exception,
Ast.Block(
tsh.Target.TransformSet(SourceSpan.None, converted, PythonOperationKind.None),
GlobalParent.AddDebugInfo(
GetTracebackHeader(this, exception, tsh.Body),
tsh.Body,
new SourceSpan(GlobalParent.IndexToLocation(tsh.StartIndex), GlobalParent.IndexToLocation(tsh.HeaderIndex))
),
AstUtils.Empty()
),
Ast.Block(
Ast.Call(AstMethods.RestoreCurrentException, previousException),
Ast.Call(AstMethods.RestoreCurrentExceptionState, previousException),
tsh.Target.TransformSet(SourceSpan.None, AstUtils.Constant(null), PythonOperationKind.None),
tsh.Target.TransformDelete()
)
Expand All @@ -350,10 +346,10 @@ private MSAst.Expression TransformHandlers(MSAst.ParameterExpression exception,
),
Ast.TryFinally(
GlobalParent.AddDebugInfo(
GetTracebackHeader(this, exception, tsh.Body),
tsh.Body,
new SourceSpan(GlobalParent.IndexToLocation(tsh.StartIndex), GlobalParent.IndexToLocation(tsh.HeaderIndex))
),
Ast.Call(AstMethods.RestoreCurrentException, previousException)
Ast.Call(AstMethods.RestoreCurrentExceptionState, previousException)
)
);
}
Expand All @@ -376,10 +372,10 @@ private MSAst.Expression TransformHandlers(MSAst.ParameterExpression exception,
catchAll =
Ast.TryFinally(
GlobalParent.AddDebugInfo(
GetTracebackHeader(this, exception, tsh.Body),
tsh.Body,
new SourceSpan(GlobalParent.IndexToLocation(tsh.StartIndex), GlobalParent.IndexToLocation(tsh.HeaderIndex))
),
Ast.Call(AstMethods.RestoreCurrentException, previousException)
Ast.Call(AstMethods.RestoreCurrentExceptionState, previousException)
);
}
}
Expand All @@ -390,7 +386,7 @@ private MSAst.Expression TransformHandlers(MSAst.ParameterExpression exception,
// rethrow the exception if we have no catch-all block
if (catchAll == null) {
catchAll = Ast.Block(
Parent.GetSaveLineNumberExpression(exception, true),
UpdateLineUpdated(true),
Ast.Throw(
Ast.Call(
typeof(ExceptionHelpers).GetMethod(nameof(ExceptionHelpers.UpdateForRethrow))!,
Expand All @@ -417,12 +413,15 @@ private MSAst.Expression TransformHandlers(MSAst.ParameterExpression exception,
}

// Codegen becomes:
// < save the line number >
// extracted = PythonOps.SetCurrentException(exception)
// try:
// < dynamic exception analysis >
// extracted = None
return Ast.Block(
args,
// save the line number first so the frame is part of the handled exception's traceback
Parent.GetSaveLineNumberExpression(exception, false),
Ast.Assign(
extracted,
Ast.Call(AstMethods.SetCurrentException, Parent.LocalContext, exception)
Expand Down
16 changes: 8 additions & 8 deletions src/core/IronPython/Compiler/Ast/WithStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Statement Body {
/// <summary>
/// WithStatement is translated to the DLR AST equivalent to
/// the following Python code snippet (from with statement spec):
///
///
/// mgr = (EXPR)
/// exit = mgr.__exit__ # Not calling it yet
/// value = mgr.__enter__()
Expand All @@ -67,7 +67,7 @@ public Statement Body {
/// # The normal and non-local-goto cases are handled here
/// if exc:
/// exit(None, None, None)
///
///
/// </summary>
public override MSAst.Expression Reduce() {
// Five statements in the result...
Expand Down Expand Up @@ -158,7 +158,7 @@ public override MSAst.Expression Reduce() {
// exit(None, None, None)
//******************************************************************

var previousException = Ast.Variable(typeof(Exception), "$previousException");
var previousException = Ast.Variable(typeof(object), "$previousException");
variables.Add(previousException);

MSAst.ParameterExpression exception;
Expand All @@ -167,7 +167,7 @@ public override MSAst.Expression Reduce() {
AstUtils.Try(
AstUtils.Try(// try statement body
PushLineUpdated(false, lineUpdated),
Ast.Assign(previousException, Ast.Call(AstMethods.SaveCurrentException)),
Ast.Assign(previousException, Ast.Call(AstMethods.SaveCurrentExceptionState)),
_var != null ?
(MSAst.Expression)Ast.Block(
// VAR = value
Expand Down Expand Up @@ -206,8 +206,8 @@ public override MSAst.Expression Reduce() {
UpdateLineUpdated(true),
Ast.Throw(
Ast.Call(
AstMethods.MakeRethrowExceptionWorker,
exception
AstMethods.MakeRethrownException,
Parent.LocalContext
)
)
)
Expand All @@ -218,9 +218,9 @@ public override MSAst.Expression Reduce() {
PopLineUpdated(lineUpdated),
Ast.Empty()
)
// finally:
// finally:
).Finally(
Ast.Call(AstMethods.RestoreCurrentException, previousException),
Ast.Call(AstMethods.RestoreCurrentExceptionState, previousException),
// if exc:
// exit(None, None, None)
AstUtils.IfThen(
Expand Down
4 changes: 2 additions & 2 deletions src/core/IronPython/Compiler/PythonScriptCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public override object Run(Scope scope) {
private object RunWorker(CodeContext ctx) {
LookupCompilationDelegate target = GetTarget(true);

Exception e = PythonOps.SaveCurrentException();
object e = PythonOps.SaveCurrentExceptionState();
PushFrame(ctx, _code);
try {
return target(ctx, _code);
} finally {
PythonOps.RestoreCurrentException(e);
PythonOps.RestoreCurrentExceptionState(e);
PopFrame();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/IronPython/Compiler/RuntimeScriptCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private object InvokeTarget(Scope scope) {
if (scope == _optimizedContext.GlobalScope && !_optimizedContext.LanguageContext.EnableTracing) {
EnsureCompiled();

Exception e = PythonOps.SaveCurrentException();
object e = PythonOps.SaveCurrentExceptionState();
var funcCode = EnsureFunctionCode(_optimizedTarget, false, true);
PushFrame(_optimizedContext, funcCode);
try {
Expand All @@ -63,7 +63,7 @@ private object InvokeTarget(Scope scope) {
}
return _optimizedTarget(funcCode);
} finally {
PythonOps.RestoreCurrentException(e);
PythonOps.RestoreCurrentExceptionState(e);
PopFrame();
}
}
Expand Down
Loading
Loading