Python sys.path_hooks() Examples
The following are 30
code examples of sys.path_hooks().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
sys
, or try the search function
.
Example #1
Source File: sandbox_test.py From python-compat-runtime with Apache License 2.0 | 6 votes |
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__)
Example #2
Source File: test_path.py From ironpython3 with Apache License 2.0 | 6 votes |
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')
Example #3
Source File: test_imp.py From ironpython3 with Apache License 2.0 | 6 votes |
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[:]
Example #4
Source File: pkgutil.py From ironpython3 with Apache License 2.0 | 6 votes |
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
Example #5
Source File: test_imp.py From ironpython2 with Apache License 2.0 | 6 votes |
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 = []
Example #6
Source File: pkgutil.py From Imogen with MIT License | 6 votes |
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
Example #7
Source File: test_path.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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')
Example #8
Source File: pkgutil.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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
Example #9
Source File: test_path.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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
Example #10
Source File: dev_appserver.py From browserscope with Apache License 2.0 | 6 votes |
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
Example #11
Source File: sandbox_test.py From browserscope with Apache License 2.0 | 6 votes |
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__)
Example #12
Source File: sandbox_test.py From browserscope with Apache License 2.0 | 6 votes |
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__)
Example #13
Source File: pkgutil.py From ironpython3 with Apache License 2.0 | 5 votes |
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 all registered top level importers (i.e. those on both sys.meta_path and sys.path_hooks). If the named module is in a package, that package is imported as a side effect of invoking this function. If no module name is specified, all top level importers are produced. """ if fullname.startswith('.'): msg = "Relative module name {!r} not supported".format(fullname) raise ImportError(msg) if '.' in fullname: # Get the containing package's __path__ pkg_name = fullname.rpartition(".")[0] pkg = importlib.import_module(pkg_name) path = getattr(pkg, '__path__', None) if path is None: return else: yield from sys.meta_path path = sys.path for item in path: yield get_importer(item)
Example #14
Source File: test_importhooks.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def setUp(self): self.path = sys.path[:] self.meta_path = sys.meta_path[:] self.path_hooks = sys.path_hooks[:] sys.path_importer_cache.clear() self.modules_before = sys.modules.copy()
Example #15
Source File: pkgutil.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
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
Example #16
Source File: test_importhooks.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def testPathHook(self): sys.path_hooks.append(PathImporter) sys.path.append(test_path) self.doTestImports()
Example #17
Source File: test_importhooks.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def testImpWrapper(self): i = ImpWrapper() sys.meta_path.append(i) sys.path_hooks.append(ImpWrapper) mnames = ("colorsys", "urlparse", "distutils.core", "compiler.misc") for mname in mnames: parent = mname.split(".")[0] for n in sys.modules.keys(): if n.startswith(parent): del sys.modules[n] with test_support.check_warnings(("The compiler package is deprecated " "and removed", DeprecationWarning)): for mname in mnames: m = __import__(mname, globals(), locals(), ["__dummy__"]) m.__loader__ # to make sure we actually handled the import
Example #18
Source File: test_importhooks.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def tearDown(self): sys.path[:] = self.path sys.meta_path[:] = self.meta_path sys.path_hooks[:] = self.path_hooks sys.path_importer_cache.clear() sys.modules.clear() sys.modules.update(self.modules_before)
Example #19
Source File: test_zipimport_support.py From ironpython3 with Apache License 2.0 | 5 votes |
def setUp(self): linecache.clearcache() zipimport._zip_directory_cache.clear() self.path = sys.path[:] self.meta_path = sys.meta_path[:] self.path_hooks = sys.path_hooks[:] sys.path_importer_cache.clear()
Example #20
Source File: test_zipimport.py From ironpython3 with Apache License 2.0 | 5 votes |
def setUp(self): self.path = sys.path[:] self.meta_path = sys.meta_path[:] self.path_hooks = sys.path_hooks[:] sys.path_importer_cache.clear() self.modules_before = support.modules_setup()
Example #21
Source File: regrtest.py From ironpython3 with Apache License 2.0 | 5 votes |
def restore_sys_path_hooks(self, saved_hooks): sys.path_hooks = saved_hooks[1] sys.path_hooks[:] = saved_hooks[2]
Example #22
Source File: regrtest.py From ironpython3 with Apache License 2.0 | 5 votes |
def get_sys_path_hooks(self): return id(sys.path_hooks), sys.path_hooks, sys.path_hooks[:]
Example #23
Source File: util.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def import_state(**kwargs): """Context manager to manage the various importers and stored state in the sys module. The 'modules' attribute is not supported as the interpreter state stores a pointer to the dict that the interpreter uses internally; reassigning to sys.modules does not have the desired effect. """ originals = {} try: for attr, default in (('meta_path', []), ('path', []), ('path_hooks', []), ('path_importer_cache', {})): originals[attr] = getattr(sys, attr) if attr in kwargs: new_value = kwargs[attr] del kwargs[attr] else: new_value = default setattr(sys, attr, new_value) if len(kwargs): raise ValueError( 'unrecognized arguments: {0}'.format(kwargs.keys())) yield finally: for attr, value in originals.items(): setattr(sys, attr, value)
Example #24
Source File: test_path.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_path_importer_cache_empty_string(self): # The empty string should create a finder using the cwd. path = '' module = '<test module>' importer = util.mock_spec(module) hook = util.mock_path_hook(os.getcwd(), importer=importer) with util.import_state(path=[path], path_hooks=[hook]): loader = self.machinery.PathFinder.find_module(module) self.assertIs(loader, importer) self.assertIn(os.getcwd(), sys.path_importer_cache)
Example #25
Source File: test_zipimport.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def setUp(self): self.path = sys.path[:] self.meta_path = sys.meta_path[:] self.path_hooks = sys.path_hooks[:] sys.path_importer_cache.clear() self.modules_before = support.modules_setup()
Example #26
Source File: util.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def mock_path_hook(*entries, importer): """A mock sys.path_hooks entry.""" def hook(entry): if entry not in entries: raise ImportError return importer return hook
Example #27
Source File: pkg_resources.py From lightbulb-framework with MIT License | 5 votes |
def get_importer(path_item): """Retrieve a PEP 302 "importer" for the given path item If there is no importer, this returns a wrapper around the builtin import machinery. The returned importer is only cached if it was created by a path hook. """ try: importer = sys.path_importer_cache[path_item] except KeyError: for hook in sys.path_hooks: try: importer = hook(path_item) except ImportError: pass else: break else: importer = None sys.path_importer_cache.setdefault(path_item,importer) if importer is None: try: importer = ImpWrapper(path_item) except ImportError: pass return importer
Example #28
Source File: pkgutil.py From Imogen with MIT License | 5 votes |
def iter_importers(fullname=""): """Yield finders for the given module name If fullname contains a '.', the finders will be for the package containing fullname, otherwise they will be all registered top level finders (i.e. those on both sys.meta_path and sys.path_hooks). If the named module is in a package, that package is imported as a side effect of invoking this function. If no module name is specified, all top level finders are produced. """ if fullname.startswith('.'): msg = "Relative module name {!r} not supported".format(fullname) raise ImportError(msg) if '.' in fullname: # Get the containing package's __path__ pkg_name = fullname.rpartition(".")[0] pkg = importlib.import_module(pkg_name) path = getattr(pkg, '__path__', None) if path is None: return else: yield from sys.meta_path path = sys.path for item in path: yield get_importer(item)
Example #29
Source File: test_namespace_pkgs.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def namespace_tree_context(**kwargs): """ Save import state and sys.modules cache and restore it on exit. Typical usage: >>> with namespace_tree_context(path=['/tmp/xxyy/portion1', ... '/tmp/xxyy/portion2']): ... pass """ # use default meta_path and path_hooks unless specified otherwise kwargs.setdefault('meta_path', sys.meta_path) kwargs.setdefault('path_hooks', sys.path_hooks) import_context = util.import_state(**kwargs) with import_context, sys_modules_context(): yield
Example #30
Source File: test_path.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_path_hooks(self): # Test that sys.path_hooks is used. # Test that sys.path_importer_cache is set. module = '<test module>' path = '<test path>' importer = util.mock_spec(module) hook = util.mock_path_hook(path, importer=importer) with util.import_state(path_hooks=[hook]): loader = self.machinery.PathFinder.find_module(module, [path]) self.assertIs(loader, importer) self.assertIn(path, sys.path_importer_cache) self.assertIs(sys.path_importer_cache[path], importer)