Skip to content

Commit ab545d3

Browse files
committed
Also print Java stack trace for Python exceptions if requested.
1 parent da33164 commit ab545d3

File tree

13 files changed

+133
-70
lines changed

13 files changed

+133
-70
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
@@ -43,6 +43,7 @@
4343
import java.util.regex.Matcher;
4444
import java.util.regex.Pattern;
4545

46+
import com.oracle.graal.python.runtime.PythonOptions;
4647
import org.graalvm.nativeimage.ImageInfo;
4748

4849
import com.oracle.graal.python.PythonLanguage;
@@ -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, getContext().getOption(PythonOptions.WithJavaStacktrace));
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, getContext().getOption(PythonOptions.WithJavaStacktrace));
798799
}
799800
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -588,13 +588,15 @@ Object run(PNone typ, PNone val, PNone tb) {
588588

589589
@Specialization
590590
Object run(@SuppressWarnings("unused") Object typ, PBaseException val, @SuppressWarnings("unused") PNone tb) {
591-
getContext().setCurrentException(PException.fromExceptionInfo(val, (LazyTraceback) null));
591+
PythonContext context = getContext();
592+
context.setCurrentException(PException.fromExceptionInfo(val, (LazyTraceback) null, context.getOption(PythonOptions.WithJavaStacktrace)));
592593
return PNone.NONE;
593594
}
594595

595596
@Specialization
596597
Object run(@SuppressWarnings("unused") Object typ, PBaseException val, PTraceback tb) {
597-
getContext().setCurrentException(PException.fromExceptionInfo(val, tb));
598+
PythonContext context = getContext();
599+
context.setCurrentException(PException.fromExceptionInfo(val, tb, context.getOption(PythonOptions.WithJavaStacktrace)));
598600
return PNone.NONE;
599601
}
600602
}
@@ -655,7 +657,8 @@ Object doClear(PNone typ, PNone val, PNone tb) {
655657

656658
@Specialization
657659
Object doFull(@SuppressWarnings("unused") Object typ, PBaseException val, PTraceback tb) {
658-
getContext().setCaughtException(PException.fromExceptionInfo(val, tb));
660+
PythonContext context = getContext();
661+
context.setCaughtException(PException.fromExceptionInfo(val, tb, context.getOption(PythonOptions.WithJavaStacktrace)));
659662
return PNone.NONE;
660663
}
661664

@@ -953,7 +956,7 @@ private void checkFunctionResult(String name, boolean indicatesError, boolean is
953956
context.setCurrentException(null);
954957
PBaseException sysExc = factory.createBaseException(PythonErrorType.SystemError, ErrorMessages.RETURNED_RESULT_WITH_ERROR_SET, new Object[]{name});
955958
sysExc.setCause(currentException.getEscapedException());
956-
throw PException.fromObject(sysExc, this);
959+
throw PException.fromObject(sysExc, this, context.getOption(PythonOptions.WithJavaStacktrace));
957960
}
958961
}
959962

@@ -1579,7 +1582,7 @@ int tbHere(PFrame frame,
15791582
traceback = getTracebackNode.execute(currentException.getTraceback());
15801583
}
15811584
PTraceback newTraceback = factory().createTraceback(frame, frame.getLine(), traceback);
1582-
context.setCurrentException(PException.fromExceptionInfo(currentException.getExceptionObject(), newTraceback));
1585+
context.setCurrentException(PException.fromExceptionInfo(currentException.getExceptionObject(), newTraceback, context.getOption(PythonOptions.WithJavaStacktrace)));
15831586
}
15841587

15851588
return 0;

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import com.oracle.graal.python.nodes.PNodeWithContext;
5656
import com.oracle.graal.python.nodes.object.GetClassNode;
5757
import com.oracle.graal.python.runtime.PythonContext;
58+
import com.oracle.graal.python.runtime.PythonOptions;
5859
import com.oracle.graal.python.runtime.exception.PException;
5960
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
6061
import com.oracle.truffle.api.Assumption;
@@ -344,7 +345,7 @@ PBaseException doCurExcValue(@SuppressWarnings("unused") String key, PBaseExcept
344345
PTraceback doCurExcTraceback(@SuppressWarnings("unused") String key, PTraceback value,
345346
@Shared("context") @CachedContext(PythonLanguage.class) PythonContext context) {
346347
PException e = context.getCurrentException();
347-
context.setCurrentException(PException.fromExceptionInfo(e.getExceptionObject(), value));
348+
context.setCurrentException(PException.fromExceptionInfo(e.getExceptionObject(), value, context.getOption(PythonOptions.WithJavaStacktrace)));
348349
return value;
349350
}
350351

@@ -367,7 +368,7 @@ PBaseException doExcValue(@SuppressWarnings("unused") String key, PBaseException
367368
PTraceback doExcTraceback(@SuppressWarnings("unused") String key, PTraceback value,
368369
@Shared("context") @CachedContext(PythonLanguage.class) PythonContext context) {
369370
PException e = context.getCaughtException();
370-
context.setCaughtException(PException.fromExceptionInfo(e.getExceptionObject(), value));
371+
context.setCaughtException(PException.fromExceptionInfo(e.getExceptionObject(), value, context.getOption(PythonOptions.WithJavaStacktrace)));
371372
return value;
372373
}
373374

@@ -386,11 +387,11 @@ Object doOverflowed(String key, int value) {
386387
}
387388

388389
private static void setCurrentException(PythonContext context, PBaseException exceptionObject) {
389-
context.setCurrentException(PException.fromExceptionInfo(exceptionObject, context.getCurrentException().getTraceback()));
390+
context.setCurrentException(PException.fromExceptionInfo(exceptionObject, context.getCurrentException().getTraceback(), context.getOption(PythonOptions.WithJavaStacktrace)));
390391
}
391392

392393
private static void setCaughtException(PythonContext context, PBaseException exceptionObject) {
393-
context.setCaughtException(PException.fromExceptionInfo(exceptionObject, context.getCaughtException().getTraceback()));
394+
context.setCaughtException(PException.fromExceptionInfo(exceptionObject, context.getCaughtException().getTraceback(), context.getOption(PythonOptions.WithJavaStacktrace)));
394395
}
395396

396397
@Specialization(guards = {"!isCurrentExceptionMember(key)", "!isCaughtExceptionMember(key)"})

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/exception/PBaseException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ RuntimeException throwException(@CachedLibrary("this") PythonObjectLibrary lib,
267267
**/
268268
public PException getExceptionForReraise(LazyTraceback reraiseTraceback) {
269269
setTraceback(reraiseTraceback);
270-
PException newException = PException.fromObject(this, exception.getLocation());
270+
PException newException = PException.fromObject(this, exception.getLocation(), false);
271271
newException.setHideLocation(true);
272272
return newException;
273273
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/generator/GeneratorBuiltins.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
import java.util.List;
4040

41+
import com.oracle.graal.python.PythonLanguage;
4142
import com.oracle.graal.python.builtins.Builtin;
4243
import com.oracle.graal.python.builtins.CoreFunctions;
4344
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
@@ -69,14 +70,19 @@
6970
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
7071
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
7172
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
73+
import com.oracle.graal.python.runtime.PythonContext;
74+
import com.oracle.graal.python.runtime.PythonOptions;
7275
import com.oracle.graal.python.runtime.exception.PException;
7376
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
7477
import com.oracle.graal.python.util.PythonUtils;
7578
import com.oracle.truffle.api.CallTarget;
7679
import com.oracle.truffle.api.CompilerDirectives;
7780
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7881
import com.oracle.truffle.api.RootCallTarget;
82+
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
7983
import com.oracle.truffle.api.dsl.Cached;
84+
import com.oracle.truffle.api.dsl.Cached.Shared;
85+
import com.oracle.truffle.api.dsl.CachedContext;
8086
import com.oracle.truffle.api.dsl.Fallback;
8187
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
8288
import com.oracle.truffle.api.dsl.ImportStatic;
@@ -387,35 +393,37 @@ private void checkExceptionClass(Object type) {
387393
@Specialization
388394
Object sendThrow(VirtualFrame frame, PGenerator self, Object typ, Object val, @SuppressWarnings("unused") PNone tb,
389395
@Cached PrepareExceptionNode prepareExceptionNode,
390-
@Cached BranchProfile alreadyRunning) {
396+
@Cached BranchProfile alreadyRunning,
397+
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
391398
if (self.isRunning()) {
392399
alreadyRunning.enter();
393400
throw raise(ValueError, ErrorMessages.GENERATOR_ALREADY_EXECUTING);
394401
}
395402
PBaseException instance = prepareExceptionNode.execute(frame, typ, val);
396-
return doThrow(self, instance);
403+
return doThrow(self, instance, contextRef.get());
397404
}
398405

399406
@Specialization
400407
Object sendThrow(VirtualFrame frame, PGenerator self, Object typ, Object val, PTraceback tb,
401408
@Cached PrepareExceptionNode prepareExceptionNode,
402-
@Cached BranchProfile alreadyRunning) {
409+
@Cached BranchProfile alreadyRunning,
410+
@Shared("contextRef") @CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
403411
if (self.isRunning()) {
404412
alreadyRunning.enter();
405413
throw raise(ValueError, ErrorMessages.GENERATOR_ALREADY_EXECUTING);
406414
}
407415
PBaseException instance = prepareExceptionNode.execute(frame, typ, val);
408416
instance.setTraceback(tb);
409-
return doThrow(self, instance);
417+
return doThrow(self, instance, contextRef.get());
410418
}
411419

412-
private Object doThrow(PGenerator self, PBaseException instance) {
420+
private Object doThrow(PGenerator self, PBaseException instance, PythonContext context) {
413421
instance.setContext(null); // Will be filled when caught
414422
if (self.isStarted()) {
415423
instance.ensureReified();
416424
// Pass it to the generator where it will be thrown by the last yield, the location
417425
// will be filled there
418-
PException pException = PException.fromObject(instance, null);
426+
PException pException = PException.fromObject(instance, null, context.getOption(PythonOptions.WithJavaStacktrace));
419427
return resumeGenerator(self, pException);
420428
} else {
421429
// Unstarted generator, we cannot pass the exception into the generator as there is
@@ -431,7 +439,7 @@ private Object doThrow(PGenerator self, PBaseException instance) {
431439
}
432440
PTraceback newTraceback = factory().createTraceback(pFrame, pFrame.getLine(), existingTraceback);
433441
instance.setTraceback(newTraceback);
434-
throw PException.fromObject(instance, location);
442+
throw PException.fromObject(instance, location, context.getOption(PythonOptions.WithJavaStacktrace));
435443
}
436444
}
437445

@@ -466,7 +474,8 @@ Object close(PGenerator self,
466474
@CachedLibrary(limit = "3") PythonObjectLibrary lib,
467475
@Cached IsBuiltinClassProfile isGeneratorExit,
468476
@Cached IsBuiltinClassProfile isStopIteration,
469-
@Cached BranchProfile alreadyRunning) {
477+
@Cached BranchProfile alreadyRunning,
478+
@CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
470479
if (self.isRunning()) {
471480
alreadyRunning.enter();
472481
throw raise(ValueError, ErrorMessages.GENERATOR_ALREADY_EXECUTING);
@@ -475,7 +484,7 @@ Object close(PGenerator self,
475484
PBaseException pythonException = factory().createBaseException(GeneratorExit);
476485
// Pass it to the generator where it will be thrown by the last yield, the location
477486
// will be filled there
478-
PException pException = PException.fromObject(pythonException, null);
487+
PException pException = PException.fromObject(pythonException, null, contextRef.get().getOption(PythonOptions.WithJavaStacktrace));
479488
try {
480489
resumeGenerator(self, pException);
481490
} catch (PException pe) {

0 commit comments

Comments
 (0)