7676import com .oracle .graal .python .runtime .exception .PythonExitException ;
7777import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
7878import com .oracle .truffle .api .CompilerDirectives ;
79+ import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
7980import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
8081import com .oracle .truffle .api .RootCallTarget ;
8182import com .oracle .truffle .api .Truffle ;
8889public class TopLevelExceptionHandler extends RootNode {
8990 private final RootCallTarget innerCallTarget ;
9091 private final PException exception ;
91- private final ContextReference <PythonContext > context ;
9292 private final SourceSection sourceSection ;
93+ @ CompilationFinal private ContextReference <PythonContext > context ;
9394
9495 @ Child private CreateArgumentsNode createArgs = CreateArgumentsNode .create ();
9596 @ Child private LookupAndCallUnaryNode callStrNode = LookupAndCallUnaryNode .create (__STR__ );
@@ -101,26 +102,32 @@ public class TopLevelExceptionHandler extends RootNode {
101102 public TopLevelExceptionHandler (PythonLanguage language , RootNode child ) {
102103 super (language );
103104 this .sourceSection = child .getSourceSection ();
104- this .context = language .getContextReference ();
105105 this .innerCallTarget = Truffle .getRuntime ().createCallTarget (child );
106106 this .exception = null ;
107107 }
108108
109109 public TopLevelExceptionHandler (PythonLanguage language , PException exception ) {
110110 super (language );
111111 this .sourceSection = exception .getSourceLocation ();
112- this .context = language .getContextReference ();
113112 this .innerCallTarget = null ;
114113 this .exception = exception ;
115114 }
116115
116+ private PythonContext getContext () {
117+ if (context == null ) {
118+ CompilerDirectives .transferToInterpreterAndInvalidate ();
119+ context = lookupContextReference (PythonLanguage .class );
120+ }
121+ return context .get ();
122+ }
123+
117124 @ Override
118125 public Object execute (VirtualFrame frame ) {
119126 if (exception != null ) {
120127 printExc (frame , exception );
121128 return null ;
122129 } else {
123- assert context . get ().getCurrentException () == null ;
130+ assert getContext ().getCurrentException () == null ;
124131 try {
125132 return run (frame );
126133 } catch (PException e ) {
@@ -132,15 +139,15 @@ public Object execute(VirtualFrame frame) {
132139 e .getExceptionObject ().setTraceback (tbHead );
133140 }
134141 printExc (frame , e );
135- if (PythonOptions .getOption (context . get (), PythonOptions .WithJavaStacktrace )) {
142+ if (PythonOptions .getOption (getContext (), PythonOptions .WithJavaStacktrace )) {
136143 printStackTrace (e );
137144 }
138145 return null ;
139146 } catch (Exception | StackOverflowError e ) {
140147 boolean exitException = e instanceof TruffleException && ((TruffleException ) e ).isExit ();
141148 if (!exitException ) {
142149 ExceptionUtils .printPythonLikeStackTrace (e );
143- if (PythonOptions .getOption (context . get (), PythonOptions .WithJavaStacktrace )) {
150+ if (PythonOptions .getOption (getContext (), PythonOptions .WithJavaStacktrace )) {
144151 printStackTrace (e );
145152 }
146153 }
@@ -160,7 +167,7 @@ public SourceSection getSourceSection() {
160167 */
161168 private void printExc (VirtualFrame frame , PException e ) {
162169 CompilerDirectives .transferToInterpreter ();
163- PythonContext theContext = context . get ();
170+ PythonContext theContext = getContext ();
164171 PythonCore core = theContext .getCore ();
165172 if (IsBuiltinClassProfile .profileClassSlowPath (e .getExceptionObject ().getLazyPythonClass (), SystemExit )) {
166173 handleSystemExit (frame , e );
@@ -201,7 +208,7 @@ private void printExc(VirtualFrame frame, PException e) {
201208 }
202209
203210 private void handleSystemExit (VirtualFrame frame , PException e ) {
204- PythonContext theContext = context . get ();
211+ PythonContext theContext = getContext ();
205212 if (PythonOptions .getOption (theContext , PythonOptions .InspectFlag ) && !getSourceSection ().getSource ().isInteractive ()) {
206213 // Don't exit if -i flag was given and we're not yet running interactively
207214 return ;
@@ -259,7 +266,7 @@ private Object run(VirtualFrame frame) {
259266 for (int i = 0 ; i < frame .getArguments ().length ; i ++) {
260267 PArguments .setArgument (arguments , i , frame .getArguments ()[i ]);
261268 }
262- PythonContext pythonContext = context . get ();
269+ PythonContext pythonContext = getContext ();
263270 if (getSourceSection ().getSource ().isInternal ()) {
264271 // internal sources are not run in the main module
265272 PArguments .setGlobals (arguments , pythonContext .getCore ().factory ().createDict ());
0 commit comments