Brett Cannon | 6f44d66 | 2012-04-15 16:08:47 -0400 | [diff] [blame] | 1 | """This module provides the components needed to build your own __import__ |
| 2 | function. Undocumented functions are obsolete. |
| 3 | |
| 4 | In most cases it is preferred you consider using the importlib module's |
| 5 | functionality over this module. |
| 6 | |
| 7 | """ |
| 8 | # (Probably) need to stay in _imp |
| 9 | from _imp import (lock_held, acquire_lock, release_lock, reload, |
Brett Cannon | 2fef4d2 | 2012-04-15 19:06:23 -0400 | [diff] [blame] | 10 | load_dynamic, get_frozen_object, is_frozen_package, |
| 11 | init_builtin, init_frozen, is_builtin, is_frozen, |
| 12 | _fix_co_filename) |
Brett Cannon | 24117a7 | 2012-04-20 18:04:03 -0400 | [diff] [blame] | 13 | # Could move out of _imp, but not worth the code |
Brett Cannon | 2f92389 | 2012-04-21 18:55:51 -0400 | [diff] [blame^] | 14 | from _imp import get_magic, get_tag |
Brett Cannon | ea59dbf | 2012-04-20 21:44:46 -0400 | [diff] [blame] | 15 | # Can (probably) move to importlib |
Brett Cannon | 2f92389 | 2012-04-21 18:55:51 -0400 | [diff] [blame^] | 16 | from _imp import get_suffixes |
Brett Cannon | 6f44d66 | 2012-04-15 16:08:47 -0400 | [diff] [blame] | 17 | # Should be re-implemented here (and mostly deprecated) |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 18 | from _imp import (find_module, NullImporter, |
Brett Cannon | 6f44d66 | 2012-04-15 16:08:47 -0400 | [diff] [blame] | 19 | SEARCH_ERROR, PY_SOURCE, PY_COMPILED, C_EXTENSION, |
| 20 | PY_RESOURCE, PKG_DIRECTORY, C_BUILTIN, PY_FROZEN, |
| 21 | PY_CODERESOURCE, IMP_HOOK) |
| 22 | |
| 23 | from importlib._bootstrap import _new_module as new_module |
Brett Cannon | ea59dbf | 2012-04-20 21:44:46 -0400 | [diff] [blame] | 24 | from importlib._bootstrap import _cache_from_source as cache_from_source |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 25 | |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 26 | from importlib import _bootstrap |
| 27 | import os |
| 28 | |
| 29 | |
Brett Cannon | a64faf0 | 2012-04-21 18:52:52 -0400 | [diff] [blame] | 30 | def source_from_cache(path): |
| 31 | """Given the path to a .pyc./.pyo file, return the path to its .py file. |
| 32 | |
| 33 | The .pyc/.pyo file does not need to exist; this simply returns the path to |
| 34 | the .py file calculated to correspond to the .pyc/.pyo file. If path does |
| 35 | not conform to PEP 3147 format, ValueError will be raised. |
| 36 | |
| 37 | """ |
| 38 | head, pycache_filename = os.path.split(path) |
| 39 | head, pycache = os.path.split(head) |
| 40 | if pycache != _bootstrap.PYCACHE: |
| 41 | raise ValueError('{} not bottom-level directory in ' |
| 42 | '{!r}'.format(_bootstrap.PYCACHE, path)) |
| 43 | if pycache_filename.count('.') != 2: |
| 44 | raise ValueError('expected only 2 dots in ' |
| 45 | '{!r}'.format(pycache_filename)) |
| 46 | base_filename = pycache_filename.partition('.')[0] |
| 47 | return os.path.join(head, base_filename + _bootstrap.SOURCE_SUFFIXES[0]) |
| 48 | |
| 49 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 50 | class _HackedGetData: |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 51 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 52 | """Compatibiilty support for 'file' arguments of various load_*() |
| 53 | functions.""" |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 54 | |
| 55 | def __init__(self, fullname, path, file=None): |
| 56 | super().__init__(fullname, path) |
| 57 | self.file = file |
| 58 | |
| 59 | def get_data(self, path): |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 60 | """Gross hack to contort loader to deal w/ load_*()'s bad API.""" |
Brett Cannon | 578393b | 2012-04-16 23:11:28 -0400 | [diff] [blame] | 61 | if self.file and path == self._path: |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 62 | with self.file: |
| 63 | # Technically should be returning bytes, but |
| 64 | # SourceLoader.get_code() just passed what is returned to |
| 65 | # compile() which can handle str. And converting to bytes would |
| 66 | # require figuring out the encoding to decode to and |
| 67 | # tokenize.detect_encoding() only accepts bytes. |
| 68 | return self.file.read() |
| 69 | else: |
| 70 | return super().get_data(path) |
| 71 | |
| 72 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 73 | class _LoadSourceCompatibility(_HackedGetData, _bootstrap._SourceFileLoader): |
| 74 | |
| 75 | """Compatibility support for implementing load_source().""" |
| 76 | |
| 77 | |
Brett Cannon | a64faf0 | 2012-04-21 18:52:52 -0400 | [diff] [blame] | 78 | # XXX deprecate after better API exposed in importlib |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 79 | def load_source(name, pathname, file=None): |
| 80 | return _LoadSourceCompatibility(name, pathname, file).load_module(name) |
| 81 | |
| 82 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 83 | class _LoadCompiledCompatibility(_HackedGetData, |
| 84 | _bootstrap._SourcelessFileLoader): |
| 85 | |
| 86 | """Compatibility support for implementing load_compiled().""" |
| 87 | |
| 88 | |
Brett Cannon | a64faf0 | 2012-04-21 18:52:52 -0400 | [diff] [blame] | 89 | # XXX deprecate |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame] | 90 | def load_compiled(name, pathname, file=None): |
| 91 | return _LoadCompiledCompatibility(name, pathname, file).load_module(name) |
| 92 | |
| 93 | |
Brett Cannon | a64faf0 | 2012-04-21 18:52:52 -0400 | [diff] [blame] | 94 | # XXX deprecate |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 95 | def load_package(name, path): |
| 96 | if os.path.isdir(path): |
| 97 | extensions = _bootstrap._suffix_list(PY_SOURCE) |
| 98 | extensions += _bootstrap._suffix_list(PY_COMPILED) |
| 99 | for extension in extensions: |
| 100 | path = os.path.join(path, '__init__'+extension) |
| 101 | if os.path.exists(path): |
| 102 | break |
| 103 | else: |
| 104 | raise ValueError('{!r} is not a package'.format(path)) |
| 105 | return _bootstrap._SourceFileLoader(name, path).load_module(name) |
| 106 | |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 107 | |
Brett Cannon | a64faf0 | 2012-04-21 18:52:52 -0400 | [diff] [blame] | 108 | # XXX deprecate |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 109 | def load_module(name, file, filename, details): |
| 110 | """Load a module, given information returned by find_module(). |
| 111 | |
| 112 | The module name must include the full package name, if any. |
| 113 | |
| 114 | """ |
| 115 | suffix, mode, type_ = details |
Brett Cannon | de10bf4 | 2012-04-16 20:44:21 -0400 | [diff] [blame] | 116 | if mode and (not mode.startswith(('r', 'U')) or '+' in mode): |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 117 | raise ValueError('invalid file open mode {!r}'.format(mode)) |
| 118 | elif file is None and type_ in {PY_SOURCE, PY_COMPILED}: |
| 119 | msg = 'file object required for import (type code {})'.format(type_) |
| 120 | raise ValueError(msg) |
| 121 | elif type_ == PY_SOURCE: |
| 122 | return load_source(name, filename, file) |
| 123 | elif type_ == PY_COMPILED: |
| 124 | return load_compiled(name, filename, file) |
| 125 | elif type_ == PKG_DIRECTORY: |
| 126 | return load_package(name, filename) |
| 127 | elif type_ == C_BUILTIN: |
| 128 | return init_builtin(name) |
| 129 | elif type_ == PY_FROZEN: |
| 130 | return init_frozen(name) |
| 131 | else: |
| 132 | msg = "Don't know how to import {} (type code {}".format(name, type_) |
| 133 | raise ImportError(msg, name=name) |