Python sys.path_hooks() Examples

The following are code examples for showing how to use sys.path_hooks(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones 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 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 2
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 3
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 4
Project: web_ctp   Author: molebot   File: test_importhooks.py    (license) View Source Project 6 votes vote down vote up
def testImpWrapper(self):
        i = ImpWrapper()
        sys.meta_path.append(i)
        sys.path_hooks.insert(0, ImpWrapper)
        mnames = (
            "colorsys", "urllib.parse", "distutils.core", "sys",
        )
        for mname in mnames:
            parent = mname.split(".")[0]
            for n in list(sys.modules):
                if n.startswith(parent):
                    del sys.modules[n]
        for mname in mnames:
            m = __import__(mname, globals(), locals(), ["__dummy__"])
            # to make sure we actually handled the import
            self.assertTrue(hasattr(m, "__loader__")) 
Example 5
Project: web_ctp   Author: molebot   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,
                          _bootstrap.FileFinder.path_hook(
                              *_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 = import_module('email')
                self.assertIsInstance(module, ModuleType)
        finally:
            if email is not missing:
                sys.modules['email'] = email 
Example 6
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 7
Project: ouroboros   Author: pybee   File: test_path.py    (license) View Source Project 6 votes vote down vote up
def test_finder_with_failing_find_module(self):
        # PathEntryFinder with find_module() defined should work.
        # Issue #20763.
        class Finder:
            path_location = 'test_finder_with_find_module'
            def __init__(self, path):
                if path != self.path_location:
                    raise ImportError

            @staticmethod
            def find_module(fullname):
                return None


        with util.import_state(path=[Finder.path_location]+sys.path[:],
                               path_hooks=[Finder]):
            self.machinery.PathFinder.find_spec('importlib') 
Example 8
Project: Deploy_XXNET_Server   Author: jzp820927   File: sandbox_test.py    (license) View Source Project 6 votes vote down vote up
def test_load_with_path_hook(self):

    class DummyPathHook(object):

      def __init__(self, path):
        if path != 'dummy/path':
          raise ImportError

      def find_module(self, unused_fullname):
        return self

      def load_module(self, fullname):
        return imp.new_module('fake name: %s' % fullname)

    self.test_policies['distutils.util'] = sandbox.ModuleOverridePolicy(
        None, [], {}, default_pass_through=True)
    sys.path_hooks = [DummyPathHook]
    util = self.hook.load_module('distutils.util')
    self.assertEqual('fake name: distutils.util', util.__name__) 
Example 9
Project: Deploy_XXNET_Server   Author: jzp820927   File: sandbox_test.py    (license) View Source Project 6 votes vote down vote up
def test_load_with_path_hook_cant_find(self):

    class DummyPathHook(object):

      def __init__(self, path):
        if path != 'dummy/path':
          raise ImportError

      def find_module(self, unused_fullname):
        return None

      def load_module(self, fullname):
        raise ImportError

    self.test_policies['distutils.util'] = sandbox.ModuleOverridePolicy(
        None, [], {}, default_pass_through=True)
    sys.path_hooks = [DummyPathHook]
    util = self.hook.load_module('distutils.util')
    self.assertEqual('distutils.util', util.__name__) 
Example 10
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 11
Project: kbe_server   Author: xiaohaoppy   File: test_path.py    (license) View Source Project 6 votes vote down vote up
def test_finder_with_failing_find_module(self):
        # PathEntryFinder with find_module() defined should work.
        # Issue #20763.
        class Finder:
            path_location = 'test_finder_with_find_module'
            def __init__(self, path):
                if path != self.path_location:
                    raise ImportError

            @staticmethod
            def find_module(fullname):
                return None


        with util.import_state(path=[Finder.path_location]+sys.path[:],
                               path_hooks=[Finder]):
            self.machinery.PathFinder.find_spec('importlib') 
Example 12
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 13
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 14
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 15
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 16
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 17
Project: filefinder2   Author: asmodehn   File: _filefinder2.py    (license) View Source Project 5 votes vote down vote up
def _path_hooks(cls, path):  # from importlib.PathFinder
            """Search sys.path_hooks for a finder for 'path'."""
            if sys.path_hooks is not None and not sys.path_hooks:
                warnings.warn('sys.path_hooks is empty', ImportWarning)
            for hook in sys.path_hooks:
                try:
                    return hook(path)
                except ImportError:
                    continue
            else:
                return None 
Example 18
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 19
Project: filefinder2   Author: asmodehn   File: _filefinder2.py    (license) View Source Project 5 votes vote down vote up
def get_filefinder_index_in_path_hooks():
        return sys.path_hooks.index(path_hook)


    # Making the activation explicit for now 
Example 20
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 21
Project: filefinder2   Author: asmodehn   File: _filefinder2.py    (license) View Source Project 5 votes vote down vote up
def get_filefinder_index_in_path_hooks():
        # we look for the hook we detected there at import time (should usually be Filefinder
        # if there isnt too many import mess...
        return sys.path_hooks.index(ffhook) 
Example 22
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 23
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 24
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 25
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 26
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 27
Project: rosimport   Author: pyros-dev   File: _ros_directory_finder.py    (license) View Source Project 5 votes vote down vote up
def _install():
    """Install the path-based import components."""
    supported_loaders = get_supported_ros_loaders()
    sys.path_hooks.extend([ROSDirectoryFinder.path_hook(*supported_loaders)])
    # TODO : sys.meta_path.append(DistroFinder)


# Useless ?
# _ros_finder_instance_obsolete_python = ROSImportFinder 
Example 28
Project: rosimport   Author: pyros-dev   File: _ros_directory_finder.py    (license) View Source Project 5 votes vote down vote up
def activate(rosdistro_path=None, *workspaces):
    global ros_distro_finder
    if rosdistro_path is None:  # autodetect most recent installed distro
        if os.path.exists('/opt/ros/lunar'):
            rosdistro_path = '/opt/ros/lunar'
        elif os.path.exists('/opt/ros/kinetic'):
            rosdistro_path = '/opt/ros/kinetic'
        elif os.path.exists('/opt/ros/jade'):
            rosdistro_path = '/opt/ros/jade'
        elif os.path.exists('/opt/ros/indigo'):
            rosdistro_path = '/opt/ros/indigo'
        else:
            raise ImportError(
                "No ROS distro detected on this system. Please specify the path when calling ROSImportMetaFinder()")

    ros_distro_finder = ROSPathFinder(rosdistro_path, *workspaces)
    sys.meta_path.append(ros_distro_finder)

    #if sys.version_info >= (2, 7, 12):  # TODO : which exact version matters ?

    # We need to be before FileFinder to be able to find our (non .py[c]) files
    # inside, maybe already imported, python packages...
    sys.path_hooks.insert(1, ROSDirectoryFinder)

    # else:  # older (trusty) version
    #     sys.path_hooks.append(_ros_finder_instance_obsolete_python)

    for hook in sys.path_hooks:
        print('Path hook: {}'.format(hook))

    # TODO : mix that with ROS PYTHONPATH shenanigans... to enable the finder only for 'ROS aware' paths
    if paths:
        sys.path.append(*paths) 
Example 29
Project: rosimport   Author: pyros-dev   File: _ros_directory_finder.py    (license) View Source Project 5 votes vote down vote up
def deactivate(*paths):
    """ CAREFUL : even if we remove our path_hooks, the created finder are still cached in sys.path_importer_cache."""
    #if sys.version_info >= (2, 7, 12):  # TODO : which exact version matters ?
    sys.path_hooks.remove(ROSImportFinder)
    # else:  # older (trusty) version
    #     sys.path_hooks.remove(_ros_finder_instance_obsolete_python)
    if paths:
        sys.path.remove(*paths)

    sys.meta_path.remove(ros_distro_finder) 
Example 30
Project: rosimport   Author: pyros-dev   File: rosmsg_metafinder.py    (license) View Source Project 5 votes vote down vote up
def deactivate(*paths):
    """ CAREFUL : even if we remove our path_hooks, the created finder are still cached in sys.path_importer_cache."""
    #if sys.version_info >= (2, 7, 12):  # TODO : which exact version matters ?
    sys.path_hooks.remove(ROSImportFinder)
    # else:  # older (trusty) version
    #     sys.path_hooks.remove(_ros_finder_instance_obsolete_python)
    if paths:
        sys.path.remove(*paths)

    sys.meta_path.remove(ros_distro_finder) 
Example 31
Project: rosimport   Author: pyros-dev   File: __init__.py    (license) View Source Project 5 votes vote down vote up
def get_rosdirectoryfinder_index_in_path_hooks():
    return sys.path_hooks.index(ros_path_hook) 
Example 32
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 33
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 34
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 35
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 36
Project: zippy   Author: securesystemslab   File: regrtest.py    (license) View Source Project 5 votes vote down vote up
def get_sys_path_hooks(self):
        return id(sys.path_hooks), sys.path_hooks, sys.path_hooks[:] 
Example 37
Project: zippy   Author: securesystemslab   File: regrtest.py    (license) View Source Project 5 votes vote down vote up
def restore_sys_path_hooks(self, saved_hooks):
        sys.path_hooks = saved_hooks[1]
        sys.path_hooks[:] = saved_hooks[2] 
Example 38
Project: zippy   Author: securesystemslab   File: test_zipimport_support.py    (license) View Source Project 5 votes vote down vote up
def tearDown(self):
        sys.path[:] = self.path
        sys.meta_path[:] = self.meta_path
        sys.path_hooks[:] = self.path_hooks
        sys.path_importer_cache.clear() 
Example 39
Project: zippy   Author: securesystemslab   File: test_importhooks.py    (license) View Source Project 5 votes vote down vote up
def __init__(self, path=test_path):
        if path != test_path:
            # if out class is on sys.path_hooks, we must raise
            # ImportError for any path item that we can't handle.
            raise ImportError
        self.path = path 
Example 40
Project: zippy   Author: securesystemslab   File: test_importhooks.py    (license) View Source Project 5 votes vote down vote up
def setUp(self):
        self.path = sys.path[:]
        self.meta_path = sys.meta_path[:]
        self.path_hooks = sys.path_hooks[:]
        sys.path_importer_cache.clear()
        self.modules_before = support.modules_setup() 
Example 41
Project: zippy   Author: securesystemslab   File: test_importhooks.py    (license) View Source Project 5 votes vote down vote up
def tearDown(self):
        sys.path[:] = self.path
        sys.meta_path[:] = self.meta_path
        sys.path_hooks[:] = self.path_hooks
        sys.path_importer_cache.clear()
        support.modules_cleanup(*self.modules_before) 
Example 42
Project: zippy   Author: securesystemslab   File: test_importhooks.py    (license) View Source Project 5 votes vote down vote up
def testPathHook(self):
        sys.path_hooks.append(PathImporter)
        sys.path.append(test_path)
        self.doTestImports() 
Example 43
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 44
Project: oil   Author: oilshell   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 45
Project: oil   Author: oilshell   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: oil   Author: oilshell   File: test_importhooks.py    (license) View Source Project 5 votes vote down vote up
def __init__(self, path=test_path):
        if path != test_path:
            # if out class is on sys.path_hooks, we must raise
            # ImportError for any path item that we can't handle.
            raise ImportError
        self.path = path 
Example 47
Project: oil   Author: oilshell   File: test_importhooks.py    (license) View Source Project 5 votes vote down vote up
def setUp(self):
        self.path = sys.path[:]
        self.meta_path = sys.meta_path[:]
        self.path_hooks = sys.path_hooks[:]
        sys.path_importer_cache.clear()
        self.modules_before = sys.modules.copy() 
Example 48
Project: oil   Author: oilshell   File: test_importhooks.py    (license) View Source Project 5 votes vote down vote up
def tearDown(self):
        sys.path[:] = self.path
        sys.meta_path[:] = self.meta_path
        sys.path_hooks[:] = self.path_hooks
        sys.path_importer_cache.clear()
        sys.modules.clear()
        sys.modules.update(self.modules_before) 
Example 49
Project: oil   Author: oilshell   File: test_importhooks.py    (license) View Source Project 5 votes vote down vote up
def testImpWrapper(self):
        i = ImpWrapper()
        sys.meta_path.append(i)
        sys.path_hooks.append(ImpWrapper)
        mnames = ("colorsys", "urlparse", "distutils.core", "compiler.misc")
        for mname in mnames:
            parent = mname.split(".")[0]
            for n in sys.modules.keys():
                if n.startswith(parent):
                    del sys.modules[n]
        with test_support.check_warnings(("The compiler package is deprecated "
                                          "and removed", DeprecationWarning)):
            for mname in mnames:
                m = __import__(mname, globals(), locals(), ["__dummy__"])
                m.__loader__  # to make sure we actually handled the import 
Example 50
Project: python2-tracer   Author: extremecoders-re   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