1.1 --- a/micropython/inspect.py Sat Jul 02 02:41:33 2011 +0200
1.2 +++ b/micropython/inspect.py Sun Jul 03 20:11:13 2011 +0200
1.3 @@ -128,13 +128,9 @@
1.4
1.5 self.astnode = module
1.6
1.7 - # Add __name__ to the namespace by adding an explicit assignment to the
1.8 - # module.
1.9 + # Add __name__ to the namespace.
1.10
1.11 - module.node.nodes.insert(0, compiler.ast.Assign(
1.12 - [compiler.ast.AssName("__name__", 0)],
1.13 - compiler.ast.Const(self.full_name())
1.14 - ))
1.15 + self.store("__name__", self._visitConst(self.full_name()))
1.16
1.17 # First, visit module-level code, recording global names.
1.18
1.19 @@ -479,10 +475,6 @@
1.20
1.21 self._visitAttrUser(expr, attrname, node)
1.22
1.23 - elif self.builtins is not None:
1.24 - attr = self.builtins.get(attrname)
1.25 - self.use_specific_attribute(self.builtins.full_name(), attrname)
1.26 -
1.27 else:
1.28 attr = None
1.29 self.use_name(attrname, node)
1.30 @@ -510,6 +502,17 @@
1.31 else:
1.32 self.use_name(attrname, node.expr, value)
1.33
1.34 + def _visitConst(self, value):
1.35 +
1.36 + """
1.37 + Register the constant given by 'value', if necessary, returning the
1.38 + resulting object. The type name is noted as being used, thus preserving
1.39 + the class in any generated program.
1.40 + """
1.41 +
1.42 + self.use_specific_attribute("__builtins__", self.importer.get_constant_type_name(value))
1.43 + return self.importer.make_constant(value)
1.44 +
1.45 def _visitFunction(self, node, name):
1.46
1.47 """
1.48 @@ -642,7 +645,7 @@
1.49
1.50 for i, n in enumerate(node.nodes):
1.51 self.dispatch(n)
1.52 - self.importer.make_constant(i) # for __getitem__(i) at run-time
1.53 + self._visitConst(i) # for __getitem__(i) at run-time
1.54 return None
1.55
1.56 def visitAssName(self, node):
1.57 @@ -769,8 +772,10 @@
1.58 self.add_object(cls)
1.59
1.60 # Process the class body in its own namespace.
1.61 + # Add __name__ to the namespace.
1.62
1.63 self.namespaces.append(cls)
1.64 + self.store("__name__", self._visitConst(node.name))
1.65 self.dispatch(node.code)
1.66 self.namespaces.pop()
1.67
1.68 @@ -808,13 +813,7 @@
1.69 return self.OP(node)
1.70
1.71 def visitConst(self, node):
1.72 -
1.73 - # Register the constant, if necessary, returning the resulting object.
1.74 - # The type name is noted as being used, thus preserving the class in any
1.75 - # generated program.
1.76 -
1.77 - self.use_specific_attribute("__builtins__", self.importer.get_constant_type_name(node.value))
1.78 - return self.importer.make_constant(node.value)
1.79 + return self._visitConst(node.value)
1.80
1.81 def visitContinue(self, node):
1.82 self.NOP(node)
1.83 @@ -974,7 +973,7 @@
1.84
1.85 def visitKeyword(self, node):
1.86 self.dispatch(node.expr)
1.87 - self.importer.make_constant(node.name)
1.88 + self._visitConst(node.name)
1.89 self.keyword_names.add(node.name)
1.90 return None
1.91
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/tests/visitor_explicit.py Sun Jul 03 20:11:13 2011 +0200
2.3 @@ -0,0 +1,44 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +class Branch:
2.7 + def __init__(self, nodes):
2.8 + self.nodes = nodes
2.9 +
2.10 + def visit(self, visitor):
2.11 + return visitor.visitBranch(self)
2.12 +
2.13 +class Leaf:
2.14 + def __init__(self, value):
2.15 + self.value = value
2.16 +
2.17 + def visit(self, visitor):
2.18 + return visitor.visitLeaf(self)
2.19 +
2.20 +class Visitor:
2.21 + def visit(self, node):
2.22 + return node.visit(self)
2.23 +
2.24 + # Visitor-specific functionality.
2.25 +
2.26 + def visitBranch(self, node):
2.27 + sum = 0
2.28 + for node in node.nodes:
2.29 + sum += self.visit(node)
2.30 + return sum
2.31 +
2.32 + def visitLeaf(self, node):
2.33 + return node.value
2.34 +
2.35 +tree = \
2.36 + Branch((
2.37 + Branch((
2.38 + Leaf(10),
2.39 + Leaf(5)
2.40 + )),
2.41 + Leaf(2)
2.42 + ))
2.43 +
2.44 +visitor = Visitor()
2.45 +result_17 = visitor.visit(tree)
2.46 +
2.47 +# vim: tabstop=4 expandtab shiftwidth=4
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/tests/visitor_getattr.py Sun Jul 03 20:11:13 2011 +0200
3.3 @@ -0,0 +1,40 @@
3.4 +#!/usr/bin/env python
3.5 +
3.6 +class Branch:
3.7 + def __init__(self, nodes):
3.8 + self.nodes = nodes
3.9 +
3.10 +class Leaf:
3.11 + def __init__(self, value):
3.12 + self.value = value
3.13 +
3.14 +class Visitor:
3.15 + def visit(self, node):
3.16 + method = getattr(self, node.__class__.__name__)
3.17 + return method(node)
3.18 +
3.19 + # Visitor-specific functionality.
3.20 + # Note that getattr does not support "visit" + __class__.__name__.
3.21 +
3.22 + def Branch(self, node):
3.23 + sum = 0
3.24 + for node in node.nodes:
3.25 + sum += self.visit(node)
3.26 + return sum
3.27 +
3.28 + def Leaf(self, node):
3.29 + return node.value
3.30 +
3.31 +tree = \
3.32 + Branch((
3.33 + Branch((
3.34 + Leaf(10),
3.35 + Leaf(5)
3.36 + )),
3.37 + Leaf(2)
3.38 + ))
3.39 +
3.40 +visitor = Visitor()
3.41 +result_17 = visitor.visit(tree)
3.42 +
3.43 +# vim: tabstop=4 expandtab shiftwidth=4