Skip to content

Commit 3f2a6ac

Browse files
committed
Move PException.chainException to a node
1 parent a80b390 commit 3f2a6ac

File tree

7 files changed

+68
-31
lines changed

7 files changed

+68
-31
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
import com.oracle.graal.python.builtins.objects.PNone;
9393
import com.oracle.graal.python.builtins.objects.str.StringUtils.SimpleTruffleStringFormatNode;
9494
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
95+
import com.oracle.graal.python.lib.PyErrChainExceptions;
9596
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
9697
import com.oracle.graal.python.lib.PyObjectGetAttr;
9798
import com.oracle.graal.python.lib.PyObjectLookupAttr;
@@ -152,7 +153,8 @@ static Object doit(VirtualFrame frame, PBuffered self,
152153
@Cached PyObjectCallMethodObjArgs callMethodClose,
153154
@Cached PyObjectCallMethodObjArgs callMethodDeallocWarn,
154155
@Cached EnterBufferedNode lock,
155-
@Cached InlinedConditionProfile profile) {
156+
@Cached InlinedConditionProfile profile,
157+
@Cached PyErrChainExceptions chainExceptions) {
156158
try {
157159
lock.enter(inliningTarget, self);
158160
if (profile.profile(inliningTarget, isClosedNode.execute(frame, inliningTarget, self))) {
@@ -173,7 +175,7 @@ static Object doit(VirtualFrame frame, PBuffered self,
173175
try {
174176
close(frame, inliningTarget, self, lock, callMethodClose);
175177
} catch (PException ee) {
176-
throw ee.chainException(e);
178+
throw chainExceptions.execute(inliningTarget, ee, e);
177179
}
178180
throw e;
179181
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import com.oracle.graal.python.builtins.CoreFunctions;
8181
import com.oracle.graal.python.builtins.PythonBuiltins;
8282
import com.oracle.graal.python.builtins.objects.PNone;
83+
import com.oracle.graal.python.lib.PyErrChainExceptions;
8384
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
8485
import com.oracle.graal.python.lib.PyObjectGetAttr;
8586
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
@@ -287,7 +288,8 @@ Object close(VirtualFrame frame, PRWPair self,
287288
@Cached PyObjectCallMethodObjArgs callMethodReader,
288289
@Cached PyObjectCallMethodObjArgs callMethodWriter,
289290
@Cached InlinedConditionProfile gotException,
290-
@Cached InlinedBranchProfile hasException) {
291+
@Cached InlinedBranchProfile hasException,
292+
@Cached PyErrChainExceptions chainExceptions) {
291293
PException writeEx = null;
292294
if (self.getWriter() != null) {
293295
try {
@@ -316,12 +318,12 @@ Object close(VirtualFrame frame, PRWPair self,
316318
}
317319

318320
hasException.enter(inliningTarget);
319-
return chainedError(writeEx, readEx, inliningTarget, gotException);
321+
return chainedError(writeEx, readEx, inliningTarget, gotException, chainExceptions);
320322
}
321323

322-
static Object chainedError(PException first, PException second, Node inliningTarget, InlinedConditionProfile gotFirst) {
324+
static Object chainedError(PException first, PException second, Node inliningTarget, InlinedConditionProfile gotFirst, PyErrChainExceptions chainExceptions) {
323325
if (gotFirst.profile(inliningTarget, first != null)) {
324-
throw second.chainException(first);
326+
throw chainExceptions.execute(inliningTarget, second, first);
325327
} else {
326328
throw second;
327329
}

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
129129
import com.oracle.graal.python.builtins.objects.exception.OSErrorEnum;
130130
import com.oracle.graal.python.builtins.objects.str.StringUtils.SimpleTruffleStringFormatNode;
131+
import com.oracle.graal.python.lib.PyErrChainExceptions;
131132
import com.oracle.graal.python.lib.PyIndexCheckNode;
132133
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
133134
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
@@ -904,16 +905,18 @@ Object simple(VirtualFrame frame, PFileIO self,
904905
}
905906

906907
@Specialization(guards = {"self.isCloseFD()", "!self.isFinalizing()"})
907-
Object common(VirtualFrame frame, PFileIO self,
908+
static Object common(VirtualFrame frame, PFileIO self,
909+
@Bind("this") Node inliningTarget,
908910
@Shared("c") @Cached PosixModuleBuiltins.CloseNode posixClose,
909-
@Exclusive @Shared("l") @Cached PyObjectCallMethodObjArgs callSuperClose) {
911+
@Shared("l") @Cached PyObjectCallMethodObjArgs callSuperClose,
912+
@Shared @Cached PyErrChainExceptions chainExceptions) {
910913
try {
911-
callSuperClose.execute(frame, getContext().lookupType(PRawIOBase), T_CLOSE, self);
914+
callSuperClose.execute(frame, PythonContext.get(inliningTarget).lookupType(PRawIOBase), T_CLOSE, self);
912915
} catch (PException e) {
913916
try {
914917
internalClose(frame, self, posixClose);
915918
} catch (PException ee) {
916-
throw ee.chainException(e);
919+
throw chainExceptions.execute(inliningTarget, ee, e);
917920
}
918921
throw e;
919922
}
@@ -922,26 +925,29 @@ Object common(VirtualFrame frame, PFileIO self,
922925
}
923926

924927
@Specialization(guards = {"self.isCloseFD()", "self.isFinalizing()"})
925-
Object slow(VirtualFrame frame, PFileIO self,
928+
static Object slow(VirtualFrame frame, PFileIO self,
929+
@Bind("this") Node inliningTarget,
926930
@Shared("c") @Cached PosixModuleBuiltins.CloseNode posixClose,
927931
@Cached WarningsModuleBuiltins.WarnNode warnNode,
928-
@Shared("l") @Cached PyObjectCallMethodObjArgs callSuperClose) {
932+
@Shared("l") @Cached PyObjectCallMethodObjArgs callSuperClose,
933+
@Shared @Cached PyErrChainExceptions chainExceptions) {
929934
PException rawIOException = null;
935+
PythonContext context = PythonContext.get(inliningTarget);
930936
try {
931-
callSuperClose.execute(frame, getContext().lookupType(PRawIOBase), T_CLOSE, self);
937+
callSuperClose.execute(frame, context.lookupType(PRawIOBase), T_CLOSE, self);
932938
} catch (PException e) {
933939
rawIOException = e;
934940
}
935941
try {
936-
deallocWarn(frame, self, warnNode, getLanguage(), getContext());
942+
deallocWarn(frame, self, warnNode, PythonLanguage.get(inliningTarget), context);
937943
} catch (PException e) {
938944
// ignore
939945
}
940946
try {
941947
internalClose(frame, self, posixClose);
942948
} catch (PException ee) {
943949
if (rawIOException != null) {
944-
throw ee.chainException(rawIOException);
950+
throw chainExceptions.execute(inliningTarget, ee, rawIOException);
945951
} else {
946952
throw ee;
947953
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
9898
import com.oracle.graal.python.builtins.objects.object.PythonObject;
9999
import com.oracle.graal.python.lib.GetNextNode;
100+
import com.oracle.graal.python.lib.PyErrChainExceptions;
100101
import com.oracle.graal.python.lib.PyObjectCallMethodObjArgs;
101102
import com.oracle.graal.python.lib.PyObjectGetAttr;
102103
import com.oracle.graal.python.lib.PyObjectGetIter;
@@ -287,12 +288,13 @@ boolean doCheckWritable(VirtualFrame frame, PythonObject self,
287288
@GenerateNodeFactory
288289
abstract static class CloseNode extends PythonUnaryBuiltinNode {
289290
@Specialization
290-
PNone close(VirtualFrame frame, PythonObject self,
291+
static PNone close(VirtualFrame frame, PythonObject self,
291292
@Bind("this") Node inliningTarget,
292293
@Cached PyObjectCallMethodObjArgs callMethod,
293294
@Cached PyObjectLookupAttr lookup,
294295
@Cached("create(T___IOBASE_CLOSED)") SetAttributeNode setAttributeNode,
295-
@Cached InlinedBranchProfile errorProfile) {
296+
@Cached InlinedBranchProfile errorProfile,
297+
@Cached PyErrChainExceptions chainExceptions) {
296298
if (!isClosed(self, frame, lookup)) {
297299
try {
298300
callMethod.execute(frame, self, T_FLUSH);
@@ -301,7 +303,7 @@ PNone close(VirtualFrame frame, PythonObject self,
301303
try {
302304
setAttributeNode.execute(frame, self, true);
303305
} catch (PException e1) {
304-
throw e1.chainException(e);
306+
throw chainExceptions.execute(inliningTarget, e1, e);
305307
}
306308
throw e;
307309
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
import com.oracle.graal.python.builtins.objects.str.StringNodes.StringReplaceNode;
140140
import com.oracle.graal.python.builtins.objects.str.StringUtils.SimpleTruffleStringFormatNode;
141141
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
142+
import com.oracle.graal.python.lib.PyErrChainExceptions;
142143
import com.oracle.graal.python.lib.PyLongAsLongNode;
143144
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
144145
import com.oracle.graal.python.lib.PyNumberIndexNode;
@@ -540,11 +541,13 @@ static Object flush(VirtualFrame frame, PTextIO self,
540541
abstract static class CloseNode extends AttachedCheckPythonUnaryBuiltinNode {
541542
@Specialization(guards = "checkAttached(self)")
542543
static Object close(VirtualFrame frame, PTextIO self,
544+
@Bind("this") Node inliningTarget,
543545
@Cached ClosedNode closedNode,
544546
@Cached PyObjectCallMethodObjArgs callMethodFlush,
545547
@Cached PyObjectCallMethodObjArgs callMethodDeallocWarn,
546548
@Cached PyObjectCallMethodObjArgs callMethodClose,
547-
@Cached PyObjectIsTrueNode isTrueNode) {
549+
@Cached PyObjectIsTrueNode isTrueNode,
550+
@Cached PyErrChainExceptions chainExceptions) {
548551
Object res = closedNode.execute(frame, self);
549552
if (isTrueNode.execute(frame, res)) {
550553
return PNone.NONE;
@@ -560,7 +563,7 @@ static Object close(VirtualFrame frame, PTextIO self,
560563
callMethodClose.execute(frame, self.getBuffer(), T_CLOSE);
561564
throw e;
562565
} catch (PException ee) {
563-
throw ee.chainException(e);
566+
throw chainExceptions.execute(inliningTarget, ee, e);
564567
}
565568
}
566569
return callMethodClose.execute(frame, self.getBuffer(), T_CLOSE);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.oracle.graal.python.lib;
2+
3+
import com.oracle.graal.python.builtins.objects.exception.ExceptionNodes;
4+
import com.oracle.graal.python.runtime.exception.PException;
5+
import com.oracle.truffle.api.dsl.Cached;
6+
import com.oracle.truffle.api.dsl.GenerateCached;
7+
import com.oracle.truffle.api.dsl.GenerateInline;
8+
import com.oracle.truffle.api.dsl.GenerateUncached;
9+
import com.oracle.truffle.api.dsl.Specialization;
10+
import com.oracle.truffle.api.nodes.Node;
11+
12+
/**
13+
* Equivalent of CPython's {_PyErr_ChainExceptions}. Performs a simple exception chaining without
14+
* checking for cycles (not suitable to implement try-except).
15+
*/
16+
@GenerateUncached
17+
@GenerateInline
18+
@GenerateCached(false)
19+
public abstract class PyErrChainExceptions extends Node {
20+
public final PException execute(Node inliningTarget, PException current, PException context) {
21+
context.ensureReified();
22+
execute(inliningTarget, current.getUnreifiedException(), context.getEscapedException());
23+
return current;
24+
}
25+
26+
abstract void execute(Node inliningTarget, Object current, Object context);
27+
28+
@Specialization
29+
static void chain(Node inliningTarget, Object current, Object context,
30+
@Cached ExceptionNodes.SetContextNode setContextNode) {
31+
setContextNode.execute(inliningTarget, current, context);
32+
}
33+
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/exception/PException.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -371,17 +371,6 @@ public PException getExceptionForReraise(boolean rootNodeVisible) {
371371
return pe;
372372
}
373373

374-
/**
375-
* Equivalent of CPython's {_PyErr_ChainExceptions}. Performs a simple exception chaining
376-
* without checking for cycles (not suitable to implement try-except).
377-
*/
378-
public PException chainException(PException e) {
379-
PBaseException chainedException = e.getEscapedException();
380-
chainedException.setTraceback(e.getTraceback());
381-
getUnreifiedException().setContext(chainedException);
382-
return this;
383-
}
384-
385374
@TruffleBoundary
386375
public void printStack() {
387376
// a convenience methods for debugging

0 commit comments

Comments
 (0)