|
49 | 49 | import com.oracle.graal.python.builtins.objects.exception.GetExceptionTracebackNode;
|
50 | 50 | import com.oracle.graal.python.builtins.objects.exception.PBaseException;
|
51 | 51 | import com.oracle.graal.python.builtins.objects.function.PKeyword;
|
52 |
| -import com.oracle.graal.python.builtins.objects.module.PythonModule; |
53 | 52 | import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
|
54 | 53 | import com.oracle.graal.python.builtins.objects.traceback.LazyTraceback;
|
55 | 54 | import com.oracle.graal.python.builtins.objects.traceback.PTraceback;
|
56 | 55 | import com.oracle.graal.python.nodes.BuiltinNames;
|
57 | 56 | import com.oracle.graal.python.nodes.call.CallNode;
|
58 | 57 | import com.oracle.graal.python.runtime.PythonContext;
|
59 |
| -import com.oracle.graal.python.runtime.PythonCore; |
60 |
| -import com.oracle.graal.python.runtime.PythonOptions; |
61 | 58 | import com.oracle.truffle.api.CompilerAsserts;
|
62 | 59 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
63 | 60 | import com.oracle.truffle.api.RootCallTarget;
|
@@ -149,36 +146,27 @@ private static void printStack(final ArrayList<String> stack) {
|
149 | 146 | */
|
150 | 147 | @TruffleBoundary
|
151 | 148 | public static void printExceptionTraceback(PythonContext context, PBaseException pythonException) {
|
152 |
| - PythonCore core = context.getCore(); |
153 |
| - |
154 | 149 | 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; |
162 | 152 |
|
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(); |
182 | 170 | }
|
183 | 171 | }
|
184 | 172 | }
|
|
0 commit comments