1.1 --- a/micropython/inspect.py Sun Apr 06 21:26:06 2008 +0200
1.2 +++ b/micropython/inspect.py Mon Apr 07 02:20:21 2008 +0200
1.3 @@ -576,11 +576,26 @@
1.4 "An inspected function."
1.5
1.6 def __init__(self, name, parent, argnames, defaults, has_star, has_dstar, global_namespace=None, node=None):
1.7 +
1.8 + """
1.9 + Initialise the function with the given 'name', 'parent', list of
1.10 + 'argnames', list of 'defaults', the 'has_star' flag (indicating the
1.11 + presence of a * parameter), the 'has_dstar' flag (indicating the
1.12 + presence of a ** parameter), optional 'global_namespace', and optional
1.13 + AST 'node'.
1.14 + """
1.15 +
1.16 NamespaceDict.__init__(self, global_namespace)
1.17 self.name = name
1.18 self.parent = parent
1.19 self.argnames = argnames
1.20 self.defaults = defaults
1.21 + self.has_star = has_star
1.22 + self.has_dstar = has_dstar
1.23 + self.node = node
1.24 +
1.25 + # Initialise the positional names.
1.26 +
1.27 self.positional_names = self.argnames[:]
1.28 if has_dstar:
1.29 self.dstar_name = self.positional_names[-1]
1.30 @@ -588,9 +603,10 @@
1.31 if has_star:
1.32 self.star_name = self.positional_names[-1]
1.33 del self.positional_names[-1]
1.34 - self.has_star = has_star
1.35 - self.has_dstar = has_dstar
1.36 - self.node = node
1.37 +
1.38 + # Initialise default storage.
1.39 +
1.40 + self.default_attrs = []
1.41
1.42 # Caches.
1.43
1.44 @@ -622,6 +638,11 @@
1.45 self.name, self.parent, self.argnames, self.defaults, self.has_star, self.has_dstar
1.46 )
1.47
1.48 + def store_default(self, value):
1.49 + attr = Attr(None, self, None, value)
1.50 + attr.update(value, 1)
1.51 + self.default_attrs.append(attr)
1.52 +
1.53 def make_global(self, name):
1.54 if name not in self.argnames and not self.has_key(name):
1.55 self.globals.add(name)
1.56 @@ -677,7 +698,14 @@
1.57
1.58 def finalise_attributes(self):
1.59
1.60 - "Make sure all attributes are fully defined."
1.61 + """
1.62 + Make sure all attributes (locals) are fully defined. Note that locals
1.63 + are not attributes in the sense of class, module or instance attributes.
1.64 + Defaults are also finalised by this method.
1.65 + """
1.66 +
1.67 + for i, default in enumerate(self.default_attrs):
1.68 + default.position = i
1.69
1.70 i = None
1.71 for i, name in enumerate(self.argnames):
1.72 @@ -688,6 +716,8 @@
1.73 for i, attr in enumerate(self.locals().values()):
1.74 attr.position = i + j
1.75
1.76 + # NOTE: May also have temporary variables.
1.77 +
1.78 def function_from_method(self):
1.79
1.80 "Make a function from a method."
1.81 @@ -1055,6 +1085,17 @@
1.82
1.83 node.unit = function
1.84
1.85 + # Process the defaults.
1.86 +
1.87 + for n in node.defaults:
1.88 + self.expr = self.dispatch(n)
1.89 + if isinstance(self.expr, Attr):
1.90 + function.store_default(self.expr.value)
1.91 + else:
1.92 + function.store_default(self.expr)
1.93 +
1.94 + # Enter the function.
1.95 +
1.96 self.namespaces.append(function)
1.97
1.98 # Current namespace is the function.