Skip to content

Commit 5b7f1e5

Browse files
committed
Fix handling interop exceptions in TopLevelExceptionHandler
1 parent ffedbc8 commit 5b7f1e5

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,15 +1213,15 @@ private int readEvalPrint(Context context, ConsoleHandler consoleHandler) {
12131213
if (e.isExit()) {
12141214
// usually from quit
12151215
throw new ExitException(e.getExitStatus());
1216-
} else if (e.isInternalError() || e.isHostException()) {
1216+
} else if (e.isInternalError()) {
12171217
/*
12181218
* The stacktrace should have been printed above by
12191219
* TopLevelExceptionHandler. We continue the repl even though the
12201220
* system may be broken
12211221
*/
12221222
System.err.println("An internal error occurred, continue at your own risk");
12231223
lastStatus = 1;
1224-
} else if (e.isGuestException()) {
1224+
} else {
12251225
// drop through to continue REPL and remember last eval was an error
12261226
lastStatus = 1;
12271227
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@
139139
import java.util.List;
140140
import java.util.Set;
141141

142-
import com.oracle.truffle.api.CompilerDirectives.ValueType;
143142
import org.graalvm.nativeimage.ImageInfo;
144143

145144
import com.oracle.graal.python.PythonLanguage;
@@ -171,7 +170,6 @@
171170
import com.oracle.graal.python.builtins.objects.dict.PDict;
172171
import com.oracle.graal.python.builtins.objects.exception.ExceptionNodes;
173172
import com.oracle.graal.python.builtins.objects.exception.GetEscapedExceptionNode;
174-
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
175173
import com.oracle.graal.python.builtins.objects.frame.PFrame;
176174
import com.oracle.graal.python.builtins.objects.frame.PFrame.Reference;
177175
import com.oracle.graal.python.builtins.objects.function.PArguments;
@@ -236,6 +234,7 @@
236234
import com.oracle.graal.python.util.PythonUtils;
237235
import com.oracle.truffle.api.CompilerDirectives;
238236
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
237+
import com.oracle.truffle.api.CompilerDirectives.ValueType;
239238
import com.oracle.truffle.api.Truffle;
240239
import com.oracle.truffle.api.TruffleLanguage.Env;
241240
import com.oracle.truffle.api.dsl.Bind;
@@ -1515,15 +1514,14 @@ void printExceptionRecursive(MaterializedFrame frame, PythonModule sys, Object o
15151514
protected void printException(MaterializedFrame frame, PythonModule sys, Object out, Object excValue) {
15161515
Object value = excValue;
15171516
final Object type = getObjectClass(value);
1518-
if (!PGuards.isPBaseException(value)) {
1517+
if (!PyExceptionInstanceCheckNode.executeUncached(value)) {
15191518
fileWriteString(frame, out, "TypeError: print_exception(): Exception expected for value, ");
15201519
fileWriteString(frame, out, getTypeName(type));
15211520
fileWriteString(frame, out, " found\n");
15221521
return;
15231522
}
15241523

1525-
final PBaseException exc = (PBaseException) value;
1526-
final Object tb = getExceptionTraceback(exc);
1524+
final Object tb = getExceptionTraceback(value);
15271525
if (tb instanceof PTraceback) {
15281526
printTraceBack(frame, sys, out, tb);
15291527
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/exception/TopLevelExceptionHandler.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import com.oracle.truffle.api.CompilerDirectives;
7676
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7777
import com.oracle.truffle.api.RootCallTarget;
78+
import com.oracle.truffle.api.exception.AbstractTruffleException;
7879
import com.oracle.truffle.api.frame.VirtualFrame;
7980
import com.oracle.truffle.api.interop.ExceptionType;
8081
import com.oracle.truffle.api.interop.InteropLibrary;
@@ -142,10 +143,9 @@ public Object execute(VirtualFrame frame) {
142143
assert pythonContext.getThreadState(lang).getCurrentException() == null;
143144
try {
144145
return run(frame);
145-
} catch (PException e) {
146+
} catch (AbstractTruffleException e) {
146147
assert !PArguments.isPythonFrame(frame);
147-
Object pythonException = e.getEscapedException();
148-
if (pythonException instanceof PBaseException managedException && getContext().isChildContext() && isSystemExit(managedException)) {
148+
if (e instanceof PException pe && pe.getEscapedException() instanceof PBaseException managedException && getContext().isChildContext() && isSystemExit(managedException)) {
149149
return handleChildContextExit(managedException);
150150
}
151151
throw handlePythonException(e);
@@ -171,8 +171,8 @@ private void checkInitialized() {
171171
}
172172

173173
@TruffleBoundary
174-
private PException handlePythonException(PException pException) {
175-
Object pythonException = pException.getEscapedException();
174+
private AbstractTruffleException handlePythonException(AbstractTruffleException e) {
175+
Object pythonException = e instanceof PException pe ? pe.getEscapedException() : e;
176176
if (pythonException instanceof PBaseException managedException && isSystemExit(managedException)) {
177177
handleSystemExit(managedException);
178178
}
@@ -186,8 +186,8 @@ private PException handlePythonException(PException pException) {
186186
sys.setAttribute(BuiltinNames.T_LAST_TRACEBACK, tb);
187187

188188
ExceptionUtils.printExceptionTraceback(getContext(), pythonException);
189-
if (PythonOptions.isPExceptionWithJavaStacktrace(getPythonLanguage())) {
190-
ExceptionUtils.printJavaStackTrace(pException);
189+
if (PythonOptions.isPExceptionWithJavaStacktrace(getPythonLanguage()) && e instanceof PException pe) {
190+
ExceptionUtils.printJavaStackTrace(pe);
191191
}
192192
if (!getSourceSection().getSource().isInteractive()) {
193193
if (getContext().isChildContext()) {
@@ -197,10 +197,10 @@ private PException handlePythonException(PException pException) {
197197
}
198198
}
199199
// Before we leave Python, format the message since outside the context
200-
if (pythonException instanceof PBaseException managedException) {
201-
pException.setMessage(managedException.getFormattedMessage());
200+
if (e instanceof PException pe && pythonException instanceof PBaseException managedException) {
201+
pe.setMessage(managedException.getFormattedMessage());
202202
}
203-
throw pException;
203+
throw e;
204204
}
205205

206206
private static boolean isSystemExit(PBaseException pythonException) {

0 commit comments

Comments
 (0)