1.1 --- a/micropython/__init__.py Sat Oct 20 01:36:44 2007 +0200
1.2 +++ b/micropython/__init__.py Sun Oct 21 00:53:20 2007 +0200
1.3 @@ -27,6 +27,7 @@
1.4
1.5 importer = Importer(sys.path)
1.6 importer.load_from_file(filename)
1.7 +importer.vacuum()
1.8
1.9 Such importer objects are the most convenient mechanism through which the
1.10 functionality of the micropython package may be accessed.
1.11 @@ -152,10 +153,10 @@
1.12 package exists.
1.13 """
1.14
1.15 + if self.modules.has_key(name) and self.modules[name].loaded:
1.16 + #print "Cached (%s)" % name
1.17 + return self.modules[name]
1.18 print "Loading", name
1.19 - if self.modules.has_key(name) and self.modules[name].loaded:
1.20 - print "Cached", name
1.21 - return self.modules[name]
1.22
1.23 # Split the name into path components, and try to find the uppermost in
1.24 # the search path.
1.25 @@ -163,7 +164,7 @@
1.26 path = name.split(".")
1.27 m = self.find_in_path(path[0])
1.28 if not m:
1.29 - print "Not found", path[0]
1.30 + print "Not found (%s)" % path[0]
1.31 return None # NOTE: Import error.
1.32 d, filename = m
1.33
1.34 @@ -176,7 +177,7 @@
1.35
1.36 if len(path) > 1:
1.37 if not d:
1.38 - print "No package", filename
1.39 + print "No package (%s)" % filename
1.40 return None # NOTE: Import error (package not found).
1.41 else:
1.42 self.add_submodules(d, module)
1.43 @@ -189,7 +190,7 @@
1.44
1.45 m = self.find(d, p)
1.46 if not m:
1.47 - print "Not found", p
1.48 + print "Not found (%s)" % p
1.49 return None # NOTE: Import error.
1.50 d, filename = m
1.51 module_name = ".".join(path_so_far)
1.52 @@ -225,15 +226,15 @@
1.53 module = self.add_module(module_name)
1.54 if not module.loaded and module not in self.loading:
1.55 self.loading.add(module)
1.56 - print "Parsing", name
1.57 + #print "Parsing", name
1.58 module.parse(name)
1.59 - print "Done", name
1.60 + #print "Done", name
1.61 self.loading.remove(module)
1.62 module.loaded = 1
1.63
1.64 # Record the module.
1.65
1.66 - print "Loaded", module_name #, "with namespace", module.namespace.keys()
1.67 + #print "Loaded", module_name, "with namespace", module.namespace.keys()
1.68 return module
1.69
1.70 def add_module(self, module_name):
2.1 --- a/micropython/inspect.py Sat Oct 20 01:36:44 2007 +0200
2.2 +++ b/micropython/inspect.py Sun Oct 21 00:53:20 2007 +0200
2.3 @@ -42,12 +42,23 @@
2.4 def keys(self):
2.5 return self.namespace.keys()
2.6
2.7 -class Class(NamespaceDict):
2.8 +class Naming:
2.9 +
2.10 + "A mix-in providing naming conveniences."
2.11 +
2.12 + def full_name(self):
2.13 + if self.name is not None:
2.14 + return self.module_name + "." + self.name
2.15 + else:
2.16 + return self.module_name
2.17 +
2.18 +class Class(NamespaceDict, Naming):
2.19
2.20 "An inspected class."
2.21
2.22 - def __init__(self, name):
2.23 + def __init__(self, name, module_name):
2.24 self.name = name
2.25 + self.module_name = module_name
2.26 self.bases = []
2.27 self.namespace = {}
2.28 self.instattr = set()
2.29 @@ -55,25 +66,66 @@
2.30 def add_base(self, base):
2.31 self.bases.append(base)
2.32
2.33 -class Function(NamespaceDict):
2.34 + def __repr__(self):
2.35 + return "Class(%r, %r)" % (self.name, self.module_name)
2.36 +
2.37 + def class_attributes(self):
2.38 +
2.39 + "Return all class attributes, indicating the class which provides them."
2.40 +
2.41 + attr = {}
2.42 + reversed_bases = self.bases[:]
2.43 + reversed_bases.reverse()
2.44 + for cls in reversed_bases:
2.45 + attr.update(cls.class_attributes())
2.46 + for name, value in self.namespace.items():
2.47 + attr[name] = self.full_name()
2.48 + return attr
2.49 +
2.50 + def instance_attributes(self):
2.51 +
2.52 + """
2.53 + Return all attributes for an instance, indicating either the class which
2.54 + provides them or that the instance itself provides them.
2.55 + """
2.56 +
2.57 + attr = {}
2.58 + attr.update(self.class_attributes())
2.59 + for name in self.instattr:
2.60 + if attr.has_key(name):
2.61 + print "Instance attribute", name, "overrides class attribute."
2.62 + attr[name] = None
2.63 + return attr
2.64 +
2.65 +class Function(NamespaceDict, Naming):
2.66
2.67 "An inspected function."
2.68
2.69 - def __init__(self, name, argnames, has_star, has_dstar):
2.70 + def __init__(self, name, module_name, argnames, has_star, has_dstar):
2.71 self.name = name
2.72 + self.module_name = module_name
2.73 self.argnames = argnames
2.74 self.has_star = has_star
2.75 self.has_dstar = has_dstar
2.76 self.namespace = {}
2.77
2.78 -class UnresolvedName(NamespaceDict):
2.79 + def __repr__(self):
2.80 + return "Function(%r, %r, %r, %r, %r)" % (
2.81 + self.name, self.module_name, self.argnames, self.has_star, self.has_dstar
2.82 + )
2.83 +
2.84 +class UnresolvedName(NamespaceDict, Naming):
2.85
2.86 "A module, class or function which was mentioned but could not be imported."
2.87
2.88 - def __init__(self, name):
2.89 + def __init__(self, name, module_name):
2.90 self.name = name
2.91 + self.module_name = module_name
2.92 self.namespace = {}
2.93
2.94 + def __repr__(self):
2.95 + return "UnresolvedName(%r, %r)" % (self.name, self.module_name)
2.96 +
2.97 class Module(ASTVisitor, NamespaceDict):
2.98
2.99 "An inspected module."
2.100 @@ -120,6 +172,12 @@
2.101 if isinstance(value, Module) and not value.loaded:
2.102 del self.namespace[name]
2.103
2.104 + def full_name(self):
2.105 + return self.name
2.106 +
2.107 + def __repr__(self):
2.108 + return "Module(%r)" % self.name
2.109 +
2.110 # Namespace methods.
2.111
2.112 def store(self, name, obj):
2.113 @@ -148,6 +206,8 @@
2.114
2.115 visitAnd = NOP
2.116
2.117 + visitAssert = NOP
2.118 +
2.119 def visitAssign(self, node):
2.120 self.expr = self.dispatch(node.expr)
2.121 for n in node.nodes:
2.122 @@ -173,21 +233,27 @@
2.123
2.124 visitAugAssign = NOP
2.125
2.126 + visitBackquote = NOP
2.127 +
2.128 visitBitand = NOP
2.129
2.130 visitBitor = NOP
2.131
2.132 + visitBitxor = NOP
2.133 +
2.134 + visitBreak = NOP
2.135 +
2.136 visitCallFunc = NOP
2.137
2.138 def visitClass(self, node):
2.139 if not self.in_global:
2.140 - raise InspectError, "Class is %s not global: cannot handle this reasonably." % node.name
2.141 + print "Class %r in %r is not global: ignored." % (node.name, self)
2.142 else:
2.143 - cls = Class(node.name)
2.144 + cls = Class(node.name, self.name)
2.145 for base in node.bases:
2.146 base_ref = self.dispatch(base)
2.147 if base_ref is None:
2.148 - raise InspectError, "Base class %s for class %s is not found: it may be hidden in some way." % (base, node.name)
2.149 + raise InspectError, "Base class %r for class %r in %r is not found: it may be hidden in some way." % (base, self, cls)
2.150 cls.add_base(base_ref)
2.151
2.152 # Make an entry for the class.
2.153 @@ -200,12 +266,28 @@
2.154
2.155 return node
2.156
2.157 + visitCompare = NOP
2.158 +
2.159 visitConst = NOP
2.160
2.161 + visitContinue = NOP
2.162 +
2.163 + visitDecorators = NOP
2.164 +
2.165 visitDict = NOP
2.166
2.167 visitDiscard = NOP
2.168
2.169 + visitDiv = NOP
2.170 +
2.171 + visitEllipsis = NOP
2.172 +
2.173 + visitExec = NOP
2.174 +
2.175 + visitExpression = NOP
2.176 +
2.177 + visitFloorDiv = NOP
2.178 +
2.179 visitFor = NOP
2.180
2.181 def visitFrom(self, node):
2.182 @@ -219,12 +301,12 @@
2.183
2.184 for name, alias in node.names:
2.185 if name != "*":
2.186 - if module is not None:
2.187 + if module is not None and module.namespace.has_key(name):
2.188 self.namespace[alias or name] = attr = module.namespace[name]
2.189 if isinstance(attr, Module) and not attr.loaded:
2.190 self.importer.load(attr.name)
2.191 else:
2.192 - self.namespace[alias or name] = UnresolvedName(name)
2.193 + self.namespace[alias or name] = UnresolvedName(name, node.modname)
2.194 else:
2.195 if module is not None:
2.196 for n in module.namespace.keys():
2.197 @@ -237,6 +319,7 @@
2.198 def visitFunction(self, node):
2.199 function = Function(
2.200 node.name,
2.201 + self.name,
2.202 node.argnames,
2.203 has_star=(node.flags & 4 != 0),
2.204 has_dstar=(node.flags & 8 != 0)
2.205 @@ -253,12 +336,24 @@
2.206 self.store(node.name, function)
2.207 return None
2.208
2.209 + visitGenExpr = NOP
2.210 +
2.211 + visitGenExprFor = NOP
2.212 +
2.213 + visitGenExprIf = NOP
2.214 +
2.215 + visitGenExprInner = NOP
2.216 +
2.217 def visitGetattr(self, node):
2.218 expr = self.dispatch(node.expr)
2.219 - if expr is not None and isinstance(expr, Module):
2.220 - return expr.namespace.get(node.attrname)
2.221 - else:
2.222 - return builtins_namespace.get(node.attrname)
2.223 + if expr is not None:
2.224 + if isinstance(expr, Module):
2.225 + return expr.namespace.get(node.attrname)
2.226 + elif isinstance(expr, UnresolvedName):
2.227 + return UnresolvedName(node.attrname, expr.full_name())
2.228 + return builtins_namespace.get(node.attrname)
2.229 +
2.230 + visitGlobal = NOP
2.231
2.232 def visitIf(self, node):
2.233 for test, body in node.tests:
2.234 @@ -273,23 +368,35 @@
2.235
2.236 for name, alias in node.names:
2.237 if alias is not None:
2.238 - self.namespace[alias] = self.importer.load(name, 1)
2.239 + self.namespace[alias] = self.importer.load(name, 1) or UnresolvedName(None, name)
2.240 else:
2.241 - self.namespace[name.split(".")[0]] = self.importer.load(name)
2.242 + self.namespace[name.split(".")[0]] = self.importer.load(name) or UnresolvedName(None, name.split(".")[0])
2.243
2.244 return None
2.245
2.246 + visitInvert = NOP
2.247 +
2.248 + visitKeyword = NOP
2.249 +
2.250 + visitLambda = NOP
2.251 +
2.252 visitLeftShift = NOP
2.253
2.254 visitList = NOP
2.255
2.256 + visitListComp = NOP
2.257 +
2.258 + visitListCompFor = NOP
2.259 +
2.260 + visitListCompIf = NOP
2.261 +
2.262 + visitMod = NOP
2.263 +
2.264 def visitModule(self, node):
2.265 return self.dispatch(node.node)
2.266
2.267 visitMul = NOP
2.268
2.269 - visitMod = NOP
2.270 -
2.271 def visitName(self, node):
2.272 name = node.name
2.273 if name == "self":
2.274 @@ -307,15 +414,27 @@
2.275
2.276 visitPass = NOP
2.277
2.278 + visitPower = NOP
2.279 +
2.280 + visitPrint = NOP
2.281 +
2.282 + visitPrintnl = NOP
2.283 +
2.284 visitRaise = NOP
2.285
2.286 + visitReturn = NOP
2.287 +
2.288 visitRightShift = NOP
2.289
2.290 + visitSlice = NOP
2.291 +
2.292 def visitStmt(self, node):
2.293 for n in node.nodes:
2.294 self.dispatch(n)
2.295 return None
2.296
2.297 + visitSub = NOP
2.298 +
2.299 visitSubscript = NOP
2.300
2.301 def visitTryExcept(self, node):
2.302 @@ -326,14 +445,28 @@
2.303 self.dispatch(node.else_)
2.304 return None
2.305
2.306 + visitTryFinally = NOP
2.307 +
2.308 visitTuple = NOP
2.309
2.310 + visitUnaryAdd = NOP
2.311 +
2.312 + visitUnarySub = NOP
2.313 +
2.314 + visitWhile = NOP
2.315 +
2.316 + visitWith = NOP
2.317 +
2.318 + visitYield = NOP
2.319 +
2.320 class Self:
2.321
2.322 "A reference to an object within a method."
2.323
2.324 pass
2.325
2.326 +# Built-in types initialisation.
2.327 +
2.328 builtins_namespace = {}
2.329 for key in ['ArithmeticError', 'AssertionError', 'AttributeError',
2.330 'BaseException', 'DeprecationWarning', 'EOFError', 'Ellipsis',
2.331 @@ -348,7 +481,9 @@
2.332 'TabError', 'True', 'TypeError', 'UnboundLocalError',
2.333 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError',
2.334 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning',
2.335 - 'ValueError', 'Warning', 'ZeroDivisionError', 'object']:
2.336 - builtins_namespace[key] = Class(key)
2.337 + 'ValueError', 'Warning', 'ZeroDivisionError',
2.338 + 'bool', 'buffer', 'complex', 'dict', 'file', 'float', 'int', 'list',
2.339 + 'long', 'object', 'slice', 'str', 'tuple', 'type', 'unicode']:
2.340 + builtins_namespace[key] = Class(key, "__builtins__")
2.341
2.342 # vim: tabstop=4 expandtab shiftwidth=4