2010-04-16 02:44:51 +08:00
|
|
|
import imp
|
2010-05-04 14:14:47 +08:00
|
|
|
import os
|
|
|
|
import sys
|
2010-04-16 02:44:51 +08:00
|
|
|
|
|
|
|
|
2010-05-04 14:14:47 +08:00
|
|
|
def module_has_submodule(package, module_name):
|
|
|
|
"""See if 'module' is in 'package'."""
|
|
|
|
name = ".".join([package.__name__, module_name])
|
2011-01-30 21:10:47 +08:00
|
|
|
try:
|
|
|
|
# None indicates a cached miss; see mark_miss() in Python/import.c.
|
|
|
|
return sys.modules[name] is not None
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2011-08-28 10:37:16 +08:00
|
|
|
try:
|
|
|
|
package_path = package.__path__ # No __path__, then not a package.
|
|
|
|
except AttributeError:
|
|
|
|
# Since the remainder of this function assumes that we're dealing with
|
|
|
|
# a package (module with a __path__), so if it's not, then bail here.
|
|
|
|
return False
|
2010-05-04 14:14:47 +08:00
|
|
|
for finder in sys.meta_path:
|
2011-08-28 10:37:16 +08:00
|
|
|
if finder.find_module(name, package_path):
|
2010-05-04 14:14:47 +08:00
|
|
|
return True
|
2011-08-28 10:37:16 +08:00
|
|
|
for entry in package_path:
|
2010-05-04 14:14:47 +08:00
|
|
|
try:
|
|
|
|
# Try the cached finder.
|
|
|
|
finder = sys.path_importer_cache[entry]
|
|
|
|
if finder is None:
|
|
|
|
# Implicit import machinery should be used.
|
|
|
|
try:
|
|
|
|
file_, _, _ = imp.find_module(module_name, [entry])
|
|
|
|
if file_:
|
|
|
|
file_.close()
|
|
|
|
return True
|
|
|
|
except ImportError:
|
|
|
|
continue
|
|
|
|
# Else see if the finder knows of a loader.
|
|
|
|
elif finder.find_module(name):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
except KeyError:
|
|
|
|
# No cached finder, so try and make one.
|
|
|
|
for hook in sys.path_hooks:
|
|
|
|
try:
|
|
|
|
finder = hook(entry)
|
|
|
|
# XXX Could cache in sys.path_importer_cache
|
|
|
|
if finder.find_module(name):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
# Once a finder is found, stop the search.
|
|
|
|
break
|
|
|
|
except ImportError:
|
|
|
|
# Continue the search for a finder.
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
# No finder found.
|
|
|
|
# Try the implicit import machinery if searching a directory.
|
|
|
|
if os.path.isdir(entry):
|
|
|
|
try:
|
|
|
|
file_, _, _ = imp.find_module(module_name, [entry])
|
|
|
|
if file_:
|
|
|
|
file_.close()
|
|
|
|
return True
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
# XXX Could insert None or NullImporter
|
|
|
|
else:
|
|
|
|
# Exhausted the search, so the module cannot be found.
|
2010-04-16 02:44:51 +08:00
|
|
|
return False
|