Skip to content

Commit a9810f4

Browse files
committed
sys.excepthook - call write method of file like object for printing to Stdout, fix null line exception
1 parent 20e5683 commit a9810f4

File tree

2 files changed

+53
-44
lines changed

2 files changed

+53
-44
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SysModuleBuiltins.java

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242

4343
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
4444
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;
4547
import static com.oracle.graal.python.nodes.BuiltinNames.BUILTINS;
4648
import static com.oracle.graal.python.nodes.SpecialAttributeNames.STDERR;
4749
import static com.oracle.graal.python.nodes.SpecialAttributeNames.STDIN;
@@ -79,7 +81,6 @@
7981
import com.oracle.graal.python.builtins.modules.io.PBuffered;
8082
import com.oracle.graal.python.builtins.modules.io.PFileIO;
8183
import com.oracle.graal.python.builtins.modules.io.PTextIO;
82-
import com.oracle.graal.python.builtins.modules.io.StringIOBuiltins;
8384
import com.oracle.graal.python.builtins.modules.io.TextIOWrapperNodes.TextIOWrapperInitNode;
8485
import com.oracle.graal.python.builtins.modules.io.TextIOWrapperNodesFactory.TextIOWrapperInitNodeGen;
8586
import com.oracle.graal.python.builtins.objects.PNone;
@@ -844,7 +845,7 @@ abstract static class ExceptHookNode extends PythonBuiltinNode {
844845
@Child private CastToJavaStringNode castToJavaStringNode;
845846
@Child private PyObjectStrAsObjectNode strAsObjNode;
846847
@Child private TracebackBuiltins.GetTracebackFrameNode getTracebackFrameNode;
847-
@Child private StringIOBuiltins.WriteNode writeNode;
848+
@Child private PyObjectCallMethodObjArgs callMethod;
848849

849850
@CompilerDirectives.ValueType
850851
static final class SyntaxErrData {
@@ -863,17 +864,20 @@ static final class SyntaxErrData {
863864
}
864865
}
865866

866-
private void print(VirtualFrame frame, Object file, String data) {
867-
if (writeNode == null) {
867+
private PyObjectCallMethodObjArgs ensureCallMethodNode() {
868+
if (callMethod == null) {
868869
CompilerDirectives.transferToInterpreterAndInvalidate();
869-
writeNode = insert(StringIOBuiltins.WriteNode.create());
870+
callMethod = insert(PyObjectCallMethodObjArgs.create());
870871
}
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);
872877
}
873878

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);
877881
}
878882

879883
private PFrame getFrame(VirtualFrame frame, PTraceback tb) {
@@ -1052,9 +1056,11 @@ CharSequence getSourceLine(String fileName, int lineNo) {
10521056

10531057
void displaySourceLine(VirtualFrame frame, Object out, String fileName, int lineNo, int indent) {
10541058
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+
}
10581064
}
10591065

10601066
void printInternal(VirtualFrame frame, Object out, PTraceback traceback, long limit) {
@@ -1233,36 +1239,33 @@ void printException(VirtualFrame frame, Object out, Object excValue) {
12331239
}
12341240
}
12351241

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, ".");
12601258
}
12611259
}
1260+
if (className == null) {
1261+
print(frame, out, VALUE_UNKNOWN);
1262+
} else {
1263+
print(frame, out, className);
1264+
}
12621265

12631266
if (value != PNone.NONE) {
12641267
// 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);
12661269
String s = tryCastToString(v);
12671270
if (v == null) {
12681271
print(frame, out, ": <exception str() failed>");
@@ -1303,8 +1306,17 @@ void printExceptionRecursive(VirtualFrame frame, Object out, Object value, Econo
13031306
}
13041307

13051308
@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)) {
13081320
final PBaseException exc = (PBaseException) value;
13091321
final PTraceback currTb = getExceptionTraceback(exc);
13101322
if (currTb == null) {
@@ -1314,6 +1326,7 @@ Object doIt(VirtualFrame frame, PythonModule sys, @SuppressWarnings("unused") Ob
13141326

13151327
Object stdErr = lookupAttr(frame, sys, STDERR);
13161328
printExceptionRecursive(frame, stdErr, value, EconomicSet.create());
1329+
flush(frame, stdErr);
13171330

13181331
return PNone.NONE;
13191332
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/StringIOBuiltins.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ Object negSize(@SuppressWarnings("unused") PStringIO self, int size) {
440440
@Builtin(name = WRITE, minNumOfPositionalArgs = 2, parameterNames = {"self", "s"})
441441
@ArgumentClinic(name = "s", conversion = ArgumentClinic.ClinicConversion.String)
442442
@GenerateNodeFactory
443-
public abstract static class WriteNode extends ClosedCheckPythonBinaryClinicBuiltinNode {
443+
abstract static class WriteNode extends ClosedCheckPythonBinaryClinicBuiltinNode {
444444

445445
@Override
446446
protected ArgumentClinicProvider getArgumentClinic() {
@@ -456,10 +456,6 @@ Object doWrite(VirtualFrame frame, PStringIO self, String s,
456456
}
457457
return size;
458458
}
459-
460-
public static WriteNode create() {
461-
return StringIOBuiltinsFactory.WriteNodeFactory.create();
462-
}
463459
}
464460

465461
@Builtin(name = TELL, minNumOfPositionalArgs = 1)

0 commit comments

Comments
 (0)