Skip to content

Commit ef7eecd

Browse files
committed
Make PrepareExceptionNode uncached-ready
1 parent ec7cf40 commit ef7eecd

File tree

1 file changed

+41
-47
lines changed

1 file changed

+41
-47
lines changed

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

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,16 @@
4747
import com.oracle.graal.python.builtins.objects.PNone;
4848
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
4949
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
50-
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
50+
import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsTypeNode;
5151
import com.oracle.graal.python.nodes.ErrorMessages;
5252
import com.oracle.graal.python.nodes.PGuards;
5353
import com.oracle.graal.python.nodes.PRaiseNode;
54-
import com.oracle.graal.python.nodes.SpecialMethodNames;
5554
import com.oracle.graal.python.nodes.call.CallNode;
5655
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
5756
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
58-
import com.oracle.truffle.api.CompilerDirectives;
5957
import com.oracle.truffle.api.dsl.Bind;
6058
import com.oracle.truffle.api.dsl.Cached;
59+
import com.oracle.truffle.api.dsl.Cached.Shared;
6160
import com.oracle.truffle.api.dsl.Fallback;
6261
import com.oracle.truffle.api.dsl.ImportStatic;
6362
import com.oracle.truffle.api.dsl.Specialization;
@@ -69,44 +68,46 @@
6968
* Creates an exception out of type and value, similarly to CPython's
7069
* {@code PyErr_NormalizeException}. Returns the normalized exception.
7170
*/
72-
@ImportStatic({PGuards.class, SpecialMethodNames.class})
71+
@ImportStatic(PGuards.class)
7372
public abstract class PrepareExceptionNode extends Node {
7473
public abstract PBaseException execute(VirtualFrame frame, Object type, Object value);
7574

76-
private PRaiseNode raiseNode;
77-
private IsSubtypeNode isSubtypeNode;
78-
7975
@Specialization
8076
static PBaseException doException(PBaseException exc, @SuppressWarnings("unused") PNone value) {
8177
return exc;
8278
}
8379

8480
@Specialization(guards = "!isPNone(value)")
85-
PBaseException doException(@SuppressWarnings("unused") PBaseException exc, @SuppressWarnings("unused") Object value) {
86-
throw raise().raise(PythonBuiltinClassType.TypeError, ErrorMessages.INSTANCE_EX_MAY_NOT_HAVE_SEP_VALUE);
81+
static PBaseException doException(@SuppressWarnings("unused") PBaseException exc, @SuppressWarnings("unused") Object value,
82+
@Shared @Cached PRaiseNode raiseNode) {
83+
throw raiseNode.raise(TypeError, ErrorMessages.INSTANCE_EX_MAY_NOT_HAVE_SEP_VALUE);
8784
}
8885

8986
@Specialization(guards = "isTypeNode.execute(type)", limit = "1")
90-
PBaseException doException(VirtualFrame frame, Object type, PBaseException value,
87+
static PBaseException doException(VirtualFrame frame, Object type, PBaseException value,
9188
@Bind("this") Node inliningTarget,
92-
@SuppressWarnings("unused") @Cached.Shared("isType") @Cached TypeNodes.IsTypeNode isTypeNode,
89+
@SuppressWarnings("unused") @Shared("isType") @Cached IsTypeNode isTypeNode,
9390
@Cached BuiltinFunctions.IsInstanceNode isInstanceNode,
9491
@Cached InlinedBranchProfile isNotInstanceProfile,
95-
@Cached.Shared("callCtor") @Cached CallNode callConstructor) {
92+
@Shared @Cached IsSubtypeNode isSubtypeNode,
93+
@Shared @Cached PRaiseNode raiseNode,
94+
@Shared("callCtor") @Cached CallNode callConstructor) {
9695
if (isInstanceNode.executeWith(frame, value, type)) {
97-
checkExceptionClass(type);
96+
checkExceptionClass(type, isSubtypeNode, raiseNode);
9897
return value;
9998
} else {
10099
isNotInstanceProfile.enter(inliningTarget);
101-
return doCreateObject(frame, type, value, isTypeNode, callConstructor);
100+
return doCreateObject(frame, type, value, isTypeNode, isSubtypeNode, raiseNode, callConstructor);
102101
}
103102
}
104103

105104
@Specialization(guards = "isTypeNode.execute(type)", limit = "1")
106-
PBaseException doCreate(VirtualFrame frame, Object type, @SuppressWarnings("unused") PNone value,
107-
@SuppressWarnings("unused") @Cached.Shared("isType") @Cached TypeNodes.IsTypeNode isTypeNode,
108-
@Cached.Shared("callCtor") @Cached CallNode callConstructor) {
109-
checkExceptionClass(type);
105+
static PBaseException doCreate(VirtualFrame frame, Object type, @SuppressWarnings("unused") PNone value,
106+
@SuppressWarnings("unused") @Shared("isType") @Cached IsTypeNode isTypeNode,
107+
@Shared @Cached IsSubtypeNode isSubtypeNode,
108+
@Shared @Cached PRaiseNode raiseNode,
109+
@Shared("callCtor") @Cached CallNode callConstructor) {
110+
checkExceptionClass(type, isSubtypeNode, raiseNode);
110111
Object instance = callConstructor.execute(frame, type);
111112
if (instance instanceof PBaseException) {
112113
return (PBaseException) instance;
@@ -116,12 +117,14 @@ PBaseException doCreate(VirtualFrame frame, Object type, @SuppressWarnings("unus
116117
}
117118

118119
@Specialization(guards = "isTypeNode.execute(type)", limit = "1")
119-
PBaseException doCreateTuple(VirtualFrame frame, Object type, PTuple value,
120+
static PBaseException doCreateTuple(VirtualFrame frame, Object type, PTuple value,
120121
@Bind("this") Node inliningTarget,
121-
@SuppressWarnings("unused") @Cached.Shared("isType") @Cached TypeNodes.IsTypeNode isTypeNode,
122+
@SuppressWarnings("unused") @Shared("isType") @Cached IsTypeNode isTypeNode,
122123
@Cached SequenceNodes.GetObjectArrayNode getObjectArrayNode,
123-
@Cached.Shared("callCtor") @Cached CallNode callConstructor) {
124-
checkExceptionClass(type);
124+
@Shared @Cached IsSubtypeNode isSubtypeNode,
125+
@Shared @Cached PRaiseNode raiseNode,
126+
@Shared("callCtor") @Cached CallNode callConstructor) {
127+
checkExceptionClass(type, isSubtypeNode, raiseNode);
125128
Object[] args = getObjectArrayNode.execute(inliningTarget, value);
126129
Object instance = callConstructor.execute(frame, type, args);
127130
if (instance instanceof PBaseException) {
@@ -132,10 +135,12 @@ PBaseException doCreateTuple(VirtualFrame frame, Object type, PTuple value,
132135
}
133136

134137
@Specialization(guards = {"isTypeNode.execute(type)", "!isPNone(value)", "!isPTuple(value)", "!isPBaseException(value)"}, limit = "1")
135-
PBaseException doCreateObject(VirtualFrame frame, Object type, Object value,
136-
@SuppressWarnings("unused") @Cached.Shared("isType") @Cached TypeNodes.IsTypeNode isTypeNode,
137-
@Cached.Shared("callCtor") @Cached CallNode callConstructor) {
138-
checkExceptionClass(type);
138+
static PBaseException doCreateObject(VirtualFrame frame, Object type, Object value,
139+
@SuppressWarnings("unused") @Shared("isType") @Cached IsTypeNode isTypeNode,
140+
@Shared @Cached IsSubtypeNode isSubtypeNode,
141+
@Shared @Cached PRaiseNode raiseNode,
142+
@Shared("callCtor") @Cached CallNode callConstructor) {
143+
checkExceptionClass(type, isSubtypeNode, raiseNode);
139144
Object instance = callConstructor.execute(frame, type, value);
140145
if (instance instanceof PBaseException) {
141146
return (PBaseException) instance;
@@ -144,34 +149,23 @@ PBaseException doCreateObject(VirtualFrame frame, Object type, Object value,
144149
}
145150
}
146151

152+
@Fallback
153+
static PBaseException doError(Object type, @SuppressWarnings("unused") Object value,
154+
@Shared @Cached PRaiseNode raiseNode) {
155+
throw raiseNode.raise(TypeError, ErrorMessages.EXCEPTIONS_MUST_BE_CLASSES_OR_INSTANCES_DERIVING_FROM_BASE_EX, type);
156+
}
157+
147158
private static PBaseException handleInstanceNotAnException(Object type, Object instance) {
148159
/*
149-
* Instead of throwing the exception here, we throw it into the generator. That's what
150-
* CPython does
160+
* Instead of throwing the exception here, we replace the created exception with it. This is
161+
* done to match CPython's behavior of `generator.throw`
151162
*/
152163
return PythonObjectFactory.getUncached().createBaseException(TypeError, ErrorMessages.CALLING_N_SHOULD_HAVE_RETURNED_AN_INSTANCE_OF_BASE_EXCEPTION_NOT_P, new Object[]{type, instance});
153164
}
154165

155-
@Fallback
156-
PBaseException doError(Object type, @SuppressWarnings("unused") Object value) {
157-
throw raise().raise(TypeError, ErrorMessages.EXCEPTIONS_MUST_BE_CLASSES_OR_INSTANCES_DERIVING_FROM_BASE_EX, type);
158-
}
159-
160-
private PRaiseNode raise() {
161-
if (raiseNode == null) {
162-
CompilerDirectives.transferToInterpreterAndInvalidate();
163-
raiseNode = insert(PRaiseNode.create());
164-
}
165-
return raiseNode;
166-
}
167-
168-
private void checkExceptionClass(Object type) {
169-
if (isSubtypeNode == null) {
170-
CompilerDirectives.transferToInterpreterAndInvalidate();
171-
isSubtypeNode = insert(IsSubtypeNode.create());
172-
}
166+
private static void checkExceptionClass(Object type, IsSubtypeNode isSubtypeNode, PRaiseNode raiseNode) {
173167
if (!isSubtypeNode.execute(type, PythonBuiltinClassType.PBaseException)) {
174-
throw raise().raise(TypeError, ErrorMessages.EXCEPTIONS_MUST_BE_CLASSES_OR_INSTANCES_DERIVING_FROM_BASE_EX, type);
168+
throw raiseNode.raise(TypeError, ErrorMessages.EXCEPTIONS_MUST_BE_CLASSES_OR_INSTANCES_DERIVING_FROM_BASE_EX, type);
175169
}
176170
}
177171
}

0 commit comments

Comments
 (0)