當前位置: 首頁>>代碼示例>>Python>>正文


Python sys.path_hooks方法代碼示例

本文整理匯總了Python中sys.path_hooks方法的典型用法代碼示例。如果您正苦於以下問題:Python sys.path_hooks方法的具體用法?Python sys.path_hooks怎麽用?Python sys.path_hooks使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在sys的用法示例。


在下文中一共展示了sys.path_hooks方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_load_with_path_hook

# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def test_load_with_path_hook(self):

    class DummyPathHook(object):

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

      def find_module(self, unused_fullname):
        return self

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

    self.test_policies['distutils.util'] = sandbox.ModuleOverridePolicy(
        None, [], {}, default_pass_through=True)
    sys.path_hooks = [DummyPathHook]
    util = self.hook.load_module('distutils.util')
    self.assertEqual('fake name: distutils.util', util.__name__) 
開發者ID:elsigh,項目名稱:browserscope,代碼行數:21,代碼來源:sandbox_test.py

示例2: test_load_with_path_hook_cant_find

# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def test_load_with_path_hook_cant_find(self):

    class DummyPathHook(object):

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

      def find_module(self, unused_fullname):
        return None

      def load_module(self, fullname):
        raise ImportError

    self.test_policies['distutils.util'] = sandbox.ModuleOverridePolicy(
        None, [], {}, default_pass_through=True)
    sys.path_hooks = [DummyPathHook]
    util = self.hook.load_module('distutils.util')
    self.assertEqual('distutils.util', util.__name__) 
開發者ID:elsigh,項目名稱:browserscope,代碼行數:21,代碼來源:sandbox_test.py

示例3: __init__

# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def __init__(self, modules):
    """Initializer.

    Args:
      modules: Dictionary containing monitored modules.
    """
    self._modules = modules

    self._default_modules = self._modules.copy()

    self._save_path_hooks = sys.path_hooks[:]








    self._modification_times = {}


    self._dirty = True 
開發者ID:elsigh,項目名稱:browserscope,代碼行數:25,代碼來源:dev_appserver.py

示例4: test_path_hooks

# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def test_path_hooks(self):
        import toimport
        def prepare(f):
            sys.path_importer_cache = {}
            sys.path_hooks = [f]
            if 'toimport' in sys.modules: del sys.modules['toimport']
        
        def hook(*args):  raise Exception('hello')
        prepare(hook)
        def f(): import toimport
        self.assertRaisesMessage(Exception, 'hello', f)

        # ImportError shouldn't propagate out
        def hook(*args):  raise ImportError('foo')
        prepare(hook)
        f()

        # returning none should be ok
        def hook(*args): pass
        prepare(hook)
        f()
        
        sys.path_hooks = [] 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:25,代碼來源:test_imp.py

示例5: get_importer

# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def get_importer(path_item):
    """Retrieve a PEP 302 importer for the given path item

    The returned importer is cached in sys.path_importer_cache
    if it was newly created by a path hook.

    The cache (or part of it) can be cleared manually if a
    rescan of sys.path_hooks is necessary.
    """
    try:
        importer = sys.path_importer_cache[path_item]
    except KeyError:
        for path_hook in sys.path_hooks:
            try:
                importer = path_hook(path_item)
                sys.path_importer_cache.setdefault(path_item, importer)
                break
            except ImportError:
                pass
        else:
            importer = None
    return importer 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:24,代碼來源:pkgutil.py

示例6: test_None_on_sys_path

# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def test_None_on_sys_path(self):
        # Putting None in sys.path[0] caused an import regression from Python
        # 3.2: http://bugs.python.org/issue16514
        new_path = sys.path[:]
        new_path.insert(0, None)
        new_path_importer_cache = sys.path_importer_cache.copy()
        new_path_importer_cache.pop(None, None)
        new_path_hooks = [zipimport.zipimporter,
                          self.machinery.FileFinder.path_hook(
                              *self.importlib._bootstrap_external._get_supported_file_loaders())]
        missing = object()
        email = sys.modules.pop('email', missing)
        try:
            with util.import_state(meta_path=sys.meta_path[:],
                                   path=new_path,
                                   path_importer_cache=new_path_importer_cache,
                                   path_hooks=new_path_hooks):
                module = self.importlib.import_module('email')
                self.assertIsInstance(module, ModuleType)
        finally:
            if email is not missing:
                sys.modules['email'] = email 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:24,代碼來源:test_path.py

示例7: test_finder_with_failing_find_module

# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def test_finder_with_failing_find_module(self):
        # PathEntryFinder with find_module() defined should work.
        # Issue #20763.
        class Finder:
            path_location = 'test_finder_with_find_module'
            def __init__(self, path):
                if path != self.path_location:
                    raise ImportError

            @staticmethod
            def find_module(fullname):
                return None


        with util.import_state(path=[Finder.path_location]+sys.path[:],
                               path_hooks=[Finder]):
            self.machinery.PathFinder.find_spec('importlib') 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:19,代碼來源:test_path.py

示例8: get_importer

# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def get_importer(path_item):
    """Retrieve a finder for the given path item

    The returned finder is cached in sys.path_importer_cache
    if it was newly created by a path hook.

    The cache (or part of it) can be cleared manually if a
    rescan of sys.path_hooks is necessary.
    """
    try:
        importer = sys.path_importer_cache[path_item]
    except KeyError:
        for path_hook in sys.path_hooks:
            try:
                importer = path_hook(path_item)
                sys.path_importer_cache.setdefault(path_item, importer)
                break
            except ImportError:
                pass
        else:
            importer = None
    return importer 
開發者ID:CedricGuillemet,項目名稱:Imogen,代碼行數:24,代碼來源:pkgutil.py

示例9: setUp

# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def setUp(self):
        super(ImpTest, self).setUp()

        self._testdir = "ImpTest"
        self._imptestdir = os.path.join(self.test_dir, self._testdir)
        self._f_init = os.path.join(self._imptestdir, "__init__.py")
        self._f_module  = os.path.join(self._imptestdir, "imptestmod.py")

        self.temp_name = ["os",
                    "os.P_WAIT",
                    "os.chmod",
                    "sys.path",
                    "xxxx"
                    ]

        # backup path values
        self.__path = sys.path[:]
        self.__path_hooks = sys.path_hooks[:]
        self.__meta_path = sys.meta_path[:] 
開發者ID:IronLanguages,項目名稱:ironpython3,代碼行數:21,代碼來源:test_imp.py

示例10: get_sys_path_hooks

# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def get_sys_path_hooks(self):
        return id(sys.path_hooks), sys.path_hooks, sys.path_hooks[:] 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:4,代碼來源:regrtest.py

示例11: restore_sys_path_hooks

# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def restore_sys_path_hooks(self, saved_hooks):
        sys.path_hooks = saved_hooks[1]
        sys.path_hooks[:] = saved_hooks[2] 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:5,代碼來源:regrtest.py

示例12: setUp

# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def setUp(self):
    super(ModuleOverrideImportHookTest, self).setUp()
    self.test_policies = {}
    self.path = sys.path[:]
    self.hook = sandbox.ModuleOverrideImportHook(self.test_policies)
    sys.path_importer_cache = {}
    sys.modules.pop('distutils', None)
    __import__('distutils').__path__.insert(0, 'dummy/path')
    sys.modules.pop('distutils.util', None)
    sys.modules.pop('thread', None)
    self.imported_modules = set(sys.modules)
    self.path_hooks = sys.path_hooks 
開發者ID:elsigh,項目名稱:browserscope,代碼行數:14,代碼來源:sandbox_test.py

示例13: tearDown

# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def tearDown(self):
    sys.path_hooks = self.path_hooks
    sys.path_importer_cache = {}
    sys.path = self.path
    added_modules = set(sys.modules) - self.imported_modules
    for name in added_modules:
      del sys.modules[name]
    distutils_modules = [module for module in sys.modules if
                         module.startswith('distutils')]
    for name in distutils_modules:
      del sys.modules[name]
    sys.modules.pop('thread', None)
    super(ModuleOverrideImportHookTest, self).tearDown() 
開發者ID:elsigh,項目名稱:browserscope,代碼行數:15,代碼來源:sandbox_test.py

示例14: get_importer

# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def get_importer(path_item):
    """Retrieve a PEP 302 importer for the given path item

    The returned importer is cached in sys.path_importer_cache
    if it was newly created by a path hook.

    If there is no importer, a wrapper around the basic import
    machinery is returned. This wrapper is never inserted into
    the importer cache (None is inserted instead).

    The cache (or part of it) can be cleared manually if a
    rescan of sys.path_hooks is necessary.
    """
    try:
        importer = sys.path_importer_cache[path_item]
    except KeyError:
        for path_hook in sys.path_hooks:
            try:
                importer = path_hook(path_item)
                break
            except ImportError:
                pass
        else:
            importer = None
        sys.path_importer_cache.setdefault(path_item, importer)

    if importer is None:
        try:
            importer = ImpImporter(path_item)
        except ImportError:
            importer = None
    return importer 
開發者ID:glmcdona,項目名稱:meddle,代碼行數:34,代碼來源:pkgutil.py

示例15: _get_importer

# 需要導入模塊: import sys [as 別名]
# 或者: from sys import path_hooks [as 別名]
def _get_importer(path_name):
    """Python version of PyImport_GetImporter C API function"""
    cache = sys.path_importer_cache
    try:
        importer = cache[path_name]
    except KeyError:
        # Not yet cached. Flag as using the
        # standard machinery until we finish
        # checking the hooks
        cache[path_name] = None
        for hook in sys.path_hooks:
            try:
                importer = hook(path_name)
                break
            except ImportError:
                pass
        else:
            # The following check looks a bit odd. The trick is that
            # NullImporter throws ImportError if the supplied path is a
            # *valid* directory entry (and hence able to be handled
            # by the standard import machinery)
            try:
                importer = imp.NullImporter(path_name)
            except ImportError:
                return None
        cache[path_name] = importer
    return importer 
開發者ID:glmcdona,項目名稱:meddle,代碼行數:29,代碼來源:runpy.py


注:本文中的sys.path_hooks方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。