# HG changeset patch # User Paul Boddie # Date 1339543212 -7200 # Node ID 8824b42d7b7131618a3b59796255cbd77d5dc06d # Parent 63a3ea888ac5b5b7fe13a6c037bc2e62c6022161 Separated function body inspection from module inspection in order to support deferred imports found in function code. diff -r 63a3ea888ac5 -r 8824b42d7b71 micropython/__init__.py --- a/micropython/__init__.py Tue Jun 12 23:54:41 2012 +0200 +++ b/micropython/__init__.py Wed Jun 13 01:20:12 2012 +0200 @@ -92,6 +92,8 @@ "Finalise the program." + self.importer.complete_modules() + # Need the tables to finalise. objtable = self.get_object_table() @@ -435,6 +437,7 @@ # Status information. + self.completed = 0 self.vacuumed = 0 self.finalised = 0 @@ -450,6 +453,28 @@ return self.modules[name] + def complete_modules(self): + + "Complete the processing of modules." + + if self.completed: + return + + processed = set() + modules = self.get_modules() + + while len(processed) != len(modules): + for module in modules: + if module in processed: + continue + if self.verbose: + print >>sys.stderr, "Completing", module.full_name() + module.process_functions() + processed.add(module) + modules = self.get_modules() + + self.completed = 1 + # General maintenance. def vacuum(self, objtable): diff -r 63a3ea888ac5 -r 8824b42d7b71 micropython/inspect.py --- a/micropython/inspect.py Tue Jun 12 23:54:41 2012 +0200 +++ b/micropython/inspect.py Wed Jun 13 01:20:12 2012 +0200 @@ -124,7 +124,12 @@ def process(self, module): - "Process the given 'module'." + """ + Process the given 'module', visiting only module-level code and only + extracting global declarations from functions. In order to support + deferred imports, the function code is processed separately after all + modules have been initially processed. + """ self.astnode = module @@ -144,12 +149,6 @@ self.finalise_attribute_usage() - # Then, visit each function, recording other names. - - for node, namespaces in self.functions: - self._visitFunctionBody(node, namespaces) - namespaces[-1].finalise_attribute_usage() - # Add references to other modules declared using the __all__ global. if self.has_key("__all__"): @@ -160,6 +159,19 @@ return processed + def process_functions(self): + + """ + Process all function bodies. Deferred imports may occur during this + process. + """ + + # Then, visit each function, recording other names. + + for node, namespaces in self.functions: + self._visitFunctionBody(node, namespaces) + namespaces[-1].finalise_attribute_usage() + def process_globals(self, node): """