76
76
import com .oracle .graal .python .runtime .exception .PythonExitException ;
77
77
import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
78
78
import com .oracle .truffle .api .CompilerDirectives ;
79
+ import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
79
80
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
80
81
import com .oracle .truffle .api .RootCallTarget ;
81
82
import com .oracle .truffle .api .Truffle ;
88
89
public class TopLevelExceptionHandler extends RootNode {
89
90
private final RootCallTarget innerCallTarget ;
90
91
private final PException exception ;
91
- private final ContextReference <PythonContext > context ;
92
92
private final SourceSection sourceSection ;
93
+ @ CompilationFinal private ContextReference <PythonContext > context ;
93
94
94
95
@ Child private CreateArgumentsNode createArgs = CreateArgumentsNode .create ();
95
96
@ Child private LookupAndCallUnaryNode callStrNode = LookupAndCallUnaryNode .create (__STR__ );
@@ -101,26 +102,32 @@ public class TopLevelExceptionHandler extends RootNode {
101
102
public TopLevelExceptionHandler (PythonLanguage language , RootNode child ) {
102
103
super (language );
103
104
this .sourceSection = child .getSourceSection ();
104
- this .context = language .getContextReference ();
105
105
this .innerCallTarget = Truffle .getRuntime ().createCallTarget (child );
106
106
this .exception = null ;
107
107
}
108
108
109
109
public TopLevelExceptionHandler (PythonLanguage language , PException exception ) {
110
110
super (language );
111
111
this .sourceSection = exception .getSourceLocation ();
112
- this .context = language .getContextReference ();
113
112
this .innerCallTarget = null ;
114
113
this .exception = exception ;
115
114
}
116
115
116
+ private PythonContext getContext () {
117
+ if (context == null ) {
118
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
119
+ context = lookupContextReference (PythonLanguage .class );
120
+ }
121
+ return context .get ();
122
+ }
123
+
117
124
@ Override
118
125
public Object execute (VirtualFrame frame ) {
119
126
if (exception != null ) {
120
127
printExc (frame , exception );
121
128
return null ;
122
129
} else {
123
- assert context . get ().getCurrentException () == null ;
130
+ assert getContext ().getCurrentException () == null ;
124
131
try {
125
132
return run (frame );
126
133
} catch (PException e ) {
@@ -132,15 +139,15 @@ public Object execute(VirtualFrame frame) {
132
139
e .getExceptionObject ().setTraceback (tbHead );
133
140
}
134
141
printExc (frame , e );
135
- if (PythonOptions .getOption (context . get (), PythonOptions .WithJavaStacktrace )) {
142
+ if (PythonOptions .getOption (getContext (), PythonOptions .WithJavaStacktrace )) {
136
143
printStackTrace (e );
137
144
}
138
145
return null ;
139
146
} catch (Exception | StackOverflowError e ) {
140
147
boolean exitException = e instanceof TruffleException && ((TruffleException ) e ).isExit ();
141
148
if (!exitException ) {
142
149
ExceptionUtils .printPythonLikeStackTrace (e );
143
- if (PythonOptions .getOption (context . get (), PythonOptions .WithJavaStacktrace )) {
150
+ if (PythonOptions .getOption (getContext (), PythonOptions .WithJavaStacktrace )) {
144
151
printStackTrace (e );
145
152
}
146
153
}
@@ -160,7 +167,7 @@ public SourceSection getSourceSection() {
160
167
*/
161
168
private void printExc (VirtualFrame frame , PException e ) {
162
169
CompilerDirectives .transferToInterpreter ();
163
- PythonContext theContext = context . get ();
170
+ PythonContext theContext = getContext ();
164
171
PythonCore core = theContext .getCore ();
165
172
if (IsBuiltinClassProfile .profileClassSlowPath (e .getExceptionObject ().getLazyPythonClass (), SystemExit )) {
166
173
handleSystemExit (frame , e );
@@ -201,7 +208,7 @@ private void printExc(VirtualFrame frame, PException e) {
201
208
}
202
209
203
210
private void handleSystemExit (VirtualFrame frame , PException e ) {
204
- PythonContext theContext = context . get ();
211
+ PythonContext theContext = getContext ();
205
212
if (PythonOptions .getOption (theContext , PythonOptions .InspectFlag ) && !getSourceSection ().getSource ().isInteractive ()) {
206
213
// Don't exit if -i flag was given and we're not yet running interactively
207
214
return ;
@@ -259,7 +266,7 @@ private Object run(VirtualFrame frame) {
259
266
for (int i = 0 ; i < frame .getArguments ().length ; i ++) {
260
267
PArguments .setArgument (arguments , i , frame .getArguments ()[i ]);
261
268
}
262
- PythonContext pythonContext = context . get ();
269
+ PythonContext pythonContext = getContext ();
263
270
if (getSourceSection ().getSource ().isInternal ()) {
264
271
// internal sources are not run in the main module
265
272
PArguments .setGlobals (arguments , pythonContext .getCore ().factory ().createDict ());
0 commit comments