Python sys.path_importer_cache() Examples

The following are code examples for showing how to use sys.path_importer_cache(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don't like. You can also save this page to your account.

Example 1
Project: filefinder2   Author: asmodehn   File: _filefinder2.py    (license) View Source Project 6 votes vote down vote up
def _path_importer_cache(cls, path):  # from importlib.PathFinder
            """Get the finder for the path entry from sys.path_importer_cache.
            If the path entry is not in the cache, find the appropriate finder
            and cache it. If no finder is available, store None.
            """
            if path == '':
                try:
                    path = os.getcwd()
                except FileNotFoundError:
                    # Don't cache the failure as the cwd can easily change to
                    # a valid directory later on.
                    return None
            try:
                finder = sys.path_importer_cache[path]
            except KeyError:
                finder = cls._path_hooks(path)
                sys.path_importer_cache[path] = finder

            return finder 
Example 2
Project: filefinder2   Author: asmodehn   File: _filefinder2.py    (license) View Source Project 6 votes vote down vote up
def find_spec(cls, fullname, path=None, target=None):
            """find the module on sys.path or 'path' based on sys.path_hooks and
            sys.path_importer_cache."""
            if path is None:
                path = sys.path
            spec = cls._get_spec(fullname, path, target)
            if spec is None:
                return None
            elif spec.loader is None:
                namespace_path = spec.submodule_search_locations
                if namespace_path:
                    # We found at least one namespace path.  Return a
                    #  spec which can create the namespace package.
                    spec.origin = 'namespace'
                    spec.submodule_search_locations = _NamespacePath(fullname, namespace_path, cls._get_spec)
                    return spec
                else:
                    return None
            else:
                return spec

    # to match importlib API 
Example 3
Project: rosimport   Author: pyros-dev   File: _utils.py    (license) View Source Project 6 votes vote down vote up
def print_importers_of(module):
    import sys
    import pprint

    print('MODULE:'),
    pprint.pprint(module)
    mod_path = module.__file__.split(os.sep)
    print()
    print('IMPORTERS USED: {')
    for i in range(len(mod_path)-1):
        parpath = os.sep.join(mod_path[:len(mod_path) - i])
        if parpath in sys.path_importer_cache:
            cached_imp = sys.path_importer_cache[parpath]
            parpath = parpath.replace(sys.prefix, '...')
            print('%s: %r' % (parpath, cached_imp))
    print('}') 
Example 4
Project: rosimport   Author: pyros-dev   File: __init__.py    (license) View Source Project 6 votes vote down vote up
def activate():
    """Install the path-based import components."""
    # We should plug filefinder first to avoid plugging ROSDirectoryFinder, when it is not a ROS thing...

    filefinder2.activate()

    if ros_path_hook not in sys.path_hooks:
        # We need to be before FileFinder to be able to find our '.msg' and '.srv' files without making a namespace package
        # Note this must be early in the path_hook list, since we change the logic
        # and a namespace package becomes a ros importable package.
        sys.path_hooks.insert(filefinder2.get_filefinder_index_in_path_hooks(), ros_path_hook)

    if ROSPathFinder not in sys.meta_path:
        # adding metahook, before the usual pathfinder, to avoid interferences with python namespace mechanism...
        sys.meta_path.insert(filefinder2.get_pathfinder_index_in_meta_hooks(), ROSPathFinder)

    # Resetting sys.path_importer_cache
    # to support the case where we have an implicit (msg/srv) package inside an already loaded package,
    # since we need to replace the default importer.
    sys.path_importer_cache.clear() 
Example 5
Project: zippy   Author: securesystemslab   File: test_path.py    (license) View Source Project 6 votes vote down vote up
def test_implicit_hooks(self):
        # Test that the implicit path hooks are used.
        bad_path = '<path>'
        module = '<module>'
        assert not os.path.exists(bad_path)
        existing_path = tempfile.mkdtemp()
        try:
            with util.import_state():
                nothing = _bootstrap._DefaultPathFinder.find_module(module,
                                                        path=[existing_path])
                self.assertTrue(nothing is None)
                self.assertTrue(existing_path in sys.path_importer_cache)
                result = isinstance(sys.path_importer_cache[existing_path],
                                    imp.NullImporter)
                self.assertFalse(result)
                nothing = _bootstrap._DefaultPathFinder.find_module(module,
                                                            path=[bad_path])
                self.assertTrue(nothing is None)
                self.assertTrue(bad_path in sys.path_importer_cache)
                self.assertTrue(isinstance(sys.path_importer_cache[bad_path],
                                           imp.NullImporter))
        finally:
            os.rmdir(existing_path) 
Example 6
Project: zippy   Author: securesystemslab   File: test_path.py    (license) View Source Project 6 votes vote down vote up
def test_path_importer_cache_has_None(self):
        # Test that the default hook is used when sys.path_importer_cache
        # contains None for a path.
        module = '<test module>'
        importer = util.mock_modules(module)
        path = '<test path>'
        # XXX Not blackbox.
        original_hook = _bootstrap._DEFAULT_PATH_HOOK
        mock_hook = import_util.mock_path_hook(path, importer=importer)
        _bootstrap._DEFAULT_PATH_HOOK = mock_hook
        try:
            with util.import_state(path_importer_cache={path: None}):
                loader = _bootstrap._DefaultPathFinder.find_module(module,
                                                                    path=[path])
                self.assertTrue(loader is importer)
        finally:
            _bootstrap._DEFAULT_PATH_HOOK = original_hook 
Example 7
Project: zippy   Author: securesystemslab   File: test_threaded_import.py    (license) View Source Project 6 votes vote down vote up
def test_parallel_path_hooks(self):
        # Here the Finder instance is only used to check concurrent calls
        # to path_hook().
        finder = Finder()
        # In order for our path hook to be called at each import, we need
        # to flush the path_importer_cache, which we do by registering a
        # dedicated meta_path entry.
        flushing_finder = FlushingFinder()
        def path_hook(path):
            finder.find_module('')
            raise ImportError
        sys.path_hooks.append(path_hook)
        sys.meta_path.append(flushing_finder)
        try:
            # Flush the cache a first time
            flushing_finder.find_module('')
            numtests = self.check_parallel_module_init()
            self.assertGreater(finder.numcalls, 0)
            self.assertEqual(finder.x, finder.numcalls)
        finally:
            sys.meta_path.remove(flushing_finder)
            sys.path_hooks.remove(path_hook) 
Example 8
Project: driveboardapp   Author: nortd   File: util.py    (license) View Source Project 6 votes vote down vote up
def _check_importer_for_path(name, path_item):
    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:
            return imp.find_module(name, [path_item])
        except ImportError:
            return None
    return importer.find_module(name) 
Example 9
Project: web_ctp   Author: molebot   File: test_threaded_import.py    (license) View Source Project 6 votes vote down vote up
def test_parallel_path_hooks(self):
        # Here the Finder instance is only used to check concurrent calls
        # to path_hook().
        finder = Finder()
        # In order for our path hook to be called at each import, we need
        # to flush the path_importer_cache, which we do by registering a
        # dedicated meta_path entry.
        flushing_finder = FlushingFinder()
        def path_hook(path):
            finder.find_module('')
            raise ImportError
        sys.path_hooks.insert(0, path_hook)
        sys.meta_path.append(flushing_finder)
        try:
            # Flush the cache a first time
            flushing_finder.find_module('')
            numtests = self.check_parallel_module_init()
            self.assertGreater(finder.numcalls, 0)
            self.assertEqual(finder.x, finder.numcalls)
        finally:
            sys.meta_path.remove(flushing_finder)
            sys.path_hooks.remove(path_hook) 
Example 10
Project: web_ctp   Author: molebot   File: test_api.py    (license) View Source Project 6 votes vote down vote up
def test_method_called(self):
        # If defined the method should be called.
        class InvalidatingNullFinder:
            def __init__(self, *ignored):
                self.called = False
            def find_module(self, *args):
                return None
            def invalidate_caches(self):
                self.called = True

        key = 'gobledeegook'
        meta_ins = InvalidatingNullFinder()
        path_ins = InvalidatingNullFinder()
        sys.meta_path.insert(0, meta_ins)
        self.addCleanup(lambda: sys.path_importer_cache.__delitem__(key))
        sys.path_importer_cache[key] = path_ins
        self.addCleanup(lambda: sys.meta_path.remove(meta_ins))
        importlib.invalidate_caches()
        self.assertTrue(meta_ins.called)
        self.assertTrue(path_ins.called) 
Example 11
Project: ouroboros   Author: pybee   File: pkgutil.py    (license) View Source Project 6 votes vote down vote up
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 12
Project: ouroboros   Author: pybee   File: test_threaded_import.py    (license) View Source Project 6 votes vote down vote up
def test_parallel_path_hooks(self):
        # Here the Finder instance is only used to check concurrent calls
        # to path_hook().
        finder = Finder()
        # In order for our path hook to be called at each import, we need
        # to flush the path_importer_cache, which we do by registering a
        # dedicated meta_path entry.
        flushing_finder = FlushingFinder()
        def path_hook(path):
            finder.find_spec('')
            raise ImportError
        sys.path_hooks.insert(0, path_hook)
        sys.meta_path.append(flushing_finder)
        try:
            # Flush the cache a first time
            flushing_finder.find_spec('')
            numtests = self.check_parallel_module_init()
            self.assertGreater(finder.numcalls, 0)
            self.assertEqual(finder.x, finder.numcalls)
        finally:
            sys.meta_path.remove(flushing_finder)
            sys.path_hooks.remove(path_hook) 
Example 13
Project: ouroboros   Author: pybee   File: test_api.py    (license) View Source Project 6 votes vote down vote up
def test_method_called(self):
        # If defined the method should be called.
        class InvalidatingNullFinder:
            def __init__(self, *ignored):
                self.called = False
            def find_module(self, *args):
                return None
            def invalidate_caches(self):
                self.called = True

        key = 'gobledeegook'
        meta_ins = InvalidatingNullFinder()
        path_ins = InvalidatingNullFinder()
        sys.meta_path.insert(0, meta_ins)
        self.addCleanup(lambda: sys.path_importer_cache.__delitem__(key))
        sys.path_importer_cache[key] = path_ins
        self.addCleanup(lambda: sys.meta_path.remove(meta_ins))
        self.init.invalidate_caches()
        self.assertTrue(meta_ins.called)
        self.assertTrue(path_ins.called) 
Example 14
Project: ouroboros   Author: pybee   File: test_path.py    (license) View Source Project 6 votes vote down vote up
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._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 15
Project: kbe_server   Author: xiaohaoppy   File: pkgutil.py    (license) View Source Project 6 votes vote down vote up
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 16
Project: kbe_server   Author: xiaohaoppy   File: test_threaded_import.py    (license) View Source Project 6 votes vote down vote up
def test_parallel_path_hooks(self):
        # Here the Finder instance is only used to check concurrent calls
        # to path_hook().
        finder = Finder()
        # In order for our path hook to be called at each import, we need
        # to flush the path_importer_cache, which we do by registering a
        # dedicated meta_path entry.
        flushing_finder = FlushingFinder()
        def path_hook(path):
            finder.find_spec('')
            raise ImportError
        sys.path_hooks.insert(0, path_hook)
        sys.meta_path.append(flushing_finder)
        try:
            # Flush the cache a first time
            flushing_finder.find_spec('')
            numtests = self.check_parallel_module_init()
            self.assertGreater(finder.numcalls, 0)
            self.assertEqual(finder.x, finder.numcalls)
        finally:
            sys.meta_path.remove(flushing_finder)
            sys.path_hooks.remove(path_hook) 
Example 17
Project: kbe_server   Author: xiaohaoppy   File: test_path.py    (license) View Source Project 6 votes vote down vote up
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._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 18
Project: mac-package-build   Author: persepolisdm   File: util.py    (license) View Source Project 6 votes vote down vote up
def _check_importer_for_path(name, path_item):
    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:
            return imp.find_module(name, [path_item])
        except ImportError:
            return None
    return importer.find_module(name) 
Example 19
Project: kinect-2-libras   Author: inessadl   File: runpy.py    (Apache License 2.0) View Source Project 5 votes vote down vote up
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 
Example 20
Project: kinect-2-libras   Author: inessadl   File: pkgutil.py    (Apache License 2.0) View Source Project 5 votes vote down vote up
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 21
Project: Flask_Blog   Author: sugarguo   File: easy_install.py    (GNU General Public License v3.0) View Source Project 5 votes vote down vote up
def uncache_zipdir(path):
    """Ensure that the importer caches dont have stale info for `path`"""
    from zipimport import _zip_directory_cache as zdc
    _uncache(path, zdc)
    _uncache(path, sys.path_importer_cache) 
Example 22
Project: hostapd-mana   Author: adde88   File: runpy.py    (license) View Source Project 5 votes vote down vote up
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 raises 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 
Example 23
Project: hostapd-mana   Author: adde88   File: modules.py    (license) View Source Project 5 votes vote down vote up
def _findEntryPathString(self, modobj):
        """
        Determine where a given Python module object came from by looking at path
        entries.
        """
        topPackageObj = modobj
        while '.' in topPackageObj.__name__:
            topPackageObj = self.moduleDict['.'.join(
                    topPackageObj.__name__.split('.')[:-1])]
        if _isPackagePath(FilePath(topPackageObj.__file__)):
            # if package 'foo' is on sys.path at /a/b/foo, package 'foo's
            # __file__ will be /a/b/foo/__init__.py, and we are looking for
            # /a/b here, the path-entry; so go up two steps.
            rval = dirname(dirname(topPackageObj.__file__))
        else:
            # the module is completely top-level, not within any packages.  The
            # path entry it's on is just its dirname.
            rval = dirname(topPackageObj.__file__)
        # There are probably some awful tricks that an importer could pull
        # which would break this, so let's just make sure... it's a loaded
        # module after all, which means that its path MUST be in
        # path_importer_cache according to PEP 302 -glyph
        from pprint import pformat
        assert rval in self.importerCache, '%r for %r not in import cache %s' % (
            rval, modobj, pformat(self.importerCache))
        return rval 
Example 24
Project: hostapd-mana   Author: adde88   File: pkgutil.py    (license) View Source Project 5 votes vote down vote up
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 25
Project: cpyparsing   Author: evhub   File: stateMachine2.py    (license) View Source Project 5 votes vote down vote up
def checkpath_iter(self, fullname):
        for dirpath in sys.path:
            # if the value in sys.path_importer_cache is None, then this
            # path *should* be imported by the builtin mechanism, and the
            # entry is thus a path to a directory on the filesystem;
            # if it's not None, then some other importer is in charge, and
            # it probably isn't even a filesystem path
            if sys.path_importer_cache.get(dirpath,False) is None:
                checkpath = os.path.join(
                        dirpath,'%s.%s' % (fullname,self.suffix))
                yield checkpath 
Example 26
Project: Red-GUI   Author: ScarletRav3n   File: Red-GUI.py    (license) View Source Project 5 votes vote down vote up
def verify_requirements():
    sys.path_importer_cache = {}  # I don't know if the cache reset has any
    basic = find_spec("discord")  # side effect. Without it, the lib folder
    audio = find_spec("nacl")     # wouldn't be seen if it didn't exist
    if not basic:                 # when the launcher was started
        return None
    elif not audio:
        return False
    else:
        return True 
Example 27
Project: filefinder2   Author: asmodehn   File: _filefinder2.py    (license) View Source Project 5 votes vote down vote up
def invalidate_caches(cls):
            """Call the invalidate_caches() method on all path entry finders
            stored in sys.path_importer_caches (where implemented)."""
            for finder in sys.path_importer_cache.values():
                if hasattr(finder, 'invalidate_caches'):
                    finder.invalidate_caches() 
Example 28
Project: filefinder2   Author: asmodehn   File: _filefinder2.py    (license) View Source Project 5 votes vote down vote up
def find_module(cls, fullname, path=None):
            """find the module on sys.path or 'path' based on sys.path_hooks and
            sys.path_importer_cache.
            This method is for python2 only
            """
            spec = cls.find_spec(fullname, path)
            if spec is None:
                return None
            elif spec.loader is None and spec.submodule_search_locations:
                # Here we need to create a namespace loader to handle namespaces since python2 doesn't...
                return NamespaceLoader2(spec.name, spec.submodule_search_locations)
            else:
                return spec.loader 
Example 29
Project: filefinder2   Author: asmodehn   File: _filefinder2.py    (license) View Source Project 5 votes vote down vote up
def activate():
        """Install the path-based import components."""
        if path_hook not in sys.path_hooks:
            sys.path_hooks.append(path_hook)
        # Resetting sys.path_importer_cache values,
        # to support the case where we have an implicit package inside an already loaded package,
        # since we need to replace the default importer.
        sys.path_importer_cache.clear()

        if PathFinder2 not in sys.meta_path:
            # Setting up the meta_path to change package finding logic
            sys.meta_path.append(PathFinder2) 
Example 30
Project: mitogen   Author: dw   File: pkgutil.py    (license) View Source Project 5 votes vote down vote up
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 31
Project: Intranet-Penetration   Author: yuxiaokui   File: runpy.py    (license) View Source Project 5 votes vote down vote up
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 raises 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 
Example 32
Project: Intranet-Penetration   Author: yuxiaokui   File: pkgutil.py    (license) View Source Project 5 votes vote down vote up
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 33
Project: Intranet-Penetration   Author: yuxiaokui   File: start.py    (license) View Source Project 5 votes vote down vote up
def unload(module):
    for m in list(sys.modules.keys()):
        if m == module or m.startswith(module+"."):
            del sys.modules[m]

    for p in list(sys.path_importer_cache.keys()):
        if module in p:
            del sys.path_importer_cache[p]

    try:
        del module
    except:
        pass 
Example 34
Project: flasky   Author: RoseOu   File: easy_install.py    (license) View Source Project 5 votes vote down vote up
def uncache_zipdir(path):
    """Ensure that the importer caches dont have stale info for `path`"""
    from zipimport import _zip_directory_cache as zdc
    _uncache(path, zdc)
    _uncache(path, sys.path_importer_cache) 
Example 35
Project: MKFQ   Author: maojingios   File: runpy.py    (license) View Source Project 5 votes vote down vote up
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 raises 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 
Example 36
Project: MKFQ   Author: maojingios   File: pkgutil.py    (license) View Source Project 5 votes vote down vote up
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 37
Project: MKFQ   Author: maojingios   File: start.py    (license) View Source Project 5 votes vote down vote up
def unload(module):
    for m in list(sys.modules.keys()):
        if m == module or m.startswith(module+"."):
            del sys.modules[m]

    for p in list(sys.path_importer_cache.keys()):
        if module in p:
            del sys.path_importer_cache[p]

    try:
        del module
    except:
        pass 
Example 38
Project: rosimport   Author: pyros-dev   File: _utils.py    (license) View Source Project 5 votes vote down vote up
def print_importers():
    import sys
    import pprint

    print('PATH:'),
    pprint.pprint(sys.path)
    print()
    print('IMPORTERS CACHE: {')
    for name, cache_value in sys.path_importer_cache.items():
        name = name.replace(sys.prefix, '...')
        print('%s: %r' % (name, cache_value))
    print('}') 
Example 39
Project: rosimport   Author: pyros-dev   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def deactivate():
    # CAREFUL : Even though we remove the path from sys.path,
    # initialized finders will remain in sys.path_importer_cache

    # removing metahook
    sys.meta_path.pop(get_rospathfinder_index_in_meta_hooks())
    # removing path_hook
    sys.path_hooks.pop(get_rosdirectoryfinder_index_in_path_hooks())

    filefinder2.deactivate()

    # Resetting sys.path_importer_cache to get rid of previous importers
    sys.path_importer_cache.clear() 
Example 40
Project: zippy   Author: securesystemslab   File: runpy.py    (license) View Source Project 5 votes vote down vote up
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 
Example 41
Project: zippy   Author: securesystemslab   File: test_path.py    (license) View Source Project 5 votes vote down vote up
def test_sys_path(self):
        # Test that sys.path is used when 'path' is None.
        # Implicitly tests that sys.path_importer_cache is used.
        module = '<test module>'
        path = '<test path>'
        importer = util.mock_modules(module)
        with util.import_state(path_importer_cache={path: importer},
                               path=[path]):
            loader = machinery.PathFinder.find_module(module)
            self.assertTrue(loader is importer) 
Example 42
Project: zippy   Author: securesystemslab   File: test_path.py    (license) View Source Project 5 votes vote down vote up
def test_path(self):
        # Test that 'path' is used when set.
        # Implicitly tests that sys.path_importer_cache is used.
        module = '<test module>'
        path = '<test path>'
        importer = util.mock_modules(module)
        with util.import_state(path_importer_cache={path: importer}):
            loader = machinery.PathFinder.find_module(module, [path])
            self.assertTrue(loader is importer) 
Example 43
Project: zippy   Author: securesystemslab   File: test_path.py    (license) View Source Project 5 votes vote down vote up
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_modules(module)
        hook = import_util.mock_path_hook(path, importer=importer)
        with util.import_state(path_hooks=[hook]):
            loader = machinery.PathFinder.find_module(module, [path])
            self.assertTrue(loader is importer)
            self.assertTrue(path in sys.path_importer_cache)
            self.assertTrue(sys.path_importer_cache[path] is importer) 
Example 44
Project: zippy   Author: securesystemslab   File: test_path.py    (license) View Source Project 5 votes vote down vote up
def test_path_importer_cache_has_None(self):
        # Test that if sys.path_importer_cache has None that None is returned.
        clear_cache = {path: None for path in sys.path}
        with util.import_state(path_importer_cache=clear_cache):
            for name in ('asynchat', 'sys', '<test module>'):
                self.assertTrue(machinery.PathFinder.find_module(name) is None) 
Example 45
Project: zippy   Author: securesystemslab   File: pkgutil.py    (license) View Source Project 5 votes vote down vote up
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 46
Project: zippy   Author: securesystemslab   File: test_threaded_import.py    (license) View Source Project 5 votes vote down vote up
def find_module(self, name, path=None):
        sys.path_importer_cache.clear() 
Example 47
Project: figura   Author: shx2   File: _python3.py    (license) View Source Project 5 votes vote down vote up
def install(compiler, suffixes):
    filefinder = [(f, i) for i, f in enumerate(sys.path_hooks)
                  if repr(f).find('.path_hook_for_FileFinder') != -1]
    if filefinder:
        filefinder, fpos = filefinder[0]
        sys.path_hooks[fpos] = PolyFileFinder.path_hook(*(_suffixer(_get_supported_file_loaders())))
        sys.path_importer_cache = {}
        pkgutil.iter_importer_modules.register(PolyFileFinder, _poly_file_finder_modules)

    PolyFileFinder._install(compiler, suffixes) 
Example 48
Project: figura   Author: shx2   File: importutils.py    (license) View Source Project 5 votes vote down vote up
def __enter__(self):
        # suppress writing of .pyc files:
        self.prev_dont_write_bytecode = sys.dont_write_bytecode
        sys.dont_write_bytecode = True
        
        if self.cleanup_import_caches:
            # remember which modules were already loaded before we run the import.
            self._backup_dict(sys.modules, 'modules', run_with_empty = False)
            self._backup_dict(sys.path_importer_cache, 'path_importer_cache', run_with_empty = True) 
Example 49
Project: figura   Author: shx2   File: importutils.py    (license) View Source Project 5 votes vote down vote up
def __exit__(self, exc_type, exc_val, exc_tb):
        if self.cleanup_import_caches:
            # remove all modules which got added to sys.modules in this import, to
            # ensure the next time we are here, they get reloaded.
            self._restore_dict(sys.modules, 'modules')
            self._restore_dict(sys.path_importer_cache, 'path_importer_cache')

        sys.dont_write_bytecode = self.prev_dont_write_bytecode 
Example 50
Project: chihu   Author: yelongyu   File: easy_install.py    (license) View Source Project 5 votes vote down vote up
def uncache_zipdir(path):
    """Ensure that the importer caches dont have stale info for `path`"""
    from zipimport import _zip_directory_cache as zdc
    _uncache(path, zdc)
    _uncache(path, sys.path_importer_cache)