1.1 --- a/micropython/data.py Sat Jun 05 15:11:26 2010 +0200
1.2 +++ b/micropython/data.py Sun Jun 06 03:03:09 2010 +0200
1.3 @@ -560,6 +560,16 @@
1.4
1.5 self.context_values.update(context_values)
1.6
1.7 + def is_strict_constant(self):
1.8 +
1.9 + """
1.10 + Return whether this attribute references something that can be regarded
1.11 + as being constant.
1.12 + """
1.13 +
1.14 + value = self.get_value()
1.15 + return not (value is None or isinstance(value, Instance))
1.16 +
1.17 def is_static_attribute(self):
1.18
1.19 """
1.20 @@ -1102,11 +1112,6 @@
1.21 self.astnode = node
1.22 node._def = self
1.23
1.24 - # For lambda functions with defaults, add a context argument.
1.25 -
1.26 - if name is None and defaults:
1.27 - self.argnames.insert(0, "<context>")
1.28 -
1.29 # Initialise the positional names.
1.30
1.31 self.positional_names = self.argnames[:]
1.32 @@ -1147,6 +1152,7 @@
1.33
1.34 # Image generation details.
1.35
1.36 + self.dynamic = None
1.37 self.location = None
1.38 self.code_location = None
1.39 self.code_body_location = None
1.40 @@ -1185,7 +1191,7 @@
1.41 def get_body_block(self):
1.42 return self.body_block
1.43
1.44 - # Namespace-related methods.
1.45 + # Defaults-related methods.
1.46
1.47 def store_default(self, attr_or_value):
1.48
1.49 @@ -1198,6 +1204,34 @@
1.50 self._set_using_attr(attr, attr_or_value)
1.51 self.default_attrs.append(attr)
1.52
1.53 + def make_dynamic(self):
1.54 +
1.55 + "Return whether this function must be handled using a dynamic object."
1.56 +
1.57 + if self.dynamic is None:
1.58 + for attr in self.default_attrs:
1.59 + if not attr.is_strict_constant():
1.60 + self.dynamic = 1
1.61 + self._make_dynamic()
1.62 + break
1.63 + else:
1.64 + self.dynamic = 0
1.65 +
1.66 + return self.dynamic
1.67 +
1.68 + is_dynamic = make_dynamic
1.69 +
1.70 + def _make_dynamic(self):
1.71 +
1.72 + "Where functions have dynamic defaults, add a context argument."
1.73 +
1.74 + name = "<context>"
1.75 + self.argnames.insert(0, name)
1.76 + self.positional_names.insert(0, name)
1.77 + self.set(name, Instance())
1.78 +
1.79 + # Namespace-related methods.
1.80 +
1.81 def make_global(self, name):
1.82 if name not in self.argnames and not self.has_key(name):
1.83 self.globals.add(name)