2525 */
2626package com .oracle .graal .python .nodes .generator ;
2727
28+ import static com .oracle .graal .python .runtime .exception .PythonErrorType .RuntimeError ;
2829import static com .oracle .graal .python .runtime .exception .PythonErrorType .StopIteration ;
2930
3031import com .oracle .graal .python .builtins .objects .PNone ;
3132import com .oracle .graal .python .nodes .PRaiseNode ;
3233import com .oracle .graal .python .nodes .expression .ExpressionNode ;
34+ import com .oracle .graal .python .nodes .object .IsBuiltinClassProfile ;
3335import com .oracle .graal .python .nodes .statement .StatementNode ;
36+ import com .oracle .graal .python .runtime .exception .PException ;
3437import com .oracle .graal .python .runtime .exception .ReturnException ;
3538import com .oracle .graal .python .runtime .exception .YieldException ;
3639import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
3740import com .oracle .truffle .api .CompilerDirectives ;
41+ import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
3842import com .oracle .truffle .api .frame .VirtualFrame ;
3943import com .oracle .truffle .api .profiles .BranchProfile ;
4044
@@ -50,6 +54,7 @@ public final class GeneratorReturnTargetNode extends ExpressionNode implements G
5054 private final BranchProfile returnProfile = BranchProfile .create ();
5155 private final BranchProfile fallthroughProfile = BranchProfile .create ();
5256 private final BranchProfile yieldProfile = BranchProfile .create ();
57+ @ CompilationFinal private IsBuiltinClassProfile errorProfile ;
5358
5459 private final int flagSlot ;
5560
@@ -68,6 +73,14 @@ public int getFlagSlot() {
6873 return flagSlot ;
6974 }
7075
76+ private IsBuiltinClassProfile getErrorProfile () {
77+ if (errorProfile == null ) {
78+ CompilerDirectives .transferToInterpreterAndInvalidate ();
79+ errorProfile = IsBuiltinClassProfile .create ();
80+ }
81+ return errorProfile ;
82+ }
83+
7184 @ Override
7285 public Object execute (VirtualFrame frame ) {
7386 if (!gen .isActive (frame , flagSlot )) {
@@ -76,7 +89,14 @@ public Object execute(VirtualFrame frame) {
7689 }
7790
7891 try {
79- body .executeVoid (frame );
92+ try {
93+ body .executeVoid (frame );
94+ } catch (PException pe ) {
95+ // PEP 479 - StopIteration raised from generator body needs to be wrapped in
96+ // RuntimeError
97+ pe .expectStopIteration (getErrorProfile ());
98+ throw raise .raise (RuntimeError , pe .getExceptionObject (), "generator raised StopIteration" );
99+ }
80100 fallthroughProfile .enter ();
81101 throw raise .raise (StopIteration );
82102 } catch (YieldException eye ) {
0 commit comments