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
(cherry picked from commit dd8b4d6333)
This commit is contained in:
Doug Hellmann 2014-12-02 12:51:53 -05:00 committed by Sean Dague
parent 4e258c4222
commit b45ac44139
1 changed files with 15 additions and 1 deletions

View File

@ -11,6 +11,7 @@
# under the License.
import imp
import inspect
import os
import re
import sys
@ -100,7 +101,20 @@ def hacking_import_rules(logical_line, physical_line, filename, noqa):
else:
# NOTE(imelnikov): we imported the thing; if it was module,
# 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
def is_module(mod):