micropython

Changeset

545:63a3ea888ac5
2012-06-12 Paul Boddie raw files shortlog changelog graph Added measures to prevent failure when modules provide neither a given name nor any submodules. Added circular import warnings and some more verbose output around module processing.
micropython/__init__.py (file) micropython/inspect.py (file)
     1.1 --- a/micropython/__init__.py	Tue Jun 12 23:52:18 2012 +0200
     1.2 +++ b/micropython/__init__.py	Tue Jun 12 23:54:41 2012 +0200
     1.3 @@ -993,6 +993,11 @@
     1.4                  module.set_module(p, submodule)
     1.5                  module = submodule
     1.6  
     1.7 +                # Stop descending if no package was found.
     1.8 +
     1.9 +                if not d:
    1.10 +                    break
    1.11 +
    1.12          # Return either the deepest or the uppermost module.
    1.13  
    1.14          if return_leaf:
    1.15 @@ -1012,9 +1017,11 @@
    1.16          module = self.add_module(module_name)
    1.17          if not module.loaded and module not in self.loading:
    1.18              self.loading.add(module)
    1.19 -            #print >>sys.stderr, "Parsing", name
    1.20 +            if self.verbose:
    1.21 +                print >>sys.stderr, "Parsing", name
    1.22              module.parse(name)
    1.23 -            #print >>sys.stderr, "Done", name
    1.24 +            if self.verbose:
    1.25 +                print >>sys.stderr, "Loaded", name
    1.26              self.loading.remove(module)
    1.27              module.loaded = 1
    1.28  
     2.1 --- a/micropython/inspect.py	Tue Jun 12 23:52:18 2012 +0200
     2.2 +++ b/micropython/inspect.py	Tue Jun 12 23:54:41 2012 +0200
     2.3 @@ -963,19 +963,25 @@
     2.4  
     2.5      def visitFrom(self, node):
     2.6          module = self.importer.load(node.modname, 1)
     2.7 +        if module and not module.loaded:
     2.8 +            print >>sys.stderr, "Warning: a circular import of %s is being attempted in %s" % (node.modname, self.full_name())
     2.9  
    2.10          #if module is None:
    2.11          #    print >>sys.stderr, "Warning:", node.modname, "not imported."
    2.12  
    2.13          for name, alias in node.names:
    2.14              if name != "*":
    2.15 -                if module is not None:
    2.16 +                if module:
    2.17  
    2.18                      # Missing names may refer to submodules.
    2.19  
    2.20                      if not module.has_key(name):
    2.21                          submodule = self.importer.load(node.modname + "." + name, 1)
    2.22 -                        if submodule is not None:
    2.23 +                        if submodule:
    2.24 +                            if not submodule.loaded:
    2.25 +                                print >>sys.stderr, "Warning: a circular import of %s.%s is being attempted in %s" % (
    2.26 +                                    node.modname, name, self.full_name())
    2.27 +
    2.28                              module.store(name, submodule)
    2.29  
    2.30                      # Complete the import if the name was found.
    2.31 @@ -993,7 +999,7 @@
    2.32                  self.store(alias or name, UnresolvedName(name, node.modname, self))
    2.33  
    2.34              else:
    2.35 -                if module is not None:
    2.36 +                if module:
    2.37                      for n in module.keys():
    2.38                          attr = module[n]
    2.39                          self.store(n, attr)