Skip to content

Commit eef0ef0

Browse files
committed
Don't print stacktraces in GraalPythonMain
TopLevelExceptionHandler already does that and does it better
1 parent 66530d3 commit eef0ef0

File tree

1 file changed

+8
-50
lines changed

1 file changed

+8
-50
lines changed

graalpython/com.oracle.graal.python.shell/src/com/oracle/graal/python/shell/GraalPythonMain.java

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import java.util.HashMap;
4444
import java.util.Iterator;
4545
import java.util.List;
46-
import java.util.ListIterator;
4746
import java.util.Map;
4847
import java.util.Set;
4948
import java.util.UUID;
@@ -57,9 +56,7 @@
5756
import org.graalvm.polyglot.Context.Builder;
5857
import org.graalvm.polyglot.Engine;
5958
import org.graalvm.polyglot.PolyglotException;
60-
import org.graalvm.polyglot.PolyglotException.StackFrame;
6159
import org.graalvm.polyglot.Source;
62-
import org.graalvm.polyglot.SourceSection;
6360
import org.graalvm.polyglot.Value;
6461
import org.graalvm.shadowed.org.jline.reader.UserInterruptException;
6562

@@ -820,9 +817,7 @@ protected void launch(Builder contextBuilder) {
820817
evalNonInteractive(context, consoleHandler);
821818
rc = 0;
822819
} catch (PolyglotException e) {
823-
if (!e.isExit()) {
824-
printPythonLikeStackTrace(e);
825-
} else {
820+
if (e.isExit()) {
826821
rc = e.getExitStatus();
827822
}
828823
} catch (NoSuchFileException e) {
@@ -1007,41 +1002,6 @@ private static void printFileNotFoundException(NoSuchFileException e) {
10071002
System.err.println(GraalPythonMain.class.getCanonicalName() + ": can't open file '" + e.getFile() + "': " + reason);
10081003
}
10091004

1010-
private static void printPythonLikeStackTrace(PolyglotException e) {
1011-
// If we're running through the launcher and an exception escapes to here,
1012-
// we didn't go through the Python code to print it. That may be because
1013-
// it's an exception from another language. In this case, we still would
1014-
// like to print it like a Python exception.
1015-
ArrayList<String> stack = new ArrayList<>();
1016-
for (StackFrame frame : e.getPolyglotStackTrace()) {
1017-
if (frame.isGuestFrame()) {
1018-
StringBuilder sb = new StringBuilder();
1019-
SourceSection sourceSection = frame.getSourceLocation();
1020-
String rootName = frame.getRootName();
1021-
if (sourceSection != null) {
1022-
sb.append(" ");
1023-
String path = sourceSection.getSource().getPath();
1024-
if (path != null) {
1025-
sb.append("File ");
1026-
}
1027-
sb.append('"');
1028-
sb.append(sourceSection.getSource().getName());
1029-
sb.append("\", line ");
1030-
sb.append(sourceSection.getStartLine());
1031-
sb.append(", in ");
1032-
sb.append(rootName);
1033-
stack.add(sb.toString());
1034-
}
1035-
}
1036-
}
1037-
System.err.println("Traceback (most recent call last):");
1038-
ListIterator<String> listIterator = stack.listIterator(stack.size());
1039-
while (listIterator.hasPrevious()) {
1040-
System.err.println(listIterator.previous());
1041-
}
1042-
System.err.println(e.getMessage());
1043-
}
1044-
10451005
private void evalNonInteractive(Context context, ConsoleHandler consoleHandler) throws IOException {
10461006
// We need to setup the terminal even when not running the REPL because code may request
10471007
// input from the terminal.
@@ -1253,15 +1213,13 @@ private int readEvalPrint(Context context, ConsoleHandler consoleHandler) {
12531213
if (e.isExit()) {
12541214
// usually from quit
12551215
throw new ExitException(e.getExitStatus());
1256-
} else if (e.isHostException()) {
1257-
// we continue the repl even though the system may be broken
1258-
lastStatus = 1;
1259-
System.out.println(e.getMessage());
1260-
} else if (e.isInternalError()) {
1261-
System.err.println("An internal error occurred:");
1262-
printPythonLikeStackTrace(e);
1263-
1264-
// we continue the repl even though the system may be broken
1216+
} else if (e.isInternalError() || e.isHostException()) {
1217+
/*
1218+
* The stacktrace should have been printed above by
1219+
* TopLevelExceptionHandler. We continue the repl even though the
1220+
* system may be broken
1221+
*/
1222+
System.err.println("An internal error occurred, continue at your own risk");
12651223
lastStatus = 1;
12661224
} else if (e.isGuestException()) {
12671225
// drop through to continue REPL and remember last eval was an error

0 commit comments

Comments
 (0)