Skip to content

Commit d05b884

Browse files
committed
Fix calling send/throw from yield from
1 parent ab7d4dd commit d05b884

File tree

1 file changed

+47
-42
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/generator

1 file changed

+47
-42
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/generator/YieldFromNode.java

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,11 @@
4242

4343
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4444
import com.oracle.graal.python.builtins.objects.PNone;
45-
import com.oracle.graal.python.builtins.objects.exception.GetExceptionTracebackNode;
4645
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
4746
import com.oracle.graal.python.builtins.objects.function.PArguments;
48-
import com.oracle.graal.python.builtins.objects.function.PKeyword;
49-
import com.oracle.graal.python.nodes.SpecialMethodNames;
47+
import com.oracle.graal.python.builtins.objects.traceback.GetTracebackNode;
5048
import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
51-
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
5249
import com.oracle.graal.python.nodes.call.CallNode;
53-
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
5450
import com.oracle.graal.python.nodes.control.GetIteratorExpressionNode.GetIteratorNode;
5551
import com.oracle.graal.python.nodes.control.GetNextNode;
5652
import com.oracle.graal.python.nodes.expression.ExpressionNode;
@@ -61,7 +57,6 @@
6157
import com.oracle.truffle.api.CompilerDirectives;
6258
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
6359
import com.oracle.truffle.api.frame.VirtualFrame;
64-
import com.oracle.truffle.api.profiles.BranchProfile;
6560

6661
public class YieldFromNode extends AbstractYieldNode implements GeneratorControlNode {
6762
@Child private GetIteratorNode iter = GetIteratorNode.create();
@@ -70,27 +65,27 @@ public class YieldFromNode extends AbstractYieldNode implements GeneratorControl
7065

7166
@Child private GetAttributeNode getValue;
7267

73-
@Child private LookupInheritedAttributeNode getCloseNode;
68+
@Child private GetAttributeNode getCloseNode;
7469
@Child private CallNode callCloseNode;
7570

76-
@Child private LookupInheritedAttributeNode getThrowNode;
71+
@Child private GetAttributeNode getThrowNode;
7772
@Child private CallNode callThrowNode;
7873
@Child private GetClassNode getExcClassNode;
7974

80-
@Child private LookupAndCallBinaryNode getSendNode;
75+
@Child private GetAttributeNode getSendNode;
8176
@Child private CallNode callSendNode;
8277

83-
@Child private GetExceptionTracebackNode getExceptionTracebackNode;
78+
@Child private GetTracebackNode getTracebackNode;
8479

8580
@Child private IsBuiltinClassProfile stopIterProfile1 = IsBuiltinClassProfile.create();
8681
@Child private IsBuiltinClassProfile stopIterProfile2 = IsBuiltinClassProfile.create();
8782
@Child private IsBuiltinClassProfile stopIterProfile3 = IsBuiltinClassProfile.create();
8883
@Child private IsBuiltinClassProfile genExitProfile = IsBuiltinClassProfile.create();
84+
@Child private IsBuiltinClassProfile hasNoCloseProfile = IsBuiltinClassProfile.create();
85+
@Child private IsBuiltinClassProfile hasNoThrowProfile = IsBuiltinClassProfile.create();
8986

9087
@CompilationFinal private int iteratorSlot;
9188

92-
private final BranchProfile noThrow = BranchProfile.create();
93-
9489
@Child private ExpressionNode right;
9590

9691
public YieldFromNode(ExpressionNode right) {
@@ -143,9 +138,17 @@ public Object execute(VirtualFrame frame) {
143138
// ....raise _e
144139
if (genExitProfile.profileException(_e, PythonBuiltinClassType.GeneratorExit)) {
145140
access.setIterator(frame, iteratorSlot, null);
146-
Object close = getGetCloseNode().execute(_i);
147-
if (close != PNone.NO_VALUE) {
148-
getCallCloseNode().execute(frame, close, new Object[]{_i}, PKeyword.EMPTY_KEYWORDS);
141+
Object close = null;
142+
try {
143+
close = getGetCloseNode().executeObject(frame, _i);
144+
} catch (PException pe) {
145+
if (!hasNoCloseProfile.profileException(pe, PythonBuiltinClassType.AttributeError)) {
146+
// TODO msimacek: CPython writes the exception (!=AttributeError) as
147+
// unraisable and discards it
148+
}
149+
}
150+
if (close != null) {
151+
getCallCloseNode().execute(frame, close);
149152
}
150153
throw _e;
151154
}
@@ -161,24 +164,26 @@ public Object execute(VirtualFrame frame) {
161164
// ........except StopIteration as _e:
162165
// ............_r = _e.value
163166
// ............break
164-
Object _m = getGetThrowNode().execute(_i);
165-
if (_m == PNone.NO_VALUE) {
166-
noThrow.enter();
167+
Object _m;
168+
try {
169+
_m = getGetThrowNode().executeObject(frame, _i);
170+
} catch (PException pe) {
171+
pe.expectAttributeError(hasNoThrowProfile);
167172
access.setIterator(frame, iteratorSlot, null);
168173
throw _e;
169-
} else {
170-
try {
171-
PBaseException pythonException = ((PException) _s).setCatchingFrameAndGetEscapedException(frame);
172-
_y = getCallThrowNode().execute(frame, _m,
173-
new Object[]{_i, getExceptionClassNode().execute(pythonException),
174-
pythonException,
175-
ensureGetTracebackNode().execute(frame, pythonException)},
176-
PKeyword.EMPTY_KEYWORDS);
177-
} catch (PException _e2) {
178-
access.setIterator(frame, iteratorSlot, null);
179-
_e2.expectStopIteration(stopIterProfile2);
180-
return getGetValue().executeObject(frame, _e2.setCatchingFrameAndGetEscapedException(frame));
174+
}
175+
try {
176+
PBaseException pythonException = ((PException) _s).setCatchingFrameAndGetEscapedException(frame);
177+
Object excType = getExceptionClassNode().execute(pythonException);
178+
Object excTraceback = ensureGetTracebackNode().execute(((PException) _s).getTraceback());
179+
if (excTraceback == null) {
180+
excTraceback = PNone.NONE;
181181
}
182+
_y = getCallThrowNode().execute(frame, _m, excType, pythonException, excTraceback);
183+
} catch (PException _e2) {
184+
access.setIterator(frame, iteratorSlot, null);
185+
_e2.expectStopIteration(stopIterProfile2);
186+
return getGetValue().executeObject(frame, _e2.setCatchingFrameAndGetEscapedException(frame));
182187
}
183188
} else {
184189
// else:
@@ -195,9 +200,9 @@ public Object execute(VirtualFrame frame) {
195200
gotNothing.enter();
196201
_y = next.execute(frame, _i);
197202
} else {
198-
Object send = getGetSendNode().executeObject(frame, _i, "send");
203+
Object send = getGetSendNode().executeObject(frame, _i);
199204
// send will be bound at this point
200-
_y = getCallSendNode().execute(frame, send, new Object[]{_s}, PKeyword.EMPTY_KEYWORDS);
205+
_y = getCallSendNode().execute(frame, send, _s);
201206
}
202207
} catch (PException _e) {
203208
access.setIterator(frame, iteratorSlot, null);
@@ -225,10 +230,10 @@ private GetClassNode getExceptionClassNode() {
225230
return getExcClassNode;
226231
}
227232

228-
private LookupInheritedAttributeNode getGetCloseNode() {
233+
private GetAttributeNode getGetCloseNode() {
229234
if (getCloseNode == null) {
230235
CompilerDirectives.transferToInterpreterAndInvalidate();
231-
getCloseNode = insert(LookupInheritedAttributeNode.create("close"));
236+
getCloseNode = insert(GetAttributeNode.create("close"));
232237
}
233238
return getCloseNode;
234239
}
@@ -241,10 +246,10 @@ private CallNode getCallCloseNode() {
241246
return callCloseNode;
242247
}
243248

244-
private LookupInheritedAttributeNode getGetThrowNode() {
249+
private GetAttributeNode getGetThrowNode() {
245250
if (getThrowNode == null) {
246251
CompilerDirectives.transferToInterpreterAndInvalidate();
247-
getThrowNode = insert(LookupInheritedAttributeNode.create("throw"));
252+
getThrowNode = insert(GetAttributeNode.create("throw"));
248253
}
249254
return getThrowNode;
250255
}
@@ -257,10 +262,10 @@ private CallNode getCallThrowNode() {
257262
return callThrowNode;
258263
}
259264

260-
private LookupAndCallBinaryNode getGetSendNode() {
265+
private GetAttributeNode getGetSendNode() {
261266
if (getSendNode == null) {
262267
CompilerDirectives.transferToInterpreterAndInvalidate();
263-
getSendNode = insert(LookupAndCallBinaryNode.create(SpecialMethodNames.__GETATTRIBUTE__));
268+
getSendNode = insert(GetAttributeNode.create("send"));
264269
}
265270
return getSendNode;
266271
}
@@ -273,12 +278,12 @@ private CallNode getCallSendNode() {
273278
return callSendNode;
274279
}
275280

276-
private GetExceptionTracebackNode ensureGetTracebackNode() {
277-
if (getExceptionTracebackNode == null) {
281+
private GetTracebackNode ensureGetTracebackNode() {
282+
if (getTracebackNode == null) {
278283
CompilerDirectives.transferToInterpreterAndInvalidate();
279-
getExceptionTracebackNode = insert(GetExceptionTracebackNode.create());
284+
getTracebackNode = insert(GetTracebackNode.create());
280285
}
281-
return getExceptionTracebackNode;
286+
return getTracebackNode;
282287
}
283288

284289
public void setIteratorSlot(int slot) {

0 commit comments

Comments
 (0)