lenv.scope_class.type.typeptr_cname,
Naming.empty_tuple))
# TODO: error handling
- # The code below assumes the local variables are innitially NULL
+ code.put_gotref(Naming.cur_scope_cname)
+ # The code below is because we assume the local variables are
+ # innitially NULL.
# Note that it is unsafe to decref the scope at this point.
for entry in lenv.arg_entries + lenv.var_entries:
if entry.in_closure and entry.type.is_pyobject:
+ code.put_gotref(entry.cname) # so the refnanny doesn't whine
code.put_var_decref_clear(entry)
if env.is_closure_scope:
if lenv.is_closure_scope:
+ code.put_gotref(outer_scope_cname)
code.put_decref(outer_scope_cname, env.scope_class.type)
code.putln("%s = (%s)%s;" % (
outer_scope_cname,
if arg.is_generic: # or arg.needs_conversion:
if arg.needs_conversion:
code.putln("PyObject *%s = 0;" % arg.hdr_cname)
- elif not entry.in_closure:
+ elif not arg.entry.in_closure:
code.put_var_declaration(arg.entry)
def generate_keyword_list(self, code):
def create_class_from_scope(self, node, target_module_scope):
- print node.entry.scope.is_closure_scope
-
as_name = "%s%s" % (Naming.closure_class_prefix, node.entry.cname)
func_scope = node.local_scope
func_scope.scope_class = entry
class_scope = entry.type.scope
if node.entry.scope.is_closure_scope:
- print "yes", class_scope
class_scope.declare_var(pos=node.pos,
name=Naming.outer_scope_cname, # this could conflict?
cname=Naming.outer_scope_cname,
entry = Scope.lookup(self, name)
if entry is not None:
if entry.scope is not self and entry.scope.is_closure_scope:
- print "making new entry for", entry.cname, "in", self
# The actual c fragment for the different scopes differs
# on the outside and inside, so we make a new entry
entry.in_closure = True
return entry
def mangle_closure_cnames(self, outer_scope_cname):
- print "mangling", self
for entry in self.entries.values():
- print entry.name, entry.in_closure, entry.from_closure
if entry.from_closure:
cname = entry.outer_entry.cname
if cname.startswith(Naming.cur_scope_cname):
elif entry.in_closure:
entry.original_cname = entry.cname
entry.cname = "%s->%s" % (Naming.cur_scope_cname, entry.cname)
- print entry.cname
-
+
class ClosureScope(LocalScope):
is_closure_scope = True
--- /dev/null
+__doc__ = u"""
+>>> f = add_n(3)
+>>> f(2)
+5
+
+>>> a(5)()
+8
+"""
+
+def add_n(int n):
+ def f(int x):
+ return x+n
+ return f
+
+def a(int x):
+ def b():
+ def c():
+ return 3+x
+ return c()
+ return b