Python pkgutil._get_spec() Examples
The following are 8
code examples of pkgutil._get_spec().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pkgutil
, or try the search function
.
Example #1
Source File: pydoc.py From Fluid-Designer with GNU General Public License v3.0 | 4 votes |
def run(self, callback, key=None, completer=None, onerror=None): if key: key = key.lower() self.quit = False seen = {} for modname in sys.builtin_module_names: if modname != '__main__': seen[modname] = 1 if key is None: callback(None, modname, '') else: name = __import__(modname).__doc__ or '' desc = name.split('\n')[0] name = modname + ' - ' + desc if name.lower().find(key) >= 0: callback(None, modname, desc) for importer, modname, ispkg in pkgutil.walk_packages(onerror=onerror): if self.quit: break if key is None: callback(None, modname, '') else: try: spec = pkgutil._get_spec(importer, modname) except SyntaxError: # raised by tests for bad coding cookies or BOM continue loader = spec.loader if hasattr(loader, 'get_source'): try: source = loader.get_source(modname) except Exception: if onerror: onerror(modname) continue desc = source_synopsis(io.StringIO(source)) or '' if hasattr(loader, 'get_filename'): path = loader.get_filename(modname) else: path = None else: try: module = importlib._bootstrap._load(spec) except ImportError: if onerror: onerror(modname) continue desc = module.__doc__.splitlines()[0] if module.__doc__ else '' path = getattr(module,'__file__',None) name = modname + ' - ' + desc if name.lower().find(key) >= 0: callback(path, modname, desc) if completer: completer()
Example #2
Source File: test_pkgutil.py From Fluid-Designer with GNU General Public License v3.0 | 4 votes |
def test_iter_importers(self): iter_importers = pkgutil.iter_importers get_importer = pkgutil.get_importer pkgname = 'spam' modname = 'eggs' dirname = self.create_init(pkgname) pathitem = os.path.join(dirname, pkgname) fullname = '{}.{}'.format(pkgname, modname) sys.modules.pop(fullname, None) sys.modules.pop(pkgname, None) try: self.create_submodule(dirname, pkgname, modname, 0) importlib.import_module(fullname) importers = list(iter_importers(fullname)) expected_importer = get_importer(pathitem) for finder in importers: spec = pkgutil._get_spec(finder, fullname) loader = spec.loader try: loader = loader.loader except AttributeError: # For now we still allow raw loaders from # find_module(). pass self.assertIsInstance(finder, importlib.machinery.FileFinder) self.assertEqual(finder, expected_importer) self.assertIsInstance(loader, importlib.machinery.SourceFileLoader) self.assertIsNone(pkgutil._get_spec(finder, pkgname)) with self.assertRaises(ImportError): list(iter_importers('invalid.module')) with self.assertRaises(ImportError): list(iter_importers('.spam')) finally: shutil.rmtree(dirname) del sys.path[0] try: del sys.modules['spam'] del sys.modules['spam.eggs'] except KeyError: pass
Example #3
Source File: pydoc.py From Imogen with MIT License | 4 votes |
def run(self, callback, key=None, completer=None, onerror=None): if key: key = key.lower() self.quit = False seen = {} for modname in sys.builtin_module_names: if modname != '__main__': seen[modname] = 1 if key is None: callback(None, modname, '') else: name = __import__(modname).__doc__ or '' desc = name.split('\n')[0] name = modname + ' - ' + desc if name.lower().find(key) >= 0: callback(None, modname, desc) for importer, modname, ispkg in pkgutil.walk_packages(onerror=onerror): if self.quit: break if key is None: callback(None, modname, '') else: try: spec = pkgutil._get_spec(importer, modname) except SyntaxError: # raised by tests for bad coding cookies or BOM continue loader = spec.loader if hasattr(loader, 'get_source'): try: source = loader.get_source(modname) except Exception: if onerror: onerror(modname) continue desc = source_synopsis(io.StringIO(source)) or '' if hasattr(loader, 'get_filename'): path = loader.get_filename(modname) else: path = None else: try: module = importlib._bootstrap._load(spec) except ImportError: if onerror: onerror(modname) continue desc = module.__doc__.splitlines()[0] if module.__doc__ else '' path = getattr(module,'__file__',None) name = modname + ' - ' + desc if name.lower().find(key) >= 0: callback(path, modname, desc) if completer: completer()
Example #4
Source File: pydoc.py From ironpython3 with Apache License 2.0 | 4 votes |
def run(self, callback, key=None, completer=None, onerror=None): if key: key = key.lower() self.quit = False seen = {} for modname in sys.builtin_module_names: if modname != '__main__': seen[modname] = 1 if key is None: callback(None, modname, '') else: name = __import__(modname).__doc__ or '' desc = name.split('\n')[0] name = modname + ' - ' + desc if name.lower().find(key) >= 0: callback(None, modname, desc) for importer, modname, ispkg in pkgutil.walk_packages(onerror=onerror): if self.quit: break if key is None: callback(None, modname, '') else: try: spec = pkgutil._get_spec(importer, modname) except SyntaxError: # raised by tests for bad coding cookies or BOM continue loader = spec.loader if hasattr(loader, 'get_source'): try: source = loader.get_source(modname) except Exception: if onerror: onerror(modname) continue desc = source_synopsis(io.StringIO(source)) or '' if hasattr(loader, 'get_filename'): path = loader.get_filename(modname) else: path = None else: _spec = importlib._bootstrap._SpecMethods(spec) try: module = _spec.load() except ImportError: if onerror: onerror(modname) continue desc = module.__doc__.splitlines()[0] if module.__doc__ else '' path = getattr(module,'__file__',None) name = modname + ' - ' + desc if name.lower().find(key) >= 0: callback(path, modname, desc) if completer: completer()
Example #5
Source File: test_pkgutil.py From ironpython3 with Apache License 2.0 | 4 votes |
def test_iter_importers(self): iter_importers = pkgutil.iter_importers get_importer = pkgutil.get_importer pkgname = 'spam' modname = 'eggs' dirname = self.create_init(pkgname) pathitem = os.path.join(dirname, pkgname) fullname = '{}.{}'.format(pkgname, modname) sys.modules.pop(fullname, None) sys.modules.pop(pkgname, None) try: self.create_submodule(dirname, pkgname, modname, 0) importlib.import_module(fullname) importers = list(iter_importers(fullname)) expected_importer = get_importer(pathitem) for finder in importers: spec = pkgutil._get_spec(finder, fullname) loader = spec.loader try: loader = loader.loader except AttributeError: # For now we still allow raw loaders from # find_module(). pass self.assertIsInstance(finder, importlib.machinery.FileFinder) self.assertEqual(finder, expected_importer) self.assertIsInstance(loader, importlib.machinery.SourceFileLoader) self.assertIsNone(pkgutil._get_spec(finder, pkgname)) with self.assertRaises(ImportError): list(iter_importers('invalid.module')) with self.assertRaises(ImportError): list(iter_importers('.spam')) finally: shutil.rmtree(dirname) del sys.path[0] try: del sys.modules['spam'] del sys.modules['spam.eggs'] except KeyError: pass
Example #6
Source File: pydoc.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 4 votes |
def run(self, callback, key=None, completer=None, onerror=None): if key: key = key.lower() self.quit = False seen = {} for modname in sys.builtin_module_names: if modname != '__main__': seen[modname] = 1 if key is None: callback(None, modname, '') else: name = __import__(modname).__doc__ or '' desc = name.split('\n')[0] name = modname + ' - ' + desc if name.lower().find(key) >= 0: callback(None, modname, desc) for importer, modname, ispkg in pkgutil.walk_packages(onerror=onerror): if self.quit: break if key is None: callback(None, modname, '') else: try: spec = pkgutil._get_spec(importer, modname) except SyntaxError: # raised by tests for bad coding cookies or BOM continue loader = spec.loader if hasattr(loader, 'get_source'): try: source = loader.get_source(modname) except Exception: if onerror: onerror(modname) continue desc = source_synopsis(io.StringIO(source)) or '' if hasattr(loader, 'get_filename'): path = loader.get_filename(modname) else: path = None else: try: module = importlib._bootstrap._load(spec) except ImportError: if onerror: onerror(modname) continue desc = module.__doc__.splitlines()[0] if module.__doc__ else '' path = getattr(module,'__file__',None) name = modname + ' - ' + desc if name.lower().find(key) >= 0: callback(path, modname, desc) if completer: completer()
Example #7
Source File: test_pkgutil.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 4 votes |
def test_iter_importers(self): iter_importers = pkgutil.iter_importers get_importer = pkgutil.get_importer pkgname = 'spam' modname = 'eggs' dirname = self.create_init(pkgname) pathitem = os.path.join(dirname, pkgname) fullname = '{}.{}'.format(pkgname, modname) sys.modules.pop(fullname, None) sys.modules.pop(pkgname, None) try: self.create_submodule(dirname, pkgname, modname, 0) importlib.import_module(fullname) importers = list(iter_importers(fullname)) expected_importer = get_importer(pathitem) for finder in importers: spec = pkgutil._get_spec(finder, fullname) loader = spec.loader try: loader = loader.loader except AttributeError: # For now we still allow raw loaders from # find_module(). pass self.assertIsInstance(finder, importlib.machinery.FileFinder) self.assertEqual(finder, expected_importer) self.assertIsInstance(loader, importlib.machinery.SourceFileLoader) self.assertIsNone(pkgutil._get_spec(finder, pkgname)) with self.assertRaises(ImportError): list(iter_importers('invalid.module')) with self.assertRaises(ImportError): list(iter_importers('.spam')) finally: shutil.rmtree(dirname) del sys.path[0] try: del sys.modules['spam'] del sys.modules['spam.eggs'] except KeyError: pass
Example #8
Source File: pydoc.py From android_universal with MIT License | 4 votes |
def run(self, callback, key=None, completer=None, onerror=None): if key: key = key.lower() self.quit = False seen = {} for modname in sys.builtin_module_names: if modname != '__main__': seen[modname] = 1 if key is None: callback(None, modname, '') else: name = __import__(modname).__doc__ or '' desc = name.split('\n')[0] name = modname + ' - ' + desc if name.lower().find(key) >= 0: callback(None, modname, desc) for importer, modname, ispkg in pkgutil.walk_packages(onerror=onerror): if self.quit: break if key is None: callback(None, modname, '') else: try: spec = pkgutil._get_spec(importer, modname) except SyntaxError: # raised by tests for bad coding cookies or BOM continue loader = spec.loader if hasattr(loader, 'get_source'): try: source = loader.get_source(modname) except Exception: if onerror: onerror(modname) continue desc = source_synopsis(io.StringIO(source)) or '' if hasattr(loader, 'get_filename'): path = loader.get_filename(modname) else: path = None else: try: module = importlib._bootstrap._load(spec) except ImportError: if onerror: onerror(modname) continue desc = module.__doc__.splitlines()[0] if module.__doc__ else '' path = getattr(module,'__file__',None) name = modname + ' - ' + desc if name.lower().find(key) >= 0: callback(path, modname, desc) if completer: completer()