def custom_import_install():
if __builtin__.__import__ == NATIVE_IMPORTER:
INVALID_MODULES.update(sys.modules.keys())
__builtin__.__import__ = custom_importer
python类__import__()的实例源码
def install(self):
self.save_import_module = __builtin__.__import__
self.save_reload = __builtin__.reload
if not hasattr(__builtin__, 'unload'):
__builtin__.unload = None
self.save_unload = __builtin__.unload
__builtin__.__import__ = self.import_module
__builtin__.reload = self.reload
__builtin__.unload = self.unload
def uninstall(self):
__builtin__.__import__ = self.save_import_module
__builtin__.reload = self.save_reload
__builtin__.unload = self.save_unload
if not __builtin__.unload:
del __builtin__.unload
def install(self):
self.save_import_module = __builtin__.__import__
self.save_reload = __builtin__.reload
if not hasattr(__builtin__, 'unload'):
__builtin__.unload = None
self.save_unload = __builtin__.unload
__builtin__.__import__ = self.import_module
__builtin__.reload = self.reload
__builtin__.unload = self.unload
def uninstall(self):
__builtin__.__import__ = self.save_import_module
__builtin__.reload = self.save_reload
__builtin__.unload = self.save_unload
if not __builtin__.unload:
del __builtin__.unload
def __import__(name, globals={}, locals={}, fromlist=[], level=-1):
"""Compatibility definition for Python 2.4.
Silently ignore the `level` argument missing in Python < 2.5.
"""
# we need the level arg because the default changed in Python 3.3
return __builtin__.__import__(name, globals, locals, fromlist)
def install(self):
self.save_import_module = __builtin__.__import__
self.save_reload = __builtin__.reload
if not hasattr(__builtin__, 'unload'):
__builtin__.unload = None
self.save_unload = __builtin__.unload
__builtin__.__import__ = self.import_module
__builtin__.reload = self.reload
__builtin__.unload = self.unload
def uninstall(self):
__builtin__.__import__ = self.save_import_module
__builtin__.reload = self.save_reload
__builtin__.unload = self.save_unload
if not __builtin__.unload:
del __builtin__.unload
def _patch_import_to_patch_pyqt_on_import(patch_qt_on_import):
# I don't like this approach very much as we have to patch __import__, but I like even less
# asking the user to configure something in the client side...
# So, our approach is to patch PyQt4/5 right before the user tries to import it (at which
# point he should've set the sip api version properly already anyways).
dotted = patch_qt_on_import + '.'
original_import = __import__
from _pydev_imps._pydev_sys_patch import patch_sys_module, patch_reload, cancel_patches_in_sys_module
patch_sys_module()
patch_reload()
def patched_import(name, *args, **kwargs):
if patch_qt_on_import == name or name.startswith(dotted):
builtins.__import__ = original_import
cancel_patches_in_sys_module()
_internal_patch_qt() # Patch it only when the user would import the qt module
return original_import(name, *args, **kwargs)
try:
import builtins
except ImportError:
import __builtin__ as builtins
builtins.__import__ = patched_import
def enable(blacklist=None):
"""Enable global module dependency tracking.
A blacklist can be specified to exclude specific modules (and their import
hierachies) from the reloading process. The blacklist can be any iterable
listing the fully-qualified names of modules that should be ignored. Note
that blacklisted modules will still appear in the dependency graph; they
will just not be reloaded.
"""
global _blacklist
builtins.__import__ = _import
if blacklist is not None:
_blacklist = frozenset(blacklist)
def disable():
"""Disable global module dependency tracking."""
global _blacklist, _parent
builtins.__import__ = _baseimport
_blacklist = None
_dependencies.clear()
_parent = None
def _import(name, globals=None, locals=None, fromlist=None, level=_default_level):
"""__import__() replacement function that tracks module dependencies."""
# Track our current parent module. This is used to find our current place
# in the dependency graph.
global _parent
parent = _parent
_parent = name
# Perform the actual import work using the base import function.
base = _baseimport(name, globals, locals, fromlist, level)
if base is not None and parent is not None:
m = base
# We manually walk through the imported hierarchy because the import
# function only returns the top-level package reference for a nested
# import statement (e.g. 'package' for `import package.module`) when
# no fromlist has been specified. It's possible that the package
# might not have all of its descendents as attributes, in which case
# we fall back to using the immediate ancestor of the module instead.
if fromlist is None:
for component in name.split('.')[1:]:
try:
m = getattr(m, component)
except AttributeError:
m = sys.modules[m.__name__ + '.' + component]
# If this is a nested import for a reloadable (source-based) module,
# we append ourself to our parent's dependency list.
if hasattr(m, '__file__'):
l = _dependencies.setdefault(parent, [])
l.append(m)
# Lastly, we always restore our global _parent pointer.
_parent = parent
return base
def install_import_hook():
"""Installs __import__ hook."""
saved_import = builtins.__import__
@functools.wraps(saved_import)
def import_hook(name, *args, **kwargs):
if name == 'end':
process_import()
end
return saved_import(name, *args, **kwargs)
end
builtins.__import__ = import_hook
def install(self):
self.save_import_module = __builtin__.__import__
self.save_reload = __builtin__.reload
if not hasattr(__builtin__, 'unload'):
__builtin__.unload = None
self.save_unload = __builtin__.unload
__builtin__.__import__ = self.import_module
__builtin__.reload = self.reload
__builtin__.unload = self.unload
def uninstall(self):
__builtin__.__import__ = self.save_import_module
__builtin__.reload = self.save_reload
__builtin__.unload = self.save_unload
if not __builtin__.unload:
del __builtin__.unload
def local_import_aux(name, reload_force=False, app='welcome'):
"""
In apps, instead of importing a local module
(in applications/app/modules) with::
import a.b.c as d
you should do::
d = local_import('a.b.c')
or (to force a reload):
d = local_import('a.b.c', reload=True)
This prevents conflict between applications and un-necessary execs.
It can be used to import any module, including regular Python modules.
"""
items = name.replace('/', '.')
name = "applications.%s.modules.%s" % (app, items)
module = __import__(name)
for item in name.split(".")[1:]:
module = getattr(module, item)
if reload_force:
reload(module)
return module
def custom_import_install():
if __builtin__.__import__ == NATIVE_IMPORTER:
INVALID_MODULES.update(sys.modules.keys())
__builtin__.__import__ = custom_importer
def install(self):
self.save_import_module = __builtin__.__import__
self.save_reload = __builtin__.reload
if not hasattr(__builtin__, 'unload'):
__builtin__.unload = None
self.save_unload = __builtin__.unload
__builtin__.__import__ = self.import_module
__builtin__.reload = self.reload
__builtin__.unload = self.unload
def uninstall(self):
__builtin__.__import__ = self.save_import_module
__builtin__.reload = self.save_reload
__builtin__.unload = self.save_unload
if not __builtin__.unload:
del __builtin__.unload
def install(self):
self.save_import_module = __builtin__.__import__
self.save_reload = __builtin__.reload
if not hasattr(__builtin__, 'unload'):
__builtin__.unload = None
self.save_unload = __builtin__.unload
__builtin__.__import__ = self.import_module
__builtin__.reload = self.reload
__builtin__.unload = self.unload