PageRenderTime 66ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/Lib/pkgutil.py

https://github.com/albertz/CPython
Python | 637 lines | 606 code | 17 blank | 14 comment | 9 complexity | 3b3272e45345aae38b3407ba3f3a34f5 MD5 | raw file
  1. """Utilities to support packages."""
  2. from collections import namedtuple
  3. from functools import singledispatch as simplegeneric
  4. import importlib
  5. import importlib.util
  6. import importlib.machinery
  7. import os
  8. import os.path
  9. import sys
  10. from types import ModuleType
  11. import warnings
  12. __all__ = [
  13. 'get_importer', 'iter_importers', 'get_loader', 'find_loader',
  14. 'walk_packages', 'iter_modules', 'get_data',
  15. 'ImpImporter', 'ImpLoader', 'read_code', 'extend_path',
  16. 'ModuleInfo',
  17. ]
  18. ModuleInfo = namedtuple('ModuleInfo', 'module_finder name ispkg')
  19. ModuleInfo.__doc__ = 'A namedtuple with minimal info about a module.'
  20. def _get_spec(finder, name):
  21. """Return the finder-specific module spec."""
  22. # Works with legacy finders.
  23. try:
  24. find_spec = finder.find_spec
  25. except AttributeError:
  26. loader = finder.find_module(name)
  27. if loader is None:
  28. return None
  29. return importlib.util.spec_from_loader(name, loader)
  30. else:
  31. return find_spec(name)
  32. def read_code(stream):
  33. # This helper is needed in order for the PEP 302 emulation to
  34. # correctly handle compiled files
  35. import marshal
  36. magic = stream.read(4)
  37. if magic != importlib.util.MAGIC_NUMBER:
  38. return None
  39. stream.read(12) # Skip rest of the header
  40. return marshal.load(stream)
  41. def walk_packages(path=None, prefix='', onerror=None):
  42. """Yields ModuleInfo for all modules recursively
  43. on path, or, if path is None, all accessible modules.
  44. 'path' should be either None or a list of paths to look for
  45. modules in.
  46. 'prefix' is a string to output on the front of every module name
  47. on output.
  48. Note that this function must import all *packages* (NOT all
  49. modules!) on the given path, in order to access the __path__
  50. attribute to find submodules.
  51. 'onerror' is a function which gets called with one argument (the
  52. name of the package which was being imported) if any exception
  53. occurs while trying to import a package. If no onerror function is
  54. supplied, ImportErrors are caught and ignored, while all other
  55. exceptions are propagated, terminating the search.
  56. Examples:
  57. # list all modules python can access
  58. walk_packages()
  59. # list all submodules of ctypes
  60. walk_packages(ctypes.__path__, ctypes.__name__+'.')
  61. """
  62. def seen(p, m={}):
  63. if p in m:
  64. return True
  65. m[p] = True
  66. for info in iter_modules(path, prefix):
  67. yield info
  68. if info.ispkg:
  69. try:
  70. __import__(info.name)
  71. except ImportError:
  72. if onerror is not None:
  73. onerror(info.name)
  74. except Exception:
  75. if onerror is not None:
  76. onerror(info.name)
  77. else:
  78. raise
  79. else:
  80. path = getattr(sys.modules[info.name], '__path__', None) or []
  81. # don't traverse path items we've seen before
  82. path = [p for p in path if not seen(p)]
  83. yield from walk_packages(path, info.name+'.', onerror)
  84. def iter_modules(path=None, prefix=''):
  85. """Yields ModuleInfo for all submodules on path,
  86. or, if path is None, all top-level modules on sys.path.
  87. 'path' should be either None or a list of paths to look for
  88. modules in.
  89. 'prefix' is a string to output on the front of every module name
  90. on output.
  91. """
  92. if path is None:
  93. importers = iter_importers()
  94. elif isinstance(path, str):
  95. raise ValueError("path must be None or list of paths to look for "
  96. "modules in")
  97. else:
  98. importers = map(get_importer, path)
  99. yielded = {}
  100. for i in importers:
  101. for name, ispkg in iter_importer_modules(i, prefix):
  102. if name not in yielded:
  103. yielded[name] = 1
  104. yield ModuleInfo(i, name, ispkg)
  105. @simplegeneric
  106. def iter_importer_modules(importer, prefix=''):
  107. if not hasattr(importer, 'iter_modules'):
  108. return []
  109. return importer.iter_modules(prefix)
  110. # Implement a file walker for the normal importlib path hook
  111. def _iter_file_finder_modules(importer, prefix=''):
  112. if importer.path is None or not os.path.isdir(importer.path):
  113. return
  114. yielded = {}
  115. import inspect
  116. try:
  117. filenames = os.listdir(importer.path)
  118. except OSError:
  119. # ignore unreadable directories like import does
  120. filenames = []
  121. filenames.sort() # handle packages before same-named modules
  122. for fn in filenames:
  123. modname = inspect.getmodulename(fn)
  124. if modname=='__init__' or modname in yielded:
  125. continue
  126. path = os.path.join(importer.path, fn)
  127. ispkg = False
  128. if not modname and os.path.isdir(path) and '.' not in fn:
  129. modname = fn
  130. try:
  131. dircontents = os.listdir(path)
  132. except OSError:
  133. # ignore unreadable directories like import does
  134. dircontents = []
  135. for fn in dircontents:
  136. subname = inspect.getmodulename(fn)
  137. if subname=='__init__':
  138. ispkg = True
  139. break
  140. else:
  141. continue # not a package
  142. if modname and '.' not in modname:
  143. yielded[modname] = 1
  144. yield prefix + modname, ispkg
  145. iter_importer_modules.register(
  146. importlib.machinery.FileFinder, _iter_file_finder_modules)
  147. def _import_imp():
  148. global imp
  149. with warnings.catch_warnings():
  150. warnings.simplefilter('ignore', DeprecationWarning)
  151. imp = importlib.import_module('imp')
  152. class ImpImporter:
  153. """PEP 302 Finder that wraps Python's "classic" import algorithm
  154. ImpImporter(dirname) produces a PEP 302 finder that searches that
  155. directory. ImpImporter(None) produces a PEP 302 finder that searches
  156. the current sys.path, plus any modules that are frozen or built-in.
  157. Note that ImpImporter does not currently support being used by placement
  158. on sys.meta_path.
  159. """
  160. def __init__(self, path=None):
  161. global imp
  162. warnings.warn("This emulation is deprecated, use 'importlib' instead",
  163. DeprecationWarning)
  164. _import_imp()
  165. self.path = path
  166. def find_module(self, fullname, path=None):
  167. # Note: we ignore 'path' argument since it is only used via meta_path
  168. subname = fullname.split(".")[-1]
  169. if subname != fullname and self.path is None:
  170. return None
  171. if self.path is None:
  172. path = None
  173. else:
  174. path = [os.path.realpath(self.path)]
  175. try:
  176. file, filename, etc = imp.find_module(subname, path)
  177. except ImportError:
  178. return None
  179. return ImpLoader(fullname, file, filename, etc)
  180. def iter_modules(self, prefix=''):
  181. if self.path is None or not os.path.isdir(self.path):
  182. return
  183. yielded = {}
  184. import inspect
  185. try:
  186. filenames = os.listdir(self.path)
  187. except OSError:
  188. # ignore unreadable directories like import does
  189. filenames = []
  190. filenames.sort() # handle packages before same-named modules
  191. for fn in filenames:
  192. modname = inspect.getmodulename(fn)
  193. if modname=='__init__' or modname in yielded:
  194. continue
  195. path = os.path.join(self.path, fn)
  196. ispkg = False
  197. if not modname and os.path.isdir(path) and '.' not in fn:
  198. modname = fn
  199. try:
  200. dircontents = os.listdir(path)
  201. except OSError:
  202. # ignore unreadable directories like import does
  203. dircontents = []
  204. for fn in dircontents:
  205. subname = inspect.getmodulename(fn)
  206. if subname=='__init__':
  207. ispkg = True
  208. break
  209. else:
  210. continue # not a package
  211. if modname and '.' not in modname:
  212. yielded[modname] = 1
  213. yield prefix + modname, ispkg
  214. class ImpLoader:
  215. """PEP 302 Loader that wraps Python's "classic" import algorithm
  216. """
  217. code = source = None
  218. def __init__(self, fullname, file, filename, etc):
  219. warnings.warn("This emulation is deprecated, use 'importlib' instead",
  220. DeprecationWarning)
  221. _import_imp()
  222. self.file = file
  223. self.filename = filename
  224. self.fullname = fullname
  225. self.etc = etc
  226. def load_module(self, fullname):
  227. self._reopen()
  228. try:
  229. mod = imp.load_module(fullname, self.file, self.filename, self.etc)
  230. finally:
  231. if self.file:
  232. self.file.close()
  233. # Note: we don't set __loader__ because we want the module to look
  234. # normal; i.e. this is just a wrapper for standard import machinery
  235. return mod
  236. def get_data(self, pathname):
  237. with open(pathname, "rb") as file:
  238. return file.read()
  239. def _reopen(self):
  240. if self.file and self.file.closed:
  241. mod_type = self.etc[2]
  242. if mod_type==imp.PY_SOURCE:
  243. self.file = open(self.filename, 'r')
  244. elif mod_type in (imp.PY_COMPILED, imp.C_EXTENSION):
  245. self.file = open(self.filename, 'rb')
  246. def _fix_name(self, fullname):
  247. if fullname is None:
  248. fullname = self.fullname
  249. elif fullname != self.fullname:
  250. raise ImportError("Loader for module %s cannot handle "
  251. "module %s" % (self.fullname, fullname))
  252. return fullname
  253. def is_package(self, fullname):
  254. fullname = self._fix_name(fullname)
  255. return self.etc[2]==imp.PKG_DIRECTORY
  256. def get_code(self, fullname=None):
  257. fullname = self._fix_name(fullname)
  258. if self.code is None:
  259. mod_type = self.etc[2]
  260. if mod_type==imp.PY_SOURCE:
  261. source = self.get_source(fullname)
  262. self.code = compile(source, self.filename, 'exec')
  263. elif mod_type==imp.PY_COMPILED:
  264. self._reopen()
  265. try:
  266. self.code = read_code(self.file)
  267. finally:
  268. self.file.close()
  269. elif mod_type==imp.PKG_DIRECTORY:
  270. self.code = self._get_delegate().get_code()
  271. return self.code
  272. def get_source(self, fullname=None):
  273. fullname = self._fix_name(fullname)
  274. if self.source is None:
  275. mod_type = self.etc[2]
  276. if mod_type==imp.PY_SOURCE:
  277. self._reopen()
  278. try:
  279. self.source = self.file.read()
  280. finally:
  281. self.file.close()
  282. elif mod_type==imp.PY_COMPILED:
  283. if os.path.exists(self.filename[:-1]):
  284. with open(self.filename[:-1], 'r') as f:
  285. self.source = f.read()
  286. elif mod_type==imp.PKG_DIRECTORY:
  287. self.source = self._get_delegate().get_source()
  288. return self.source
  289. def _get_delegate(self):
  290. finder = ImpImporter(self.filename)
  291. spec = _get_spec(finder, '__init__')
  292. return spec.loader
  293. def get_filename(self, fullname=None):
  294. fullname = self._fix_name(fullname)
  295. mod_type = self.etc[2]
  296. if mod_type==imp.PKG_DIRECTORY:
  297. return self._get_delegate().get_filename()
  298. elif mod_type in (imp.PY_SOURCE, imp.PY_COMPILED, imp.C_EXTENSION):
  299. return self.filename
  300. return None
  301. try:
  302. import zipimport
  303. from zipimport import zipimporter
  304. def iter_zipimport_modules(importer, prefix=''):
  305. dirlist = sorted(zipimport._zip_directory_cache[importer.archive])
  306. _prefix = importer.prefix
  307. plen = len(_prefix)
  308. yielded = {}
  309. import inspect
  310. for fn in dirlist:
  311. if not fn.startswith(_prefix):
  312. continue
  313. fn = fn[plen:].split(os.sep)
  314. if len(fn)==2 and fn[1].startswith('__init__.py'):
  315. if fn[0] not in yielded:
  316. yielded[fn[0]] = 1
  317. yield prefix + fn[0], True
  318. if len(fn)!=1:
  319. continue
  320. modname = inspect.getmodulename(fn[0])
  321. if modname=='__init__':
  322. continue
  323. if modname and '.' not in modname and modname not in yielded:
  324. yielded[modname] = 1
  325. yield prefix + modname, False
  326. iter_importer_modules.register(zipimporter, iter_zipimport_modules)
  327. except ImportError:
  328. pass
  329. def get_importer(path_item):
  330. """Retrieve a finder for the given path item
  331. The returned finder is cached in sys.path_importer_cache
  332. if it was newly created by a path hook.
  333. The cache (or part of it) can be cleared manually if a
  334. rescan of sys.path_hooks is necessary.
  335. """
  336. try:
  337. importer = sys.path_importer_cache[path_item]
  338. except KeyError:
  339. for path_hook in sys.path_hooks:
  340. try:
  341. importer = path_hook(path_item)
  342. sys.path_importer_cache.setdefault(path_item, importer)
  343. break
  344. except ImportError:
  345. pass
  346. else:
  347. importer = None
  348. return importer
  349. def iter_importers(fullname=""):
  350. """Yield finders for the given module name
  351. If fullname contains a '.', the finders will be for the package
  352. containing fullname, otherwise they will be all registered top level
  353. finders (i.e. those on both sys.meta_path and sys.path_hooks).
  354. If the named module is in a package, that package is imported as a side
  355. effect of invoking this function.
  356. If no module name is specified, all top level finders are produced.
  357. """
  358. if fullname.startswith('.'):
  359. msg = "Relative module name {!r} not supported".format(fullname)
  360. raise ImportError(msg)
  361. if '.' in fullname:
  362. # Get the containing package's __path__
  363. pkg_name = fullname.rpartition(".")[0]
  364. pkg = importlib.import_module(pkg_name)
  365. path = getattr(pkg, '__path__', None)
  366. if path is None:
  367. return
  368. else:
  369. yield from sys.meta_path
  370. path = sys.path
  371. for item in path:
  372. yield get_importer(item)
  373. def get_loader(module_or_name):
  374. """Get a "loader" object for module_or_name
  375. Returns None if the module cannot be found or imported.
  376. If the named module is not already imported, its containing package
  377. (if any) is imported, in order to establish the package __path__.
  378. """
  379. if module_or_name in sys.modules:
  380. module_or_name = sys.modules[module_or_name]
  381. if module_or_name is None:
  382. return None
  383. if isinstance(module_or_name, ModuleType):
  384. module = module_or_name
  385. loader = getattr(module, '__loader__', None)
  386. if loader is not None:
  387. return loader
  388. if getattr(module, '__spec__', None) is None:
  389. return None
  390. fullname = module.__name__
  391. else:
  392. fullname = module_or_name
  393. return find_loader(fullname)
  394. def find_loader(fullname):
  395. """Find a "loader" object for fullname
  396. This is a backwards compatibility wrapper around
  397. importlib.util.find_spec that converts most failures to ImportError
  398. and only returns the loader rather than the full spec
  399. """
  400. if fullname.startswith('.'):
  401. msg = "Relative module name {!r} not supported".format(fullname)
  402. raise ImportError(msg)
  403. try:
  404. spec = importlib.util.find_spec(fullname)
  405. except (ImportError, AttributeError, TypeError, ValueError) as ex:
  406. # This hack fixes an impedance mismatch between pkgutil and
  407. # importlib, where the latter raises other errors for cases where
  408. # pkgutil previously raised ImportError
  409. msg = "Error while finding loader for {!r} ({}: {})"
  410. raise ImportError(msg.format(fullname, type(ex), ex)) from ex
  411. return spec.loader if spec is not None else None
  412. def extend_path(path, name):
  413. """Extend a package's path.
  414. Intended use is to place the following code in a package's __init__.py:
  415. from pkgutil import extend_path
  416. __path__ = extend_path(__path__, __name__)
  417. This will add to the package's __path__ all subdirectories of
  418. directories on sys.path named after the package. This is useful
  419. if one wants to distribute different parts of a single logical
  420. package as multiple directories.
  421. It also looks for *.pkg files beginning where * matches the name
  422. argument. This feature is similar to *.pth files (see site.py),
  423. except that it doesn't special-case lines starting with 'import'.
  424. A *.pkg file is trusted at face value: apart from checking for
  425. duplicates, all entries found in a *.pkg file are added to the
  426. path, regardless of whether they are exist the filesystem. (This
  427. is a feature.)
  428. If the input path is not a list (as is the case for frozen
  429. packages) it is returned unchanged. The input path is not
  430. modified; an extended copy is returned. Items are only appended
  431. to the copy at the end.
  432. It is assumed that sys.path is a sequence. Items of sys.path that
  433. are not (unicode or 8-bit) strings referring to existing
  434. directories are ignored. Unicode items of sys.path that cause
  435. errors when used as filenames may cause this function to raise an
  436. exception (in line with os.path.isdir() behavior).
  437. """
  438. if not isinstance(path, list):
  439. # This could happen e.g. when this is called from inside a
  440. # frozen package. Return the path unchanged in that case.
  441. return path
  442. sname_pkg = name + ".pkg"
  443. path = path[:] # Start with a copy of the existing path
  444. parent_package, _, final_name = name.rpartition('.')
  445. if parent_package:
  446. try:
  447. search_path = sys.modules[parent_package].__path__
  448. except (KeyError, AttributeError):
  449. # We can't do anything: find_loader() returns None when
  450. # passed a dotted name.
  451. return path
  452. else:
  453. search_path = sys.path
  454. for dir in search_path:
  455. if not isinstance(dir, str):
  456. continue
  457. finder = get_importer(dir)
  458. if finder is not None:
  459. portions = []
  460. if hasattr(finder, 'find_spec'):
  461. spec = finder.find_spec(final_name)
  462. if spec is not None:
  463. portions = spec.submodule_search_locations or []
  464. # Is this finder PEP 420 compliant?
  465. elif hasattr(finder, 'find_loader'):
  466. _, portions = finder.find_loader(final_name)
  467. for portion in portions:
  468. # XXX This may still add duplicate entries to path on
  469. # case-insensitive filesystems
  470. if portion not in path:
  471. path.append(portion)
  472. # XXX Is this the right thing for subpackages like zope.app?
  473. # It looks for a file named "zope.app.pkg"
  474. pkgfile = os.path.join(dir, sname_pkg)
  475. if os.path.isfile(pkgfile):
  476. try:
  477. f = open(pkgfile)
  478. except OSError as msg:
  479. sys.stderr.write("Can't open %s: %s\n" %
  480. (pkgfile, msg))
  481. else:
  482. with f:
  483. for line in f:
  484. line = line.rstrip('\n')
  485. if not line or line.startswith('#'):
  486. continue
  487. path.append(line) # Don't check for existence!
  488. return path
  489. def get_data(package, resource):
  490. """Get a resource from a package.
  491. This is a wrapper round the PEP 302 loader get_data API. The package
  492. argument should be the name of a package, in standard module format
  493. (foo.bar). The resource argument should be in the form of a relative
  494. filename, using '/' as the path separator. The parent directory name '..'
  495. is not allowed, and nor is a rooted name (starting with a '/').
  496. The function returns a binary string, which is the contents of the
  497. specified resource.
  498. For packages located in the filesystem, which have already been imported,
  499. this is the rough equivalent of
  500. d = os.path.dirname(sys.modules[package].__file__)
  501. data = open(os.path.join(d, resource), 'rb').read()
  502. If the package cannot be located or loaded, or it uses a PEP 302 loader
  503. which does not support get_data(), then None is returned.
  504. """
  505. spec = importlib.util.find_spec(package)
  506. if spec is None:
  507. return None
  508. loader = spec.loader
  509. if loader is None or not hasattr(loader, 'get_data'):
  510. return None
  511. # XXX needs test
  512. mod = (sys.modules.get(package) or
  513. importlib._bootstrap._load(spec))
  514. if mod is None or not hasattr(mod, '__file__'):
  515. return None
  516. # Modify the resource name to be compatible with the loader.get_data
  517. # signature - an os.path format "filename" starting with the dirname of
  518. # the package's __file__
  519. parts = resource.split('/')
  520. parts.insert(0, os.path.dirname(mod.__file__))
  521. resource_name = os.path.join(*parts)
  522. return loader.get_data(resource_name)