Skip to content

Commit 19e34dd

Browse files
committed
Lookup '__exit__' only once in 'WithNode'.
1 parent 4e77634 commit 19e34dd

File tree

1 file changed

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

1 file changed

+4
-4
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ protected Object runWith(VirtualFrame frame, PythonObject withObject,
9696
@Cached("create()") IsCallableNode isExitCallableNode) {
9797

9898
boolean gotException = false;
99-
Object enterCallable = enterGetter.execute(withObject, "__enter__");
99+
// CPython first looks up '__exit__
100100
Object exitCallable = exitGetter.execute(withObject, "__exit__");
101+
Object enterCallable = enterGetter.execute(withObject, "__enter__");
101102

102103
if (isCallableNode.execute(enterCallable)) {
103104
applyValues(frame, enterDispatch.executeCall(enterCallable, createArgs.execute(withObject), new PKeyword[0]));
@@ -109,7 +110,7 @@ protected Object runWith(VirtualFrame frame, PythonObject withObject,
109110
body.execute(frame);
110111
} catch (PException exception) {
111112
gotException = true;
112-
return handleException(withObject, exception, isExitCallableNode);
113+
return handleException(withObject, exitCallable, exception, isExitCallableNode);
113114
} finally {
114115
if (!gotException) {
115116
if (isExitCallableNode.execute(exitCallable)) {
@@ -122,8 +123,7 @@ protected Object runWith(VirtualFrame frame, PythonObject withObject,
122123
return PNone.NONE;
123124
}
124125

125-
private Object handleException(PythonObject withObject, PException e, IsCallableNode isExitCallableNode) {
126-
Object exitCallable = exitGetter.execute(withObject, "__exit__");
126+
private Object handleException(PythonObject withObject, Object exitCallable, PException e, IsCallableNode isExitCallableNode) {
127127
if (!isExitCallableNode.execute(exitCallable)) {
128128
throw raise(TypeError, "%p is not callable", exitCallable);
129129
}

0 commit comments

Comments
 (0)