def find_module(self, name, path, parent=None):
if parent is not None:
# assert path is not None
fullname = parent.__name__+'.'+name
else:
fullname = name
if fullname in self.excludes:
self.msgout(3, "find_module -> Excluded", fullname)
raise ImportError, name
if path is None:
if name in sys.builtin_module_names:
return (None, None, ("", "", imp.C_BUILTIN))
path = self.path
return imp.find_module(name, path)
python类C_BUILTIN的实例源码
def find_module(self, name, path, parent=None):
if parent is not None:
# assert path is not None
fullname = parent.__name__+'.'+name
else:
fullname = name
if fullname in self.excludes:
self.msgout(3, "find_module -> Excluded", fullname)
raise ImportError, name
if path is None:
if name in sys.builtin_module_names:
return (None, None, ("", "", imp.C_BUILTIN))
path = self.path
return imp.find_module(name, path)
def find_module(self, name, path, parent=None):
if parent is not None:
# assert path is not None
fullname = parent.__name__+'.'+name
else:
fullname = name
if fullname in self.excludes:
self.msgout(3, "find_module -> Excluded", fullname)
raise ImportError(name)
if path is None:
if name in sys.builtin_module_names:
return (None, None, ("", "", imp.C_BUILTIN))
path = self.path
return imp.find_module(name, path)
def find_module(self, name, path, parent=None):
if parent is not None:
# assert path is not None
fullname = parent.__name__+'.'+name
else:
fullname = name
if fullname in self.excludes:
self.msgout(3, "find_module -> Excluded", fullname)
raise ImportError, name
if path is None:
if name in sys.builtin_module_names:
return (None, None, ("", "", imp.C_BUILTIN))
path = self.path
return imp.find_module(name, path)
def find_module(self, name, path, parent=None):
if parent is not None:
# assert path is not None
fullname = parent.__name__+'.'+name
else:
fullname = name
if fullname in self.excludes:
self.msgout(3, "find_module -> Excluded", fullname)
raise ImportError, name
if path is None:
if name in sys.builtin_module_names:
return (None, None, ("", "", imp.C_BUILTIN))
path = self.path
return imp.find_module(name, path)
def find_module(self, name, path, parent=None):
if parent is not None:
# assert path is not None
fullname = parent.__name__+'.'+name
else:
fullname = name
if fullname in self.excludes:
self.msgout(3, "find_module -> Excluded", fullname)
raise ImportError, name
if path is None:
if name in sys.builtin_module_names:
return (None, None, ("", "", imp.C_BUILTIN))
path = self.path
return imp.find_module(name, path)
def find_module(self, name, path, parent=None):
if parent is not None:
# assert path is not None
fullname = parent.__name__+'.'+name
else:
fullname = name
if fullname in self.excludes:
self.msgout(3, "find_module -> Excluded", fullname)
raise ImportError, name
if path is None:
if name in sys.builtin_module_names:
return (None, None, ("", "", imp.C_BUILTIN))
path = self.path
return imp.find_module(name, path)
def find_module(self, name, path, parent=None):
if parent is not None:
# assert path is not None
fullname = parent.__name__+'.'+name
else:
fullname = name
if fullname in self.excludes:
self.msgout(3, "find_module -> Excluded", fullname)
raise ImportError(name)
if path is None:
if name in sys.builtin_module_names:
return (None, None, ("", "", imp.C_BUILTIN))
path = self.path
return imp.find_module(name, path)
def find_module(self, name, path, parent=None):
if parent is not None:
# assert path is not None
fullname = parent.__name__+'.'+name
else:
fullname = name
if fullname in self.excludes:
self.msgout(3, "find_module -> Excluded", fullname)
raise ImportError, name
if path is None:
if name in sys.builtin_module_names:
return (None, None, ("", "", imp.C_BUILTIN))
path = self.path
return imp.find_module(name, path)
def find_module(self, name, path, parent=None):
if parent is not None:
# assert path is not None
fullname = parent.__name__+'.'+name
else:
fullname = name
if fullname in self.excludes:
self.msgout(3, "find_module -> Excluded", fullname)
raise ImportError, name
if path is None:
if name in sys.builtin_module_names:
return (None, None, ("", "", imp.C_BUILTIN))
path = self.path
return imp.find_module(name, path)
def find_module(self, name, path, parent=None):
if parent is not None:
# assert path is not None
fullname = parent.__name__+'.'+name
else:
fullname = name
if fullname in self.excludes:
self.msgout(3, "find_module -> Excluded", fullname)
raise ImportError(name)
if path is None:
if name in sys.builtin_module_names:
return (None, None, ("", "", imp.C_BUILTIN))
path = self.path
return imp.find_module(name, path)
def imp_walk(name):
"""
yields namepart, tuple_or_importer for each path item
raise ImportError if a name can not be found.
"""
warnings.warn("imp_walk will be removed in a future version", DeprecationWarning)
if name in sys.builtin_module_names:
yield name, (None, None, ("", "", imp.C_BUILTIN))
return
paths = sys.path
res = None
for namepart in name.split('.'):
for path_item in paths:
res = _check_importer_for_path(namepart, path_item)
if hasattr(res, 'load_module'):
if res.path.endswith('.py') or res.path.endswith('.pyw'):
fp = StringIO(res.get_source(namepart))
res = (fp, res.path, ('.py', 'rU', imp.PY_SOURCE))
elif res.path.endswith('.pyc') or res.path.endswith('.pyo'):
co = res.get_code(namepart)
fp = BytesIO(imp.get_magic() + b'\0\0\0\0' + marshal.dumps(co))
res = (fp, res.path, ('.pyc', 'rb', imp.PY_COMPILED))
else:
res = (None, loader.path, (os.path.splitext(loader.path)[-1], 'rb', imp.C_EXTENSION))
break
elif isinstance(res, tuple):
break
else:
break
yield namepart, res
paths = [os.path.join(path_item, namepart)]
else:
return
raise ImportError('No module named %s' % (name,))
def _file_from_modpath(modpath, path=None, context=None):
"""given a mod path (i.e. splitted module / package name), return the
corresponding file
this function is used internally, see `file_from_modpath`'s
documentation for more information
"""
assert len(modpath) > 0
if context is not None:
try:
mtype, mp_filename = _module_file(modpath, [context])
except ImportError:
mtype, mp_filename = _module_file(modpath, path)
else:
mtype, mp_filename = _module_file(modpath, path)
if mtype == imp.PY_COMPILED:
try:
return get_source_file(mp_filename), imp.PY_SOURCE
except NoSourceFile:
return mp_filename, imp.PY_COMPILED
elif mtype == imp.C_BUILTIN:
# integrated builtin module
return None, imp.C_BUILTIN
elif mtype == imp.PKG_DIRECTORY:
mp_filename = _has_init(mp_filename)
mtype = imp.PY_SOURCE
return mp_filename, mtype
def ast_from_module_name(self, modname, context_file=None):
"""given a module name, return the astroid object"""
if modname in self.astroid_cache:
return self.astroid_cache[modname]
if modname == '__main__':
return self._build_stub_module(modname)
old_cwd = os.getcwd()
if context_file:
os.chdir(dirname(context_file))
try:
filepath, mp_type = self.file_from_module_name(modname, context_file)
if mp_type == modutils.PY_ZIPMODULE:
module = self.zip_import_data(filepath)
if module is not None:
return module
elif mp_type in (imp.C_BUILTIN, imp.C_EXTENSION):
if mp_type == imp.C_EXTENSION and not self._can_load_extension(modname):
return self._build_stub_module(modname)
try:
module = modutils.load_module_from_name(modname)
except Exception as ex:
msg = 'Unable to load module %s (%s)' % (modname, ex)
raise AstroidBuildingException(msg)
return self.ast_from_module(module, modname)
elif mp_type == imp.PY_COMPILED:
raise AstroidBuildingException("Unable to load compiled module %s" % (modname,))
if filepath is None:
raise AstroidBuildingException("Unable to load module %s" % (modname,))
return self.ast_from_file(filepath, modname, fallback=False)
except AstroidBuildingException as e:
for hook in self._failed_import_hooks:
try:
return hook(modname)
except AstroidBuildingException:
pass
raise e
finally:
os.chdir(old_cwd)
def _file_from_modpath(modpath, path=None, context=None):
"""given a mod path (i.e. splitted module / package name), return the
corresponding file
this function is used internally, see `file_from_modpath`'s
documentation for more information
"""
assert len(modpath) > 0
if context is not None:
try:
mtype, mp_filename = _module_file(modpath, [context])
except ImportError:
mtype, mp_filename = _module_file(modpath, path)
else:
mtype, mp_filename = _module_file(modpath, path)
if mtype == PY_COMPILED:
try:
return get_source_file(mp_filename)
except NoSourceFile:
return mp_filename
elif mtype == C_BUILTIN:
# integrated builtin module
return None
elif mtype == PKG_DIRECTORY:
mp_filename = _has_init(mp_filename)
return mp_filename
def _file_from_modpath(modpath, path=None, context=None):
"""given a mod path (i.e. splitted module / package name), return the
corresponding file
this function is used internally, see `file_from_modpath`'s
documentation for more information
"""
assert len(modpath) > 0
if context is not None:
try:
mtype, mp_filename = _module_file(modpath, [context])
except ImportError:
mtype, mp_filename = _module_file(modpath, path)
else:
mtype, mp_filename = _module_file(modpath, path)
if mtype == imp.PY_COMPILED:
try:
return get_source_file(mp_filename), imp.PY_SOURCE
except NoSourceFile:
return mp_filename, imp.PY_COMPILED
elif mtype == imp.C_BUILTIN:
# integrated builtin module
return None, imp.C_BUILTIN
elif mtype == imp.PKG_DIRECTORY:
mp_filename = _has_init(mp_filename)
mtype = imp.PY_SOURCE
return mp_filename, mtype
def ast_from_module_name(self, modname, context_file=None):
"""given a module name, return the astroid object"""
if modname in self.astroid_cache:
return self.astroid_cache[modname]
if modname == '__main__':
return self._build_stub_module(modname)
old_cwd = os.getcwd()
if context_file:
os.chdir(dirname(context_file))
try:
filepath, mp_type = self.file_from_module_name(modname, context_file)
if mp_type == modutils.PY_ZIPMODULE:
module = self.zip_import_data(filepath)
if module is not None:
return module
elif mp_type in (imp.C_BUILTIN, imp.C_EXTENSION):
if mp_type == imp.C_EXTENSION and not self._can_load_extension(modname):
return self._build_stub_module(modname)
try:
module = modutils.load_module_from_name(modname)
except Exception as ex:
msg = 'Unable to load module %s (%s)' % (modname, ex)
raise AstroidBuildingException(msg)
return self.ast_from_module(module, modname)
elif mp_type == imp.PY_COMPILED:
raise AstroidBuildingException("Unable to load compiled module %s" % (modname,))
if filepath is None:
raise AstroidBuildingException("Unable to load module %s" % (modname,))
return self.ast_from_file(filepath, modname, fallback=False)
except AstroidBuildingException as e:
for hook in self._failed_import_hooks:
try:
return hook(modname)
except AstroidBuildingException:
pass
raise e
finally:
os.chdir(old_cwd)
def _file_from_modpath(modpath, path=None, context=None):
"""given a mod path (i.e. splitted module / package name), return the
corresponding file
this function is used internally, see `file_from_modpath`'s
documentation for more information
"""
assert len(modpath) > 0
if context is not None:
try:
mtype, mp_filename = _module_file(modpath, [context])
except ImportError:
mtype, mp_filename = _module_file(modpath, path)
else:
mtype, mp_filename = _module_file(modpath, path)
if mtype == PY_COMPILED:
try:
return get_source_file(mp_filename)
except NoSourceFile:
return mp_filename
elif mtype == C_BUILTIN:
# integrated builtin module
return None
elif mtype == PKG_DIRECTORY:
mp_filename = _has_init(mp_filename)
return mp_filename
def find_module(self, fullname, path=None):
if (fullname in _WHITE_LIST_C_MODULES or
any(regex.match(fullname) for regex in self._enabled_regexes)):
return None
if self._module_type(fullname, path) in [imp.C_EXTENSION, imp.C_BUILTIN]:
return self
return None
def test_c_builtin(self):
imp.find_module('bar', ['foo']).AndReturn((None, 'bar',
(None, None, imp.C_BUILTIN)))
self.mox.ReplayAll()
self.assertIsNone(self.hook.find_module('foo.bar', ['foo']))
def imp_walk(name):
"""
yields namepart, tuple_or_importer for each path item
raise ImportError if a name can not be found.
"""
warnings.warn("imp_walk will be removed in a future version", DeprecationWarning)
if name in sys.builtin_module_names:
yield name, (None, None, ("", "", imp.C_BUILTIN))
return
paths = sys.path
res = None
for namepart in name.split('.'):
for path_item in paths:
res = _check_importer_for_path(namepart, path_item)
if hasattr(res, 'load_module'):
if res.path.endswith('.py') or res.path.endswith('.pyw'):
fp = StringIO(res.get_source(namepart))
res = (fp, res.path, ('.py', 'rU', imp.PY_SOURCE))
elif res.path.endswith('.pyc') or res.path.endswith('.pyo'):
co = res.get_code(namepart)
fp = BytesIO(imp.get_magic() + b'\0\0\0\0' + marshal.dumps(co))
res = (fp, res.path, ('.pyc', 'rb', imp.PY_COMPILED))
else:
res = (None, loader.path, (os.path.splitext(loader.path)[-1], 'rb', imp.C_EXTENSION))
break
elif isinstance(res, tuple):
break
else:
break
yield namepart, res
paths = [os.path.join(path_item, namepart)]
else:
return
raise ImportError('No module named %s' % (name,))
def _file_from_modpath(modpath, path=None, context=None):
"""given a mod path (i.e. splitted module / package name), return the
corresponding file
this function is used internally, see `file_from_modpath`'s
documentation for more information
"""
assert len(modpath) > 0
if context is not None:
try:
mtype, mp_filename = _module_file(modpath, [context])
except ImportError:
mtype, mp_filename = _module_file(modpath, path)
else:
mtype, mp_filename = _module_file(modpath, path)
if mtype == imp.PY_COMPILED:
try:
return get_source_file(mp_filename), imp.PY_SOURCE
except NoSourceFile:
return mp_filename, imp.PY_COMPILED
elif mtype == imp.C_BUILTIN:
# integrated builtin module
return None, imp.C_BUILTIN
elif mtype == imp.PKG_DIRECTORY:
mp_filename = _has_init(mp_filename)
mtype = imp.PY_SOURCE
return mp_filename, mtype
def ast_from_module_name(self, modname, context_file=None):
"""given a module name, return the astroid object"""
if modname in self.astroid_cache:
return self.astroid_cache[modname]
if modname == '__main__':
return self._build_stub_module(modname)
old_cwd = os.getcwd()
if context_file:
os.chdir(dirname(context_file))
try:
filepath, mp_type = self.file_from_module_name(modname, context_file)
if mp_type == modutils.PY_ZIPMODULE:
module = self.zip_import_data(filepath)
if module is not None:
return module
elif mp_type in (imp.C_BUILTIN, imp.C_EXTENSION):
if mp_type == imp.C_EXTENSION and not self._can_load_extension(modname):
return self._build_stub_module(modname)
try:
module = modutils.load_module_from_name(modname)
except Exception as ex:
msg = 'Unable to load module %s (%s)' % (modname, ex)
raise AstroidBuildingException(msg)
return self.ast_from_module(module, modname)
elif mp_type == imp.PY_COMPILED:
raise AstroidBuildingException("Unable to load compiled module %s" % (modname,))
if filepath is None:
raise AstroidBuildingException("Unable to load module %s" % (modname,))
return self.ast_from_file(filepath, modname, fallback=False)
except AstroidBuildingException as e:
for hook in self._failed_import_hooks:
try:
return hook(modname)
except AstroidBuildingException:
pass
raise e
finally:
os.chdir(old_cwd)
def _file_from_modpath(modpath, path=None, context=None):
"""given a mod path (i.e. splitted module / package name), return the
corresponding file
this function is used internally, see `file_from_modpath`'s
documentation for more information
"""
assert len(modpath) > 0
if context is not None:
try:
mtype, mp_filename = _module_file(modpath, [context])
except ImportError:
mtype, mp_filename = _module_file(modpath, path)
else:
mtype, mp_filename = _module_file(modpath, path)
if mtype == PY_COMPILED:
try:
return get_source_file(mp_filename)
except NoSourceFile:
return mp_filename
elif mtype == C_BUILTIN:
# integrated builtin module
return None
elif mtype == PKG_DIRECTORY:
mp_filename = _has_init(mp_filename)
return mp_filename
def _file_from_modpath(modpath, path=None, context=None):
"""given a mod path (i.e. splitted module / package name), return the
corresponding file
this function is used internally, see `file_from_modpath`'s
documentation for more information
"""
assert len(modpath) > 0
if context is not None:
try:
mtype, mp_filename = _module_file(modpath, [context])
except ImportError:
mtype, mp_filename = _module_file(modpath, path)
else:
mtype, mp_filename = _module_file(modpath, path)
if mtype == imp.PY_COMPILED:
try:
return get_source_file(mp_filename), imp.PY_SOURCE
except NoSourceFile:
return mp_filename, imp.PY_COMPILED
elif mtype == imp.C_BUILTIN:
# integrated builtin module
return None, imp.C_BUILTIN
elif mtype == imp.PKG_DIRECTORY:
mp_filename = _has_init(mp_filename)
mtype = imp.PY_SOURCE
return mp_filename, mtype
def ast_from_module_name(self, modname, context_file=None):
"""given a module name, return the astroid object"""
if modname in self.astroid_cache:
return self.astroid_cache[modname]
if modname == '__main__':
return self._build_stub_module(modname)
old_cwd = os.getcwd()
if context_file:
os.chdir(dirname(context_file))
try:
filepath, mp_type = self.file_from_module_name(modname, context_file)
if mp_type == modutils.PY_ZIPMODULE:
module = self.zip_import_data(filepath)
if module is not None:
return module
elif mp_type in (imp.C_BUILTIN, imp.C_EXTENSION):
if mp_type == imp.C_EXTENSION and not self._can_load_extension(modname):
return self._build_stub_module(modname)
try:
module = modutils.load_module_from_name(modname)
except Exception as ex:
msg = 'Unable to load module %s (%s)' % (modname, ex)
raise AstroidBuildingException(msg)
return self.ast_from_module(module, modname)
elif mp_type == imp.PY_COMPILED:
raise AstroidBuildingException("Unable to load compiled module %s" % (modname,))
if filepath is None:
raise AstroidBuildingException("Unable to load module %s" % (modname,))
return self.ast_from_file(filepath, modname, fallback=False)
except AstroidBuildingException as e:
for hook in self._failed_import_hooks:
try:
return hook(modname)
except AstroidBuildingException:
pass
raise e
finally:
os.chdir(old_cwd)
def _file_from_modpath(modpath, path=None, context=None):
"""given a mod path (i.e. splitted module / package name), return the
corresponding file
this function is used internally, see `file_from_modpath`'s
documentation for more information
"""
assert len(modpath) > 0
if context is not None:
try:
mtype, mp_filename = _module_file(modpath, [context])
except ImportError:
mtype, mp_filename = _module_file(modpath, path)
else:
mtype, mp_filename = _module_file(modpath, path)
if mtype == PY_COMPILED:
try:
return get_source_file(mp_filename)
except NoSourceFile:
return mp_filename
elif mtype == C_BUILTIN:
# integrated builtin module
return None
elif mtype == PKG_DIRECTORY:
mp_filename = _has_init(mp_filename)
return mp_filename
def _find_module(self, name, path, parent=None):
"""
Get a 3-tuple detailing the physical location of the Python module with
the passed name if that module is found *or* raise `ImportError`
otherwise.
This high-level method wraps the low-level `modulegraph.find_module()`
function with additional support for graph-based module caching.
Parameters
----------
name : str
Fully-qualified name of the Python module to be found.
path : list
List of the absolute paths of all directories to search for this
module *or* `None` if the default path list `self.path` is to be
searched.
parent : Node
Optional parent module of this module if this module is a submodule
of another module *or* `None` if this module is a top-level module.
Returns
----------
(file_handle, filename, metadata)
See `modulegraph._find_module()` for details.
"""
if parent is not None:
# assert path is not None
fullname = parent.identifier + '.' + name
else:
fullname = name
node = self.findNode(fullname)
if node is not None:
self.msg(3, "find_module: already included?", node)
raise ImportError(name)
if path is None:
if name in sys.builtin_module_names:
return (None, None, ("", "", imp.C_BUILTIN))
path = self.path
return self._find_module_path(fullname, name, path)
def _import_module(self, module_name):
logger.info('Importing module "%s"', module_name)
file_ = None
# Import the module
try:
file_, pathname, description = imp.find_module(module_name)
except ImportError as e:
msg = 'Failed to import module "{}": {}'.format(module_name, e)
raise FlexerException(msg, type(e))
# Do not allow using builtin modules as handlers
_, _, module_type = description
if file_ is None:
if module_type == imp.C_BUILTIN:
msg = ('Built-in module "{}" cannot be a handler module'
.format(module_name))
raise FlexerException(msg, Exception)
# Load the module
try:
module = imp.load_module(module_name, file_, pathname, description)
except ImportError as e:
msg = 'Failed to import module "{}": {}'.format(module_name, e)
stack_trace = format_stack_trace(sys.exc_info())
raise FlexerException(msg, type(e), stack_trace)
except SyntaxError as e:
stack_trace = (
'File \"%s\", line %s\n\t%s' % (e.filename.split('/')[-1],
e.lineno,
e.text)
)
msg = 'Syntax error in module "{}": {}'.format(module_name, e)
raise FlexerException(msg, type(e), stack_trace)
except Exception as e:
stack_trace = format_stack_trace(sys.exc_info())
msg = 'Failed to initialise "{}": {}'.format(module_name, e)
raise FlexerException(msg, type(e), stack_trace)
finally:
if file_ is not None:
file_.close()
return module
# utility methods
def _InternalImportModule(self, name, deferredImports, namespace = False):
"""Internal method used for importing a module which assumes that the
name given is an absolute name. None is returned if the module
cannot be found."""
try:
# Check in module cache before trying to import it again.
return self._modules[name]
except KeyError:
pass
if name in self._builtinModules:
module = self._AddModule(name)
logging.debug("Adding module [%s] [C_BUILTIN]", name)
self._RunHook("load", module.name, module)
module.inImport = False
return module
pos = name.rfind(".")
if pos < 0: # Top-level module
path = self.path
searchName = name
parentModule = None
else: # Dotted module name - look up the parent module
parentName = name[:pos]
parentModule = \
self._InternalImportModule(parentName, deferredImports,
namespace = namespace)
if parentModule is None:
return None
if namespace:
parentModule.ExtendPath()
path = parentModule.path
searchName = name[pos + 1:]
if name in self.aliases:
actualName = self.aliases[name]
module = self._InternalImportModule(actualName, deferredImports)
self._modules[name] = module
return module
try:
fp, path, info = self._FindModule(searchName, path, namespace)
if info[-1] == imp.C_BUILTIN and parentModule is not None:
return None
module = self._LoadModule(name, fp, path, info, deferredImports,
parentModule, namespace)
except ImportError:
logging.debug("Module [%s] cannot be imported", name)
self._modules[name] = None
return None
return module