@@ -96,8 +96,9 @@ protected Object runWith(VirtualFrame frame, PythonObject withObject,
96
96
@ Cached ("create()" ) IsCallableNode isExitCallableNode ) {
97
97
98
98
boolean gotException = false ;
99
- Object enterCallable = enterGetter . execute ( withObject , "__enter__" );
99
+ // CPython first looks up '__exit__
100
100
Object exitCallable = exitGetter .execute (withObject , "__exit__" );
101
+ Object enterCallable = enterGetter .execute (withObject , "__enter__" );
101
102
102
103
if (isCallableNode .execute (enterCallable )) {
103
104
applyValues (frame , enterDispatch .executeCall (enterCallable , createArgs .execute (withObject ), new PKeyword [0 ]));
@@ -109,7 +110,7 @@ protected Object runWith(VirtualFrame frame, PythonObject withObject,
109
110
body .execute (frame );
110
111
} catch (PException exception ) {
111
112
gotException = true ;
112
- return handleException (withObject , exception , isExitCallableNode );
113
+ return handleException (withObject , exitCallable , exception , isExitCallableNode );
113
114
} finally {
114
115
if (!gotException ) {
115
116
if (isExitCallableNode .execute (exitCallable )) {
@@ -122,8 +123,7 @@ protected Object runWith(VirtualFrame frame, PythonObject withObject,
122
123
return PNone .NONE ;
123
124
}
124
125
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 ) {
127
127
if (!isExitCallableNode .execute (exitCallable )) {
128
128
throw raise (TypeError , "%p is not callable" , exitCallable );
129
129
}
0 commit comments