42
42
43
43
import static com .oracle .graal .python .builtins .PythonBuiltinClassType .TypeError ;
44
44
import static com .oracle .graal .python .builtins .PythonBuiltinClassType .ValueError ;
45
+ import static com .oracle .graal .python .builtins .modules .io .IONodes .FLUSH ;
46
+ import static com .oracle .graal .python .builtins .modules .io .IONodes .WRITE ;
45
47
import static com .oracle .graal .python .nodes .BuiltinNames .BUILTINS ;
46
48
import static com .oracle .graal .python .nodes .SpecialAttributeNames .STDERR ;
47
49
import static com .oracle .graal .python .nodes .SpecialAttributeNames .STDIN ;
79
81
import com .oracle .graal .python .builtins .modules .io .PBuffered ;
80
82
import com .oracle .graal .python .builtins .modules .io .PFileIO ;
81
83
import com .oracle .graal .python .builtins .modules .io .PTextIO ;
82
- import com .oracle .graal .python .builtins .modules .io .StringIOBuiltins ;
83
84
import com .oracle .graal .python .builtins .modules .io .TextIOWrapperNodes .TextIOWrapperInitNode ;
84
85
import com .oracle .graal .python .builtins .modules .io .TextIOWrapperNodesFactory .TextIOWrapperInitNodeGen ;
85
86
import com .oracle .graal .python .builtins .objects .PNone ;
@@ -844,7 +845,7 @@ abstract static class ExceptHookNode extends PythonBuiltinNode {
844
845
@ Child private CastToJavaStringNode castToJavaStringNode ;
845
846
@ Child private PyObjectStrAsObjectNode strAsObjNode ;
846
847
@ Child private TracebackBuiltins .GetTracebackFrameNode getTracebackFrameNode ;
847
- @ Child private StringIOBuiltins . WriteNode writeNode ;
848
+ @ Child private PyObjectCallMethodObjArgs callMethod ;
848
849
849
850
@ CompilerDirectives .ValueType
850
851
static final class SyntaxErrData {
@@ -863,17 +864,20 @@ static final class SyntaxErrData {
863
864
}
864
865
}
865
866
866
- private void print ( VirtualFrame frame , Object file , String data ) {
867
- if (writeNode == null ) {
867
+ private PyObjectCallMethodObjArgs ensureCallMethodNode ( ) {
868
+ if (callMethod == null ) {
868
869
CompilerDirectives .transferToInterpreterAndInvalidate ();
869
- writeNode = insert (StringIOBuiltins . WriteNode .create ());
870
+ callMethod = insert (PyObjectCallMethodObjArgs .create ());
870
871
}
871
- writeNode .execute (frame , file , data );
872
+ return callMethod ;
873
+ }
874
+
875
+ private void print (VirtualFrame frame , Object file , String data ) {
876
+ ensureCallMethodNode ().execute (frame , file , WRITE , data );
872
877
}
873
878
874
- private boolean isEmptyString (Object value ) {
875
- final String s = tryCastToString (value );
876
- return s != null && s .isEmpty ();
879
+ private void flush (VirtualFrame frame , Object file ) {
880
+ ensureCallMethodNode ().execute (frame , file , FLUSH );
877
881
}
878
882
879
883
private PFrame getFrame (VirtualFrame frame , PTraceback tb ) {
@@ -1052,9 +1056,11 @@ CharSequence getSourceLine(String fileName, int lineNo) {
1052
1056
1053
1057
void displaySourceLine (VirtualFrame frame , Object out , String fileName , int lineNo , int indent ) {
1054
1058
final CharSequence line = getSourceLine (fileName , lineNo );
1055
- print (frame , out , getIndent (indent ));
1056
- print (frame , out , PythonUtils .trimLeft (line ));
1057
- print (frame , out , "\n " );
1059
+ if (line != null ) {
1060
+ print (frame , out , getIndent (indent ));
1061
+ print (frame , out , PythonUtils .trimLeft (line ));
1062
+ print (frame , out , "\n " );
1063
+ }
1058
1064
}
1059
1065
1060
1066
void printInternal (VirtualFrame frame , Object out , PTraceback traceback , long limit ) {
@@ -1233,36 +1239,33 @@ void printException(VirtualFrame frame, Object out, Object excValue) {
1233
1239
}
1234
1240
}
1235
1241
1236
- {
1237
- // TODO: assert(PyExceptionClass_Check(type));
1238
- String className ;
1239
- try {
1240
- className = getName (type );
1241
- className = classNameNoDot (className );
1242
- } catch (PException pe ) {
1243
- className = null ;
1244
- }
1245
- String moduleName ;
1246
- Object v = lookupAttr (frame , type , __MODULE__ );
1247
- if (v == PNone .NO_VALUE || !PGuards .isString (v )) {
1248
- print (frame , out , VALUE_UNKNOWN );
1249
- } else {
1250
- moduleName = castToString (v );
1251
- if (!moduleName .equals (BUILTINS )) {
1252
- print (frame , out , moduleName );
1253
- print (frame , out , "." );
1254
- }
1255
- }
1256
- if (className == null ) {
1257
- print (frame , out , VALUE_UNKNOWN );
1258
- } else {
1259
- print (frame , out , className );
1242
+ String className ;
1243
+ try {
1244
+ className = getName (type );
1245
+ className = classNameNoDot (className );
1246
+ } catch (PException pe ) {
1247
+ className = null ;
1248
+ }
1249
+ String moduleName ;
1250
+ Object v = lookupAttr (frame , type , __MODULE__ );
1251
+ if (v == PNone .NO_VALUE || !PGuards .isString (v )) {
1252
+ print (frame , out , VALUE_UNKNOWN );
1253
+ } else {
1254
+ moduleName = castToString (v );
1255
+ if (!moduleName .equals (BUILTINS )) {
1256
+ print (frame , out , moduleName );
1257
+ print (frame , out , "." );
1260
1258
}
1261
1259
}
1260
+ if (className == null ) {
1261
+ print (frame , out , VALUE_UNKNOWN );
1262
+ } else {
1263
+ print (frame , out , className );
1264
+ }
1262
1265
1263
1266
if (value != PNone .NONE ) {
1264
1267
// only print colon if the str() of the object is not the empty string
1265
- Object v = str (frame , value );
1268
+ v = str (frame , value );
1266
1269
String s = tryCastToString (v );
1267
1270
if (v == null ) {
1268
1271
print (frame , out , ": <exception str() failed>" );
@@ -1303,8 +1306,17 @@ void printExceptionRecursive(VirtualFrame frame, Object out, Object value, Econo
1303
1306
}
1304
1307
1305
1308
@ Specialization
1306
- Object doIt (VirtualFrame frame , PythonModule sys , @ SuppressWarnings ("unused" ) Object excType , Object value , PTraceback traceBack ) {
1307
- if (PGuards .isPBaseException (value ) && traceBack != null ) {
1309
+ Object doWithoutTb (VirtualFrame frame , PythonModule sys , @ SuppressWarnings ("unused" ) Object excType , Object value , @ SuppressWarnings ("unused" ) PNone traceBack ) {
1310
+ Object stdErr = lookupAttr (frame , sys , STDERR );
1311
+ printExceptionRecursive (frame , stdErr , value , EconomicSet .create ());
1312
+ flush (frame , stdErr );
1313
+
1314
+ return PNone .NONE ;
1315
+ }
1316
+
1317
+ @ Specialization
1318
+ Object doWithTb (VirtualFrame frame , PythonModule sys , @ SuppressWarnings ("unused" ) Object excType , Object value , PTraceback traceBack ) {
1319
+ if (PGuards .isPBaseException (value )) {
1308
1320
final PBaseException exc = (PBaseException ) value ;
1309
1321
final PTraceback currTb = getExceptionTraceback (exc );
1310
1322
if (currTb == null ) {
@@ -1314,6 +1326,7 @@ Object doIt(VirtualFrame frame, PythonModule sys, @SuppressWarnings("unused") Ob
1314
1326
1315
1327
Object stdErr = lookupAttr (frame , sys , STDERR );
1316
1328
printExceptionRecursive (frame , stdErr , value , EconomicSet .create ());
1329
+ flush (frame , stdErr );
1317
1330
1318
1331
return PNone .NONE ;
1319
1332
}
0 commit comments