60
60
import java .nio .charset .Charset ;
61
61
import java .util .Arrays ;
62
62
import java .util .Date ;
63
+ import java .util .HashSet ;
63
64
import java .util .List ;
64
65
import java .util .Map ;
66
+ import java .util .Set ;
65
67
66
- import org .graalvm .collections .EconomicSet ;
67
68
import org .graalvm .nativeimage .ImageInfo ;
68
69
69
70
import com .oracle .graal .python .PythonLanguage ;
@@ -833,6 +834,7 @@ abstract static class ExceptHookNode extends PythonBuiltinNode {
833
834
static final String ATTR_TEXT = "text" ;
834
835
static final String VALUE_STRING = "<string>" ;
835
836
static final String VALUE_UNKNOWN = "<unknown>" ;
837
+ static final String NL = "\n " ;
836
838
837
839
@ Child private ReadAttributeFromObjectNode readAttributeFromObjectNode ;
838
840
@ Child private PyLongAsLongAndOverflowNode pyLongAsLongAndOverflowNode ;
@@ -999,9 +1001,8 @@ private Object readAttr(Object object, String attribute) {
999
1001
return readAttributeFromObjectNode .execute (object , attribute );
1000
1002
}
1001
1003
1002
- void printTraceBack (VirtualFrame frame , Object out , PTraceback tb ) {
1004
+ void printTraceBack (VirtualFrame frame , PythonModule sys , Object out , PTraceback tb ) {
1003
1005
long limit = TRACEBACK_LIMIT ;
1004
- final PythonModule sys = getContext ().getCore ().lookupBuiltinModule ("sys" );
1005
1006
final Object limitv = readAttr (sys , ATTR_TB_LIMIT );
1006
1007
if (checkLong (limitv )) {
1007
1008
limit = asLongAndOverflow (frame , limitv , MAXSIZE );
@@ -1027,7 +1028,7 @@ void displayLine(VirtualFrame frame, Object out, String fileName, int lineNo, St
1027
1028
}
1028
1029
1029
1030
final StringBuilder sb = PythonUtils .newStringBuilder (" File \" " );
1030
- PythonUtils .append (sb , fileName , "\" , line " , lineNo , ", in " , name , " \n " );
1031
+ PythonUtils .append (sb , fileName , "\" , line " , lineNo , ", in " , name , NL );
1031
1032
print (frame , out , PythonUtils .sbToString (sb ));
1032
1033
// ignore errors since we can't report them, can we?
1033
1034
displaySourceLine (frame , out , fileName , lineNo , 4 );
@@ -1059,7 +1060,7 @@ void displaySourceLine(VirtualFrame frame, Object out, String fileName, int line
1059
1060
if (line != null ) {
1060
1061
print (frame , out , getIndent (indent ));
1061
1062
print (frame , out , PythonUtils .trimLeft (line ));
1062
- print (frame , out , " \n " );
1063
+ print (frame , out , NL );
1063
1064
}
1064
1065
}
1065
1066
@@ -1192,7 +1193,7 @@ void printErrorText(VirtualFrame frame, Object out, SyntaxErrData syntaxErrData)
1192
1193
print (frame , out , " " );
1193
1194
print (frame , out , text );
1194
1195
if (text .charAt (0 ) == '\0' || text .charAt (text .length () - 1 ) != '\n' ) {
1195
- print (frame , out , " \n " );
1196
+ print (frame , out , NL );
1196
1197
}
1197
1198
if (offset == -1 ) {
1198
1199
return ;
@@ -1209,7 +1210,7 @@ private String classNameNoDot(String name) {
1209
1210
return (i > 0 ) ? PythonUtils .substring (name , i + 1 ) : name ;
1210
1211
}
1211
1212
1212
- void printException (VirtualFrame frame , Object out , Object excValue ) {
1213
+ void printException (VirtualFrame frame , PythonModule sys , Object out , Object excValue ) {
1213
1214
Object value = excValue ;
1214
1215
final Object type = getClass (value );
1215
1216
if (!PGuards .isPBaseException (value )) {
@@ -1222,7 +1223,7 @@ void printException(VirtualFrame frame, Object out, Object excValue) {
1222
1223
final PBaseException exc = (PBaseException ) value ;
1223
1224
final PTraceback tb = getExceptionTraceback (exc );
1224
1225
if (tb != null ) {
1225
- printTraceBack (frame , out , tb );
1226
+ printTraceBack (frame , sys , out , tb );
1226
1227
}
1227
1228
1228
1229
if (hasAttr (frame , value , ATTR_PRINT_FILE_AND_LINE )) {
@@ -1277,38 +1278,53 @@ void printException(VirtualFrame frame, Object out, Object excValue) {
1277
1278
}
1278
1279
}
1279
1280
1280
- print (frame , out , " \n " );
1281
+ print (frame , out , NL );
1281
1282
}
1282
1283
1283
- void printExceptionRecursive (VirtualFrame frame , Object out , Object value , EconomicSet <Object > seen ) {
1284
+ void printExceptionRecursive (VirtualFrame frame , PythonModule sys , Object out , Object value , Set <Object > seen ) {
1284
1285
if (seen != null ) {
1285
1286
// Exception chaining
1286
- seen . add (value );
1287
+ add (seen , value );
1287
1288
if (PGuards .isPBaseException (value )) {
1288
1289
final PBaseException exc = (PBaseException ) value ;
1289
1290
final PBaseException cause = exc .getCause ();
1290
1291
final PBaseException context = exc .getContext ();
1291
1292
1292
1293
if (cause != null ) {
1293
- if (!seen . contains (cause )) {
1294
- printExceptionRecursive (frame , out , cause , seen );
1294
+ if (!contains (seen , cause )) {
1295
+ printExceptionRecursive (frame , sys , out , cause , seen );
1295
1296
print (frame , out , CAUSE_MESSAGE );
1296
1297
}
1297
1298
} else if (context != null && !exc .getSuppressContext ()) {
1298
- if (!seen . contains (context )) {
1299
- printExceptionRecursive (frame , out , context , seen );
1299
+ if (!contains (seen , context )) {
1300
+ printExceptionRecursive (frame , sys , out , context , seen );
1300
1301
print (frame , out , CONTEXT_MESSAGE );
1301
1302
}
1302
1303
}
1303
1304
}
1304
1305
}
1305
- printException (frame , out , value );
1306
+ printException (frame , sys , out , value );
1307
+ }
1308
+
1309
+ @ TruffleBoundary
1310
+ static void add (Set <Object > set , Object value ) {
1311
+ set .add (value );
1312
+ }
1313
+
1314
+ @ TruffleBoundary
1315
+ static boolean contains (Set <Object > set , Object value ) {
1316
+ return set .contains (value );
1317
+ }
1318
+
1319
+ @ TruffleBoundary
1320
+ static Set <Object > createSet () {
1321
+ return new HashSet <>();
1306
1322
}
1307
1323
1308
1324
@ Specialization
1309
1325
Object doWithoutTb (VirtualFrame frame , PythonModule sys , @ SuppressWarnings ("unused" ) Object excType , Object value , @ SuppressWarnings ("unused" ) PNone traceBack ) {
1310
1326
Object stdErr = lookupAttr (frame , sys , STDERR );
1311
- printExceptionRecursive (frame , stdErr , value , EconomicSet . create ());
1327
+ printExceptionRecursive (frame , sys , stdErr , value , createSet ());
1312
1328
flush (frame , stdErr );
1313
1329
1314
1330
return PNone .NONE ;
@@ -1325,7 +1341,7 @@ Object doWithTb(VirtualFrame frame, PythonModule sys, @SuppressWarnings("unused"
1325
1341
}
1326
1342
1327
1343
Object stdErr = lookupAttr (frame , sys , STDERR );
1328
- printExceptionRecursive (frame , stdErr , value , EconomicSet . create ());
1344
+ printExceptionRecursive (frame , sys , stdErr , value , createSet ());
1329
1345
flush (frame , stdErr );
1330
1346
1331
1347
return PNone .NONE ;
0 commit comments