43
43
import static com .oracle .graal .python .nodes .BuiltinNames .T_SYS ;
44
44
45
45
import java .io .IOException ;
46
+ import java .io .PrintWriter ;
46
47
import java .util .ArrayList ;
47
48
import java .util .List ;
48
49
import java .util .ListIterator ;
@@ -97,7 +98,7 @@ public static void printPythonLikeStackTrace() {
97
98
appendStackLine (stack , location , rootNode , true , lineno );
98
99
return null ;
99
100
});
100
- printStack (stack );
101
+ printStack (new PrintWriter ( System . err ), stack );
101
102
}
102
103
103
104
private static int getLineno (Frame frame ) {
@@ -114,10 +115,23 @@ private static int getLineno(Frame frame) {
114
115
}
115
116
116
117
/**
117
- * this method is similar to 'PyErr_WriteUnraisable'
118
+ * this method is similar to 'PyErr_WriteUnraisable' and may be used when the context is not
119
+ * available.
118
120
*/
119
121
@ TruffleBoundary
120
122
public static void printPythonLikeStackTrace (Throwable e ) {
123
+ printPythonLikeStackTrace (new PrintWriter (System .err ), e );
124
+ }
125
+
126
+ /**
127
+ * this method is similar to 'PyErr_WriteUnraisable'
128
+ */
129
+ @ TruffleBoundary
130
+ public static void printPythonLikeStackTrace (PythonContext context , Throwable e ) {
131
+ printPythonLikeStackTrace (new PrintWriter (context .getEnv ().err ()), e );
132
+ }
133
+
134
+ private static void printPythonLikeStackTrace (PrintWriter p , Throwable e ) {
121
135
List <TruffleStackTraceElement > stackTrace = TruffleStackTrace .getStackTrace (e );
122
136
if (stackTrace != null ) {
123
137
ArrayList <String > stack = new ArrayList <>();
@@ -127,17 +141,17 @@ public static void printPythonLikeStackTrace(Throwable e) {
127
141
int lineno = getLineno (frame .getFrame ());
128
142
appendStackLine (stack , location , rootNode , false , lineno );
129
143
}
130
- printStack (stack );
144
+ printStack (p , stack );
131
145
}
132
146
InteropLibrary lib = InteropLibrary .getUncached ();
133
147
if (lib .isException (e ) && lib .hasExceptionMessage (lib )) {
134
148
try {
135
- System . err .println (lib .getExceptionMessage (e ));
149
+ p .println (lib .getExceptionMessage (e ));
136
150
} catch (UnsupportedMessageException unsupportedMessageException ) {
137
151
throw CompilerDirectives .shouldNotReachHere ();
138
152
}
139
153
} else {
140
- System . err .println (e .getMessage ());
154
+ p .println (e .getMessage ());
141
155
}
142
156
}
143
157
@@ -175,11 +189,11 @@ private static void appendStackLine(ArrayList<String> stack, Node location, Root
175
189
}
176
190
}
177
191
178
- private static void printStack (final ArrayList <String > stack ) {
179
- System . err .println ("Traceback (most recent call last):" );
192
+ private static void printStack (PrintWriter p , final ArrayList <String > stack ) {
193
+ p .println ("Traceback (most recent call last):" );
180
194
ListIterator <String > listIterator = stack .listIterator (stack .size ());
181
195
while (listIterator .hasPrevious ()) {
182
- System . err .println (listIterator .previous ());
196
+ p .println (listIterator .previous ());
183
197
}
184
198
}
185
199
0 commit comments