PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lib-python/2.7/imputil.py

https://bitbucket.org/kkris/pypy
Python | 725 lines | 636 code | 29 blank | 60 comment | 22 complexity | 5f57c02e206586ab2db2a8bac009f2bf MD5 | raw file
  1. """
  2. Import utilities
  3. Exported classes:
  4. ImportManager Manage the import process
  5. Importer Base class for replacing standard import functions
  6. BuiltinImporter Emulate the import mechanism for builtin and frozen modules
  7. DynLoadSuffixImporter
  8. """
  9. from warnings import warnpy3k
  10. warnpy3k("the imputil module has been removed in Python 3.0", stacklevel=2)
  11. del warnpy3k
  12. # note: avoid importing non-builtin modules
  13. import imp ### not available in Jython?
  14. import sys
  15. import __builtin__
  16. # for the DirectoryImporter
  17. import struct
  18. import marshal
  19. __all__ = ["ImportManager","Importer","BuiltinImporter"]
  20. _StringType = type('')
  21. _ModuleType = type(sys) ### doesn't work in Jython...
  22. class ImportManager:
  23. "Manage the import process."
  24. def install(self, namespace=vars(__builtin__)):
  25. "Install this ImportManager into the specified namespace."
  26. if isinstance(namespace, _ModuleType):
  27. namespace = vars(namespace)
  28. # Note: we have no notion of "chaining"
  29. # Record the previous import hook, then install our own.
  30. self.previous_importer = namespace['__import__']
  31. self.namespace = namespace
  32. namespace['__import__'] = self._import_hook
  33. ### fix this
  34. #namespace['reload'] = self._reload_hook
  35. def uninstall(self):
  36. "Restore the previous import mechanism."
  37. self.namespace['__import__'] = self.previous_importer
  38. def add_suffix(self, suffix, importFunc):
  39. assert hasattr(importFunc, '__call__')
  40. self.fs_imp.add_suffix(suffix, importFunc)
  41. ######################################################################
  42. #
  43. # PRIVATE METHODS
  44. #
  45. clsFilesystemImporter = None
  46. def __init__(self, fs_imp=None):
  47. # we're definitely going to be importing something in the future,
  48. # so let's just load the OS-related facilities.
  49. if not _os_stat:
  50. _os_bootstrap()
  51. # This is the Importer that we use for grabbing stuff from the
  52. # filesystem. It defines one more method (import_from_dir) for our use.
  53. if fs_imp is None:
  54. cls = self.clsFilesystemImporter or _FilesystemImporter
  55. fs_imp = cls()
  56. self.fs_imp = fs_imp
  57. # Initialize the set of suffixes that we recognize and import.
  58. # The default will import dynamic-load modules first, followed by
  59. # .py files (or a .py file's cached bytecode)
  60. for desc in imp.get_suffixes():
  61. if desc[2] == imp.C_EXTENSION:
  62. self.add_suffix(desc[0],
  63. DynLoadSuffixImporter(desc).import_file)
  64. self.add_suffix('.py', py_suffix_importer)
  65. def _import_hook(self, fqname, globals=None, locals=None, fromlist=None):
  66. """Python calls this hook to locate and import a module."""
  67. parts = fqname.split('.')
  68. # determine the context of this import
  69. parent = self._determine_import_context(globals)
  70. # if there is a parent, then its importer should manage this import
  71. if parent:
  72. module = parent.__importer__._do_import(parent, parts, fromlist)
  73. if module:
  74. return module
  75. # has the top module already been imported?
  76. try:
  77. top_module = sys.modules[parts[0]]
  78. except KeyError:
  79. # look for the topmost module
  80. top_module = self._import_top_module(parts[0])
  81. if not top_module:
  82. # the topmost module wasn't found at all.
  83. raise ImportError, 'No module named ' + fqname
  84. # fast-path simple imports
  85. if len(parts) == 1:
  86. if not fromlist:
  87. return top_module
  88. if not top_module.__dict__.get('__ispkg__'):
  89. # __ispkg__ isn't defined (the module was not imported by us),
  90. # or it is zero.
  91. #
  92. # In the former case, there is no way that we could import
  93. # sub-modules that occur in the fromlist (but we can't raise an
  94. # error because it may just be names) because we don't know how
  95. # to deal with packages that were imported by other systems.
  96. #
  97. # In the latter case (__ispkg__ == 0), there can't be any sub-
  98. # modules present, so we can just return.
  99. #
  100. # In both cases, since len(parts) == 1, the top_module is also
  101. # the "bottom" which is the defined return when a fromlist
  102. # exists.
  103. return top_module
  104. importer = top_module.__dict__.get('__importer__')
  105. if importer:
  106. return importer._finish_import(top_module, parts[1:], fromlist)
  107. # Grrr, some people "import os.path" or do "from os.path import ..."
  108. if len(parts) == 2 and hasattr(top_module, parts[1]):
  109. if fromlist:
  110. return getattr(top_module, parts[1])
  111. else:
  112. return top_module
  113. # If the importer does not exist, then we have to bail. A missing
  114. # importer means that something else imported the module, and we have
  115. # no knowledge of how to get sub-modules out of the thing.
  116. raise ImportError, 'No module named ' + fqname
  117. def _determine_import_context(self, globals):
  118. """Returns the context in which a module should be imported.
  119. The context could be a loaded (package) module and the imported module
  120. will be looked for within that package. The context could also be None,
  121. meaning there is no context -- the module should be looked for as a
  122. "top-level" module.
  123. """
  124. if not globals or not globals.get('__importer__'):
  125. # globals does not refer to one of our modules or packages. That
  126. # implies there is no relative import context (as far as we are
  127. # concerned), and it should just pick it off the standard path.
  128. return None
  129. # The globals refer to a module or package of ours. It will define
  130. # the context of the new import. Get the module/package fqname.
  131. parent_fqname = globals['__name__']
  132. # if a package is performing the import, then return itself (imports
  133. # refer to pkg contents)
  134. if globals['__ispkg__']:
  135. parent = sys.modules[parent_fqname]
  136. assert globals is parent.__dict__
  137. return parent
  138. i = parent_fqname.rfind('.')
  139. # a module outside of a package has no particular import context
  140. if i == -1:
  141. return None
  142. # if a module in a package is performing the import, then return the
  143. # package (imports refer to siblings)
  144. parent_fqname = parent_fqname[:i]
  145. parent = sys.modules[parent_fqname]
  146. assert parent.__name__ == parent_fqname
  147. return parent
  148. def _import_top_module(self, name):
  149. # scan sys.path looking for a location in the filesystem that contains
  150. # the module, or an Importer object that can import the module.
  151. for item in sys.path:
  152. if isinstance(item, _StringType):
  153. module = self.fs_imp.import_from_dir(item, name)
  154. else:
  155. module = item.import_top(name)
  156. if module:
  157. return module
  158. return None
  159. def _reload_hook(self, module):
  160. "Python calls this hook to reload a module."
  161. # reloading of a module may or may not be possible (depending on the
  162. # importer), but at least we can validate that it's ours to reload
  163. importer = module.__dict__.get('__importer__')
  164. if not importer:
  165. ### oops. now what...
  166. pass
  167. # okay. it is using the imputil system, and we must delegate it, but
  168. # we don't know what to do (yet)
  169. ### we should blast the module dict and do another get_code(). need to
  170. ### flesh this out and add proper docco...
  171. raise SystemError, "reload not yet implemented"
  172. class Importer:
  173. "Base class for replacing standard import functions."
  174. def import_top(self, name):
  175. "Import a top-level module."
  176. return self._import_one(None, name, name)
  177. ######################################################################
  178. #
  179. # PRIVATE METHODS
  180. #
  181. def _finish_import(self, top, parts, fromlist):
  182. # if "a.b.c" was provided, then load the ".b.c" portion down from
  183. # below the top-level module.
  184. bottom = self._load_tail(top, parts)
  185. # if the form is "import a.b.c", then return "a"
  186. if not fromlist:
  187. # no fromlist: return the top of the import tree
  188. return top
  189. # the top module was imported by self.
  190. #
  191. # this means that the bottom module was also imported by self (just
  192. # now, or in the past and we fetched it from sys.modules).
  193. #
  194. # since we imported/handled the bottom module, this means that we can
  195. # also handle its fromlist (and reliably use __ispkg__).
  196. # if the bottom node is a package, then (potentially) import some
  197. # modules.
  198. #
  199. # note: if it is not a package, then "fromlist" refers to names in
  200. # the bottom module rather than modules.
  201. # note: for a mix of names and modules in the fromlist, we will
  202. # import all modules and insert those into the namespace of
  203. # the package module. Python will pick up all fromlist names
  204. # from the bottom (package) module; some will be modules that
  205. # we imported and stored in the namespace, others are expected
  206. # to be present already.
  207. if bottom.__ispkg__:
  208. self._import_fromlist(bottom, fromlist)
  209. # if the form is "from a.b import c, d" then return "b"
  210. return bottom
  211. def _import_one(self, parent, modname, fqname):
  212. "Import a single module."
  213. # has the module already been imported?
  214. try:
  215. return sys.modules[fqname]
  216. except KeyError:
  217. pass
  218. # load the module's code, or fetch the module itself
  219. result = self.get_code(parent, modname, fqname)
  220. if result is None:
  221. return None
  222. module = self._process_result(result, fqname)
  223. # insert the module into its parent
  224. if parent:
  225. setattr(parent, modname, module)
  226. return module
  227. def _process_result(self, result, fqname):
  228. ispkg, code, values = result
  229. # did get_code() return an actual module? (rather than a code object)
  230. is_module = isinstance(code, _ModuleType)
  231. # use the returned module, or create a new one to exec code into
  232. if is_module:
  233. module = code
  234. else:
  235. module = imp.new_module(fqname)
  236. ### record packages a bit differently??
  237. module.__importer__ = self
  238. module.__ispkg__ = ispkg
  239. # insert additional values into the module (before executing the code)
  240. module.__dict__.update(values)
  241. # the module is almost ready... make it visible
  242. sys.modules[fqname] = module
  243. # execute the code within the module's namespace
  244. if not is_module:
  245. try:
  246. exec code in module.__dict__
  247. except:
  248. if fqname in sys.modules:
  249. del sys.modules[fqname]
  250. raise
  251. # fetch from sys.modules instead of returning module directly.
  252. # also make module's __name__ agree with fqname, in case
  253. # the "exec code in module.__dict__" played games on us.
  254. module = sys.modules[fqname]
  255. module.__name__ = fqname
  256. return module
  257. def _load_tail(self, m, parts):
  258. """Import the rest of the modules, down from the top-level module.
  259. Returns the last module in the dotted list of modules.
  260. """
  261. for part in parts:
  262. fqname = "%s.%s" % (m.__name__, part)
  263. m = self._import_one(m, part, fqname)
  264. if not m:
  265. raise ImportError, "No module named " + fqname
  266. return m
  267. def _import_fromlist(self, package, fromlist):
  268. 'Import any sub-modules in the "from" list.'
  269. # if '*' is present in the fromlist, then look for the '__all__'
  270. # variable to find additional items (modules) to import.
  271. if '*' in fromlist:
  272. fromlist = list(fromlist) + \
  273. list(package.__dict__.get('__all__', []))
  274. for sub in fromlist:
  275. # if the name is already present, then don't try to import it (it
  276. # might not be a module!).
  277. if sub != '*' and not hasattr(package, sub):
  278. subname = "%s.%s" % (package.__name__, sub)
  279. submod = self._import_one(package, sub, subname)
  280. if not submod:
  281. raise ImportError, "cannot import name " + subname
  282. def _do_import(self, parent, parts, fromlist):
  283. """Attempt to import the module relative to parent.
  284. This method is used when the import context specifies that <self>
  285. imported the parent module.
  286. """
  287. top_name = parts[0]
  288. top_fqname = parent.__name__ + '.' + top_name
  289. top_module = self._import_one(parent, top_name, top_fqname)
  290. if not top_module:
  291. # this importer and parent could not find the module (relatively)
  292. return None
  293. return self._finish_import(top_module, parts[1:], fromlist)
  294. ######################################################################
  295. #
  296. # METHODS TO OVERRIDE
  297. #
  298. def get_code(self, parent, modname, fqname):
  299. """Find and retrieve the code for the given module.
  300. parent specifies a parent module to define a context for importing. It
  301. may be None, indicating no particular context for the search.
  302. modname specifies a single module (not dotted) within the parent.
  303. fqname specifies the fully-qualified module name. This is a
  304. (potentially) dotted name from the "root" of the module namespace
  305. down to the modname.
  306. If there is no parent, then modname==fqname.
  307. This method should return None, or a 3-tuple.
  308. * If the module was not found, then None should be returned.
  309. * The first item of the 2- or 3-tuple should be the integer 0 or 1,
  310. specifying whether the module that was found is a package or not.
  311. * The second item is the code object for the module (it will be
  312. executed within the new module's namespace). This item can also
  313. be a fully-loaded module object (e.g. loaded from a shared lib).
  314. * The third item is a dictionary of name/value pairs that will be
  315. inserted into new module before the code object is executed. This
  316. is provided in case the module's code expects certain values (such
  317. as where the module was found). When the second item is a module
  318. object, then these names/values will be inserted *after* the module
  319. has been loaded/initialized.
  320. """
  321. raise RuntimeError, "get_code not implemented"
  322. ######################################################################
  323. #
  324. # Some handy stuff for the Importers
  325. #
  326. # byte-compiled file suffix character
  327. _suffix_char = __debug__ and 'c' or 'o'
  328. # byte-compiled file suffix
  329. _suffix = '.py' + _suffix_char
  330. def _compile(pathname, timestamp):
  331. """Compile (and cache) a Python source file.
  332. The file specified by <pathname> is compiled to a code object and
  333. returned.
  334. Presuming the appropriate privileges exist, the bytecodes will be
  335. saved back to the filesystem for future imports. The source file's
  336. modification timestamp must be provided as a Long value.
  337. """
  338. codestring = open(pathname, 'rU').read()
  339. if codestring and codestring[-1] != '\n':
  340. codestring = codestring + '\n'
  341. code = __builtin__.compile(codestring, pathname, 'exec')
  342. # try to cache the compiled code
  343. try:
  344. f = open(pathname + _suffix_char, 'wb')
  345. except IOError:
  346. pass
  347. else:
  348. f.write('\0\0\0\0')
  349. f.write(struct.pack('<I', timestamp))
  350. marshal.dump(code, f)
  351. f.flush()
  352. f.seek(0, 0)
  353. f.write(imp.get_magic())
  354. f.close()
  355. return code
  356. _os_stat = _os_path_join = None
  357. def _os_bootstrap():
  358. "Set up 'os' module replacement functions for use during import bootstrap."
  359. names = sys.builtin_module_names
  360. join = None
  361. if 'posix' in names:
  362. sep = '/'
  363. from posix import stat
  364. elif 'nt' in names:
  365. sep = '\\'
  366. from nt import stat
  367. elif 'dos' in names:
  368. sep = '\\'
  369. from dos import stat
  370. elif 'os2' in names:
  371. sep = '\\'
  372. from os2 import stat
  373. else:
  374. raise ImportError, 'no os specific module found'
  375. if join is None:
  376. def join(a, b, sep=sep):
  377. if a == '':
  378. return b
  379. lastchar = a[-1:]
  380. if lastchar == '/' or lastchar == sep:
  381. return a + b
  382. return a + sep + b
  383. global _os_stat
  384. _os_stat = stat
  385. global _os_path_join
  386. _os_path_join = join
  387. def _os_path_isdir(pathname):
  388. "Local replacement for os.path.isdir()."
  389. try:
  390. s = _os_stat(pathname)
  391. except OSError:
  392. return None
  393. return (s.st_mode & 0170000) == 0040000
  394. def _timestamp(pathname):
  395. "Return the file modification time as a Long."
  396. try:
  397. s = _os_stat(pathname)
  398. except OSError:
  399. return None
  400. return long(s.st_mtime)
  401. ######################################################################
  402. #
  403. # Emulate the import mechanism for builtin and frozen modules
  404. #
  405. class BuiltinImporter(Importer):
  406. def get_code(self, parent, modname, fqname):
  407. if parent:
  408. # these modules definitely do not occur within a package context
  409. return None
  410. # look for the module
  411. if imp.is_builtin(modname):
  412. type = imp.C_BUILTIN
  413. elif imp.is_frozen(modname):
  414. type = imp.PY_FROZEN
  415. else:
  416. # not found
  417. return None
  418. # got it. now load and return it.
  419. module = imp.load_module(modname, None, modname, ('', '', type))
  420. return 0, module, { }
  421. ######################################################################
  422. #
  423. # Internal importer used for importing from the filesystem
  424. #
  425. class _FilesystemImporter(Importer):
  426. def __init__(self):
  427. self.suffixes = [ ]
  428. def add_suffix(self, suffix, importFunc):
  429. assert hasattr(importFunc, '__call__')
  430. self.suffixes.append((suffix, importFunc))
  431. def import_from_dir(self, dir, fqname):
  432. result = self._import_pathname(_os_path_join(dir, fqname), fqname)
  433. if result:
  434. return self._process_result(result, fqname)
  435. return None
  436. def get_code(self, parent, modname, fqname):
  437. # This importer is never used with an empty parent. Its existence is
  438. # private to the ImportManager. The ImportManager uses the
  439. # import_from_dir() method to import top-level modules/packages.
  440. # This method is only used when we look for a module within a package.
  441. assert parent
  442. for submodule_path in parent.__path__:
  443. code = self._import_pathname(_os_path_join(submodule_path, modname), fqname)
  444. if code is not None:
  445. return code
  446. return self._import_pathname(_os_path_join(parent.__pkgdir__, modname),
  447. fqname)
  448. def _import_pathname(self, pathname, fqname):
  449. if _os_path_isdir(pathname):
  450. result = self._import_pathname(_os_path_join(pathname, '__init__'),
  451. fqname)
  452. if result:
  453. values = result[2]
  454. values['__pkgdir__'] = pathname
  455. values['__path__'] = [ pathname ]
  456. return 1, result[1], values
  457. return None
  458. for suffix, importFunc in self.suffixes:
  459. filename = pathname + suffix
  460. try:
  461. finfo = _os_stat(filename)
  462. except OSError:
  463. pass
  464. else:
  465. return importFunc(filename, finfo, fqname)
  466. return None
  467. ######################################################################
  468. #
  469. # SUFFIX-BASED IMPORTERS
  470. #
  471. def py_suffix_importer(filename, finfo, fqname):
  472. file = filename[:-3] + _suffix
  473. t_py = long(finfo[8])
  474. t_pyc = _timestamp(file)
  475. code = None
  476. if t_pyc is not None and t_pyc >= t_py:
  477. f = open(file, 'rb')
  478. if f.read(4) == imp.get_magic():
  479. t = struct.unpack('<I', f.read(4))[0]
  480. if t == t_py:
  481. code = marshal.load(f)
  482. f.close()
  483. if code is None:
  484. file = filename
  485. code = _compile(file, t_py)
  486. return 0, code, { '__file__' : file }
  487. class DynLoadSuffixImporter:
  488. def __init__(self, desc):
  489. self.desc = desc
  490. def import_file(self, filename, finfo, fqname):
  491. fp = open(filename, self.desc[1])
  492. module = imp.load_module(fqname, fp, filename, self.desc)
  493. module.__file__ = filename
  494. return 0, module, { }
  495. ######################################################################
  496. def _print_importers():
  497. items = sys.modules.items()
  498. items.sort()
  499. for name, module in items:
  500. if module:
  501. print name, module.__dict__.get('__importer__', '-- no importer')
  502. else:
  503. print name, '-- non-existent module'
  504. def _test_revamp():
  505. ImportManager().install()
  506. sys.path.insert(0, BuiltinImporter())
  507. ######################################################################
  508. #
  509. # TODO
  510. #
  511. # from Finn Bock:
  512. # type(sys) is not a module in Jython. what to use instead?
  513. # imp.C_EXTENSION is not in Jython. same for get_suffixes and new_module
  514. #
  515. # given foo.py of:
  516. # import sys
  517. # sys.modules['foo'] = sys
  518. #
  519. # ---- standard import mechanism
  520. # >>> import foo
  521. # >>> foo
  522. # <module 'sys' (built-in)>
  523. #
  524. # ---- revamped import mechanism
  525. # >>> import imputil
  526. # >>> imputil._test_revamp()
  527. # >>> import foo
  528. # >>> foo
  529. # <module 'foo' from 'foo.py'>
  530. #
  531. #
  532. # from MAL:
  533. # should BuiltinImporter exist in sys.path or hard-wired in ImportManager?
  534. # need __path__ processing
  535. # performance
  536. # move chaining to a subclass [gjs: it's been nuked]
  537. # deinstall should be possible
  538. # query mechanism needed: is a specific Importer installed?
  539. # py/pyc/pyo piping hooks to filter/process these files
  540. # wish list:
  541. # distutils importer hooked to list of standard Internet repositories
  542. # module->file location mapper to speed FS-based imports
  543. # relative imports
  544. # keep chaining so that it can play nice with other import hooks
  545. #
  546. # from Gordon:
  547. # push MAL's mapper into sys.path[0] as a cache (hard-coded for apps)
  548. #
  549. # from Guido:
  550. # need to change sys.* references for rexec environs
  551. # need hook for MAL's walk-me-up import strategy, or Tim's absolute strategy
  552. # watch out for sys.modules[...] is None
  553. # flag to force absolute imports? (speeds _determine_import_context and
  554. # checking for a relative module)
  555. # insert names of archives into sys.path (see quote below)
  556. # note: reload does NOT blast module dict
  557. # shift import mechanisms and policies around; provide for hooks, overrides
  558. # (see quote below)
  559. # add get_source stuff
  560. # get_topcode and get_subcode
  561. # CRLF handling in _compile
  562. # race condition in _compile
  563. # refactoring of os.py to deal with _os_bootstrap problem
  564. # any special handling to do for importing a module with a SyntaxError?
  565. # (e.g. clean up the traceback)
  566. # implement "domain" for path-type functionality using pkg namespace
  567. # (rather than FS-names like __path__)
  568. # don't use the word "private"... maybe "internal"
  569. #
  570. #
  571. # Guido's comments on sys.path caching:
  572. #
  573. # We could cache this in a dictionary: the ImportManager can have a
  574. # cache dict mapping pathnames to importer objects, and a separate
  575. # method for coming up with an importer given a pathname that's not yet
  576. # in the cache. The method should do a stat and/or look at the
  577. # extension to decide which importer class to use; you can register new
  578. # importer classes by registering a suffix or a Boolean function, plus a
  579. # class. If you register a new importer class, the cache is zapped.
  580. # The cache is independent from sys.path (but maintained per
  581. # ImportManager instance) so that rearrangements of sys.path do the
  582. # right thing. If a path is dropped from sys.path the corresponding
  583. # cache entry is simply no longer used.
  584. #
  585. # My/Guido's comments on factoring ImportManager and Importer:
  586. #
  587. # > However, we still have a tension occurring here:
  588. # >
  589. # > 1) implementing policy in ImportManager assists in single-point policy
  590. # > changes for app/rexec situations
  591. # > 2) implementing policy in Importer assists in package-private policy
  592. # > changes for normal, operating conditions
  593. # >
  594. # > I'll see if I can sort out a way to do this. Maybe the Importer class will
  595. # > implement the methods (which can be overridden to change policy) by
  596. # > delegating to ImportManager.
  597. #
  598. # Maybe also think about what kind of policies an Importer would be
  599. # likely to want to change. I have a feeling that a lot of the code
  600. # there is actually not so much policy but a *necessity* to get things
  601. # working given the calling conventions for the __import__ hook: whether
  602. # to return the head or tail of a dotted name, or when to do the "finish
  603. # fromlist" stuff.
  604. #