1.1 --- a/docs/syspython.txt Fri Nov 29 01:38:25 2013 +0100
1.2 +++ b/docs/syspython.txt Fri Nov 29 18:08:59 2013 +0100
1.3 @@ -166,9 +166,9 @@
1.4 ...
1.5
1.6 if something:
1.7 - storelocal(inner, static(outer.inner))
1.8 + storelocal(inner, static(module.outer.inner))
1.9 else:
1.10 - storelocal(inner, static("outer.inner#2"))
1.11 + storelocal(inner, static("module.outer.inner#2"))
1.12 return inner
1.13
1.14 Where functions are dynamic - that is they have additional state associated
1.15 @@ -189,7 +189,7 @@
1.16 def inner(__context__, y, z=x):
1.17 localnames(__context__, y, z)
1.18 ...
1.19 - storelocal(inner, makedynamic(static(outer.inner), x))
1.20 + storelocal(inner, makedynamic(static(module.outer.inner), x))
1.21 return inner
1.22
1.23 The special makedynamic invocation creates an object referring to the function
1.24 @@ -199,6 +199,21 @@
1.25 to any default information that needs to be transferred to the local
1.26 namespace.
1.27
1.28 +Function defaults that do not require a dynamic function are initialised using
1.29 +the setdefaults special function as follows:
1.30 +
1.31 + class C:
1.32 + const = ...
1.33 + def f(a, b=const):
1.34 + ...
1.35 +
1.36 + ...
1.37 + setdefaults(module.f, module.C.const)
1.38 +
1.39 +Where multiple definitions of a name are involved, setdefaults may also be
1.40 +used in a store operation and can be assumed to return a reference to the
1.41 +function whose defaults are being set.
1.42 +
1.43 Imports
1.44 -------
1.45
2.1 --- a/micropython/syspython.py Fri Nov 29 01:38:25 2013 +0100
2.2 +++ b/micropython/syspython.py Fri Nov 29 18:08:59 2013 +0100
2.3 @@ -387,32 +387,41 @@
2.4 if not used_by_unit(node):
2.5 return compiler.ast.Stmt([])
2.6
2.7 - self.units.append(node.unit)
2.8 + if not self.processing_definition(node):
2.9
2.10 - try:
2.11 - if not self.processing_definition(node):
2.12 -
2.13 - # Generate rebindings of functions where multiple definitions
2.14 - # exist within a scope. Also generate dynamic function object
2.15 - # initialisation.
2.16 + # Generate rebindings of functions where multiple definitions
2.17 + # exist within a scope. Also generate dynamic function object
2.18 + # initialisation.
2.19
2.20 - fn = node.unit
2.21 - if fn.name == fn.original_name and not fn.is_dynamic():
2.22 - return compiler.ast.Stmt([])
2.23 - else:
2.24 - if fn.is_dynamic():
2.25 - ref = compiler.ast.CallFunc(
2.26 - special_name("makedynamic"),
2.27 - [quoted_ref(fn)] + fn.defaults
2.28 - )
2.29 - else:
2.30 - ref = quoted_ref(fn)
2.31 + fn = node.unit
2.32 + if fn.is_dynamic():
2.33 + ref = compiler.ast.CallFunc(
2.34 + special_name("makedynamic"),
2.35 + [quoted_ref(fn)] + [self.dispatch(n) for n in fn.defaults]
2.36 + )
2.37 + elif fn.defaults:
2.38 + ref = compiler.ast.CallFunc(
2.39 + special_name("setdefaults"),
2.40 + [quoted_ref(fn)] + [self.dispatch(n) for n in fn.defaults]
2.41 + )
2.42 + if fn.name == fn.original_name:
2.43 + return ref
2.44 + elif fn.name == fn.original_name:
2.45 + return compiler.ast.Stmt([])
2.46 + else:
2.47 + ref = quoted_ref(fn)
2.48
2.49 - return self.store_value(fn.parent, "local", fn.original_name, ref)
2.50 - else:
2.51 + return self.store_value(fn.parent, "local", fn.original_name, ref)
2.52 +
2.53 + # Where this function is being processed, visit the definition in its
2.54 + # entirety.
2.55 +
2.56 + else:
2.57 + self.units.append(node.unit)
2.58 + try:
2.59 return self._visitFunctionDefinition(node)
2.60 - finally:
2.61 - self.units.pop()
2.62 + finally:
2.63 + self.units.pop()
2.64
2.65 def _visitFunctionDefinition(self, node):
2.66 fn = node.unit
2.67 @@ -440,8 +449,6 @@
2.68 definitions = self.process_definitions(node)
2.69 self.current_definition = current
2.70
2.71 - defaults = [self.dispatch(n) for n in node.defaults]
2.72 -
2.73 # NOTE: Any required defaults should be copied into the local namespace
2.74 # NOTE: using the __context__ reference to the instance of the function.
2.75
2.76 @@ -449,7 +456,7 @@
2.77
2.78 code = self.dispatch(node.code)
2.79
2.80 - return compiler.ast.Function(node.decorators, node.name, node.argnames, defaults, node.flags, node.doc,
2.81 + return compiler.ast.Function(node.decorators, node.name, node.argnames, node.defaults, node.flags, node.doc,
2.82 compiler.ast.Stmt(localnames + globalnames + definitions + code.nodes))
2.83
2.84 visitGlobal = NOP