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