micropython

Changeset

329:ac8d3b0cf626
2010-06-05 Paul Boddie raw files shortlog changelog graph 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.
micropython/data.py (file) tests/lambda_defaults_local_non_constant.py (file) tests/lambda_defaults_non_constant.py (file) tests/nested_functions_using_defaults.py (file)
     1.1 --- a/micropython/data.py	Sat Jun 05 01:39:16 2010 +0200
     1.2 +++ b/micropython/data.py	Sat Jun 05 15:11:26 2010 +0200
     1.3 @@ -206,6 +206,9 @@
     1.4              self.namespace[name] = Attr(None, self, name)
     1.5  
     1.6          attr = self.namespace[name]
     1.7 +        self._set_using_attr(attr, attr_or_value, single_assignment)
     1.8 +
     1.9 +    def _set_using_attr(self, attr, attr_or_value, single_assignment=1):
    1.10  
    1.11          # Handle attribute assignment as well as assignment of basic objects.
    1.12          # Attempt to fix the context if not explicitly defined.
    1.13 @@ -648,6 +651,11 @@
    1.14              self._context_values_str(), self.assignments
    1.15              )
    1.16  
    1.17 +    def __shortrepr__(self):
    1.18 +        return "Attr(%r, %s, %r)" % (
    1.19 +            self.position, shortrepr(self.parent), self.name
    1.20 +            )
    1.21 +
    1.22      def _context_values_str(self):
    1.23          l = []
    1.24          for (c, v) in self.context_values:
    1.25 @@ -1157,7 +1165,7 @@
    1.26              if isinstance(name, tuple):
    1.27                  self._add_parameters(name)
    1.28              else:
    1.29 -                self.set(name, None)
    1.30 +                self.set(name, Instance())
    1.31  
    1.32      def __repr__(self):
    1.33          if self.location is not None:
    1.34 @@ -1179,9 +1187,15 @@
    1.35  
    1.36      # Namespace-related methods.
    1.37  
    1.38 -    def store_default(self, value):
    1.39 +    def store_default(self, attr_or_value):
    1.40 +
    1.41 +        """
    1.42 +        Reserve space for defaults, set outside the function, potentially on a
    1.43 +        dynamic basis, using the 'attr_or_value'.
    1.44 +        """
    1.45 +
    1.46          attr = Attr(None, self, None)
    1.47 -        attr.update([self.get_context_and_value(value)], 1)
    1.48 +        self._set_using_attr(attr, attr_or_value)
    1.49          self.default_attrs.append(attr)
    1.50  
    1.51      def make_global(self, name):
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/tests/lambda_defaults_local_non_constant.py	Sat Jun 05 15:11:26 2010 +0200
     2.3 @@ -0,0 +1,13 @@
     2.4 +#!/usr/bin/env python
     2.5 +
     2.6 +def make_add(x):
     2.7 +    return lambda a, b=x: a + b
     2.8 +
     2.9 +def g(f, x):
    2.10 +    return f(x)
    2.11 +
    2.12 +add_2 = make_add(2)
    2.13 +result_3 = add_2(1)
    2.14 +result2_3 = g(add_2, 1)
    2.15 +
    2.16 +# vim: tabstop=4 expandtab shiftwidth=4
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/tests/lambda_defaults_non_constant.py	Sat Jun 05 15:11:26 2010 +0200
     3.3 @@ -0,0 +1,16 @@
     3.4 +#!/usr/bin/env python
     3.5 +
     3.6 +x = 1
     3.7 +x = 2
     3.8 +
     3.9 +def make_add():
    3.10 +    return lambda a, b=x: a + x
    3.11 +
    3.12 +def g(f, x):
    3.13 +    return f(x)
    3.14 +
    3.15 +add_2 = make_add()
    3.16 +result_3 = add_2(1)
    3.17 +result2_3 = g(add_2, 1)
    3.18 +
    3.19 +# vim: tabstop=4 expandtab shiftwidth=4
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/tests/nested_functions_using_defaults.py	Sat Jun 05 15:11:26 2010 +0200
     4.3 @@ -0,0 +1,11 @@
     4.4 +#!/usr/bin/env python
     4.5 +
     4.6 +def a(x):
     4.7 +    def b(p=x):
     4.8 +        return p
     4.9 +    return b
    4.10 +
    4.11 +f = a(2)
    4.12 +result_2 = f()
    4.13 +
    4.14 +# vim: tabstop=4 expandtab shiftwidth=4