本文整理匯總了Python中sys.path_hooks方法的典型用法代碼示例。如果您正苦於以下問題:Python sys.path_hooks方法的具體用法?Python sys.path_hooks怎麽用?Python sys.path_hooks使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類sys
的用法示例。
在下文中一共展示了sys.path_hooks方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_load_with_path_hook
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def test_load_with_path_hook(self):
class DummyPathHook(object):
def __init__(self, path):
if path != 'dummy/path':
raise ImportError
def find_module(self, unused_fullname):
return self
def load_module(self, fullname):
return imp.new_module('fake name: %s' % fullname)
self.test_policies['distutils.util'] = sandbox.ModuleOverridePolicy(
None, [], {}, default_pass_through=True)
sys.path_hooks = [DummyPathHook]
util = self.hook.load_module('distutils.util')
self.assertEqual('fake name: distutils.util', util.__name__)
示例2: test_load_with_path_hook_cant_find
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def test_load_with_path_hook_cant_find(self):
class DummyPathHook(object):
def __init__(self, path):
if path != 'dummy/path':
raise ImportError
def find_module(self, unused_fullname):
return None
def load_module(self, fullname):
raise ImportError
self.test_policies['distutils.util'] = sandbox.ModuleOverridePolicy(
None, [], {}, default_pass_through=True)
sys.path_hooks = [DummyPathHook]
util = self.hook.load_module('distutils.util')
self.assertEqual('distutils.util', util.__name__)
示例3: __init__
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def __init__(self, modules):
"""Initializer.
Args:
modules: Dictionary containing monitored modules.
"""
self._modules = modules
self._default_modules = self._modules.copy()
self._save_path_hooks = sys.path_hooks[:]
self._modification_times = {}
self._dirty = True
示例4: test_path_hooks
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def test_path_hooks(self):
import toimport
def prepare(f):
sys.path_importer_cache = {}
sys.path_hooks = [f]
if 'toimport' in sys.modules: del sys.modules['toimport']
def hook(*args): raise Exception('hello')
prepare(hook)
def f(): import toimport
self.assertRaisesMessage(Exception, 'hello', f)
# ImportError shouldn't propagate out
def hook(*args): raise ImportError('foo')
prepare(hook)
f()
# returning none should be ok
def hook(*args): pass
prepare(hook)
f()
sys.path_hooks = []
示例5: get_importer
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
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.
The cache (or part of it) can be cleared manually if a
rescan of sys.path_hooks is necessary.
"""
try:
importer = sys.path_importer_cache[path_item]
except KeyError:
for path_hook in sys.path_hooks:
try:
importer = path_hook(path_item)
sys.path_importer_cache.setdefault(path_item, importer)
break
except ImportError:
pass
else:
importer = None
return importer
示例6: test_None_on_sys_path
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def test_None_on_sys_path(self):
# Putting None in sys.path[0] caused an import regression from Python
# 3.2: http://bugs.python.org/issue16514
new_path = sys.path[:]
new_path.insert(0, None)
new_path_importer_cache = sys.path_importer_cache.copy()
new_path_importer_cache.pop(None, None)
new_path_hooks = [zipimport.zipimporter,
self.machinery.FileFinder.path_hook(
*self.importlib._bootstrap_external._get_supported_file_loaders())]
missing = object()
email = sys.modules.pop('email', missing)
try:
with util.import_state(meta_path=sys.meta_path[:],
path=new_path,
path_importer_cache=new_path_importer_cache,
path_hooks=new_path_hooks):
module = self.importlib.import_module('email')
self.assertIsInstance(module, ModuleType)
finally:
if email is not missing:
sys.modules['email'] = email
示例7: test_finder_with_failing_find_module
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def test_finder_with_failing_find_module(self):
# PathEntryFinder with find_module() defined should work.
# Issue #20763.
class Finder:
path_location = 'test_finder_with_find_module'
def __init__(self, path):
if path != self.path_location:
raise ImportError
@staticmethod
def find_module(fullname):
return None
with util.import_state(path=[Finder.path_location]+sys.path[:],
path_hooks=[Finder]):
self.machinery.PathFinder.find_spec('importlib')
示例8: get_importer
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def get_importer(path_item):
"""Retrieve a finder for the given path item
The returned finder is cached in sys.path_importer_cache
if it was newly created by a path hook.
The cache (or part of it) can be cleared manually if a
rescan of sys.path_hooks is necessary.
"""
try:
importer = sys.path_importer_cache[path_item]
except KeyError:
for path_hook in sys.path_hooks:
try:
importer = path_hook(path_item)
sys.path_importer_cache.setdefault(path_item, importer)
break
except ImportError:
pass
else:
importer = None
return importer
示例9: setUp
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def setUp(self):
super(ImpTest, self).setUp()
self._testdir = "ImpTest"
self._imptestdir = os.path.join(self.test_dir, self._testdir)
self._f_init = os.path.join(self._imptestdir, "__init__.py")
self._f_module = os.path.join(self._imptestdir, "imptestmod.py")
self.temp_name = ["os",
"os.P_WAIT",
"os.chmod",
"sys.path",
"xxxx"
]
# backup path values
self.__path = sys.path[:]
self.__path_hooks = sys.path_hooks[:]
self.__meta_path = sys.meta_path[:]
示例10: get_sys_path_hooks
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def get_sys_path_hooks(self):
return id(sys.path_hooks), sys.path_hooks, sys.path_hooks[:]
示例11: restore_sys_path_hooks
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def restore_sys_path_hooks(self, saved_hooks):
sys.path_hooks = saved_hooks[1]
sys.path_hooks[:] = saved_hooks[2]
示例12: setUp
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def setUp(self):
super(ModuleOverrideImportHookTest, self).setUp()
self.test_policies = {}
self.path = sys.path[:]
self.hook = sandbox.ModuleOverrideImportHook(self.test_policies)
sys.path_importer_cache = {}
sys.modules.pop('distutils', None)
__import__('distutils').__path__.insert(0, 'dummy/path')
sys.modules.pop('distutils.util', None)
sys.modules.pop('thread', None)
self.imported_modules = set(sys.modules)
self.path_hooks = sys.path_hooks
示例13: tearDown
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def tearDown(self):
sys.path_hooks = self.path_hooks
sys.path_importer_cache = {}
sys.path = self.path
added_modules = set(sys.modules) - self.imported_modules
for name in added_modules:
del sys.modules[name]
distutils_modules = [module for module in sys.modules if
module.startswith('distutils')]
for name in distutils_modules:
del sys.modules[name]
sys.modules.pop('thread', None)
super(ModuleOverrideImportHookTest, self).tearDown()
示例14: get_importer
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
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.
"""
try:
importer = sys.path_importer_cache[path_item]
except KeyError:
for path_hook in sys.path_hooks:
try:
importer = path_hook(path_item)
break
except ImportError:
pass
else:
importer = None
sys.path_importer_cache.setdefault(path_item, importer)
if importer is None:
try:
importer = ImpImporter(path_item)
except ImportError:
importer = None
return importer
示例15: _get_importer
# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def _get_importer(path_name):
"""Python version of PyImport_GetImporter C API function"""
cache = sys.path_importer_cache
try:
importer = cache[path_name]
except KeyError:
# Not yet cached. Flag as using the
# standard machinery until we finish
# checking the hooks
cache[path_name] = None
for hook in sys.path_hooks:
try:
importer = hook(path_name)
break
except ImportError:
pass
else:
# The following check looks a bit odd. The trick is that
# NullImporter throws ImportError if the supplied path is a
# *valid* directory entry (and hence able to be handled
# by the standard import machinery)
try:
importer = imp.NullImporter(path_name)
except ImportError:
return None
cache[path_name] = importer
return importer