25
25
*/
26
26
package com .oracle .graal .python .nodes .generator ;
27
27
28
+ import static com .oracle .graal .python .runtime .exception .PythonErrorType .RuntimeError ;
28
29
import static com .oracle .graal .python .runtime .exception .PythonErrorType .StopIteration ;
29
30
30
31
import com .oracle .graal .python .builtins .objects .PNone ;
31
32
import com .oracle .graal .python .nodes .PRaiseNode ;
32
33
import com .oracle .graal .python .nodes .expression .ExpressionNode ;
34
+ import com .oracle .graal .python .nodes .object .IsBuiltinClassProfile ;
33
35
import com .oracle .graal .python .nodes .statement .StatementNode ;
36
+ import com .oracle .graal .python .runtime .exception .PException ;
34
37
import com .oracle .graal .python .runtime .exception .ReturnException ;
35
38
import com .oracle .graal .python .runtime .exception .YieldException ;
36
39
import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
37
40
import com .oracle .truffle .api .CompilerDirectives ;
41
+ import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
38
42
import com .oracle .truffle .api .frame .VirtualFrame ;
39
43
import com .oracle .truffle .api .profiles .BranchProfile ;
40
44
@@ -50,6 +54,7 @@ public final class GeneratorReturnTargetNode extends ExpressionNode implements G
50
54
private final BranchProfile returnProfile = BranchProfile .create ();
51
55
private final BranchProfile fallthroughProfile = BranchProfile .create ();
52
56
private final BranchProfile yieldProfile = BranchProfile .create ();
57
+ @ CompilationFinal private IsBuiltinClassProfile errorProfile ;
53
58
54
59
private final int flagSlot ;
55
60
@@ -68,6 +73,14 @@ public int getFlagSlot() {
68
73
return flagSlot ;
69
74
}
70
75
76
+ private IsBuiltinClassProfile getErrorProfile () {
77
+ if (errorProfile == null ) {
78
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
79
+ errorProfile = IsBuiltinClassProfile .create ();
80
+ }
81
+ return errorProfile ;
82
+ }
83
+
71
84
@ Override
72
85
public Object execute (VirtualFrame frame ) {
73
86
if (!gen .isActive (frame , flagSlot )) {
@@ -76,7 +89,14 @@ public Object execute(VirtualFrame frame) {
76
89
}
77
90
78
91
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
+ }
80
100
fallthroughProfile .enter ();
81
101
throw raise .raise (StopIteration );
82
102
} catch (YieldException eye ) {
0 commit comments