1.1 --- a/micropython/inspect.py Sun May 11 18:43:58 2008 +0200
1.2 +++ b/micropython/inspect.py Sun May 11 21:50:30 2008 +0200
1.3 @@ -112,6 +112,9 @@
1.4 if not (self.namespaces and isinstance(self.namespaces[-1], Function)):
1.5 self.all_objects.add(obj)
1.6
1.7 + def store_lambda(self, obj):
1.8 + self.all_objects.add(obj)
1.9 +
1.10 def store_instance_attr(self, name):
1.11
1.12 "Record instance attribute 'name' in the current class."
1.13 @@ -147,6 +150,59 @@
1.14 self.dispatch(n)
1.15 return Instance()
1.16
1.17 + def _visitFunction(self, node, name):
1.18 +
1.19 + """
1.20 + Return a function object for the function defined by 'node' with the
1.21 + given 'name'. If a lambda expression is being visited, 'name' should be
1.22 + None.
1.23 + """
1.24 +
1.25 + function = Function(
1.26 + name,
1.27 + self.get_parent(),
1.28 + node.argnames,
1.29 + node.defaults,
1.30 + (node.flags & 4 != 0),
1.31 + (node.flags & 8 != 0),
1.32 + self,
1.33 + node
1.34 + )
1.35 +
1.36 + # Make a back reference from the node for code generation.
1.37 +
1.38 + node.unit = function
1.39 +
1.40 + # Process the defaults.
1.41 +
1.42 + for n in node.defaults:
1.43 + self.expr = self.dispatch(n)
1.44 + if isinstance(self.expr, Attr):
1.45 + function.store_default(self.expr.value)
1.46 + else:
1.47 + function.store_default(self.expr)
1.48 +
1.49 + # Enter the function.
1.50 +
1.51 + self.namespaces.append(function)
1.52 +
1.53 + # Current namespace is the function.
1.54 + # Previous namespace is the class.
1.55 +
1.56 + if name == "__init__" and isinstance(self.namespaces[-2], Class):
1.57 + self.in_init = 1
1.58 +
1.59 + self.dispatch(node.code)
1.60 + self.in_init = 0
1.61 + self.namespaces.pop()
1.62 +
1.63 + if name is not None:
1.64 + self.store(name, function)
1.65 + else:
1.66 + self.store_lambda(function)
1.67 +
1.68 + return function
1.69 +
1.70 visitAdd = OP
1.71
1.72 visitAnd = OP
1.73 @@ -298,46 +354,7 @@
1.74 return None
1.75
1.76 def visitFunction(self, node):
1.77 - function = Function(
1.78 - node.name,
1.79 - self.get_parent(),
1.80 - node.argnames,
1.81 - node.defaults,
1.82 - (node.flags & 4 != 0),
1.83 - (node.flags & 8 != 0),
1.84 - self,
1.85 - node
1.86 - )
1.87 -
1.88 - # Make a back reference from the node for code generation.
1.89 -
1.90 - node.unit = function
1.91 -
1.92 - # Process the defaults.
1.93 -
1.94 - for n in node.defaults:
1.95 - self.expr = self.dispatch(n)
1.96 - if isinstance(self.expr, Attr):
1.97 - function.store_default(self.expr.value)
1.98 - else:
1.99 - function.store_default(self.expr)
1.100 -
1.101 - # Enter the function.
1.102 -
1.103 - self.namespaces.append(function)
1.104 -
1.105 - # Current namespace is the function.
1.106 - # Previous namespace is the class.
1.107 -
1.108 - if node.name == "__init__" and isinstance(self.namespaces[-2], Class):
1.109 - self.in_init = 1
1.110 -
1.111 - self.dispatch(node.code)
1.112 - self.in_init = 0
1.113 - self.namespaces.pop()
1.114 -
1.115 - self.store(node.name, function)
1.116 - return function
1.117 + return self._visitFunction(node, node.name)
1.118
1.119 visitGenExpr = OP
1.120
1.121 @@ -397,7 +414,8 @@
1.122 self.keyword_names.add(node.name)
1.123 return None
1.124
1.125 - visitLambda = OP
1.126 + def visitLambda(self, node):
1.127 + return self._visitFunction(node, None)
1.128
1.129 visitLeftShift = OP
1.130