"""Utilities to support packages."""
from types import ModuleType
'get_importer', 'iter_importers', 'get_loader', 'find_loader',
'walk_packages', 'iter_modules', 'get_data',
'ImpImporter', 'ImpLoader', 'read_code', 'extend_path',
# This helper is needed in order for the PEP 302 emulation to
# correctly handle compiled files
if magic != imp.get_magic():
stream.read(4) # Skip timestamp
return marshal.load(stream)
"""Make a trivial single-dispatch generic function"""
def wrapper(*args, **kw):
mro = object, # must be an ExtensionClass or some such :(
return registry[t](*args, **kw)
wrapper.__name__ = func.__name__
except (TypeError, AttributeError):
pass # Python 2.3 doesn't allow functions to be renamed
def register(typ, func=None):
return lambda f: register(typ, f)
wrapper.__dict__ = func.__dict__
wrapper.__doc__ = func.__doc__
wrapper.register = register
def walk_packages(path=None, prefix='', onerror=None):
"""Yields (module_loader, name, ispkg) for all modules recursively
on path, or, if path is None, all accessible modules.
'path' should be either None or a list of paths to look for
'prefix' is a string to output on the front of every module name
Note that this function must import all *packages* (NOT all
modules!) on the given path, in order to access the __path__
attribute to find submodules.
'onerror' is a function which gets called with one argument (the
name of the package which was being imported) if any exception
occurs while trying to import a package. If no onerror function is
supplied, ImportErrors are caught and ignored, while all other
exceptions are propagated, terminating the search.
# list all modules python can access
# list all submodules of ctypes
walk_packages(ctypes.__path__, ctypes.__name__+'.')
for importer, name, ispkg in iter_modules(path, prefix):
yield importer, name, ispkg
path = getattr(sys.modules[name], '__path__', None) or []
# don't traverse path items we've seen before
path = [p for p in path if not seen(p)]
for item in walk_packages(path, name+'.', onerror):
def iter_modules(path=None, prefix=''):
"""Yields (module_loader, name, ispkg) for all submodules on path,
or, if path is None, all top-level modules on sys.path.
'path' should be either None or a list of paths to look for
'prefix' is a string to output on the front of every module name
importers = iter_importers()
importers = map(get_importer, path)
for name, ispkg in iter_importer_modules(i, prefix):
def iter_importer_modules(importer, prefix=''):
if not hasattr(importer, 'iter_modules'):
return importer.iter_modules(prefix)
iter_importer_modules = simplegeneric(iter_importer_modules)
"""PEP 302 Importer that wraps Python's "classic" import algorithm
ImpImporter(dirname) produces a PEP 302 importer that searches that
directory. ImpImporter(None) produces a PEP 302 importer that searches
the current sys.path, plus any modules that are frozen or built-in.
Note that ImpImporter does not currently support being used by placement
def __init__(self, path=None):
def find_module(self, fullname, path=None):
# Note: we ignore 'path' argument since it is only used via meta_path
subname = fullname.split(".")[-1]
if subname != fullname and self.path is None:
path = [os.path.realpath(self.path)]
file, filename, etc = imp.find_module(subname, path)
return ImpLoader(fullname, file, filename, etc)
def iter_modules(self, prefix=''):
if self.path is None or not os.path.isdir(self.path):
filenames = os.listdir(self.path)
# ignore unreadable directories like import does
filenames.sort() # handle packages before same-named modules
modname = inspect.getmodulename(fn)
if modname=='__init__' or modname in yielded:
path = os.path.join(self.path, fn)
if not modname and os.path.isdir(path) and '.' not in fn:
dircontents = os.listdir(path)
# ignore unreadable directories like import does
subname = inspect.getmodulename(fn)
if modname and '.' not in modname:
yield prefix + modname, ispkg
"""PEP 302 Loader that wraps Python's "classic" import algorithm
def __init__(self, fullname, file, filename, etc):
def load_module(self, fullname):
mod = imp.load_module(fullname, self.file, self.filename, self.etc)
# Note: we don't set __loader__ because we want the module to look
# normal; i.e. this is just a wrapper for standard import machinery
def get_data(self, pathname):
with open(pathname, "rb") as file:
if self.file and self.file.closed:
if mod_type==imp.PY_SOURCE:
self.file = open(self.filename, 'rU')
elif mod_type in (imp.PY_COMPILED, imp.C_EXTENSION):
self.file = open(self.filename, 'rb')
def _fix_name(self, fullname):
elif fullname != self.fullname:
raise ImportError("Loader for module %s cannot handle "
"module %s" % (self.fullname, fullname))
def is_package(self, fullname):
fullname = self._fix_name(fullname)
return self.etc[2]==imp.PKG_DIRECTORY
def get_code(self, fullname=None):
fullname = self._fix_name(fullname)
if mod_type==imp.PY_SOURCE:
source = self.get_source(fullname)
self.code = compile(source, self.filename, 'exec')
elif mod_type==imp.PY_COMPILED:
self.code = read_code(self.file)
elif mod_type==imp.PKG_DIRECTORY:
self.code = self._get_delegate().get_code()
def get_source(self, fullname=None):
fullname = self._fix_name(fullname)
if mod_type==imp.PY_SOURCE:
self.source = self.file.read()
elif mod_type==imp.PY_COMPILED:
if os.path.exists(self.filename[:-1]):
f = open(self.filename[:-1], 'rU')
elif mod_type==imp.PKG_DIRECTORY:
self.source = self._get_delegate().get_source()
return ImpImporter(self.filename).find_module('__init__')
def get_filename(self, fullname=None):
fullname = self._fix_name(fullname)
if self.etc[2]==imp.PKG_DIRECTORY:
return self._get_delegate().get_filename()
elif self.etc[2] in (imp.PY_SOURCE, imp.PY_COMPILED, imp.C_EXTENSION):
from zipimport import zipimporter
def iter_zipimport_modules(importer, prefix=''):
dirlist = zipimport._zip_directory_cache[importer.archive].keys()
_prefix = importer.prefix
if not fn.startswith(_prefix):
fn = fn[plen:].split(os.sep)
if len(fn)==2 and fn[1].startswith('__init__.py'):
modname = inspect.getmodulename(fn[0])
if modname and '.' not in modname and modname not in yielded:
yield prefix + modname, False
iter_importer_modules.register(zipimporter, iter_zipimport_modules)
def get_importer(path_item):
"""Retrieve a PEP 302 importer for the given path item
The returned importer is cached in sys.path_importer_cache
if it was newly created by a path hook.
If there is no importer, a wrapper around the basic import
machinery is returned. This wrapper is never inserted into
the importer cache (None is inserted instead).
The cache (or part of it) can be cleared manually if a
rescan of sys.path_hooks is necessary.
importer = sys.path_importer_cache[path_item]
for path_hook in sys.path_hooks:
importer = path_hook(path_item)
sys.path_importer_cache.setdefault(path_item, importer)
importer = ImpImporter(path_item)
def iter_importers(fullname=""):
"""Yield PEP 302 importers for the given module name
If fullname contains a '.', the importers will be for the package
containing fullname, otherwise they will be importers for sys.meta_path,
sys.path, and Python's "classic" import machinery, in that order. If
the named module is in a package, that package is imported as a side
effect of invoking this function.
Non PEP 302 mechanisms (e.g. the Windows registry) used by the
standard import machinery to find files in alternative locations
are partially supported, but are searched AFTER sys.path. Normally,
these locations are searched BEFORE sys.path, preventing sys.path
entries from shadowing them.
For this to cause a visible difference in behaviour, there must
be a module or package name that is accessible via both sys.path
and one of the non PEP 302 file system mechanisms. In this case,
the emulation will find the former version, while the builtin
import mechanism will find the latter.
Items of the following types can be affected by this discrepancy:
imp.C_EXTENSION, imp.PY_SOURCE, imp.PY_COMPILED, imp.PKG_DIRECTORY
if fullname.startswith('.'):
raise ImportError("Relative module names not supported")
# Get the containing package's __path__
pkg = '.'.join(fullname.split('.')[:-1])
if pkg not in sys.modules:
path = getattr(sys.modules[pkg], '__path__', None) or []
for importer in sys.meta_path:
def get_loader(module_or_name):
"""Get a PEP 302 "loader" object for module_or_name
If the module or package is accessible via the normal import
mechanism, a wrapper around the relevant part of that machinery
is returned. Returns None if the module cannot be found or imported.
If the named module is not already imported, its containing package
(if any) is imported, in order to establish the package __path__.
This function uses iter_importers(), and is thus subject to the same
limitations regarding platform-specific special import locations such
if module_or_name in sys.modules:
module_or_name = sys.modules[module_or_name]
if isinstance(module_or_name, ModuleType):
loader = getattr(module, '__loader__', None)
fullname = module.__name__
fullname = module_or_name
return find_loader(fullname)
def find_loader(fullname):
"""Find a PEP 302 "loader" object for fullname
If fullname contains dots, path must be the containing package's __path__.
Returns None if the module cannot be found or imported. This function uses
iter_importers(), and is thus subject to the same limitations regarding
platform-specific special import locations such as the Windows registry.
for importer in iter_importers(fullname):
loader = importer.find_module(fullname)
def extend_path(path, name):
"""Extend a package's path.
Intended use is to place the following code in a package's __init__.py:
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
This will add to the package's __path__ all subdirectories of
directories on sys.path named after the package. This is useful
if one wants to distribute different parts of a single logical
package as multiple directories.
It also looks for *.pkg files beginning where * matches the name
argument. This feature is similar to *.pth files (see site.py),
except that it doesn't special-case lines starting with 'import'.
A *.pkg file is trusted at face value: apart from checking for
duplicates, all entries found in a *.pkg file are added to the
path, regardless of whether they are exist the filesystem. (This