Python sys.path_importer_cache() Examples

The following are code examples for showing how to use sys.path_importer_cache(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: NiujiaoDebugger   Author: MrSrc   File: pkgutil.py    GNU General Public License v3.0 6 votes vote down vote up
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 2
Project: NiujiaoDebugger   Author: MrSrc   File: test_threaded_import.py    GNU General Public License v3.0 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 3
Project: NiujiaoDebugger   Author: MrSrc   File: util.py    GNU General Public License v3.0 6 votes vote down vote up
def tearDownClass(cls):
        try:
            sys.path.remove(cls._zip_path)
        except ValueError:
            pass

        try:
            del sys.path_importer_cache[cls._zip_path]
            del sys.modules[cls.data.__name__]
        except KeyError:
            pass

        try:
            del cls.data
            del cls._zip_path
        except AttributeError:
            pass 
Example 4
Project: NiujiaoDebugger   Author: MrSrc   File: test_path.py    GNU General Public License v3.0 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_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 5
Project: python-netsurv   Author: sofia-netsurv   File: spec.py    MIT License 6 votes vote down vote up
def _precache_zipimporters(path=None):
    pic = sys.path_importer_cache

    # When measured, despite having the same complexity (O(n)),
    # converting to tuples and then caching the conversion to sets
    # and the set difference is faster than converting to sets
    # and then only caching the set difference.

    req_paths = tuple(path or sys.path)
    cached_paths = tuple(pic)
    new_paths = _cached_set_diff(req_paths, cached_paths)
    for entry_path in new_paths:
        try:
            pic[entry_path] = zipimport.zipimporter(entry_path)
        except zipimport.ZipImportError:
            continue
    return pic 
Example 6
Project: python-netsurv   Author: sofia-netsurv   File: spec.py    MIT License 6 votes vote down vote up
def _precache_zipimporters(path=None):
    pic = sys.path_importer_cache

    # When measured, despite having the same complexity (O(n)),
    # converting to tuples and then caching the conversion to sets
    # and the set difference is faster than converting to sets
    # and then only caching the set difference.

    req_paths = tuple(path or sys.path)
    cached_paths = tuple(pic)
    new_paths = _cached_set_diff(req_paths, cached_paths)
    for entry_path in new_paths:
        try:
            pic[entry_path] = zipimport.zipimporter(entry_path)
        except zipimport.ZipImportError:
            continue
    return pic 
Example 7
Project: SandwichApp   Author: lovexiaov   File: util.py    Apache License 2.0 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 8
Project: ironpython2   Author: IronLanguages   File: test_imp.py    Apache License 2.0 6 votes vote down vote up
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 9
Project: Blockly-rduino-communication   Author: technologiescollege   File: pkgutil.py    GNU General Public License v3.0 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 10
Project: Blockly-rduino-communication   Author: technologiescollege   File: test_threaded_import.py    GNU General Public License v3.0 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 11
Project: PennApps2015-Heartmates   Author: natanlailari   File: easy_install.py    Apache License 2.0 6 votes vote down vote up
def uncache_zipdir(path):
    """
    Remove any globally cached zip file related data for `path`

    Stale zipimport.zipimporter objects need to be removed when a zip file is
    replaced as they contain cached zip file directory information. If they are
    asked to get data from their zip file, they will use that cached
    information to calculate the data location in the zip file. This calculated
    location may be incorrect for the replaced zip file, which may in turn
    cause the read operation to either fail or return incorrect data.

    Note we have no way to clear any local caches from here. That is left up to
    whomever is in charge of maintaining that cache.

    """
    normalized_path = normalize_path(path)
    _uncache(normalized_path, zipimport._zip_directory_cache)
    _uncache(normalized_path, sys.path_importer_cache) 
Example 12
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: pkgutil.py    GNU General Public License v2.0 6 votes vote down vote up
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 13
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_threaded_import.py    GNU General Public License v2.0 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 14
Project: aws-lambda-runtime-pypy   Author: uscheller   File: pkgutil.py    Apache License 2.0 6 votes vote down vote up
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 15
Project: setup   Author: mindbender-studio   File: pkgutil.py    MIT License 6 votes vote down vote up
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 16
Project: conf   Author: XonqNopp   File: unittest_manager.py    GNU General Public License v3.0 6 votes vote down vote up
def _test_ast_from_zip(self, archive):
        origpath = sys.path[:]
        sys.modules.pop('mypypa', None)
        archive_path = resources.find(archive)
        sys.path.insert(0, archive_path)
        try:
            module = self.manager.ast_from_module_name('mypypa')
            self.assertEqual(module.name, 'mypypa')
            end = os.path.join(archive, 'mypypa')
            self.assertTrue(module.file.endswith(end),
                            "%s doesn't endswith %s" % (module.file, end))
        finally:
            # remove the module, else after importing egg, we don't get the zip
            if 'mypypa' in self.manager.astroid_cache:
                del self.manager.astroid_cache['mypypa']
                del self.manager._mod_file_cache[('mypypa', None)]
            if archive_path in sys.path_importer_cache:
                del sys.path_importer_cache[archive_path]
            sys.path = origpath 
Example 17
Project: conf   Author: XonqNopp   File: spec.py    GNU General Public License v3.0 6 votes vote down vote up
def _precache_zipimporters(path=None):
    pic = sys.path_importer_cache

    # When measured, despite having the same complexity (O(n)),
    # converting to tuples and then caching the conversion to sets
    # and the set difference is faster than converting to sets
    # and then only caching the set difference.

    req_paths = tuple(path or sys.path)
    cached_paths = tuple(pic)
    new_paths = _cached_set_diff(req_paths, cached_paths)
    for entry_path in new_paths:
        try:
            pic[entry_path] = zipimport.zipimporter(entry_path)
        except zipimport.ZipImportError:
            continue
    return pic 
Example 18
Project: conf   Author: XonqNopp   File: spec.py    GNU General Public License v3.0 6 votes vote down vote up
def _precache_zipimporters(path=None):
    pic = sys.path_importer_cache

    # When measured, despite having the same complexity (O(n)),
    # converting to tuples and then caching the conversion to sets
    # and the set difference is faster than converting to sets
    # and then only caching the set difference.

    req_paths = tuple(path or sys.path)
    cached_paths = tuple(pic)
    new_paths = _cached_set_diff(req_paths, cached_paths)
    for entry_path in new_paths:
        try:
            pic[entry_path] = zipimport.zipimporter(entry_path)
        except zipimport.ZipImportError:
            continue
    return pic 
Example 19
Project: eden   Author: facebookexperimental   File: demandimportpy3.py    GNU General Public License v2.0 6 votes vote down vote up
def deactivated():
    # This implementation is a bit different from Python 2's. Python 3
    # maintains a per-package finder cache in sys.path_importer_cache (see
    # PEP 302). This means that we can't just call disable + enable.
    # If we do that, in situations like:
    #
    #   demandimport.enable()
    #   ...
    #   from foo.bar import mod1
    #   with demandimport.deactivated():
    #       from foo.bar import mod2
    #
    # mod2 will be imported lazily. (The converse also holds -- whatever finder
    # first gets cached will be used.)
    #
    # Instead, have a global flag the LazyLoader can use.
    global _deactivated
    demandenabled = isenabled()
    if demandenabled:
        _deactivated = True
    try:
        yield
    finally:
        if demandenabled:
            _deactivated = False 
Example 20
Project: pyblish-win   Author: pyblish   File: pkgutil.py    GNU Lesser General Public License v3.0 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: pyblish-win   Author: pyblish   File: runpy.py    GNU Lesser General Public License v3.0 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 22
Project: flasky   Author: RoseOu   File: easy_install.py    MIT License 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 23
Project: vscode-mayapy   Author: FXTD-ODYSSEY   File: _pydev_pkgutil_old.py    MIT License 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 24
Project: Flask_Blog   Author: sugarguo   File: easy_install.py    GNU General Public License v3.0 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 25
Project: NiujiaoDebugger   Author: MrSrc   File: test_threaded_import.py    GNU General Public License v3.0 5 votes vote down vote up
def find_spec(self, name, path=None, target=None):
        sys.path_importer_cache.clear() 
Example 26
Project: NiujiaoDebugger   Author: MrSrc   File: util.py    GNU General Public License v3.0 5 votes vote down vote up
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 27
Project: NiujiaoDebugger   Author: MrSrc   File: test_api.py    GNU General Public License v3.0 5 votes vote down vote up
def test_method_lacking(self):
        # There should be no issues if the method is not defined.
        key = 'gobbledeegook'
        sys.path_importer_cache[key] = None
        self.addCleanup(lambda: sys.path_importer_cache.pop(key, None))
        self.init.invalidate_caches()  # Shouldn't trigger an exception. 
Example 28
Project: NiujiaoDebugger   Author: MrSrc   File: test_path.py    GNU General Public License v3.0 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_spec(module)
        with util.import_state(path_importer_cache={path: importer},
                               path=[path]):
            found = self.find(module)
            self.check_found(found, importer) 
Example 29
Project: NiujiaoDebugger   Author: MrSrc   File: test_path.py    GNU General Public License v3.0 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_spec(module)
        with util.import_state(path_importer_cache={path: importer}):
            found = self.find(module, [path])
            self.check_found(found, importer) 
Example 30
Project: NiujiaoDebugger   Author: MrSrc   File: test_path.py    GNU General Public License v3.0 5 votes vote down vote up
def test_empty_list(self):
        # An empty list should not count as asking for sys.path.
        module = 'module'
        path = '<test path>'
        importer = util.mock_spec(module)
        with util.import_state(path_importer_cache={path: importer},
                               path=[path]):
            self.assertIsNone(self.find('module', [])) 
Example 31
Project: NiujiaoDebugger   Author: MrSrc   File: test_path.py    GNU General Public License v3.0 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_spec(module)
        hook = util.mock_path_hook(path, importer=importer)
        with util.import_state(path_hooks=[hook]):
            found = self.find(module, [path])
            self.check_found(found, importer)
            self.assertIn(path, sys.path_importer_cache)
            self.assertIs(sys.path_importer_cache[path], importer) 
Example 32
Project: NiujiaoDebugger   Author: MrSrc   File: test_path.py    GNU General Public License v3.0 5 votes vote down vote up
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]):
            found = self.find(module)
            self.check_found(found, importer)
            self.assertIn(os.getcwd(), sys.path_importer_cache) 
Example 33
Project: NiujiaoDebugger   Author: MrSrc   File: test_path.py    GNU General Public License v3.0 5 votes vote down vote up
def test_finder_with_find_module(self):
        class TestFinder:
            def find_module(self, fullname):
                return self.to_return
        failing_finder = TestFinder()
        failing_finder.to_return = None
        path = 'testing path'
        with util.import_state(path_importer_cache={path: failing_finder}):
            self.assertIsNone(
                    self.machinery.PathFinder.find_spec('whatever', [path]))
        success_finder = TestFinder()
        success_finder.to_return = __loader__
        with util.import_state(path_importer_cache={path: success_finder}):
            spec = self.machinery.PathFinder.find_spec('whatever', [path])
        self.assertEqual(spec.loader, __loader__) 
Example 34
Project: NiujiaoDebugger   Author: MrSrc   File: test_path.py    GNU General Public License v3.0 5 votes vote down vote up
def test_finder_with_find_loader(self):
        class TestFinder:
            loader = None
            portions = []
            def find_loader(self, fullname):
                return self.loader, self.portions
        path = 'testing path'
        with util.import_state(path_importer_cache={path: TestFinder()}):
            self.assertIsNone(
                    self.machinery.PathFinder.find_spec('whatever', [path]))
        success_finder = TestFinder()
        success_finder.loader = __loader__
        with util.import_state(path_importer_cache={path: success_finder}):
            spec = self.machinery.PathFinder.find_spec('whatever', [path])
        self.assertEqual(spec.loader, __loader__) 
Example 35
Project: NiujiaoDebugger   Author: MrSrc   File: test_path.py    GNU General Public License v3.0 5 votes vote down vote up
def test_finder_with_find_spec(self):
        class TestFinder:
            spec = None
            def find_spec(self, fullname, target=None):
                return self.spec
        path = 'testing path'
        with util.import_state(path_importer_cache={path: TestFinder()}):
            self.assertIsNone(
                    self.machinery.PathFinder.find_spec('whatever', [path]))
        success_finder = TestFinder()
        success_finder.spec = self.machinery.ModuleSpec('whatever', __loader__)
        with util.import_state(path_importer_cache={path: success_finder}):
            got = self.machinery.PathFinder.find_spec('whatever', [path])
        self.assertEqual(got, success_finder.spec) 
Example 36
Project: NiujiaoDebugger   Author: MrSrc   File: test_path.py    GNU General Public License v3.0 5 votes vote down vote up
def test_invalidate_caches_clear_out_None(self):
        # Clear out None in sys.path_importer_cache() when invalidating caches.
        cache = {'clear_out': None}
        with util.import_state(path_importer_cache=cache):
            self.machinery.PathFinder.invalidate_caches()
        self.assertEqual(len(cache), 0) 
Example 37
Project: platzi-hello-gae   Author: xertica-cloud   File: pkg_resources.py    GNU General Public License v2.0 5 votes vote down vote up
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 38
Project: figura   Author: shx2   File: _python3.py    MIT License 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 39
Project: figura   Author: shx2   File: importutils.py    MIT License 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 40
Project: figura   Author: shx2   File: importutils.py    MIT License 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 41
Project: neo4j-social-network   Author: bestvibes   File: _pkgutil.py    MIT License 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 42
Project: neo4j-social-network   Author: bestvibes   File: easy_install.py    MIT License 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 43
Project: neo4j-social-network   Author: bestvibes   File: pkg_resources.py    MIT License 5 votes vote down vote up
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 44
Project: vagrant-prometheus-lab   Author: alexclear   File: pkgutil.py    MIT License 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 45
Project: centreon-discovery   Author: Centreon-Community   File: easy_install.py    GNU General Public License v2.0 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 46
Project: centreon-discovery   Author: Centreon-Community   File: pkg_resources.py    GNU General Public License v2.0 5 votes vote down vote up
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 47
Project: ironpython2   Author: IronLanguages   File: pkgutil.py    Apache License 2.0 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 48
Project: ironpython2   Author: IronLanguages   File: runpy.py    Apache License 2.0 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 49
Project: ironpython2   Author: IronLanguages   File: test_imp.py    Apache License 2.0 5 votes vote down vote up
def test_NullImporter(self):
        def f():
            class x(imp.NullImporter): pass
            
        self.assertRaises(TypeError, f)
        
        self.assertEqual(imp.NullImporter.__module__, 'imp')
        
        sys.path.append('directory_that_does_not_exist')
        try:
            import SomeFileThatDoesNotExist
        except ImportError:
            pass
            
        self.assertTrue(isinstance(sys.path_importer_cache['directory_that_does_not_exist'], imp.NullImporter)) 
Example 50
Project: pdbclone   Author: corpusops   File: bdb.py    GNU General Public License v2.0 5 votes vote down vote up
def close(self):
        self.hooked = False
        if self in sys.path_hooks:
            sys.path_hooks.remove(self)
        if self.PATH_ENTRY in sys.path:
            sys.path.remove(self.PATH_ENTRY)
        if self.PATH_ENTRY in sys.path_importer_cache:
            del sys.path_importer_cache[self.PATH_ENTRY] 
Example 51
Project: open-recipe   Author: dspray95   File: modules.py    The Unlicense 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
        if rval not in self.importerCache:
            warnings.warn(
                "%s (for module %s) not in path importer cache "
                "(PEP 302 violation - check your local configuration)." % (
                    rval, modobj.__name__),
                stacklevel=3)

        return rval 
Example 52
Project: Blockly-rduino-communication   Author: technologiescollege   File: test_threaded_import.py    GNU General Public License v3.0 5 votes vote down vote up
def find_spec(self, name, path=None, target=None):
        sys.path_importer_cache.clear() 
Example 53
Project: IronHydra   Author: microdee   File: pkgutil.py    MIT License 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 54
Project: IronHydra   Author: microdee   File: runpy.py    MIT License 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 55
Project: polyloader   Author: elfsternberg   File: _python3.py    MIT License 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 56
Project: polyloader   Author: elfsternberg   File: test_polyloader.py    MIT License 5 votes vote down vote up
def __enter__(self):
        polyloader.reset()
        self.path = copy.copy(sys.path)
        self.path_hooks = copy.copy(sys.path_hooks)
        self.meta_path = copy.copy(sys.meta_path)
        self.modules = copy.copy(sys.modules)
        self.path_importer_cache = copy.copy(sys.path_importer_cache)
        return sys 
Example 57
Project: polyloader   Author: elfsternberg   File: test_polyloader.py    MIT License 5 votes vote down vote up
def __exit__(self, type, value, traceback):
        sys.path = self.path
        sys.path_hooks = self.path_hooks
        sys.meta_path = self.meta_path
        sys.modules = self.modules
        sys.path_importer_cache = self.path_importer_cache 
Example 58
Project: polyloader   Author: elfsternberg   File: test_polyloader.py    MIT License 5 votes vote down vote up
def __enter__(self):
        polyloader.reset()
        self.path = copy.copy(sys.path)
        self.path_hooks = copy.copy(sys.path_hooks)
        self.meta_path = copy.copy(sys.meta_path)
        self.modules = copy.copy(sys.modules)
        self.path_importer_cache = copy.copy(sys.path_importer_cache)
        return sys 
Example 59
Project: polyloader   Author: elfsternberg   File: test_polyloader.py    MIT License 5 votes vote down vote up
def __exit__(self, type, value, traceback):
        sys.path = self.path
        sys.path_hooks = self.path_hooks
        sys.meta_path = self.meta_path
        sys.modules = self.modules
        sys.path_importer_cache = self.path_importer_cache 
Example 60
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_threaded_import.py    GNU General Public License v2.0 5 votes vote down vote up
def find_spec(self, name, path=None, target=None):
        sys.path_importer_cache.clear() 
Example 61
Project: xuemc   Author: skycucumber   File: easy_install.py    GNU General Public License v2.0 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 62
Project: Computable   Author: ktraunmueller   File: pkgutil.py    MIT License 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 63
Project: chihu   Author: yelongyu   File: easy_install.py    GNU General Public License v3.0 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 64
Project: oss-ftp   Author: aliyun   File: pkgutil.py    MIT License 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 65
Project: oss-ftp   Author: aliyun   File: runpy.py    MIT License 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 66
Project: Safejumper-for-Desktop   Author: proxysh   File: modules.py    GNU General Public License v2.0 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
        if rval not in self.importerCache:
            warnings.warn(
                "%s (for module %s) not in path importer cache "
                "(PEP 302 violation - check your local configuration)." % (
                    rval, modobj.__name__),
                stacklevel=3)

        return rval 
Example 67
Project: construct   Author: construct-org   File: utils.py    MIT License 5 votes vote down vote up
def isolated_imports(path=None, restore=True):
    '''Contextmanager that isolates all import'''

    old_path_cache = {k: v for k, v in sys.path_importer_cache.items()}
    old_modules = {k: v for k, v in sys.modules.items()}
    old_path = sys.path[:]

    try:

        if path:
            sys.path.insert(0, path)
        yield

    finally:

        sys.path[:] = old_path

        if restore:
            for k, v in list(sys.modules.items()):
                if k in old_modules:
                    sys.modules[k] = old_modules[k]
                else:
                    del(sys.modules[k])

            for k, v in list(sys.path_importer_cache.items()):
                if k in old_path_cache:
                    sys.path_importer_cache[k] = old_path_cache[k]
                else:
                    del(sys.path_importer_cache[k]) 
Example 68
Project: OpenNMT-tf   Author: OpenNMT   File: config.py    MIT License 5 votes vote down vote up
def load_model_from_file(path):
  """Loads a model from a configuration file.

  Args:
    path: The relative path to the configuration file.

  Returns:
    A :class:`opennmt.models.Model` instance.
  """
  module = load_model_module(path)
  model = module.model()
  del sys.path_importer_cache[os.path.dirname(module.__file__)]
  del sys.modules[module.__name__]
  return model 
Example 69
Project: conf   Author: XonqNopp   File: resources.py    GNU General Public License v3.0 5 votes vote down vote up
def tearDown(self):
        del sys.path[0]
        datadir = find('')
        for key in list(sys.path_importer_cache):
            if key.startswith(datadir):
                del sys.path_importer_cache[key] 
Example 70
Project: conf   Author: XonqNopp   File: unittest_modutils.py    GNU General Public License v3.0 5 votes vote down vote up
def tearDown(self):
        for k in list(sys.path_importer_cache):
            if 'MyPyPa' in k:
                del sys.path_importer_cache[k] 
Example 71
Project: conf   Author: XonqNopp   File: unittest_modutils.py    GNU General Public License v3.0 5 votes vote down vote up
def setUp(self):
        super(ModuleFileTC, self).setUp()
        for k in list(sys.path_importer_cache.keys()):
            if 'MyPyPa' in k:
                del sys.path_importer_cache[k] 
Example 72
Project: conf   Author: XonqNopp   File: resources.py    GNU General Public License v3.0 5 votes vote down vote up
def tearDown(self):
        del sys.path[0]
        datadir = find('')
        for key in list(sys.path_importer_cache):
            if key.startswith(datadir):
                del sys.path_importer_cache[key] 
Example 73
Project: conf   Author: XonqNopp   File: unittest_modutils.py    GNU General Public License v3.0 5 votes vote down vote up
def tearDown(self):
        for k in list(sys.path_importer_cache):
            if 'MyPyPa' in k:
                del sys.path_importer_cache[k] 
Example 74
Project: pmatic   Author: LarsMichelsen   File: pkgutil.py    GNU General Public License v2.0 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 75
Project: godot-zeronet-plugin   Author: zam-org   File: pkgutil.py    GNU General Public License v2.0 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 76
Project: godot-zeronet-plugin   Author: zam-org   File: runpy.py    GNU General Public License v2.0 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 77
Project: mimic   Author: googlearchive   File: target_env.py    Apache License 2.0 4 votes vote down vote up
def _TearDown(self):
    """Remove the TargetEnvironment (this call is idempotent)."""
    if not self._active:
      return
    assert TargetEnvironment._instance is self
    # Eraddicate user source, which is cached during traceback formatting.
    linecache.clearcache()
    self._CleanupModules()
    # clean up sys.path, which must be modified in place (not replaced)
    while len(sys.path):
      sys.path.pop()
    sys.path.extend(self._saved_sys_path)
    # clean up sys.path_importer_cache
    for p in sys.path_importer_cache.keys():  # pylint: disable-msg=C6401
      if p == _TARGET_ROOT or p.startswith(_TARGET_PREFIX):
        del sys.path_importer_cache[p]
    sys.path_hooks.remove(self._PathHook)
    for p in self._patches:
      p.Remove()
    self._active = False
    TargetEnvironment._instance = None

    # prevent memory leaks due to cyclic references
    self._patches = None

    # tmp = '\n\n'
    # tmp += '\n\nGARBAGE:\n'
    # gc.collect()

    # tmp += '\n\nGARBAGE OBJECTS:\n'
    # for x in gc.garbage:
    #     s = str(x)
    #     if len(s) > 80: s = s[:80]
    #     tmp += '{}\n  {}'.format(type(x), s)
    #     for p in dir(x):
    #       tmp += '  {} = {}\n\n'.format(str(p), str(getattr(x, p)))

    # tmp += '\n\n'
    # logging.warn(tmp)

  # _CleanupModules must deal with a subtle problem related to the
  # encodings module.  The decode() function relies on a registry in
  # the codecs module.  Individual encoders are implemented as
  # submodules of the encodings package.  When a new encoder is needed
  # the appropriate module is loaded under encodings and then bits of
  # that module are registered within codecs.  If that submodule is
  # cleaned up according to the normal process its refcount will hit 0
  # even though references to code within the submodule still exist in
  # the codecs module.  When a module's refcount hits 0 all entries in
  # its global dict are set to None.  This means that future
  # invocations of decode() will use stale references into code for a
  # module that is no longer valid, and exceptions similar to this
  # will be raised:
  #
  # AttributeError: 'NoneType' object has no attribute 'codecs'
  #
  # Since the encodings package has no reliance on user code, it is safe
  # to allow these to persist across target environments.  Just to be safe,
  # this approach has been extended to cover all modules coming from the
  # standard python runtime, as determined by _PYTHON_LIB_PREFIX 
Example 78
Project: open-recipe   Author: dspray95   File: modules.py    The Unlicense 4 votes vote down vote up
def __init__(self,
                 sysPath=None,
                 moduleDict=sys.modules,
                 sysPathHooks=sys.path_hooks,
                 importerCache=sys.path_importer_cache,
                 moduleLoader=namedAny,
                 sysPathFactory=None):
        """
        Create a PythonPath.  You almost certainly want to use
        modules.theSystemPath, or its aliased methods, rather than creating a
        new instance yourself, though.

        All parameters are optional, and if unspecified, will use 'system'
        equivalents that makes this PythonPath like the global L{theSystemPath}
        instance.

        @param sysPath: a sys.path-like list to use for this PythonPath, to
        specify where to load modules from.

        @param moduleDict: a sys.modules-like dictionary to use for keeping
        track of what modules this PythonPath has loaded.

        @param sysPathHooks: sys.path_hooks-like list of PEP-302 path hooks to
        be used for this PythonPath, to determie which importers should be
        used.

        @param importerCache: a sys.path_importer_cache-like list of PEP-302
        importers.  This will be used in conjunction with the given
        sysPathHooks.

        @param moduleLoader: a module loader function which takes a string and
        returns a module.  That is to say, it is like L{namedAny} - *not* like
        L{__import__}.

        @param sysPathFactory: a 0-argument callable which returns the current
        value of a sys.path-like list of strings.  Specify either this, or
        sysPath, not both.  This alternative interface is provided because the
        way the Python import mechanism works, you can re-bind the 'sys.path'
        name and that is what is used for current imports, so it must be a
        factory rather than a value to deal with modification by rebinding
        rather than modification by mutation.  Note: it is not recommended to
        rebind sys.path.  Although this mechanism can deal with that, it is a
        subtle point which some tools that it is easy for tools which interact
        with sys.path to miss.
        """
        if sysPath is not None:
            sysPathFactory = lambda : sysPath
        elif sysPathFactory is None:
            sysPathFactory = _defaultSysPathFactory
        self._sysPathFactory = sysPathFactory
        self._sysPath = sysPath
        self.moduleDict = moduleDict
        self.sysPathHooks = sysPathHooks
        self.importerCache = importerCache
        self.moduleLoader = moduleLoader 
Example 79
Project: openode   Author: JvGinkel   File: django_patches.py    GNU Affero General Public License v3.0 4 votes vote down vote up
def module_has_submodule(package, module_name):
    """See if 'module' is in 'package'."""
    name = ".".join([package.__name__, module_name])
    if name in sys.modules:
        return True
    for finder in sys.meta_path:
        if finder.find_module(name):
            return True
    for entry in package.__path__:  # No __path__, then not a package.
        try:
            # Try the cached finder.
            finder = sys.path_importer_cache[entry]
            if finder is None:
                # Implicit import machinery should be used.
                try:
                    file_, _, _ = imp.find_module(module_name, [entry])
                    if file_:
                        file_.close()
                    return True
                except ImportError:
                    continue
            # Else see if the finder knows of a loader.
            elif finder.find_module(name):
                return True
            else:
                continue
        except KeyError:
            # No cached finder, so try and make one.
            for hook in sys.path_hooks:
                try:
                    finder = hook(entry)
                    # XXX Could cache in sys.path_importer_cache
                    if finder.find_module(name):
                        return True
                    else:
                        # Once a finder is found, stop the search.
                        break
                except ImportError:
                    # Continue the search for a finder.
                    continue
            else:
                # No finder found.
                # Try the implicit import machinery if searching a directory.
                if os.path.isdir(entry):
                    try:
                        file_, _, _ = imp.find_module(module_name, [entry])
                        if file_:
                            file_.close()
                        return True
                    except ImportError:
                        pass
                # XXX Could insert None or NullImporter
    else:
        # Exhausted the search, so the module cannot be found.
        return False 
Example 80
Project: Safejumper-for-Desktop   Author: proxysh   File: modules.py    GNU General Public License v2.0 4 votes vote down vote up
def __init__(self,
                 sysPath=None,
                 moduleDict=sys.modules,
                 sysPathHooks=sys.path_hooks,
                 importerCache=sys.path_importer_cache,
                 moduleLoader=namedAny,
                 sysPathFactory=None):
        """
        Create a PythonPath.  You almost certainly want to use
        modules.theSystemPath, or its aliased methods, rather than creating a
        new instance yourself, though.

        All parameters are optional, and if unspecified, will use 'system'
        equivalents that makes this PythonPath like the global L{theSystemPath}
        instance.

        @param sysPath: a sys.path-like list to use for this PythonPath, to
        specify where to load modules from.

        @param moduleDict: a sys.modules-like dictionary to use for keeping
        track of what modules this PythonPath has loaded.

        @param sysPathHooks: sys.path_hooks-like list of PEP-302 path hooks to
        be used for this PythonPath, to determie which importers should be
        used.

        @param importerCache: a sys.path_importer_cache-like list of PEP-302
        importers.  This will be used in conjunction with the given
        sysPathHooks.

        @param moduleLoader: a module loader function which takes a string and
        returns a module.  That is to say, it is like L{namedAny} - *not* like
        L{__import__}.

        @param sysPathFactory: a 0-argument callable which returns the current
        value of a sys.path-like list of strings.  Specify either this, or
        sysPath, not both.  This alternative interface is provided because the
        way the Python import mechanism works, you can re-bind the 'sys.path'
        name and that is what is used for current imports, so it must be a
        factory rather than a value to deal with modification by rebinding
        rather than modification by mutation.  Note: it is not recommended to
        rebind sys.path.  Although this mechanism can deal with that, it is a
        subtle point which some tools that it is easy for tools which interact
        with sys.path to miss.
        """
        if sysPath is not None:
            sysPathFactory = lambda : sysPath
        elif sysPathFactory is None:
            sysPathFactory = _defaultSysPathFactory
        self._sysPathFactory = sysPathFactory
        self._sysPath = sysPath
        self.moduleDict = moduleDict
        self.sysPathHooks = sysPathHooks
        self.importerCache = importerCache
        self.moduleLoader = moduleLoader