我们从Python开源项目中,提取了以下37个代码示例,用于说明如何使用imp.NullImporter()。
def test_implicit_hooks(self): # Test that the implicit path hooks are used. bad_path = '<path>' module = '<module>' assert not os.path.exists(bad_path) existing_path = tempfile.mkdtemp() try: with util.import_state(): nothing = _bootstrap._DefaultPathFinder.find_module(module, path=[existing_path]) self.assertTrue(nothing is None) self.assertTrue(existing_path in sys.path_importer_cache) result = isinstance(sys.path_importer_cache[existing_path], imp.NullImporter) self.assertFalse(result) nothing = _bootstrap._DefaultPathFinder.find_module(module, path=[bad_path]) self.assertTrue(nothing is None) self.assertTrue(bad_path in sys.path_importer_cache) self.assertTrue(isinstance(sys.path_importer_cache[bad_path], imp.NullImporter)) finally: os.rmdir(existing_path)
def test_pythontypes(self): # check all types defined in Python/ h = self.header vh = self.vheader size = self.calcsize check = self.check_sizeof # _ast.AST import _ast check(_ast.AST(), size(h + '')) # imp.NullImporter import imp check(imp.NullImporter(self.file.name), size(h + '')) try: raise TypeError except TypeError: tb = sys.exc_info()[2] # traceback if tb != None: check(tb, size(h + '2P2i')) # symtable entry # XXX # sys.flags check(sys.flags, size(vh) + self.P * len(sys.flags))
def test_pythontypes(self): # check all types defined in Python/ size = test.test_support.calcobjsize vsize = test.test_support.calcvobjsize check = self.check_sizeof # _ast.AST import _ast check(_ast.AST(), size('')) # imp.NullImporter import imp check(imp.NullImporter(self.file.name), size('')) try: raise TypeError except TypeError: tb = sys.exc_info()[2] # traceback if tb != None: check(tb, size('2P2i')) # symtable entry # XXX # sys.flags check(sys.flags, vsize('') + self.P * len(sys.flags))
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
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
def test_unencodeable(self): name = support.TESTFN_UNENCODABLE os.mkdir(name) try: self.assertRaises(ImportError, imp.NullImporter, name) finally: os.rmdir(name)
def test_pythontypes(self): # check all types defined in Python/ size = test.test_support.calcobjsize vsize = test.test_support.calcvobjsize check = self.check_sizeof # _ast.AST import _ast check(_ast.AST(), size('')) # imp.NullImporter import imp f = open(test.test_support.TESTFN, 'wb') try: check(imp.NullImporter(f.name), size('')) finally: f.close() test.test_support.unlink(test.test_support.TESTFN) try: raise TypeError except TypeError: tb = sys.exc_info()[2] # traceback if tb != None: check(tb, size('2P2i')) # symtable entry # XXX # sys.flags check(sys.flags, vsize('') + self.P * len(sys.flags))
def test_method_lacking(self): # There should be no issues if the method is not defined. key = 'gobbledeegook' sys.path_importer_cache[key] = imp.NullImporter('abc') self.addCleanup(lambda: sys.path_importer_cache.__delitem__(key)) importlib.invalidate_caches() # Shouldn't trigger an exception.
def run_path(path_name, init_globals=None, run_name=None): """Execute code located at the specified filesystem location Returns the resulting top level namespace dictionary The file path may refer directly to a Python script (i.e. one that could be directly executed with execfile) or else it may refer to a zipfile or directory containing a top level __main__.py script. """ if run_name is None: run_name = "<run_path>" importer = _get_importer(path_name) if isinstance(importer, imp.NullImporter): # Not a valid sys.path entry, so run the code directly # execfile() doesn't help as we want to allow compiled files code = _get_code_from_file(path_name) return _run_module_code(code, init_globals, run_name, path_name) else: # Importer is defined for path, so add it to # the start of sys.path sys.path.insert(0, path_name) try: # Here's where things are a little different from the run_module # case. There, we only had to replace the module in sys while the # code was running and doing so was somewhat optional. Here, we # have no choice and we have to remove it even while we read the # code. If we don't do this, a __loader__ attribute in the # existing __main__ module may prevent location of the new module. main_name = "__main__" saved_main = sys.modules[main_name] del sys.modules[main_name] try: mod_name, loader, code, fname = _get_main_module_details() finally: sys.modules[main_name] = saved_main pkg_name = "" with _TempModule(run_name) as temp_module, \ _ModifiedArgv0(path_name): mod_globals = temp_module.module.__dict__ return _run_code(code, mod_globals, init_globals, run_name, fname, loader, pkg_name).copy() finally: try: sys.path.remove(path_name) except ValueError: pass