Skip to content

Commit 7f8097f

Browse files
committed
Make native exception tracebacks work
1 parent 7b9d7f7 commit 7f8097f

File tree

17 files changed

+174
-159
lines changed

17 files changed

+174
-159
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_err.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -692,10 +692,7 @@ def test_raise_instance(self):
692692

693693
def test_traceback(self):
694694
try:
695-
try:
696-
raise_native_exception()
697-
finally:
698-
assert True # no-op, just to test implicit reraising
695+
raise_native_exception()
699696
except ExceptionSubclass as e:
700697
tb = e.__traceback__
701698
else:
@@ -713,6 +710,22 @@ def test_traceback(self):
713710
e2 = e2.with_traceback(tb)
714711
assert e2.__traceback__ is tb
715712

713+
def test_traceback_reraise(self):
714+
try:
715+
try:
716+
raise_native_exception()
717+
except Exception as e1:
718+
e1.__traceback__ = None
719+
raise
720+
except ExceptionSubclass as e:
721+
tb = e.__traceback__
722+
else:
723+
assert False
724+
assert tb
725+
assert tb.tb_frame.f_code is TestNativeExceptionSubclass.test_traceback_reraise.__code__
726+
assert tb.tb_next
727+
assert tb.tb_next.tb_frame.f_code is raise_native_exception.__code__
728+
716729
def test_chaining(self):
717730
inner_e = ExceptionSubclass()
718731
outer_e = ExceptionSubclass()
@@ -802,4 +815,3 @@ def gen():
802815
e = ExceptionSubclass()
803816
e1 = g.throw(e)
804817
assert e1 is e
805-

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ static RuntimeException doRaise(TruffleEncoder encoder, Object inputObject,
212212
int end = start + encoder.getErrorLength();
213213
Object exception = callNode.execute(UnicodeEncodeError, encoder.getEncodingName(), inputObject, start, end, encoder.getErrorReason());
214214
if (exception instanceof PBaseException) {
215-
throw raiseNode.raiseExceptionObject((PBaseException) exception);
215+
throw raiseNode.raiseExceptionObject(exception);
216216
} else {
217217
// Shouldn't happen unless the user manually replaces the method, which is really
218218
// unexpected and shouldn't be permitted at all, but currently it is
@@ -373,7 +373,7 @@ static Object doRaise(TruffleDecoder decoder, Object inputObject, boolean justMa
373373
return exception;
374374
}
375375
if (exception instanceof PBaseException) {
376-
throw raiseNode.raiseExceptionObject((PBaseException) exception);
376+
throw raiseNode.raiseExceptionObject(exception);
377377
} else {
378378
// Shouldn't happen unless the user manually replaces the method, which is really
379379
// unexpected and shouldn't be permitted at all, but currently it is

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@
8282
import com.oracle.graal.python.builtins.objects.code.PCode;
8383
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
8484
import com.oracle.graal.python.builtins.objects.dict.PDict;
85-
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
8685
import com.oracle.graal.python.builtins.objects.frame.PFrame;
8786
import com.oracle.graal.python.builtins.objects.list.PList;
8887
import com.oracle.graal.python.builtins.objects.module.PythonModule;
8988
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
9089
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
9190
import com.oracle.graal.python.lib.PyCallableCheckNode;
9291
import com.oracle.graal.python.lib.PyDictGetItem;
92+
import com.oracle.graal.python.lib.PyExceptionInstanceCheckNode;
9393
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
9494
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
9595
import com.oracle.graal.python.lib.PyObjectIsTrueNode;
@@ -118,7 +118,6 @@
118118
import com.oracle.graal.python.nodes.util.CastToTruffleStringNode;
119119
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
120120
import com.oracle.graal.python.runtime.PythonContext;
121-
import com.oracle.graal.python.runtime.PythonOptions;
122121
import com.oracle.graal.python.runtime.exception.PException;
123122
import com.oracle.graal.python.runtime.formatting.ErrorMessageFormatter;
124123
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
@@ -851,11 +850,11 @@ private static void warnExplicitPart2(PythonContext context, Node node, PythonMo
851850
Object category, Object message, Object text, Object key, Object item, TruffleString action) {
852851

853852
if (action.equalsUncached(T_ERROR, TS_ENCODING)) {
854-
if (!(message instanceof PBaseException)) {
853+
if (!PyExceptionInstanceCheckNode.executeUncached(message)) {
855854
throw PRaiseNode.raiseUncached(node, PythonBuiltinClassType.SystemError, ErrorMessages.EXCEPTION_NOT_BASEEXCEPTION,
856855
PyObjectReprAsTruffleStringNode.getUncached().execute(null, message));
857856
} else {
858-
throw PRaiseNode.raise(node, (PBaseException) message, PythonOptions.isPExceptionWithJavaStacktrace(PythonLanguage.get(node)));
857+
throw PRaiseNode.raiseExceptionObject(node, message);
859858
}
860859
}
861860

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextErrBuiltins.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,7 @@ Object create(Object type, Object value,
269269
@SuppressWarnings("unused") @Cached IsSubClassNode isSubClassNode,
270270
@Cached PrepareExceptionNode prepareExceptionNode) {
271271
Object exception = prepareExceptionNode.execute(null, type, value);
272-
if (exception instanceof PBaseException managedException) {
273-
managedException.ensureReified();
274-
}
275-
throw PRaiseNode.raiseNoReify(this, exception, PythonOptions.isPExceptionWithJavaStacktrace(PythonLanguage.get(this)));
272+
throw PRaiseNode.raiseExceptionObject(this, exception);
276273
}
277274

278275
protected static boolean isExceptionClass(Object obj, IsTypeNode isTypeNode, IsSubClassNode isSubClassNode) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/AbstractBufferedIOBuiltins.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import static com.oracle.graal.python.nodes.ErrorMessages.IO_UNINIT;
4848
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
4949

50-
import com.oracle.graal.python.PythonLanguage;
5150
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
5251
import com.oracle.graal.python.builtins.PythonBuiltins;
5352
import com.oracle.graal.python.builtins.modules.io.BufferedIONodes.RawTellNode;
@@ -66,7 +65,6 @@
6665
import com.oracle.graal.python.nodes.object.InlinedGetClassNode.GetPythonObjectClassNode;
6766
import com.oracle.graal.python.runtime.PosixSupportLibrary;
6867
import com.oracle.graal.python.runtime.PosixSupportLibrary.PosixException;
69-
import com.oracle.graal.python.runtime.PythonOptions;
7068
import com.oracle.graal.python.runtime.exception.PException;
7169
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
7270
import com.oracle.truffle.api.CompilerDirectives;
@@ -227,7 +225,7 @@ static PException raise(Node node, Object errno, TruffleString message, int writ
227225
attrs[OsErrorBuiltins.IDX_STRERROR] = message;
228226
attrs[OsErrorBuiltins.IDX_WRITTEN] = written;
229227
exception.setExceptionAttributes(attrs);
230-
return PRaiseNode.raise(node, exception, PythonOptions.isPExceptionWithJavaStacktrace(PythonLanguage.get(node)));
228+
return PRaiseNode.raiseExceptionObject(node, exception);
231229
}
232230
}
233231

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/json/JSONScannerBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,12 +540,12 @@ private static RuntimeException decodeError(Node raisingNode, String jsonString,
540540
Object module = AbstractImportNode.importModule(toTruffleStringUncached("json.decoder"));
541541
Object errorClass = PyObjectLookupAttr.getUncached().execute(null, module, T_JSON_DECODE_ERROR);
542542
Object exception = CallNode.getUncached().execute(errorClass, format, toTruffleStringUncached(jsonString), pos);
543-
throw PRaiseNode.raise(raisingNode, (PBaseException) exception, false);
543+
throw PRaiseNode.raiseExceptionObject(raisingNode, exception, false);
544544
}
545545

546546
private static RuntimeException stopIteration(Node raisingNode, Object value) {
547547
CompilerAsserts.neverPartOfCompilation();
548548
Object exception = CallNode.getUncached().execute(PythonContext.get(raisingNode).lookupType(PythonBuiltinClassType.StopIteration), value);
549-
throw PRaiseNode.raise(raisingNode, (PBaseException) exception, false);
549+
throw PRaiseNode.raiseExceptionObject(raisingNode, exception, false);
550550
}
551551
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/ExternalFunctionNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2145,7 +2145,7 @@ private static PException raiseNullButNoError(Node node, TruffleString name, Tru
21452145
private static PException raiseResultWithError(PythonLanguage language, Node node, TruffleString name, PException currentException, TruffleString resultWithErrorMessage) {
21462146
PBaseException sysExc = PythonObjectFactory.getUncached().createBaseException(PythonErrorType.SystemError, resultWithErrorMessage, new Object[]{name});
21472147
sysExc.setCause(currentException.getEscapedException());
2148-
throw PRaiseNode.raise(node, sysExc, PythonOptions.isPExceptionWithJavaStacktrace(language));
2148+
throw PRaiseNode.raiseExceptionObject(node, sysExc, PythonOptions.isPExceptionWithJavaStacktrace(language));
21492149
}
21502150

21512151
protected static boolean isNativeNull(Object object) {

0 commit comments

Comments
 (0)