Skip to content

Commit 5381bca

Browse files
committed
Support 'terminating return' opt in TryFinallyNode
1 parent d3a98e2 commit 5381bca

File tree

1 file changed

+31
-13
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement

1 file changed

+31
-13
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/statement/TryFinallyNode.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,40 @@ public void executeVoid(VirtualFrame frame) {
4949
if (finalbody == null) {
5050
body.executeVoid(frame);
5151
} else {
52-
try {
53-
body.executeVoid(frame);
54-
} catch (PException handledException) {
55-
handleException(frame, handledException);
56-
} catch (AbstractTruffleException | ControlFlowException e) {
57-
finalbody.executeVoid(frame);
52+
executeImpl(frame, false);
53+
}
54+
}
55+
56+
@Override
57+
public Object returnExecute(VirtualFrame frame) {
58+
if (finalbody == null) {
59+
return body.returnExecute(frame);
60+
} else {
61+
return executeImpl(frame, true);
62+
}
63+
}
64+
65+
public Object executeImpl(VirtualFrame frame, boolean isReturn) {
66+
assert finalbody != null;
67+
Object result = null;
68+
try {
69+
// The assumption is that finally blocks usually do not return, we execute those in
70+
// control flow exceptions mode to be able to execute the body with 'returnExecute'
71+
result = body.genericExecute(frame, isReturn);
72+
} catch (PException handledException) {
73+
handleException(frame, handledException);
74+
} catch (AbstractTruffleException | ControlFlowException e) {
75+
finalbody.executeVoid(frame);
76+
throw e;
77+
} catch (Throwable e) {
78+
PException pe = wrapJavaExceptionIfApplicable(e);
79+
if (pe == null) {
5880
throw e;
59-
} catch (Throwable e) {
60-
PException pe = wrapJavaExceptionIfApplicable(e);
61-
if (pe == null) {
62-
throw e;
63-
}
64-
handleException(frame, pe);
6581
}
66-
finalbody.executeVoid(frame);
82+
handleException(frame, pe);
6783
}
84+
finalbody.executeVoid(frame);
85+
return result;
6886
}
6987

7088
private void handleException(VirtualFrame frame, PException handledException) {

0 commit comments

Comments
 (0)