Skip to content

Commit 78c00d7

Browse files
committed
Fix TopLevelExceptionHandler
1 parent 8dfb076 commit 78c00d7

File tree

4 files changed

+58
-47
lines changed

4 files changed

+58
-47
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ private static void handleException(PythonContext context, PException e) {
120120
PythonObjectLibrary lib = PythonObjectLibrary.getUncached();
121121
if (!IsBuiltinClassProfile.profileClassSlowPath(lib.getLazyPythonClass(pythonException), PythonBuiltinClassType.SystemExit)) {
122122
lib.lookupAndCallRegularMethod(context.getCore().getStderr(), null, "write", "Error in atexit._run_exitfuncs:\n");
123-
ExceptionUtils.printExceptionTraceback(context, pythonException);
123+
try {
124+
ExceptionUtils.printExceptionTraceback(context, pythonException);
125+
} catch (PException pe) {
126+
lib.lookupAndCallRegularMethod(context.getCore().getStderr(), null, "write", "Failed to print traceback\n");
127+
}
124128
}
125129
}
126130
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/WriteUnraisableNode.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
*/
4141
package com.oracle.graal.python.nodes;
4242

43+
import java.io.IOException;
44+
4345
import com.oracle.graal.python.PythonLanguage;
4446
import com.oracle.graal.python.builtins.objects.PNone;
4547
import com.oracle.graal.python.builtins.objects.exception.GetExceptionTracebackNode;
@@ -94,21 +96,25 @@ static void writeUnraisable(VirtualFrame frame, PBaseException exception, String
9496
factory.createTuple(new Object[]{exceptionType, exception, traceback, messageObj, object != null ? object : PNone.NONE}));
9597
callUnraisableHook.execute(frame, unraisablehook, hookArguments);
9698
} catch (PException e) {
97-
ignoreException(message);
99+
ignoreException(contextRef.get(), message);
98100
}
99101
}
100102

101103
@TruffleBoundary
102-
private static void ignoreException(String message) {
103-
if (message != null) {
104-
System.err.println(formatMessage(message));
105-
} else {
106-
System.err.println("Exception ignored in sys.unraisablehook");
104+
private static void ignoreException(PythonContext context, String message) {
105+
try {
106+
if (message != null) {
107+
context.getEnv().err().write(formatMessage(message).getBytes());
108+
} else {
109+
context.getEnv().err().write("Exception ignored in sys.unraisablehook".getBytes());
110+
}
111+
} catch (IOException ioException) {
112+
ioException.printStackTrace();
107113
}
108114
}
109115

110116
@TruffleBoundary
111-
private static Object formatMessage(String message) {
117+
private static String formatMessage(String message) {
112118
return "Exception ignored " + message;
113119
}
114120

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@
4747
import com.oracle.graal.python.PythonLanguage;
4848
import com.oracle.graal.python.builtins.objects.PNone;
4949
import com.oracle.graal.python.builtins.objects.dict.PDict;
50+
import com.oracle.graal.python.builtins.objects.exception.GetExceptionTracebackNode;
5051
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
5152
import com.oracle.graal.python.builtins.objects.frame.PFrame;
5253
import com.oracle.graal.python.builtins.objects.function.PArguments;
5354
import com.oracle.graal.python.builtins.objects.module.PythonModule;
5455
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
56+
import com.oracle.graal.python.builtins.objects.traceback.PTraceback;
57+
import com.oracle.graal.python.nodes.BuiltinNames;
5558
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
5659
import com.oracle.graal.python.nodes.statement.ExceptionHandlingStatementNode;
5760
import com.oracle.graal.python.nodes.util.CannotCastException;
@@ -141,15 +144,25 @@ private PException handlePythonException(PBaseException pythonException) {
141144
if (IsBuiltinClassProfile.profileClassSlowPath(PythonObjectLibrary.getUncached().getLazyPythonClass(pythonException), SystemExit)) {
142145
handleSystemExit(pythonException);
143146
}
144-
ExceptionUtils.printExceptionTraceback(getContext(), pythonException);
145-
if (PythonOptions.isPExceptionWithJavaStacktrace(getPythonLanguage())) {
146-
ExceptionUtils.printJavaStackTrace(pythonException.getException());
147-
}
148-
if (getSourceSection().getSource().isInteractive()) {
149-
throw pythonException.getExceptionForReraise(pythonException.getTraceback());
150-
} else {
151-
throw new PythonExitException(this, 1);
147+
if (getContext().getOption(PythonOptions.AlwaysRunExcepthook)) {
148+
Object type = PythonObjectLibrary.getUncached().getLazyPythonClass(pythonException);
149+
PTraceback tracebackOrNull = GetExceptionTracebackNode.getUncached().execute(pythonException);
150+
Object tb = tracebackOrNull != null ? tracebackOrNull : PNone.NONE;
151+
152+
PythonModule sys = getContext().getCore().lookupBuiltinModule("sys");
153+
sys.setAttribute(BuiltinNames.LAST_TYPE, type);
154+
sys.setAttribute(BuiltinNames.LAST_VALUE, pythonException);
155+
sys.setAttribute(BuiltinNames.LAST_TRACEBACK, tb);
156+
157+
ExceptionUtils.printExceptionTraceback(getContext(), pythonException);
158+
if (PythonOptions.isPExceptionWithJavaStacktrace(getPythonLanguage())) {
159+
ExceptionUtils.printJavaStackTrace(pythonException.getException());
160+
}
161+
if (!getSourceSection().getSource().isInteractive()) {
162+
throw new PythonExitException(this, 1);
163+
}
152164
}
165+
throw pythonException.getExceptionForReraise(pythonException.getTraceback());
153166
}
154167

155168
@TruffleBoundary

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/exception/ExceptionUtils.java

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,12 @@
4949
import com.oracle.graal.python.builtins.objects.exception.GetExceptionTracebackNode;
5050
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
5151
import com.oracle.graal.python.builtins.objects.function.PKeyword;
52-
import com.oracle.graal.python.builtins.objects.module.PythonModule;
5352
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
5453
import com.oracle.graal.python.builtins.objects.traceback.LazyTraceback;
5554
import com.oracle.graal.python.builtins.objects.traceback.PTraceback;
5655
import com.oracle.graal.python.nodes.BuiltinNames;
5756
import com.oracle.graal.python.nodes.call.CallNode;
5857
import com.oracle.graal.python.runtime.PythonContext;
59-
import com.oracle.graal.python.runtime.PythonCore;
60-
import com.oracle.graal.python.runtime.PythonOptions;
6158
import com.oracle.truffle.api.CompilerAsserts;
6259
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6360
import com.oracle.truffle.api.RootCallTarget;
@@ -149,36 +146,27 @@ private static void printStack(final ArrayList<String> stack) {
149146
*/
150147
@TruffleBoundary
151148
public static void printExceptionTraceback(PythonContext context, PBaseException pythonException) {
152-
PythonCore core = context.getCore();
153-
154149
Object type = PythonObjectLibrary.getUncached().getLazyPythonClass(pythonException);
155-
PTraceback execute = GetExceptionTracebackNode.getUncached().execute(pythonException);
156-
Object tb = execute != null ? execute : PNone.NONE;
157-
158-
PythonModule sys = core.lookupBuiltinModule("sys");
159-
sys.setAttribute(BuiltinNames.LAST_TYPE, type);
160-
sys.setAttribute(BuiltinNames.LAST_VALUE, pythonException);
161-
sys.setAttribute(BuiltinNames.LAST_TRACEBACK, tb);
150+
PTraceback tracebackOrNull = GetExceptionTracebackNode.getUncached().execute(pythonException);
151+
Object tb = tracebackOrNull != null ? tracebackOrNull : PNone.NONE;
162152

163-
Object hook = sys.getAttribute(BuiltinNames.EXCEPTHOOK);
164-
if (context.getOption(PythonOptions.AlwaysRunExcepthook)) {
165-
if (hook != PNone.NO_VALUE) {
166-
try {
167-
// Note: it is important to pass frame 'null' because that will cause the
168-
// CallNode to tread the invoke like a foreign call and access the top frame ref
169-
// in the context.
170-
CallNode.getUncached().execute(null, hook, new Object[]{type, pythonException, tb}, PKeyword.EMPTY_KEYWORDS);
171-
} catch (PException internalError) {
172-
// More complex handling of errors in exception printing is done in our
173-
// Python code, if we get here, we just fall back to the launcher
174-
throw pythonException.getExceptionForReraise(pythonException.getTraceback());
175-
}
176-
} else {
177-
try {
178-
context.getEnv().err().write("sys.excepthook is missing\n".getBytes());
179-
} catch (IOException ioException) {
180-
ioException.printStackTrace();
181-
}
153+
Object hook = context.getCore().lookupBuiltinModule("sys").getAttribute(BuiltinNames.EXCEPTHOOK);
154+
if (hook != PNone.NO_VALUE) {
155+
try {
156+
// Note: it is important to pass frame 'null' because that will cause the
157+
// CallNode to tread the invoke like a foreign call and access the top frame ref
158+
// in the context.
159+
CallNode.getUncached().execute(null, hook, new Object[]{type, pythonException, tb}, PKeyword.EMPTY_KEYWORDS);
160+
} catch (PException internalError) {
161+
// More complex handling of errors in exception printing is done in our
162+
// Python code, if we get here, we just fall back to the launcher
163+
throw pythonException.getExceptionForReraise(pythonException.getTraceback());
164+
}
165+
} else {
166+
try {
167+
context.getEnv().err().write("sys.excepthook is missing\n".getBytes());
168+
} catch (IOException ioException) {
169+
ioException.printStackTrace();
182170
}
183171
}
184172
}

0 commit comments

Comments
 (0)