def get_code(self, parent, modname, fqname):
if parent:
# these modules definitely do not occur within a package context
return None
# look for the module
if imp.is_builtin(modname):
type = imp.C_BUILTIN
elif imp.is_frozen(modname):
type = imp.PY_FROZEN
else:
# not found
return None
# got it. now load and return it.
module = imp.load_module(modname, None, modname, ('', '', type))
return 0, module, { }
######################################################################
#
# Internal importer used for importing from the filesystem
#
python类modules()的实例源码
def synopsis(filename, cache={}):
"""Get the one-line summary out of a module file."""
mtime = os.stat(filename).st_mtime
lastupdate, result = cache.get(filename, (0, None))
if lastupdate < mtime:
info = inspect.getmoduleinfo(filename)
try:
file = open(filename)
except IOError:
# module can't be opened, so skip it
return None
if info and 'b' in info[2]: # binary modules have to be imported
try: module = imp.load_module('__temp__', file, filename, info[1:])
except: return None
result = (module.__doc__ or '').splitlines()[0]
del sys.modules['__temp__']
else: # text modules can be directly examined
result = source_synopsis(file)
file.close()
cache[filename] = (mtime, result)
return result
def help(self, request):
if type(request) is type(''):
request = request.strip()
if request == 'help': self.intro()
elif request == 'keywords': self.listkeywords()
elif request == 'symbols': self.listsymbols()
elif request == 'topics': self.listtopics()
elif request == 'modules': self.listmodules()
elif request[:8] == 'modules ':
self.listmodules(split(request)[1])
elif request in self.symbols: self.showsymbol(request)
elif request in self.keywords: self.showtopic(request)
elif request in self.topics: self.showtopic(request)
elif request: doc(request, 'Help on %s:')
elif isinstance(request, Helper): self()
else: doc(request, 'Help on %s:')
self.output.write('\n')
def intro(self):
self.output.write('''
Welcome to Python %s! This is the online help utility.
If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/tutorial/.
Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules. To quit this help utility and
return to the interpreter, just type "quit".
To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics". Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".
''' % sys.version[:3])
def _normalize_module(module, depth=2):
"""
Return the module specified by `module`. In particular:
- If `module` is a module, then return module.
- If `module` is a string, then import and return the
module with that name.
- If `module` is None, then return the calling module.
The calling module is assumed to be the module of
the stack frame at the given depth in the call stack.
"""
if inspect.ismodule(module):
return module
elif isinstance(module, (str, unicode)):
return __import__(module, globals(), locals(), ["*"])
elif module is None:
return sys.modules[sys._getframe(depth).f_globals['__name__']]
else:
raise TypeError("Expected a module, string, or None")
def _do_download(version, download_base, to_dir, download_delay):
"""Download Setuptools."""
egg = os.path.join(to_dir, 'setuptools-%s-py%d.%d.egg'
% (version, sys.version_info[0], sys.version_info[1]))
if not os.path.exists(egg):
archive = download_setuptools(version, download_base,
to_dir, download_delay)
_build_egg(egg, archive, to_dir)
sys.path.insert(0, egg)
# Remove previously-imported pkg_resources if present (see
# https://bitbucket.org/pypa/setuptools/pull-request/7/ for details).
if 'pkg_resources' in sys.modules:
del sys.modules['pkg_resources']
import setuptools
setuptools.bootstrap_install_from = egg
def _do_download(version, download_base, to_dir, download_delay):
egg = os.path.join(to_dir, 'setuptools-%s-py%d.%d.egg'
% (version, sys.version_info[0], sys.version_info[1]))
if not os.path.exists(egg):
archive = download_setuptools(version, download_base,
to_dir, download_delay)
_build_egg(egg, archive, to_dir)
sys.path.insert(0, egg)
# Remove previously-imported pkg_resources if present (see
# https://bitbucket.org/pypa/setuptools/pull-request/7/ for details).
if 'pkg_resources' in sys.modules:
del sys.modules['pkg_resources']
import setuptools
setuptools.bootstrap_install_from = egg
def _do_download(version, download_base, to_dir, download_delay):
egg = os.path.join(to_dir, 'setuptools-%s-py%d.%d.egg'
% (version, sys.version_info[0], sys.version_info[1]))
if not os.path.exists(egg):
archive = download_setuptools(version, download_base,
to_dir, download_delay)
_build_egg(egg, archive, to_dir)
sys.path.insert(0, egg)
# Remove previously-imported pkg_resources if present (see
# https://bitbucket.org/pypa/setuptools/pull-request/7/ for details).
if 'pkg_resources' in sys.modules:
del sys.modules['pkg_resources']
import setuptools
setuptools.bootstrap_install_from = egg
def _setup(module, extras):
"""Install common submodules"""
Qt.__binding__ = module.__name__
for name in list(_common_members) + extras:
try:
# print("Trying %s" % name)
submodule = importlib.import_module(
module.__name__ + "." + name)
except ImportError:
# print("Failed %s" % name)
continue
setattr(Qt, "_" + name, submodule)
if name not in extras:
# Store reference to original binding,
# but don't store speciality modules
# such as uic or QtUiTools
setattr(Qt, name, _new_module(name))
def use_setuptools(version=DEFAULT_VERSION, download_base=DEFAULT_URL,
to_dir=os.curdir, download_delay=15):
to_dir = os.path.abspath(to_dir)
rep_modules = 'pkg_resources', 'setuptools'
imported = set(sys.modules).intersection(rep_modules)
try:
import pkg_resources
except ImportError:
return _do_download(version, download_base, to_dir, download_delay)
try:
pkg_resources.require("setuptools>=" + version)
return
except pkg_resources.DistributionNotFound:
return _do_download(version, download_base, to_dir, download_delay)
except pkg_resources.VersionConflict as VC_err:
if imported:
msg = textwrap.dedent("""
The required version of setuptools (>={version}) is not available,
and can't be installed while this script is running. Please
install a more recent version first, using
'easy_install -U setuptools'.
(Currently using {VC_err.args[0]!r})
""").format(VC_err=VC_err, version=version)
sys.stderr.write(msg)
sys.exit(2)
# otherwise, reload ok
del pkg_resources, sys.modules['pkg_resources']
return _do_download(version, download_base, to_dir, download_delay)
def _get_valuedoc(value):
"""
If a C{ValueDoc} for the given value exists in the valuedoc
cache, then return it; otherwise, create a new C{ValueDoc},
add it to the cache, and return it. When possible, the new
C{ValueDoc}'s C{pyval}, C{repr}, and C{canonical_name}
attributes will be set appropriately.
"""
pyid = id(value)
val_doc = _valuedoc_cache.get(pyid)
if val_doc is None:
try: canonical_name = get_canonical_name(value, strict=True)
except DottedName.InvalidDottedName: canonical_name = UNKNOWN
val_doc = ValueDoc(pyval=value, canonical_name = canonical_name,
docs_extracted_by='introspecter')
_valuedoc_cache[pyid] = val_doc
# If it's a module, then do some preliminary introspection.
# Otherwise, check what the containing module is (used e.g.
# to decide what markup language should be used for docstrings)
if inspect.ismodule(value):
introspect_module(value, val_doc, preliminary=True)
val_doc.defining_module = val_doc
else:
module_name = str(get_containing_module(value))
module = sys.modules.get(module_name)
if module is not None and inspect.ismodule(module):
val_doc.defining_module = _get_valuedoc(module)
return val_doc
#////////////////////////////////////////////////////////////
# Module Introspection
#////////////////////////////////////////////////////////////
#: A list of module variables that should not be included in a
#: module's API documentation.
def _find_function_module(func):
"""
@return: The module that defines the given function.
@rtype: C{module}
@param func: The function whose module should be found.
@type func: C{function}
"""
if hasattr(func, '__module__'):
return func.__module__
try:
module = inspect.getmodule(func)
if module: return module.__name__
except KeyboardInterrupt: raise
except: pass
# This fallback shouldn't usually be needed. But it is needed in
# a couple special cases (including using epydoc to document
# itself). In particular, if a module gets loaded twice, using
# two different names for the same file, then this helps.
for module in sys.modules.values():
if (hasattr(module, '__dict__') and
hasattr(func, 'func_globals') and
func.func_globals is module.__dict__):
return module.__name__
return None
#////////////////////////////////////////////////////////////
# Introspection Dispatch Table
#////////////////////////////////////////////////////////////
def load(self, name, force_reload_if_unmanaged=False):
if name in sys.modules and name not in self.plugins:
# we're getting an already loaded module, which we has not been
# loaded through PluginManager, return it from sys.modules and
# add it to our list
module = sys.modules[name]
if force_reload_if_unmanaged:
importlib.reload(module)
else:
module = importlib.import_module(name)
self.plugins.add(name)
return module
def is_valid_module_path_component(token):
"Validate strings to be used when importing modules dynamically."
return not token.startswith('_') and not keyword.iskeyword(token) and \
all( ( (x.isalnum() or x == '_') for x in token ) )
def meta_autodetect_platform(cls):
"""
Dark magic to autodetect the platform for built-in shellcodes.
User-defined shellcodes must define *arch* and *os*.
"""
abspath = path.abspath
join = path.join
split = path.split
splitext = path.splitext
sep = path.sep
module = cls.__module__
if module != '__main__':
tokens = cls.__module__.split('.')
if len(tokens) < 2 or tokens[0] != base_package or \
tokens[1] == base_file:
return
tokens.insert(-1, 'any')
tokens = tokens[1:3]
else:
module = abspath(sys.modules[module].__file__)
if not module.startswith(base_dir):
return
tokens = module.split(sep)
tokens = tokens[len(base_dir.split(sep)):-1]
while len(tokens) < 2:
tokens.append('any')
cls.arch, cls.os = tokens
def test_load_pylib(self):
"""
tests the dynamic loading of a python package
"""
# A working dir
work_dir = join(self.tmp_dir, 'Utils_Test.load_pylib')
# Add a few modules
assert(self.touch(join(work_dir, 'test01.py')))
# A module we can attempt to load
work_module = load_pylib('test01', join(work_dir, 'test01.py'))
assert(work_module is not None)
assert(work_module.__class__.__name__ == 'module')
work_module = load_pylib(join(work_dir, 'test01.py'))
assert(work_module is not None)
assert(work_module.__class__.__name__ == 'module')
# Now we'll protect our original directory
chmod(work_dir, 0000)
# We should fail to load our module
work_module = load_pylib('test01', join(work_dir, 'test01.py'))
assert(work_module is None)
# Restore our permissions
chmod(work_dir, 0700)
# Protect our module
chmod(join(work_dir, 'test01.py'), 0000)
work_module = load_pylib('test01', join(work_dir, 'test01.py'))
assert(work_module is None)
# Restore our permissions
chmod(join(work_dir, 'test01.py'), 0600)
def test_load_var():
class mod:
boo = 10
foo = 20
sys.modules['fake_module'] = mod
assert load_var('fake_module', 'boo') == 10
assert load_var('fake_module:foo', 'boo') == 20
def load_module(self, fullname):
if fullname in sys.modules:
result = sys.modules[fullname]
else:
if fullname not in self.libs:
raise ImportError('unable to find extension for %s' % fullname)
result = imp.load_dynamic(fullname, self.libs[fullname])
result.__loader__ = self
parts = fullname.rsplit('.', 1)
if len(parts) > 1:
result.__package__ = parts[0]
return result
def resolve(module_name, dotted_path):
if module_name in sys.modules:
mod = sys.modules[module_name]
else:
mod = __import__(module_name)
if dotted_path is None:
result = mod
else:
parts = dotted_path.split('.')
result = getattr(mod, parts.pop(0))
for p in parts:
result = getattr(result, p)
return result
def _import_module(name):
"""Import module, returning the module after the last dot."""
__import__(name)
return sys.modules[name]