Skip to content

Commit cc7bc38

Browse files
committed
symtable: fix yield/yield from to set generator flag
1 parent 6c151b7 commit cc7bc38

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

symtable/make_symtable_test.py

+9
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ def method(self):
173173
def fn():
174174
from potato import *
175175
''', "exec", SyntaxError),
176+
# Yield
177+
('''\
178+
def f():
179+
yield
180+
''', "exec"),
181+
('''\
182+
def f():
183+
yield from range(10)
184+
''', "exec"),
176185
]
177186

178187
def dump_bool(b):

symtable/symtable.go

+2
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ func (st *SymTable) Parse(Ast ast.Ast) {
357357
if node.Value != nil {
358358
st.ReturnsValue = true
359359
}
360+
case *ast.Yield, *ast.YieldFrom:
361+
st.Generator = true
360362
}
361363
return true
362364
})

symtable/symtable_data_test.go

+85
Original file line numberDiff line numberDiff line change
@@ -2699,4 +2699,89 @@ var symtableTestData = []struct {
26992699
Children: Children{},
27002700
}, nil, ""},
27012701
{"def fn():\n from potato import *\n", "exec", nil, py.SyntaxError, "import * only allowed at module level"},
2702+
{"def f():\n yield\n ", "exec", &SymTable{
2703+
Type: ModuleBlock,
2704+
Name: "top",
2705+
Lineno: 0,
2706+
Unoptimized: optTopLevel,
2707+
Nested: false,
2708+
Free: false,
2709+
ChildFree: false,
2710+
Generator: false,
2711+
Varargs: false,
2712+
Varkeywords: false,
2713+
ReturnsValue: false,
2714+
NeedsClassClosure: false,
2715+
Varnames: []string{},
2716+
Symbols: Symbols{
2717+
"f": Symbol{
2718+
Flags: DefLocal,
2719+
Scope: ScopeLocal,
2720+
},
2721+
},
2722+
Children: Children{
2723+
&SymTable{
2724+
Type: FunctionBlock,
2725+
Name: "f",
2726+
Lineno: 1,
2727+
Unoptimized: 0,
2728+
Nested: false,
2729+
Free: false,
2730+
ChildFree: false,
2731+
Generator: true,
2732+
Varargs: false,
2733+
Varkeywords: false,
2734+
ReturnsValue: false,
2735+
NeedsClassClosure: false,
2736+
Varnames: []string{},
2737+
Symbols: Symbols{},
2738+
Children: Children{},
2739+
},
2740+
},
2741+
}, nil, ""},
2742+
{"def f():\n yield from range(10)\n ", "exec", &SymTable{
2743+
Type: ModuleBlock,
2744+
Name: "top",
2745+
Lineno: 0,
2746+
Unoptimized: optTopLevel,
2747+
Nested: false,
2748+
Free: false,
2749+
ChildFree: false,
2750+
Generator: false,
2751+
Varargs: false,
2752+
Varkeywords: false,
2753+
ReturnsValue: false,
2754+
NeedsClassClosure: false,
2755+
Varnames: []string{},
2756+
Symbols: Symbols{
2757+
"f": Symbol{
2758+
Flags: DefLocal,
2759+
Scope: ScopeLocal,
2760+
},
2761+
},
2762+
Children: Children{
2763+
&SymTable{
2764+
Type: FunctionBlock,
2765+
Name: "f",
2766+
Lineno: 1,
2767+
Unoptimized: 0,
2768+
Nested: false,
2769+
Free: false,
2770+
ChildFree: false,
2771+
Generator: true,
2772+
Varargs: false,
2773+
Varkeywords: false,
2774+
ReturnsValue: false,
2775+
NeedsClassClosure: false,
2776+
Varnames: []string{},
2777+
Symbols: Symbols{
2778+
"range": Symbol{
2779+
Flags: DefUse,
2780+
Scope: ScopeGlobalImplicit,
2781+
},
2782+
},
2783+
Children: Children{},
2784+
},
2785+
},
2786+
}, nil, ""},
27022787
}

0 commit comments

Comments
 (0)