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 | 6f44d66 | 2012-04-15 16:08:47 -0400 | [diff] [blame] | 13 | # Can (probably) move to importlib |
| 14 | from _imp import (get_magic, get_tag, get_suffixes, cache_from_source, |
| 15 | source_from_cache) |
| 16 | # Should be re-implemented here (and mostly deprecated) |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame^] | 17 | from _imp import (find_module, NullImporter, |
Brett Cannon | 6f44d66 | 2012-04-15 16:08:47 -0400 | [diff] [blame] | 18 | SEARCH_ERROR, PY_SOURCE, PY_COMPILED, C_EXTENSION, |
| 19 | PY_RESOURCE, PKG_DIRECTORY, C_BUILTIN, PY_FROZEN, |
| 20 | PY_CODERESOURCE, IMP_HOOK) |
| 21 | |
| 22 | from importlib._bootstrap import _new_module as new_module |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 23 | |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 24 | from importlib import _bootstrap |
| 25 | import os |
| 26 | |
| 27 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame^] | 28 | class _HackedGetData: |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 29 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame^] | 30 | """Compatibiilty support for 'file' arguments of various load_*() |
| 31 | functions.""" |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 32 | |
| 33 | def __init__(self, fullname, path, file=None): |
| 34 | super().__init__(fullname, path) |
| 35 | self.file = file |
| 36 | |
| 37 | def get_data(self, path): |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame^] | 38 | """Gross hack to contort loader to deal w/ load_*()'s bad API.""" |
Brett Cannon | 578393b | 2012-04-16 23:11:28 -0400 | [diff] [blame] | 39 | if self.file and path == self._path: |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 40 | with self.file: |
| 41 | # Technically should be returning bytes, but |
| 42 | # SourceLoader.get_code() just passed what is returned to |
| 43 | # compile() which can handle str. And converting to bytes would |
| 44 | # require figuring out the encoding to decode to and |
| 45 | # tokenize.detect_encoding() only accepts bytes. |
| 46 | return self.file.read() |
| 47 | else: |
| 48 | return super().get_data(path) |
| 49 | |
| 50 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame^] | 51 | class _LoadSourceCompatibility(_HackedGetData, _bootstrap._SourceFileLoader): |
| 52 | |
| 53 | """Compatibility support for implementing load_source().""" |
| 54 | |
| 55 | |
Brett Cannon | 16475ad | 2012-04-16 22:11:25 -0400 | [diff] [blame] | 56 | def load_source(name, pathname, file=None): |
| 57 | return _LoadSourceCompatibility(name, pathname, file).load_module(name) |
| 58 | |
| 59 | |
Brett Cannon | 64befe9 | 2012-04-17 19:14:26 -0400 | [diff] [blame^] | 60 | class _LoadCompiledCompatibility(_HackedGetData, |
| 61 | _bootstrap._SourcelessFileLoader): |
| 62 | |
| 63 | """Compatibility support for implementing load_compiled().""" |
| 64 | |
| 65 | |
| 66 | def load_compiled(name, pathname, file=None): |
| 67 | return _LoadCompiledCompatibility(name, pathname, file).load_module(name) |
| 68 | |
| 69 | |
Brett Cannon | 2ee6142 | 2012-04-15 22:28:28 -0400 | [diff] [blame] | 70 | def load_package(name, path): |
| 71 | if os.path.isdir(path): |
| 72 | extensions = _bootstrap._suffix_list(PY_SOURCE) |
| 73 | extensions += _bootstrap._suffix_list(PY_COMPILED) |
| 74 | for extension in extensions: |
| 75 | path = os.path.join(path, '__init__'+extension) |
| 76 | if os.path.exists(path): |
| 77 | break |
| 78 | else: |
| 79 | raise ValueError('{!r} is not a package'.format(path)) |
| 80 | return _bootstrap._SourceFileLoader(name, path).load_module(name) |
| 81 | |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 82 | |
| 83 | def load_module(name, file, filename, details): |
| 84 | """Load a module, given information returned by find_module(). |
| 85 | |
| 86 | The module name must include the full package name, if any. |
| 87 | |
| 88 | """ |
| 89 | suffix, mode, type_ = details |
Brett Cannon | de10bf4 | 2012-04-16 20:44:21 -0400 | [diff] [blame] | 90 | if mode and (not mode.startswith(('r', 'U')) or '+' in mode): |
Brett Cannon | 01a7617 | 2012-04-15 20:25:23 -0400 | [diff] [blame] | 91 | raise ValueError('invalid file open mode {!r}'.format(mode)) |
| 92 | elif file is None and type_ in {PY_SOURCE, PY_COMPILED}: |
| 93 | msg = 'file object required for import (type code {})'.format(type_) |
| 94 | raise ValueError(msg) |
| 95 | elif type_ == PY_SOURCE: |
| 96 | return load_source(name, filename, file) |
| 97 | elif type_ == PY_COMPILED: |
| 98 | return load_compiled(name, filename, file) |
| 99 | elif type_ == PKG_DIRECTORY: |
| 100 | return load_package(name, filename) |
| 101 | elif type_ == C_BUILTIN: |
| 102 | return init_builtin(name) |
| 103 | elif type_ == PY_FROZEN: |
| 104 | return init_frozen(name) |
| 105 | else: |
| 106 | msg = "Don't know how to import {} (type code {}".format(name, type_) |
| 107 | raise ImportError(msg, name=name) |