6868import com .oracle .truffle .api .RootCallTarget ;
6969import com .oracle .truffle .api .Truffle ;
7070import com .oracle .truffle .api .TruffleException ;
71+ import com .oracle .truffle .api .TruffleLanguage .ContextReference ;
7172import com .oracle .truffle .api .frame .VirtualFrame ;
7273import com .oracle .truffle .api .nodes .RootNode ;
7374import com .oracle .truffle .api .source .SourceSection ;
7475
7576public class TopLevelExceptionHandler extends RootNode {
7677 private final RootCallTarget innerCallTarget ;
7778 private final PException exception ;
78- private final PythonContext context ;
79+ private final ContextReference <PythonContext > context ;
80+ private final SourceSection sourceSection ;
81+
7982 @ Child private CreateArgumentsNode createArgs = CreateArgumentsNode .create ();
8083 @ Child private LookupAndCallUnaryNode callStrNode = LookupAndCallUnaryNode .create (__STR__ );
8184 @ Child private CallNode callNode = CallNode .create ();
82- private SourceSection sourceSection ;
8385
8486 public TopLevelExceptionHandler (PythonLanguage language , RootNode child ) {
8587 super (language );
8688 this .sourceSection = child .getSourceSection ();
87- this .context = language .getContextReference (). get () ;
89+ this .context = language .getContextReference ();
8890 this .innerCallTarget = Truffle .getRuntime ().createCallTarget (child );
8991 this .exception = null ;
9092 }
9193
9294 public TopLevelExceptionHandler (PythonLanguage language , PException exception ) {
9395 super (language );
9496 this .sourceSection = exception .getSourceLocation ();
95- this .context = language .getContextReference (). get () ;
97+ this .context = language .getContextReference ();
9698 this .innerCallTarget = null ;
9799 this .exception = exception ;
98100 }
@@ -103,14 +105,14 @@ public Object execute(VirtualFrame frame) {
103105 printExc (exception );
104106 return null ;
105107 } else {
106- assert context .getCurrentException () == null ;
108+ assert context .get (). getCurrentException () == null ;
107109 try {
108110 return run (frame );
109111 } catch (PException e ) {
110112 printExc (e );
111113 return null ;
112114 } catch (Exception e ) {
113- if (PythonOptions .getOption (context , PythonOptions .WithJavaStacktrace )) {
115+ if (PythonOptions .getOption (context . get () , PythonOptions .WithJavaStacktrace )) {
114116 boolean exitException = e instanceof TruffleException && ((TruffleException ) e ).isExit ();
115117 if (!exitException ) {
116118 printStackTrace (e );
@@ -132,7 +134,8 @@ public SourceSection getSourceSection() {
132134 */
133135 private void printExc (PException e ) {
134136 CompilerDirectives .transferToInterpreter ();
135- PythonCore core = context .getCore ();
137+ PythonContext theContext = context .get ();
138+ PythonCore core = theContext .getCore ();
136139 if (core .getErrorClass (SystemExit ) == e .getType ()) {
137140 handleSystemExit (e );
138141 }
@@ -147,7 +150,7 @@ private void printExc(PException e) {
147150 sys .setAttribute (BuiltinNames .LAST_TRACEBACK , tb );
148151
149152 Object hook = sys .getAttribute (BuiltinNames .EXCEPTHOOK );
150- if (PythonOptions .getOption (context , PythonOptions .AlwaysRunExcepthook )) {
153+ if (PythonOptions .getOption (theContext , PythonOptions .AlwaysRunExcepthook )) {
151154 if (hook != PNone .NO_VALUE ) {
152155 try {
153156 callNode .execute (null , hook , new Object []{type , value , tb }, PKeyword .EMPTY_KEYWORDS );
@@ -161,7 +164,7 @@ private void printExc(PException e) {
161164 }
162165 } else {
163166 try {
164- context .getEnv ().err ().write ("sys.excepthook is missing\n " .getBytes ());
167+ theContext .getEnv ().err ().write ("sys.excepthook is missing\n " .getBytes ());
165168 } catch (IOException ioException ) {
166169 ioException .printStackTrace ();
167170 }
@@ -171,7 +174,8 @@ private void printExc(PException e) {
171174 }
172175
173176 private void handleSystemExit (PException e ) {
174- if (PythonOptions .getOption (context , PythonOptions .InspectFlag ) && !getSourceSection ().getSource ().isInteractive ()) {
177+ PythonContext theContext = context .get ();
178+ if (PythonOptions .getOption (theContext , PythonOptions .InspectFlag ) && !getSourceSection ().getSource ().isInteractive ()) {
175179 // Don't exit if -i flag was given and we're not yet running interactively
176180 return ;
177181 }
@@ -187,11 +191,11 @@ private void handleSystemExit(PException e) {
187191 if (exitcode != null ) {
188192 throw new PythonExitException (this , exitcode );
189193 }
190- if (PythonOptions .getOption (context , PythonOptions .AlwaysRunExcepthook )) {
194+ if (PythonOptions .getOption (theContext , PythonOptions .AlwaysRunExcepthook )) {
191195 // If we failed to dig out the exit code we just print and leave
192196 try {
193- context .getEnv ().err ().write (callStrNode .executeObject (e .getExceptionObject ()).toString ().getBytes ());
194- context .getEnv ().err ().write ('\n' );
197+ theContext .getEnv ().err ().write (callStrNode .executeObject (e .getExceptionObject ()).toString ().getBytes ());
198+ theContext .getEnv ().err ().write ('\n' );
195199 } catch (IOException e1 ) {
196200 }
197201 throw new PythonExitException (this , 1 );
@@ -207,7 +211,7 @@ private static void printStackTrace(Exception e) {
207211
208212 private Object run (VirtualFrame frame ) {
209213 Object [] arguments = createArgs .execute (frame .getArguments ());
210- PArguments .setGlobals (arguments , context .getMainModule ());
214+ PArguments .setGlobals (arguments , context .get (). getMainModule ());
211215 return innerCallTarget .call (arguments );
212216 }
213217
0 commit comments