Skip to content

Commit fc3a29a

Browse files
committed
[GR-24283] Also print Java stack trace for Python exceptions if requested.
PullRequest: graalpython/1052
2 parents 9e205b1 + 64cc4b9 commit fc3a29a

File tree

18 files changed

+271
-125
lines changed

18 files changed

+271
-125
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
import com.oracle.graal.python.runtime.PythonCodeSerializer;
182182
import com.oracle.graal.python.runtime.PythonContext;
183183
import com.oracle.graal.python.runtime.PythonCore;
184+
import com.oracle.graal.python.runtime.PythonOptions;
184185
import com.oracle.graal.python.runtime.PythonParser;
185186
import com.oracle.graal.python.runtime.PythonParser.ParserMode;
186187
import com.oracle.graal.python.runtime.exception.PException;
@@ -559,7 +560,7 @@ public PException raise(PythonBuiltinClassType type, String format, Object... ar
559560
} else {
560561
instance = objectFactory.createBaseException(type);
561562
}
562-
throw PException.fromObject(instance, null);
563+
throw PException.fromObject(instance, null, PythonOptions.isPExceptionWithJavaStacktrace(getLanguage()));
563564
}
564565

565566
private void publishBuiltinModules() {
@@ -794,6 +795,6 @@ public RuntimeException raiseInvalidSyntax(PythonParser.ErrorType type, Node loc
794795
}
795796
}
796797
instance.setAttribute("msg", msg);
797-
throw PException.fromObject(instance, location);
798+
throw PException.fromObject(instance, location, PythonOptions.isPExceptionWithJavaStacktrace(getLanguage()));
798799
}
799800
}

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

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

43+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
4344
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.ValueError;
4445
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SystemError;
4546

@@ -50,7 +51,6 @@
5051
import com.oracle.graal.python.builtins.Builtin;
5152
import com.oracle.graal.python.builtins.CoreFunctions;
5253
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
53-
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
5454
import com.oracle.graal.python.builtins.PythonBuiltins;
5555
import com.oracle.graal.python.builtins.objects.PNone;
5656
import com.oracle.graal.python.builtins.objects.array.PArray;
@@ -66,10 +66,13 @@
6666
import com.oracle.graal.python.builtins.objects.type.PythonAbstractClass;
6767
import com.oracle.graal.python.nodes.ErrorMessages;
6868
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
69+
import com.oracle.graal.python.nodes.call.CallNode;
6970
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
7071
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
7172
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
7273
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
74+
import com.oracle.graal.python.nodes.statement.RaiseNode;
75+
import com.oracle.graal.python.nodes.statement.RaiseNodeGen;
7376
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
7477
import com.oracle.graal.python.runtime.PythonCore;
7578
import com.oracle.graal.python.runtime.exception.PException;
@@ -140,7 +143,9 @@ private byte[] b64decode(byte[] data) {
140143
@Builtin(name = "a2b_hex", minNumOfPositionalArgs = 2, declaresExplicitSelf = true)
141144
@GenerateNodeFactory
142145
abstract static class A2bHexNode extends PythonBinaryBuiltinNode {
143-
private ReadAttributeFromObjectNode readAttrNode;
146+
@Child private ReadAttributeFromObjectNode readAttrNode;
147+
@Child private RaiseNode raiseNode;
148+
@Child private CallNode callExceptionConstructor;
144149

145150
@Specialization
146151
@TruffleBoundary
@@ -204,11 +209,15 @@ private int digitValue(PythonModule self, char b) {
204209
}
205210

206211
private PException oddLengthError(PythonModule self) {
207-
return raise(getAttrNode().execute(self, ERROR), ErrorMessages.ODD_LENGTH_STRING);
212+
raiseObject(getAttrNode().execute(self, ERROR), ErrorMessages.ODD_LENGTH_STRING);
213+
CompilerDirectives.transferToInterpreterAndInvalidate();
214+
throw new IllegalStateException("should not be reached");
208215
}
209216

210217
private PException nonHexError(PythonModule self) {
211-
return raise(getAttrNode().execute(self, ERROR), ErrorMessages.NON_HEX_DIGIT_FOUND);
218+
raiseObject(getAttrNode().execute(self, ERROR), ErrorMessages.NON_HEX_DIGIT_FOUND);
219+
CompilerDirectives.transferToInterpreterAndInvalidate();
220+
throw new IllegalStateException("should not be reached");
212221
}
213222

214223
private ReadAttributeFromObjectNode getAttrNode() {
@@ -218,6 +227,18 @@ private ReadAttributeFromObjectNode getAttrNode() {
218227
}
219228
return readAttrNode;
220229
}
230+
231+
private void raiseObject(Object exceptionObject, String message) {
232+
if (callExceptionConstructor == null) {
233+
CompilerDirectives.transferToInterpreterAndInvalidate();
234+
callExceptionConstructor = insert(CallNode.create());
235+
}
236+
if (raiseNode == null) {
237+
CompilerDirectives.transferToInterpreterAndInvalidate();
238+
raiseNode = insert(RaiseNodeGen.create(null, null));
239+
}
240+
raiseNode.execute(callExceptionConstructor.execute(exceptionObject, message), PNone.NO_VALUE);
241+
}
221242
}
222243

223244
@Builtin(name = "b2a_base64", minNumOfPositionalArgs = 1, parameterNames = {"data", "newline"})

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

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -587,14 +587,18 @@ Object run(PNone typ, PNone val, PNone tb) {
587587
}
588588

589589
@Specialization
590-
Object run(@SuppressWarnings("unused") Object typ, PBaseException val, @SuppressWarnings("unused") PNone tb) {
591-
getContext().setCurrentException(PException.fromExceptionInfo(val, (LazyTraceback) null));
590+
Object run(@SuppressWarnings("unused") Object typ, PBaseException val, @SuppressWarnings("unused") PNone tb,
591+
@Shared("language") @CachedLanguage PythonLanguage language) {
592+
PythonContext context = getContext();
593+
context.setCurrentException(PException.fromExceptionInfo(val, (LazyTraceback) null, PythonOptions.isPExceptionWithJavaStacktrace(language)));
592594
return PNone.NONE;
593595
}
594596

595597
@Specialization
596-
Object run(@SuppressWarnings("unused") Object typ, PBaseException val, PTraceback tb) {
597-
getContext().setCurrentException(PException.fromExceptionInfo(val, tb));
598+
Object run(@SuppressWarnings("unused") Object typ, PBaseException val, PTraceback tb,
599+
@Shared("language") @CachedLanguage PythonLanguage language) {
600+
PythonContext context = getContext();
601+
context.setCurrentException(PException.fromExceptionInfo(val, tb, PythonOptions.isPExceptionWithJavaStacktrace(language)));
598602
return PNone.NONE;
599603
}
600604
}
@@ -654,14 +658,17 @@ Object doClear(PNone typ, PNone val, PNone tb) {
654658
}
655659

656660
@Specialization
657-
Object doFull(@SuppressWarnings("unused") Object typ, PBaseException val, PTraceback tb) {
658-
getContext().setCaughtException(PException.fromExceptionInfo(val, tb));
661+
Object doFull(@SuppressWarnings("unused") Object typ, PBaseException val, PTraceback tb,
662+
@Shared("language") @CachedLanguage PythonLanguage language) {
663+
PythonContext context = getContext();
664+
context.setCaughtException(PException.fromExceptionInfo(val, tb, PythonOptions.isPExceptionWithJavaStacktrace(language)));
659665
return PNone.NONE;
660666
}
661667

662668
@Specialization
663-
Object doWithoutTraceback(@SuppressWarnings("unused") Object typ, PBaseException val, @SuppressWarnings("unused") PNone tb) {
664-
return doFull(typ, val, null);
669+
Object doWithoutTraceback(@SuppressWarnings("unused") Object typ, PBaseException val, @SuppressWarnings("unused") PNone tb,
670+
@Shared("language") @CachedLanguage PythonLanguage language) {
671+
return doFull(typ, val, null, language);
665672
}
666673

667674
@Fallback
@@ -864,59 +871,65 @@ Object doNativeWrapper(String name, DynamicObjectNativeWrapper.PythonObjectNativ
864871

865872
@Specialization(guards = "!isPythonObjectNativeWrapper(result)")
866873
Object doPrimitiveWrapper(String name, @SuppressWarnings("unused") PythonNativeWrapper result,
874+
@Shared("language") @CachedLanguage PythonLanguage language,
867875
@Shared("ctxt") @CachedContext(PythonLanguage.class) PythonContext context,
868876
@Shared("fact") @Cached PythonObjectFactory factory,
869877
@Shared("raise") @Cached PRaiseNode raise) {
870-
checkFunctionResult(name, false, false, context, raise, factory);
878+
checkFunctionResult(name, false, false, language, context, raise, factory);
871879
return result;
872880
}
873881

874882
@Specialization(guards = "isNoValue(result)")
875883
Object doNoValue(String name, @SuppressWarnings("unused") PNone result,
884+
@Shared("language") @CachedLanguage PythonLanguage language,
876885
@Shared("ctxt") @CachedContext(PythonLanguage.class) PythonContext context,
877886
@Shared("fact") @Cached PythonObjectFactory factory,
878887
@Shared("raise") @Cached PRaiseNode raise) {
879-
checkFunctionResult(name, true, false, context, raise, factory);
888+
checkFunctionResult(name, true, false, language, context, raise, factory);
880889
return PNone.NO_VALUE;
881890
}
882891

883892
@Specialization(guards = "!isNoValue(result)")
884893
Object doPythonObject(String name, @SuppressWarnings("unused") PythonAbstractObject result,
894+
@Shared("language") @CachedLanguage PythonLanguage language,
885895
@Shared("ctxt") @CachedContext(PythonLanguage.class) PythonContext context,
886896
@Shared("fact") @Cached PythonObjectFactory factory,
887897
@Shared("raise") @Cached PRaiseNode raise) {
888-
checkFunctionResult(name, false, false, context, raise, factory);
898+
checkFunctionResult(name, false, false, language, context, raise, factory);
889899
return result;
890900
}
891901

892902
@Specialization
893903
Object doPythonNativeNull(String name, @SuppressWarnings("unused") PythonNativeNull result,
904+
@Shared("language") @CachedLanguage PythonLanguage language,
894905
@Shared("ctxt") @CachedContext(PythonLanguage.class) PythonContext context,
895906
@Shared("fact") @Cached PythonObjectFactory factory,
896907
@Shared("raise") @Cached PRaiseNode raise) {
897-
checkFunctionResult(name, true, false, context, raise, factory);
908+
checkFunctionResult(name, true, false, language, context, raise, factory);
898909
return result;
899910
}
900911

901912
@Specialization
902913
int doInteger(String name, int result,
914+
@Shared("language") @CachedLanguage PythonLanguage language,
903915
@Shared("ctxt") @CachedContext(PythonLanguage.class) PythonContext context,
904916
@Shared("fact") @Cached PythonObjectFactory factory,
905917
@Shared("raise") @Cached PRaiseNode raise) {
906918
// If the native functions returns a primitive int, only a value '-1' indicates an
907919
// error.
908-
checkFunctionResult(name, result == -1, true, context, raise, factory);
920+
checkFunctionResult(name, result == -1, true, language, context, raise, factory);
909921
return result;
910922
}
911923

912924
@Specialization
913925
long doLong(String name, long result,
926+
@Shared("language") @CachedLanguage PythonLanguage language,
914927
@Shared("ctxt") @CachedContext(PythonLanguage.class) PythonContext context,
915928
@Shared("fact") @Cached PythonObjectFactory factory,
916929
@Shared("raise") @Cached PRaiseNode raise) {
917930
// If the native functions returns a primitive int, only a value '-1' indicates an
918931
// error.
919-
checkFunctionResult(name, result == -1, true, context, raise, factory);
932+
checkFunctionResult(name, result == -1, true, language, context, raise, factory);
920933
return result;
921934
}
922935

@@ -930,14 +943,16 @@ long doLong(String name, long result,
930943
Object doForeign(String name, Object result,
931944
@Exclusive @Cached("createBinaryProfile()") ConditionProfile isNullProfile,
932945
@Exclusive @CachedLibrary(limit = "3") InteropLibrary lib,
946+
@Shared("language") @CachedLanguage PythonLanguage language,
933947
@Shared("ctxt") @CachedContext(PythonLanguage.class) PythonContext context,
934948
@Shared("fact") @Cached PythonObjectFactory factory,
935949
@Shared("raise") @Cached PRaiseNode raise) {
936-
checkFunctionResult(name, isNullProfile.profile(lib.isNull(result)), false, context, raise, factory);
950+
checkFunctionResult(name, isNullProfile.profile(lib.isNull(result)), false, language, context, raise, factory);
937951
return result;
938952
}
939953

940-
private void checkFunctionResult(String name, boolean indicatesError, boolean isPrimitiveResult, PythonContext context, PRaiseNode raise, PythonObjectFactory factory) {
954+
private void checkFunctionResult(String name, boolean indicatesError, boolean isPrimitiveResult, PythonLanguage language, PythonContext context, PRaiseNode raise,
955+
PythonObjectFactory factory) {
941956
PException currentException = context.getCurrentException();
942957
boolean errOccurred = currentException != null;
943958
if (indicatesError) {
@@ -953,7 +968,7 @@ private void checkFunctionResult(String name, boolean indicatesError, boolean is
953968
context.setCurrentException(null);
954969
PBaseException sysExc = factory.createBaseException(PythonErrorType.SystemError, ErrorMessages.RETURNED_RESULT_WITH_ERROR_SET, new Object[]{name});
955970
sysExc.setCause(currentException.getEscapedException());
956-
throw PException.fromObject(sysExc, this);
971+
throw PException.fromObject(sysExc, this, PythonOptions.isPExceptionWithJavaStacktrace(language));
957972
}
958973
}
959974

@@ -1570,7 +1585,8 @@ Object newFrame(Object threadState, PCode code, PythonObject globals, Object loc
15701585
abstract static class PyTraceBackHereNode extends PythonUnaryBuiltinNode {
15711586
@Specialization
15721587
int tbHere(PFrame frame,
1573-
@Cached GetTracebackNode getTracebackNode) {
1588+
@Cached GetTracebackNode getTracebackNode,
1589+
@CachedLanguage PythonLanguage language) {
15741590
PythonContext context = getContext();
15751591
PException currentException = context.getCurrentException();
15761592
if (currentException != null) {
@@ -1579,7 +1595,8 @@ int tbHere(PFrame frame,
15791595
traceback = getTracebackNode.execute(currentException.getTraceback());
15801596
}
15811597
PTraceback newTraceback = factory().createTraceback(frame, frame.getLine(), traceback);
1582-
context.setCurrentException(PException.fromExceptionInfo(currentException.getExceptionObject(), newTraceback));
1598+
boolean withJavaStacktrace = PythonOptions.isPExceptionWithJavaStacktrace(language);
1599+
context.setCurrentException(PException.fromExceptionInfo(currentException.getExceptionObject(), newTraceback, withJavaStacktrace));
15831600
}
15841601

15851602
return 0;

0 commit comments

Comments
 (0)