1.1 --- a/micropython/inspect.py Sat Jun 12 03:04:08 2010 +0200
1.2 +++ b/micropython/inspect.py Sat Jun 12 21:33:36 2010 +0200
1.3 @@ -75,7 +75,6 @@
1.4 from micropython.common import *
1.5 from micropython.data import *
1.6 import compiler.ast
1.7 -from compiler.visitor import ASTVisitor
1.8
1.9 # Program visitors.
1.10
1.11 @@ -271,8 +270,20 @@
1.12
1.13 "Record attribute or local 'name', storing 'obj'."
1.14
1.15 + # Store in the module.
1.16 +
1.17 if not self.namespaces:
1.18 - self.set(name, obj, not self.in_loop)
1.19 + if self.used_in_scope(name, "builtins"):
1.20 + raise InspectError("Name %r already used as a built-in." % name)
1.21 + else:
1.22 + self.set(name, obj, not self.in_loop)
1.23 +
1.24 + # Or store locally.
1.25 +
1.26 + elif self.namespaces[-1].used_in_scope(name, "global"):
1.27 + raise InspectError("Name %r already used as global." % name)
1.28 + elif self.namespaces[-1].used_in_scope(name, "builtins"):
1.29 + raise InspectError("Name %r already used as a built-in." % name)
1.30 else:
1.31 self.namespaces[-1].set(name, obj, not self.in_loop)
1.32
1.33 @@ -390,7 +401,7 @@
1.34 # Visitor methods.
1.35
1.36 def default(self, node, *args):
1.37 - raise InspectError(self.full_name(), node, "Node class %r is not supported." % node.__class__)
1.38 + raise InspectError("Node class %r is not supported." % node.__class__)
1.39
1.40 def NOP(self, node):
1.41 for n in node.getChildNodes():
1.42 @@ -549,7 +560,7 @@
1.43 def visitAssName(self, node):
1.44 if hasattr(node, "flags") and node.flags == "OP_DELETE":
1.45 print "Warning: deletion of attribute %r in %r is not supported." % (node.name, self.full_name())
1.46 - #raise InspectError(self.full_name(), node, "Deletion of attribute %r is not supported." % node.name)
1.47 + #raise InspectError("Deletion of attribute %r is not supported." % node.name)
1.48
1.49 self.store(node.name, self.expr)
1.50 self.define_attribute_user(node)
1.51 @@ -582,7 +593,7 @@
1.52 elif isinstance(node.node, compiler.ast.Getattr):
1.53 self.visitAssAttr(node.node)
1.54 else:
1.55 - raise InspectError(self.full_name(), node, "AugAssign(Slice or Subscript)")
1.56 + raise InspectError("AugAssign(Slice or Subscript)")
1.57
1.58 return None
1.59
1.60 @@ -621,13 +632,11 @@
1.61 expr = self.dispatch(base)
1.62 if isinstance(expr, Attr):
1.63 if expr.assignments != 1:
1.64 - raise InspectError(self.full_name(), node,
1.65 - "Base class %r for %r is not constant." % (base, cls.full_name()))
1.66 + raise InspectError("Base class %r for %r is not constant." % (base, cls.full_name()))
1.67 else:
1.68 cls.add_base(expr.get_value())
1.69 else: # if expr is None:
1.70 - raise InspectError(self.full_name(), node,
1.71 - "Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name()))
1.72 + raise InspectError("Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name()))
1.73
1.74 # NOTE: Potentially dubious measure to permit __init__ availability.
1.75 # If no bases exist, adopt the 'object' class.
1.76 @@ -819,7 +828,7 @@
1.77 for name in node.names:
1.78 ns = self.namespaces[-1]
1.79 if not ns.make_global(name):
1.80 - raise InspectError(ns.full_name(), node, "Name %r is global and local in %r" % (name, ns.full_name()))
1.81 + raise InspectError("Name %r is global and local in %r" % (name, ns.full_name()))
1.82
1.83 # The name is recorded in an earlier process.
1.84
1.85 @@ -899,7 +908,6 @@
1.86
1.87 if self.importer.predefined_constants.has_key(name):
1.88 attr = self.importer.get_predefined_constant(name)
1.89 - node._scope = "constant"
1.90
1.91 # Locals.
1.92
1.93 @@ -909,7 +917,6 @@
1.94 # Note usage of the local (potentially a class attribute).
1.95
1.96 self.use_specific_attribute(None, name)
1.97 - node._scope = "local"
1.98
1.99 # Globals.
1.100
1.101 @@ -919,21 +926,33 @@
1.102 # Note usage of the module attribute.
1.103
1.104 self.use_specific_attribute(self.full_name(), name)
1.105 - node._scope = "global"
1.106 +
1.107 + # Note global usage in any local namespace.
1.108 +
1.109 + if self.namespaces:
1.110 + if not self.namespaces[-1].note_scope(name, "global"):
1.111 + raise InspectError("Name %r cannot be used as global." % name)
1.112
1.113 # Builtins.
1.114
1.115 elif self.builtins is not None and self.builtins.has_key(name):
1.116 attr = self.builtins[name]
1.117 self.use_specific_attribute(self.builtins.full_name(), name)
1.118 - node._scope = "builtins"
1.119 +
1.120 + # Note builtins usage in any local namespace.
1.121 +
1.122 + if self.namespaces:
1.123 + if not self.namespaces[-1].note_scope(name, "builtins"):
1.124 + raise InspectError("Name %r cannot be used as a built-in." % name)
1.125 + else:
1.126 + if not self.note_scope(name, "builtins"):
1.127 + raise InspectError("Name %r cannot be used as a built-in." % name)
1.128
1.129 # Unknown.
1.130
1.131 else:
1.132 attr = None
1.133 self.use_name(name)
1.134 - node._scope = "unknown"
1.135
1.136 return attr
1.137