# HG changeset patch # User Paul Boddie # Date 1192575630 -7200 # Node ID c6101e50e385fb06828ca38b3cda107c146d6308 # Parent fbafd6f0be075f18da54a61e4be4da25b9564c60 Added "pre-registration" of submodules in order to handle "from package import module" situations where __all__ does not declare the module concerned. Added __all__ list processing. Added a name to the Module object initialisation. Fixed initialisation of Class objects. Fixed import names and aliases. Added "object" to the built-in names. Added some missing AST node handlers. diff -r fbafd6f0be07 -r c6101e50e385 micropython/__init__.py --- a/micropython/__init__.py Tue Oct 16 01:29:33 2007 +0200 +++ b/micropython/__init__.py Wed Oct 17 01:00:30 2007 +0200 @@ -168,6 +168,8 @@ if len(path) > 1: if not d: return None # NOTE: Import error (package not found). + else: + self.add_submodules(d, module) path_so_far = path[:1] for p in path[1:]: @@ -188,6 +190,9 @@ else: submodule = self.load_from_file(filename, module_name) + if d: + self.add_submodules(d, module) + # Store the submodule within its parent module. module.namespace[p] = submodule @@ -209,10 +214,7 @@ if module_name is None: module_name = "__main__" - if not self.modules.has_key(module_name): - self.modules[module_name] = module = micropython.inspect.Module(self) - else: - module = self.modules[module_name] + module = self.add_module(module_name) print "Parsing", name module.parse(name) @@ -224,4 +226,28 @@ #print "Loaded", module_name, "with namespace", module.namespace.keys() return module + def add_module(self, module_name): + + """ + Return the module with the given 'module_name', adding a new module + object if one does not already exist. + """ + + if not self.modules.has_key(module_name): + self.modules[module_name] = module = micropython.inspect.Module(module_name, self) + else: + module = self.modules[module_name] + return module + + def add_submodules(self, pathname, module): + + """ + Work around insufficient __all__ declarations and examine the directory + with the given 'pathname', adding submodules to the given 'module'. + """ + + for filename in os.listdir(pathname): + submodule = os.path.splitext(filename)[0] + module.namespace[submodule] = self.add_module(module.name + "." + submodule) + # vim: tabstop=4 expandtab shiftwidth=4 diff -r fbafd6f0be07 -r c6101e50e385 micropython/inspect.py --- a/micropython/inspect.py Tue Oct 16 01:29:33 2007 +0200 +++ b/micropython/inspect.py Wed Oct 17 01:00:30 2007 +0200 @@ -56,19 +56,26 @@ "An inspected module." - def __init__(self, importer=None): + def __init__(self, name, importer=None): ASTVisitor.__init__(self) self.visitor = self + self.name = name self.importer = importer + # Module namespace. + self.namespace = {} self.namespace.update(builtins_namespace) + # Current expression state. + + self.expr = None + + # Namespace state. + self.in_global = 1 self.in_init = 0 - self.expr = None - self.classes = [] self.module = None @@ -78,10 +85,16 @@ def process(self, module): self.module = module - return self.dispatch(module) + processed = self.dispatch(module) + if self.namespace.has_key("__all__"): + all = self.namespace["__all__"] + if isinstance(all, compiler.ast.List): + for n in all.nodes: + self.namespace[n.value] = self.importer.add_module(self.name + "." + n.value) + return processed def default(self, node, *args): - raise InspectError, (None, node) + raise InspectError, node.__class__ def dispatch(self, node, *args): return ASTVisitor.dispatch(self, node, *args) @@ -100,6 +113,8 @@ def NOP(self, node): return node + visitAdd = NOP + visitAnd = NOP def visitAssign(self, node): @@ -131,7 +146,7 @@ if not self.in_global: raise InspectError, "Class is %s not global: cannot handle this reasonably." % node.name else: - cls = Class(node) + cls = Class(node.name) for base in node.bases: base_ref = self.dispatch(base) if base_ref is None: @@ -168,7 +183,7 @@ for name, alias in node.names: if name != "*": - self.namespace[alias] = module.namespace[name] + self.namespace[alias or name] = module.namespace[name] else: for n in module.namespace.keys(): self.namespace[n] = module.namespace[n] @@ -206,10 +221,12 @@ raise InspectError, "Please use the micropython.Importer class for code which uses the 'import' statement." for name, alias in node.names: - self.namespace[alias] = self.importer.load(name, 1) + self.namespace[alias or name] = self.importer.load(name, 1) return None + visitLeftShift = NOP + visitList = NOP def visitModule(self, node): @@ -232,6 +249,8 @@ visitRaise = NOP + visitRightShift = NOP + def visitStmt(self, node): for n in node.nodes: self.dispatch(n) @@ -247,6 +266,8 @@ self.dispatch(node.else_) return None + visitTuple = NOP + class Self: "A reference to an object within a method." @@ -267,7 +288,7 @@ 'TabError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', - 'ValueError', 'Warning', 'ZeroDivisionError']: + 'ValueError', 'Warning', 'ZeroDivisionError', 'object']: builtins_namespace[key] = Class(key) # vim: tabstop=4 expandtab shiftwidth=4