Commit 42928403 authored by Vitja Makarov's avatar Vitja Makarov

Don't store yield expressions list as yields can be copied

parent 7970cad9
...@@ -2944,11 +2944,10 @@ class GeneratorBodyDefNode(DefNode): ...@@ -2944,11 +2944,10 @@ class GeneratorBodyDefNode(DefNode):
child_attrs = ["args", "star_arg", "starstar_arg", "body", "decorators"] child_attrs = ["args", "star_arg", "starstar_arg", "body", "decorators"]
def __init__(self, pos=None, name=None, body=None, yields=None): def __init__(self, pos=None, name=None, body=None):
super(GeneratorBodyDefNode, self).__init__(pos=pos, body=body, name=name, doc=None, super(GeneratorBodyDefNode, self).__init__(pos=pos, body=body, name=name, doc=None,
args=[], args=[],
star_arg=None, starstar_arg=None) star_arg=None, starstar_arg=None)
self.yields = yields
def declare_generator_body(self, env): def declare_generator_body(self, env):
prefix = env.next_id(env.scope_prefix) prefix = env.next_id(env.scope_prefix)
...@@ -3030,7 +3029,11 @@ class GeneratorBodyDefNode(DefNode): ...@@ -3030,7 +3029,11 @@ class GeneratorBodyDefNode(DefNode):
# ----- Generator resume code # ----- Generator resume code
resume_code.putln("switch (%s->%s.resume_label) {" % (Naming.cur_scope_cname, Naming.obj_base_cname)); resume_code.putln("switch (%s->%s.resume_label) {" % (Naming.cur_scope_cname, Naming.obj_base_cname));
resume_code.putln("case 0: goto %s;" % first_run_label) resume_code.putln("case 0: goto %s;" % first_run_label)
for yield_expr in self.yields:
from ParseTreeTransforms import YieldNodeCollector
collector = YieldNodeCollector()
collector.visitchildren(self)
for yield_expr in collector.yields:
resume_code.putln("case %d: goto %s;" % (yield_expr.label_num, yield_expr.label_name)); resume_code.putln("case %d: goto %s;" % (yield_expr.label_num, yield_expr.label_name));
resume_code.putln("default: /* CPython raises the right error here */"); resume_code.putln("default: /* CPython raises the right error here */");
resume_code.put_finish_refcount_context() resume_code.put_finish_refcount_context()
......
...@@ -1332,7 +1332,6 @@ class YieldNodeCollector(TreeVisitor): ...@@ -1332,7 +1332,6 @@ class YieldNodeCollector(TreeVisitor):
if self.has_return_value: if self.has_return_value:
error(node.pos, "'yield' outside function") error(node.pos, "'yield' outside function")
self.yields.append(node) self.yields.append(node)
node.label_num = len(self.yields)
def visit_ReturnStatNode(self, node): def visit_ReturnStatNode(self, node):
if node.value: if node.value:
...@@ -1370,10 +1369,12 @@ class MarkClosureVisitor(CythonTransform): ...@@ -1370,10 +1369,12 @@ class MarkClosureVisitor(CythonTransform):
collector.visitchildren(node) collector.visitchildren(node)
if collector.yields: if collector.yields:
for i, yield_expr in enumerate(collector.yields):
yield_expr.label_num = i + 1
gbody = Nodes.GeneratorBodyDefNode(pos=node.pos, gbody = Nodes.GeneratorBodyDefNode(pos=node.pos,
name=node.name, name=node.name,
body=node.body, body=node.body)
yields=collector.yields)
generator = Nodes.GeneratorDefNode(pos=node.pos, generator = Nodes.GeneratorDefNode(pos=node.pos,
name=node.name, name=node.name,
args=node.args, args=node.args,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment