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,
args=[],
star_arg=None, starstar_arg=None)
- self.yields = yields
def declare_generator_body(self, env):
prefix = env.next_id(env.scope_prefix)
# ----- Generator resume code
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)
- 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("default: /* CPython raises the right error here */");
resume_code.put_finish_refcount_context()
if self.has_return_value:
error(node.pos, "'yield' outside function")
self.yields.append(node)
- node.label_num = len(self.yields)
def visit_ReturnStatNode(self, node):
if node.value:
collector.visitchildren(node)
if collector.yields:
+ for i, yield_expr in enumerate(collector.yields):
+ yield_expr.label_num = i + 1
+
gbody = Nodes.GeneratorBodyDefNode(pos=node.pos,
name=node.name,
- body=node.body,
- yields=collector.yields)
+ body=node.body)
generator = Nodes.GeneratorDefNode(pos=node.pos,
name=node.name,
args=node.args,