Skip to content

Commit 2a0666e

Browse files
authored
Save/restore exc_info in context manager (#735)
1 parent 752e14a commit 2a0666e

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

Src/IronPython/Compiler/Ast/WithStatement.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,16 @@ public override MSAst.Expression Reduce() {
159159
// exit(None, None, None)
160160
//******************************************************************
161161

162+
var previousException = Ast.Variable(typeof(Exception), "$previousException");
163+
variables.Add(previousException);
164+
162165
MSAst.ParameterExpression exception;
163166
statements.Add(
164167
// try:
165168
AstUtils.Try(
166169
AstUtils.Try(// try statement body
167170
PushLineUpdated(false, lineUpdated),
171+
Ast.Assign(previousException, Ast.Call(AstMethods.SaveCurrentException)),
168172
_var != null ?
169173
(MSAst.Expression)Ast.Block(
170174
// VAR = value
@@ -182,6 +186,7 @@ public override MSAst.Expression Reduce() {
182186
exception,
183187
GlobalParent.AddDebugInfoAndVoid(
184188
Ast.Block(
189+
Ast.Call(AstMethods.SetCurrentException, Parent.LocalContext, exception),
185190
// exc = False
186191
MakeAssignment(
187192
exc,
@@ -216,6 +221,7 @@ public override MSAst.Expression Reduce() {
216221
)
217222
// finally:
218223
).Finally(
224+
Ast.Call(AstMethods.RestoreCurrentException, previousException),
219225
// if exc:
220226
// exit(None, None, None)
221227
AstUtils.IfThen(

Tests/test_excinfo.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,6 @@ def __init__(self, s):
399399
with M1(self):
400400
pass
401401

402-
# https://github.com/IronLanguages/ironpython3/issues/726
403-
@unittest.expectedFailure
404402
def test_with_fail(self):
405403
"""with.__exit__ doesn't see exception in exception case."""
406404
class M2(ManBase):
@@ -441,8 +439,6 @@ def __exit__(self, t, v, tb):
441439
pass
442440
self.A(15)
443441

444-
# https://github.com/IronLanguages/ironpython3/issues/726
445-
@unittest.expectedFailure
446442
# call 'with' from an except block, do failure case
447443
def test_with_except_fail(self):
448444
class M2(ManBase):

0 commit comments

Comments
 (0)