micropython

Changeset

546:8824b42d7b71
2012-06-13 Paul Boddie raw files shortlog changelog graph Separated function body inspection from module inspection in order to support deferred imports found in function code.
micropython/__init__.py (file) micropython/inspect.py (file)
     1.1 --- a/micropython/__init__.py	Tue Jun 12 23:54:41 2012 +0200
     1.2 +++ b/micropython/__init__.py	Wed Jun 13 01:20:12 2012 +0200
     1.3 @@ -92,6 +92,8 @@
     1.4  
     1.5          "Finalise the program."
     1.6  
     1.7 +        self.importer.complete_modules()
     1.8 +
     1.9          # Need the tables to finalise.
    1.10  
    1.11          objtable = self.get_object_table()
    1.12 @@ -435,6 +437,7 @@
    1.13  
    1.14          # Status information.
    1.15  
    1.16 +        self.completed = 0
    1.17          self.vacuumed = 0
    1.18          self.finalised = 0
    1.19  
    1.20 @@ -450,6 +453,28 @@
    1.21  
    1.22          return self.modules[name]
    1.23  
    1.24 +    def complete_modules(self):
    1.25 +
    1.26 +        "Complete the processing of modules."
    1.27 +
    1.28 +        if self.completed:
    1.29 +            return
    1.30 +
    1.31 +        processed = set()
    1.32 +        modules = self.get_modules()
    1.33 +
    1.34 +        while len(processed) != len(modules):
    1.35 +            for module in modules:
    1.36 +                if module in processed:
    1.37 +                    continue
    1.38 +                if self.verbose:
    1.39 +                    print >>sys.stderr, "Completing", module.full_name()
    1.40 +                module.process_functions()
    1.41 +                processed.add(module)
    1.42 +            modules = self.get_modules()
    1.43 +
    1.44 +        self.completed = 1
    1.45 +
    1.46      # General maintenance.
    1.47  
    1.48      def vacuum(self, objtable):
     2.1 --- a/micropython/inspect.py	Tue Jun 12 23:54:41 2012 +0200
     2.2 +++ b/micropython/inspect.py	Wed Jun 13 01:20:12 2012 +0200
     2.3 @@ -124,7 +124,12 @@
     2.4  
     2.5      def process(self, module):
     2.6  
     2.7 -        "Process the given 'module'."
     2.8 +        """
     2.9 +        Process the given 'module', visiting only module-level code and only
    2.10 +        extracting global declarations from functions. In order to support
    2.11 +        deferred imports, the function code is processed separately after all
    2.12 +        modules have been initially processed.
    2.13 +        """
    2.14  
    2.15          self.astnode = module
    2.16  
    2.17 @@ -144,12 +149,6 @@
    2.18  
    2.19          self.finalise_attribute_usage()
    2.20  
    2.21 -        # Then, visit each function, recording other names.
    2.22 -
    2.23 -        for node, namespaces in self.functions:
    2.24 -            self._visitFunctionBody(node, namespaces)
    2.25 -            namespaces[-1].finalise_attribute_usage()
    2.26 -
    2.27          # Add references to other modules declared using the __all__ global.
    2.28  
    2.29          if self.has_key("__all__"):
    2.30 @@ -160,6 +159,19 @@
    2.31  
    2.32          return processed
    2.33  
    2.34 +    def process_functions(self):
    2.35 +
    2.36 +        """
    2.37 +        Process all function bodies. Deferred imports may occur during this
    2.38 +        process.
    2.39 +        """
    2.40 +
    2.41 +        # Then, visit each function, recording other names.
    2.42 +
    2.43 +        for node, namespaces in self.functions:
    2.44 +            self._visitFunctionBody(node, namespaces)
    2.45 +            namespaces[-1].finalise_attribute_usage()
    2.46 +
    2.47      def process_globals(self, node):
    2.48  
    2.49          """