# HG changeset patch # User Paul Boddie # Date 1275743486 -7200 # Node ID ac8d3b0cf626b4b7aa2ad35324247f9d0d9356d1 # Parent 62c4e5c5a2540bdb5d0379d092f6acdd81a1404e Changed parameter initialisation to explicitly use Instance() instead of None. Made the default parameter value code use the same mechanisms as other attribute-setting code. Added tests of lambdas and nested functions with locally defined defaults. diff -r 62c4e5c5a254 -r ac8d3b0cf626 micropython/data.py --- a/micropython/data.py Sat Jun 05 01:39:16 2010 +0200 +++ b/micropython/data.py Sat Jun 05 15:11:26 2010 +0200 @@ -206,6 +206,9 @@ self.namespace[name] = Attr(None, self, name) attr = self.namespace[name] + self._set_using_attr(attr, attr_or_value, single_assignment) + + def _set_using_attr(self, attr, attr_or_value, single_assignment=1): # Handle attribute assignment as well as assignment of basic objects. # Attempt to fix the context if not explicitly defined. @@ -648,6 +651,11 @@ self._context_values_str(), self.assignments ) + def __shortrepr__(self): + return "Attr(%r, %s, %r)" % ( + self.position, shortrepr(self.parent), self.name + ) + def _context_values_str(self): l = [] for (c, v) in self.context_values: @@ -1157,7 +1165,7 @@ if isinstance(name, tuple): self._add_parameters(name) else: - self.set(name, None) + self.set(name, Instance()) def __repr__(self): if self.location is not None: @@ -1179,9 +1187,15 @@ # Namespace-related methods. - def store_default(self, value): + def store_default(self, attr_or_value): + + """ + Reserve space for defaults, set outside the function, potentially on a + dynamic basis, using the 'attr_or_value'. + """ + attr = Attr(None, self, None) - attr.update([self.get_context_and_value(value)], 1) + self._set_using_attr(attr, attr_or_value) self.default_attrs.append(attr) def make_global(self, name): diff -r 62c4e5c5a254 -r ac8d3b0cf626 tests/lambda_defaults_local_non_constant.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/lambda_defaults_local_non_constant.py Sat Jun 05 15:11:26 2010 +0200 @@ -0,0 +1,13 @@ +#!/usr/bin/env python + +def make_add(x): + return lambda a, b=x: a + b + +def g(f, x): + return f(x) + +add_2 = make_add(2) +result_3 = add_2(1) +result2_3 = g(add_2, 1) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 62c4e5c5a254 -r ac8d3b0cf626 tests/lambda_defaults_non_constant.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/lambda_defaults_non_constant.py Sat Jun 05 15:11:26 2010 +0200 @@ -0,0 +1,16 @@ +#!/usr/bin/env python + +x = 1 +x = 2 + +def make_add(): + return lambda a, b=x: a + x + +def g(f, x): + return f(x) + +add_2 = make_add() +result_3 = add_2(1) +result2_3 = g(add_2, 1) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 62c4e5c5a254 -r ac8d3b0cf626 tests/nested_functions_using_defaults.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/nested_functions_using_defaults.py Sat Jun 05 15:11:26 2010 +0200 @@ -0,0 +1,11 @@ +#!/usr/bin/env python + +def a(x): + def b(p=x): + return p + return b + +f = a(2) +result_2 = f() + +# vim: tabstop=4 expandtab shiftwidth=4