Skip to content

Commit b8d1253

Browse files
committed
Wrap getting/setting exception context/cause/suppress_context into nodes
1 parent 3979198 commit b8d1253

25 files changed

+524
-226
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
import com.oracle.graal.python.builtins.objects.common.EconomicMapStorage;
170170
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes.HashingStorageSetItem;
171171
import com.oracle.graal.python.builtins.objects.dict.PDict;
172+
import com.oracle.graal.python.builtins.objects.exception.ExceptionNodes;
172173
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
173174
import com.oracle.graal.python.builtins.objects.frame.PFrame;
174175
import com.oracle.graal.python.builtins.objects.frame.PFrame.Reference;
@@ -184,12 +185,13 @@
184185
import com.oracle.graal.python.builtins.objects.str.StringNodes;
185186
import com.oracle.graal.python.builtins.objects.str.StringUtils;
186187
import com.oracle.graal.python.builtins.objects.thread.PThread;
187-
import com.oracle.graal.python.builtins.objects.traceback.GetTracebackNode;
188188
import com.oracle.graal.python.builtins.objects.traceback.LazyTraceback;
189+
import com.oracle.graal.python.builtins.objects.traceback.MaterializeLazyTracebackNode;
189190
import com.oracle.graal.python.builtins.objects.traceback.PTraceback;
190191
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
191192
import com.oracle.graal.python.builtins.objects.tuple.StructSequence;
192193
import com.oracle.graal.python.builtins.objects.tuple.TupleBuiltins;
194+
import com.oracle.graal.python.lib.PyExceptionInstanceCheckNode;
193195
import com.oracle.graal.python.lib.PyFloatAsDoubleNode;
194196
import com.oracle.graal.python.lib.PyFloatCheckExactNode;
195197
import com.oracle.graal.python.lib.PyImportImport;
@@ -789,7 +791,7 @@ public PTuple run(VirtualFrame frame,
789791
@Bind("this") Node inliningTarget,
790792
@Cached InlinedGetClassNode getClassNode,
791793
@Cached GetCaughtExceptionNode getCaughtExceptionNode,
792-
@Cached GetTracebackNode getTracebackNode) {
794+
@Cached MaterializeLazyTracebackNode materializeLazyTracebackNode) {
793795
PException currentException = getCaughtExceptionNode.execute(frame);
794796
assert currentException != PException.NO_EXCEPTION;
795797
if (currentException == null) {
@@ -799,7 +801,7 @@ public PTuple run(VirtualFrame frame,
799801
LazyTraceback lazyTraceback = currentException.getTraceback();
800802
PTraceback traceback = null;
801803
if (lazyTraceback != null) {
802-
traceback = getTracebackNode.execute(lazyTraceback);
804+
traceback = materializeLazyTracebackNode.execute(lazyTraceback);
803805
}
804806
return factory().createTuple(new Object[]{getClassNode.execute(inliningTarget, exception), exception, traceback == null ? PNone.NONE : traceback});
805807
}
@@ -1445,17 +1447,16 @@ void printExceptionRecursive(MaterializedFrame frame, PythonModule sys, Object o
14451447
if (seen != null) {
14461448
// Exception chaining
14471449
add(seen, value);
1448-
if (PGuards.isPBaseException(value)) {
1449-
final PBaseException exc = (PBaseException) value;
1450-
final PBaseException cause = exc.getCause();
1451-
final PBaseException context = exc.getContext();
1450+
if (PyExceptionInstanceCheckNode.executeUncached(value)) {
1451+
Object cause = ExceptionNodes.GetCauseNode.executeUncached(value);
1452+
Object context = ExceptionNodes.GetContextNode.executeUncached(value);
14521453

1453-
if (cause != null) {
1454+
if (cause != PNone.NONE) {
14541455
if (notSeen(seen, cause)) {
14551456
printExceptionRecursive(frame, sys, out, cause, seen);
14561457
fileWriteString(frame, out, T_CAUSE_MESSAGE);
14571458
}
1458-
} else if (context != null && !exc.getSuppressContext()) {
1459+
} else if (context != PNone.NONE && !ExceptionNodes.GetSuppressContextNode.executeUncached(value)) {
14591460
if (notSeen(seen, context)) {
14601461
printExceptionRecursive(frame, sys, out, context, seen);
14611462
fileWriteString(frame, out, T_CONTEXT_MESSAGE);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
import com.oracle.graal.python.builtins.objects.exception.PrepareExceptionNode;
9393
import com.oracle.graal.python.builtins.objects.function.PKeyword;
9494
import com.oracle.graal.python.builtins.objects.module.PythonModule;
95-
import com.oracle.graal.python.builtins.objects.traceback.GetTracebackNode;
95+
import com.oracle.graal.python.builtins.objects.traceback.MaterializeLazyTracebackNode;
9696
import com.oracle.graal.python.builtins.objects.traceback.LazyTraceback;
9797
import com.oracle.graal.python.builtins.objects.traceback.PTraceback;
9898
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
@@ -164,7 +164,7 @@ abstract static class PyTruffleErr_Fetch extends CApiNullaryBuiltinNode {
164164
@Specialization
165165
Object run(@Cached GetThreadStateNode getThreadStateNode,
166166
@Cached GetClassNode getClassNode,
167-
@Cached GetTracebackNode getTracebackNode) {
167+
@Cached MaterializeLazyTracebackNode materializeLazyTracebackNode) {
168168
PException currentException = getThreadStateNode.getCurrentException();
169169
Object result;
170170
if (currentException == null) {
@@ -173,7 +173,7 @@ Object run(@Cached GetThreadStateNode getThreadStateNode,
173173
PBaseException exception = currentException.getEscapedException();
174174
Object traceback = null;
175175
if (currentException.getTraceback() != null) {
176-
traceback = getTracebackNode.execute(currentException.getTraceback());
176+
traceback = materializeLazyTracebackNode.execute(currentException.getTraceback());
177177
}
178178
if (traceback == null) {
179179
traceback = getNativeNull();
@@ -351,7 +351,7 @@ abstract static class PyTruffleErr_GetExcInfo extends CApiNullaryBuiltinNode {
351351
Object info(
352352
@Cached GetCaughtExceptionNode getCaughtExceptionNode,
353353
@Cached GetClassNode getClassNode,
354-
@Cached GetTracebackNode getTracebackNode,
354+
@Cached MaterializeLazyTracebackNode materializeLazyTracebackNode,
355355
@Cached BranchProfile noExceptionProfile) {
356356
PException currentException = getCaughtExceptionNode.executeFromNative();
357357
if (currentException == null) {
@@ -363,7 +363,7 @@ Object info(
363363
LazyTraceback lazyTraceback = currentException.getTraceback();
364364
PTraceback traceback = null;
365365
if (lazyTraceback != null) {
366-
traceback = getTracebackNode.execute(lazyTraceback);
366+
traceback = materializeLazyTracebackNode.execute(lazyTraceback);
367367
}
368368
return factory().createTuple(new Object[]{getClassNode.execute(exception), exception, traceback == null ? PNone.NONE : traceback});
369369
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
import com.oracle.graal.python.builtins.modules.cext.PythonCextCodeBuiltins.PyCode_NewEmpty;
5454
import com.oracle.graal.python.builtins.objects.PNone;
5555
import com.oracle.graal.python.builtins.objects.frame.PFrame;
56-
import com.oracle.graal.python.builtins.objects.traceback.GetTracebackNode;
56+
import com.oracle.graal.python.builtins.objects.traceback.MaterializeLazyTracebackNode;
5757
import com.oracle.graal.python.builtins.objects.traceback.PTraceback;
5858
import com.oracle.graal.python.runtime.PythonContext;
5959
import com.oracle.graal.python.runtime.PythonOptions;
@@ -80,14 +80,14 @@ Object tbHere(TruffleString funcname, TruffleString filename, int lineno,
8080
abstract static class PyTraceBack_Here extends CApiUnaryBuiltinNode {
8181
@Specialization
8282
int tbHere(PFrame frame,
83-
@Cached GetTracebackNode getTracebackNode) {
83+
@Cached MaterializeLazyTracebackNode materializeLazyTracebackNode) {
8484
PythonLanguage language = getLanguage();
8585
PythonContext.PythonThreadState threadState = getContext().getThreadState(language);
8686
PException currentException = threadState.getCurrentException();
8787
if (currentException != null) {
8888
PTraceback traceback = null;
8989
if (currentException.getTraceback() != null) {
90-
traceback = getTracebackNode.execute(currentException.getTraceback());
90+
traceback = materializeLazyTracebackNode.execute(currentException.getTraceback());
9191
}
9292
PTraceback newTraceback = factory().createTraceback(frame, frame.getLine(), traceback);
9393
boolean withJavaStacktrace = PythonOptions.isPExceptionWithJavaStacktrace(language);

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,23 +305,32 @@ public enum NativeMember {
305305
CO_FREEVARS("co_freevars", OBJECT),
306306
CO_CELLVARS("co_cellvars", OBJECT),
307307

308+
// PyBaseException
309+
TRACEBACK("traceback", OBJECT),
310+
CAUSE("cause", OBJECT),
311+
CONTEXT("context", OBJECT),
312+
SUPPRESS_CONTEXT("suppress_context", PRIMITIVE),
313+
ARGS("args", OBJECT),
314+
308315
// PyStopIterationObject
309316
VALUE("value", OBJECT);
310317

311318
private final String jMemberName;
312319
private final TruffleString tMemberName;
313320
private final NativeMemberType type;
314321
private final NativeCAPISymbol getter;
322+
private final NativeCAPISymbol setter;
315323

316-
private NativeMember(String name) {
324+
NativeMember(String name) {
317325
this(name, POINTER);
318326
}
319327

320-
private NativeMember(String name, NativeMemberType type) {
328+
NativeMember(String name, NativeMemberType type) {
321329
this.jMemberName = name;
322330
this.tMemberName = toTruffleStringUncached(name);
323331
this.type = type;
324332
this.getter = NativeCAPISymbol.getByName(StringLiterals.J_GET_ + name);
333+
this.setter = NativeCAPISymbol.getByName(StringLiterals.J_SET_ + name);
325334
}
326335

327336
public TruffleString getMemberNameTruffleString() {
@@ -345,6 +354,14 @@ public NativeCAPISymbol getGetterFunctionName() {
345354
return getter;
346355
}
347356

357+
public NativeCAPISymbol getSetterFunctionName() {
358+
if (setter == null) {
359+
CompilerDirectives.transferToInterpreterAndInvalidate();
360+
throw CompilerDirectives.shouldNotReachHere("no setter for native member " + jMemberName);
361+
}
362+
return setter;
363+
}
364+
348365
@CompilationFinal(dimensions = 1) private static final NativeMember[] VALUES = values();
349366

350367
public static NativeMember byName(String name) {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions.NativeToPythonNode;
5252
import com.oracle.graal.python.builtins.objects.dict.PDict;
5353
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
54-
import com.oracle.graal.python.builtins.objects.traceback.GetTracebackNode;
54+
import com.oracle.graal.python.builtins.objects.traceback.MaterializeLazyTracebackNode;
5555
import com.oracle.graal.python.builtins.objects.traceback.LazyTraceback;
5656
import com.oracle.graal.python.builtins.objects.traceback.PTraceback;
5757
import com.oracle.graal.python.builtins.objects.type.PythonClass;
@@ -191,12 +191,12 @@ static Object doCurExcValue(PThreadState receiver, @SuppressWarnings("unused") S
191191
@Specialization(guards = "eq(key, J_CUR_EXC_TRACEBACK)")
192192
static Object doCurExcTraceback(PThreadState receiver, @SuppressWarnings("unused") String key,
193193
@Shared("toSulong") @Cached ToSulongNode toSulongNode,
194-
@Shared("getTraceback") @Cached GetTracebackNode getTracebackNode) {
194+
@Shared("getTraceback") @Cached MaterializeLazyTracebackNode materializeLazyTracebackNode) {
195195
PException currentException = receiver.threadState.getCurrentException();
196196
PTraceback result = null;
197197
if (currentException != null) {
198198
LazyTraceback traceback = currentException.getTraceback();
199-
result = traceback != null ? getTracebackNode.execute(traceback) : null;
199+
result = traceback != null ? materializeLazyTracebackNode.execute(traceback) : null;
200200
}
201201
return toSulongNode.execute(result != null ? result : PNone.NO_VALUE);
202202
}
@@ -227,11 +227,11 @@ static Object doExcValue(PThreadState receiver, @SuppressWarnings("unused") Stri
227227
@Specialization(guards = "eq(key, J_EXC_TRACEBACK)")
228228
static Object doExcTraceback(PThreadState receiver, @SuppressWarnings("unused") String key,
229229
@Shared("toSulong") @Cached ToSulongNode toSulongNode,
230-
@Shared("getTraceback") @Cached GetTracebackNode getTracebackNode) {
230+
@Shared("getTraceback") @Cached MaterializeLazyTracebackNode materializeLazyTracebackNode) {
231231
PException currentException = receiver.threadState.getCaughtException();
232232
PTraceback result = null;
233233
if (currentException != null) {
234-
result = getTracebackNode.execute(currentException.getTraceback());
234+
result = materializeLazyTracebackNode.execute(currentException.getTraceback());
235235
}
236236
return toSulongNode.execute(result != null ? result : PNone.NO_VALUE);
237237
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.ToSulongNode;
4545
import com.oracle.graal.python.builtins.objects.cext.capi.transitions.CApiTransitions;
4646
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
47-
import com.oracle.graal.python.builtins.objects.traceback.GetTracebackNode;
47+
import com.oracle.graal.python.builtins.objects.traceback.MaterializeLazyTracebackNode;
4848
import com.oracle.graal.python.nodes.object.GetClassNode;
4949
import com.oracle.graal.python.runtime.PythonContext;
5050
import com.oracle.truffle.api.dsl.Bind;
@@ -95,7 +95,7 @@ boolean isMemberReadable(String key) {
9595
@ExportMessage
9696
Object readMember(String key,
9797
@Cached GetClassNode getClassNode,
98-
@Cached GetTracebackNode getTracebackNode,
98+
@Cached MaterializeLazyTracebackNode materializeLazyTracebackNode,
9999
@Cached ToSulongNode toSulongNode) {
100100
Object result = null;
101101
if (exception != null) {
@@ -108,7 +108,7 @@ Object readMember(String key,
108108
break;
109109
case J_EXC_TRACEBACK:
110110
if (exception.getTraceback() != null) {
111-
result = getTracebackNode.execute(exception.getTraceback());
111+
result = materializeLazyTracebackNode.execute(exception.getTraceback());
112112
}
113113
break;
114114
}

0 commit comments

Comments
 (0)