Skip to content

Commit 53180fe

Browse files
committed
Rethrow internal errors during PythonLanguage#initializeContext
* Such as AssertionError, etc. * Ensure to not print Python exceptions twice by always throwing an exit exception after printing stacktraces with AlwaysRunExcepthook.
1 parent 2aa7ac9 commit 53180fe

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2024, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2025, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -825,6 +825,8 @@ protected void launch(Builder contextBuilder) {
825825
} catch (PolyglotException e) {
826826
if (e.isExit()) {
827827
rc = e.getExitStatus();
828+
} else {
829+
throw e;
828830
}
829831
} catch (NoSuchFileException e) {
830832
printFileNotFoundException(e);

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

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,7 @@ private AbstractTruffleException handlePythonException(AbstractTruffleException
195195
if (PythonOptions.isPExceptionWithJavaStacktrace(getPythonLanguage()) && e instanceof PException pe) {
196196
ExceptionUtils.printJavaStackTrace(pe);
197197
}
198-
if (!getSourceSection().getSource().isInteractive()) {
199-
if (getContext().isChildContext()) {
200-
getContext().getChildContextData().setExitCode(1);
201-
}
202-
throw new PythonExitException(this, 1);
203-
}
198+
exit(1);
204199
}
205200
// Before we leave Python, format the message since outside the context
206201
if (e instanceof PException pe) {
@@ -209,6 +204,15 @@ private AbstractTruffleException handlePythonException(AbstractTruffleException
209204
throw e;
210205
}
211206

207+
private void exit(int exitCode) {
208+
if (!getSourceSection().getSource().isInteractive()) {
209+
if (getContext().isChildContext()) {
210+
getContext().getChildContextData().setExitCode(1);
211+
}
212+
throw new PythonExitException(this, exitCode);
213+
}
214+
}
215+
212216
private static boolean isSystemExit(PBaseException pythonException) {
213217
return IsBuiltinClassProfile.profileClassSlowPath(GetPythonObjectClassNode.executeUncached(pythonException), SystemExit);
214218
}
@@ -227,6 +231,7 @@ private void handleJavaException(Throwable e) {
227231
if (PythonOptions.shouldPrintJavaStacktrace(getPythonLanguage(), e)) {
228232
e.printStackTrace();
229233
}
234+
exit(1);
230235
}
231236
} catch (UnsupportedMessageException unsupportedMessageException) {
232237
throw CompilerDirectives.shouldNotReachHere();
@@ -250,12 +255,12 @@ private void handleSystemExit(PBaseException pythonException) {
250255
int exitcode = getExitCode(pythonException);
251256
throw new PythonExitException(this, exitcode);
252257
} catch (CannotCastException e) {
253-
// fall through
254-
}
255-
if (handleAlwaysRunExceptHook(theContext, pythonException)) {
256-
throw new PythonExitException(this, 1);
258+
if (handleAlwaysRunExceptHook(theContext, pythonException)) {
259+
throw new PythonExitException(this, 1);
260+
} else {
261+
throw pythonException.getExceptionForReraise(pythonException.getTraceback());
262+
}
257263
}
258-
throw pythonException.getExceptionForReraise(pythonException.getTraceback());
259264
}
260265

261266
@TruffleBoundary
@@ -264,12 +269,12 @@ private Object handleChildContextExit(PBaseException pythonException) throws PEx
264269
try {
265270
return getExitCode(pythonException);
266271
} catch (CannotCastException cce) {
267-
// fall through
268-
}
269-
if (handleAlwaysRunExceptHook(getContext(), pythonException)) {
270-
return 1;
272+
if (handleAlwaysRunExceptHook(getContext(), pythonException)) {
273+
return 1;
274+
} else {
275+
throw pythonException.getExceptionForReraise(pythonException.getTraceback());
276+
}
271277
}
272-
throw pythonException.getExceptionForReraise(pythonException.getTraceback());
273278
}
274279

275280
private static int getExitCode(PBaseException pythonException) throws CannotCastException {

0 commit comments

Comments
 (0)