Allow import redirections

When we move modules out of namespace packages we set up redirects to
import the new module under the old name. Python's import machinery
doesn't detect those things as modules, and the names don't show up in
sys.modules. However, if we look at what we actually got when we did the
import we can see that it is a module.

Change-Id: I4b42d081965b6d898b178cbe9232b47cfed17d8a
This commit is contained in:
Doug Hellmann
2014-12-02 12:51:53 -05:00
parent 64ef5bf59d
commit 71a9d1d64f

View File

@@ -11,6 +11,7 @@
# under the License. # under the License.
import imp import imp
import inspect
import os import os
import re import re
import sys import sys
@@ -100,7 +101,20 @@ def hacking_import_rules(logical_line, physical_line, filename, noqa):
else: else:
# NOTE(imelnikov): we imported the thing; if it was module, # NOTE(imelnikov): we imported the thing; if it was module,
# it must be there: # it must be there:
return mod in sys.modules if mod in sys.modules:
return True
else:
# NOTE(dhellmann): If the thing isn't there under
# its own name, look to see if it is a module
# redirection import in one of the oslo libraries
# where we are moving things out of the namespace
# package.
pack_name, _sep, mod_name = mod.rpartition('.')
if pack_name in sys.modules:
the_mod = getattr(sys.modules[pack_name], mod_name,
None)
return inspect.ismodule(the_mod)
return False
return True return True
def is_module(mod): def is_module(mod):