68
68
import com .oracle .truffle .api .RootCallTarget ;
69
69
import com .oracle .truffle .api .Truffle ;
70
70
import com .oracle .truffle .api .TruffleException ;
71
+ import com .oracle .truffle .api .TruffleLanguage .ContextReference ;
71
72
import com .oracle .truffle .api .frame .VirtualFrame ;
72
73
import com .oracle .truffle .api .nodes .RootNode ;
73
74
import com .oracle .truffle .api .source .SourceSection ;
74
75
75
76
public class TopLevelExceptionHandler extends RootNode {
76
77
private final RootCallTarget innerCallTarget ;
77
78
private final PException exception ;
78
- private final PythonContext context ;
79
+ private final ContextReference <PythonContext > context ;
80
+ private final SourceSection sourceSection ;
81
+
79
82
@ Child private CreateArgumentsNode createArgs = CreateArgumentsNode .create ();
80
83
@ Child private LookupAndCallUnaryNode callStrNode = LookupAndCallUnaryNode .create (__STR__ );
81
84
@ Child private CallNode callNode = CallNode .create ();
82
- private SourceSection sourceSection ;
83
85
84
86
public TopLevelExceptionHandler (PythonLanguage language , RootNode child ) {
85
87
super (language );
86
88
this .sourceSection = child .getSourceSection ();
87
- this .context = language .getContextReference (). get () ;
89
+ this .context = language .getContextReference ();
88
90
this .innerCallTarget = Truffle .getRuntime ().createCallTarget (child );
89
91
this .exception = null ;
90
92
}
91
93
92
94
public TopLevelExceptionHandler (PythonLanguage language , PException exception ) {
93
95
super (language );
94
96
this .sourceSection = exception .getSourceLocation ();
95
- this .context = language .getContextReference (). get () ;
97
+ this .context = language .getContextReference ();
96
98
this .innerCallTarget = null ;
97
99
this .exception = exception ;
98
100
}
@@ -103,14 +105,14 @@ public Object execute(VirtualFrame frame) {
103
105
printExc (exception );
104
106
return null ;
105
107
} else {
106
- assert context .getCurrentException () == null ;
108
+ assert context .get (). getCurrentException () == null ;
107
109
try {
108
110
return run (frame );
109
111
} catch (PException e ) {
110
112
printExc (e );
111
113
return null ;
112
114
} catch (Exception e ) {
113
- if (PythonOptions .getOption (context , PythonOptions .WithJavaStacktrace )) {
115
+ if (PythonOptions .getOption (context . get () , PythonOptions .WithJavaStacktrace )) {
114
116
boolean exitException = e instanceof TruffleException && ((TruffleException ) e ).isExit ();
115
117
if (!exitException ) {
116
118
printStackTrace (e );
@@ -132,7 +134,8 @@ public SourceSection getSourceSection() {
132
134
*/
133
135
private void printExc (PException e ) {
134
136
CompilerDirectives .transferToInterpreter ();
135
- PythonCore core = context .getCore ();
137
+ PythonContext theContext = context .get ();
138
+ PythonCore core = theContext .getCore ();
136
139
if (core .getErrorClass (SystemExit ) == e .getType ()) {
137
140
handleSystemExit (e );
138
141
}
@@ -147,7 +150,7 @@ private void printExc(PException e) {
147
150
sys .setAttribute (BuiltinNames .LAST_TRACEBACK , tb );
148
151
149
152
Object hook = sys .getAttribute (BuiltinNames .EXCEPTHOOK );
150
- if (PythonOptions .getOption (context , PythonOptions .AlwaysRunExcepthook )) {
153
+ if (PythonOptions .getOption (theContext , PythonOptions .AlwaysRunExcepthook )) {
151
154
if (hook != PNone .NO_VALUE ) {
152
155
try {
153
156
callNode .execute (null , hook , new Object []{type , value , tb }, PKeyword .EMPTY_KEYWORDS );
@@ -161,7 +164,7 @@ private void printExc(PException e) {
161
164
}
162
165
} else {
163
166
try {
164
- context .getEnv ().err ().write ("sys.excepthook is missing\n " .getBytes ());
167
+ theContext .getEnv ().err ().write ("sys.excepthook is missing\n " .getBytes ());
165
168
} catch (IOException ioException ) {
166
169
ioException .printStackTrace ();
167
170
}
@@ -171,7 +174,8 @@ private void printExc(PException e) {
171
174
}
172
175
173
176
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 ()) {
175
179
// Don't exit if -i flag was given and we're not yet running interactively
176
180
return ;
177
181
}
@@ -187,11 +191,11 @@ private void handleSystemExit(PException e) {
187
191
if (exitcode != null ) {
188
192
throw new PythonExitException (this , exitcode );
189
193
}
190
- if (PythonOptions .getOption (context , PythonOptions .AlwaysRunExcepthook )) {
194
+ if (PythonOptions .getOption (theContext , PythonOptions .AlwaysRunExcepthook )) {
191
195
// If we failed to dig out the exit code we just print and leave
192
196
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' );
195
199
} catch (IOException e1 ) {
196
200
}
197
201
throw new PythonExitException (this , 1 );
@@ -207,7 +211,7 @@ private static void printStackTrace(Exception e) {
207
211
208
212
private Object run (VirtualFrame frame ) {
209
213
Object [] arguments = createArgs .execute (frame .getArguments ());
210
- PArguments .setGlobals (arguments , context .getMainModule ());
214
+ PArguments .setGlobals (arguments , context .get (). getMainModule ());
211
215
return innerCallTarget .call (arguments );
212
216
}
213
217
0 commit comments