我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用importlib.abc()。
def test_load_module_API(self): # If fullname is not specified that assume self.name is desired. class TesterMixin(importlib.abc.Loader): def load_module(self, fullname): return fullname def module_repr(self, module): return '<module>' class Tester(importlib.abc.FileLoader, TesterMixin): def get_code(self, _): pass def get_source(self, _): pass def is_package(self, _): pass name = 'mod_name' loader = Tester(name, 'some_path') self.assertEqual(name, loader.load_module()) self.assertEqual(name, loader.load_module(None)) self.assertEqual(name, loader.load_module(name)) with self.assertRaises(ImportError): loader.load_module(loader.name + 'XXX')
def test_get_filename_API(self): # If fullname is not set then assume self.path is desired. class Tester(importlib.abc.FileLoader): def get_code(self, _): pass def get_source(self, _): pass def is_package(self, _): pass def module_repr(self, _): pass path = 'some_path' name = 'some_name' loader = Tester(name, path) self.assertEqual(path, loader.get_filename(name)) self.assertEqual(path, loader.get_filename()) self.assertEqual(path, loader.get_filename(None)) with self.assertRaises(ImportError): loader.get_filename(name + 'XXX') # [basic]
def open_module(self, event=None): # XXX Shouldn't this be in IOBinding? try: name = self.text.get("sel.first", "sel.last") except TclError: name = "" else: name = name.strip() name = tkSimpleDialog.askstring("Module", "Enter the name of a Python module\n" "to search on sys.path and open:", parent=self.text, initialvalue=name) if name: name = name.strip() if not name: return # XXX Ought to insert current file's directory in front of path try: spec = importlib.util.find_spec(name) except (ValueError, ImportError) as msg: tkMessageBox.showerror("Import error", str(msg), parent=self.text) return if spec is None: tkMessageBox.showerror("Import error", "module not found", parent=self.text) return if not isinstance(spec.loader, importlib.abc.SourceLoader): tkMessageBox.showerror("Import error", "not a source-based module", parent=self.text) return try: file_path = spec.loader.get_filename(name) except AttributeError: tkMessageBox.showerror("Import error", "loader does not support get_filename", parent=self.text) return if self.flist: self.flist.open(file_path) else: self.io.loadfile(file_path)
def open_module(self, event=None): # XXX Shouldn't this be in IOBinding? try: name = self.text.get("sel.first", "sel.last") except TclError: name = "" else: name = name.strip() name = tkSimpleDialog.askstring("Module", "Enter the name of a Python module\n" "to search on sys.path and open:", parent=self.text, initialvalue=name) if name: name = name.strip() if not name: return # XXX Ought to insert current file's directory in front of path try: spec = importlib.util.find_spec(name) except (ValueError, ImportError) as msg: tkMessageBox.showerror("Import error", str(msg), parent=self.text) return if spec is None: tkMessageBox.showerror("Import error", "module not found", parent=self.text) return if not isinstance(spec.loader, importlib.abc.SourceLoader): tkMessageBox.showerror("Import error", "not a source-based module", parent=self.text) return try: file_path = spec.loader.get_filename(name) except AttributeError: tkMessageBox.showerror("Import error", "loader does not support get_filename", parent=self.text) return if self.flist: self.flist.open(file_path) else: self.io.loadfile(file_path) return file_path