blob: dbec8d323cea8792a9d21c10499204b8dda10415 [file] [log] [blame]
Brett Cannon6f44d662012-04-15 16:08:47 -04001"""This module provides the components needed to build your own __import__
2function. Undocumented functions are obsolete.
3
4In most cases it is preferred you consider using the importlib module's
5functionality over this module.
6
7"""
8# (Probably) need to stay in _imp
9from _imp import (lock_held, acquire_lock, release_lock, reload,
Brett Cannon2fef4d22012-04-15 19:06:23 -040010 load_dynamic, get_frozen_object, is_frozen_package,
11 init_builtin, init_frozen, is_builtin, is_frozen,
12 _fix_co_filename)
Brett Cannon24117a72012-04-20 18:04:03 -040013# Could move out of _imp, but not worth the code
14from _imp import get_magic
Brett Cannonea59dbf2012-04-20 21:44:46 -040015# Can (probably) move to importlib
Brett Cannona64faf02012-04-21 18:52:52 -040016from _imp import (get_tag, get_suffixes)
Brett Cannon6f44d662012-04-15 16:08:47 -040017# Should be re-implemented here (and mostly deprecated)
Brett Cannon64befe92012-04-17 19:14:26 -040018from _imp import (find_module, NullImporter,
Brett Cannon6f44d662012-04-15 16:08:47 -040019 SEARCH_ERROR, PY_SOURCE, PY_COMPILED, C_EXTENSION,
20 PY_RESOURCE, PKG_DIRECTORY, C_BUILTIN, PY_FROZEN,
21 PY_CODERESOURCE, IMP_HOOK)
22
23from importlib._bootstrap import _new_module as new_module
Brett Cannonea59dbf2012-04-20 21:44:46 -040024from importlib._bootstrap import _cache_from_source as cache_from_source
Brett Cannon01a76172012-04-15 20:25:23 -040025
Brett Cannon2ee61422012-04-15 22:28:28 -040026from importlib import _bootstrap
27import os
28
29
Brett Cannona64faf02012-04-21 18:52:52 -040030def 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 Cannon64befe92012-04-17 19:14:26 -040050class _HackedGetData:
Brett Cannon16475ad2012-04-16 22:11:25 -040051
Brett Cannon64befe92012-04-17 19:14:26 -040052 """Compatibiilty support for 'file' arguments of various load_*()
53 functions."""
Brett Cannon16475ad2012-04-16 22:11:25 -040054
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 Cannon64befe92012-04-17 19:14:26 -040060 """Gross hack to contort loader to deal w/ load_*()'s bad API."""
Brett Cannon578393b2012-04-16 23:11:28 -040061 if self.file and path == self._path:
Brett Cannon16475ad2012-04-16 22:11:25 -040062 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 Cannon64befe92012-04-17 19:14:26 -040073class _LoadSourceCompatibility(_HackedGetData, _bootstrap._SourceFileLoader):
74
75 """Compatibility support for implementing load_source()."""
76
77
Brett Cannona64faf02012-04-21 18:52:52 -040078# XXX deprecate after better API exposed in importlib
Brett Cannon16475ad2012-04-16 22:11:25 -040079def load_source(name, pathname, file=None):
80 return _LoadSourceCompatibility(name, pathname, file).load_module(name)
81
82
Brett Cannon64befe92012-04-17 19:14:26 -040083class _LoadCompiledCompatibility(_HackedGetData,
84 _bootstrap._SourcelessFileLoader):
85
86 """Compatibility support for implementing load_compiled()."""
87
88
Brett Cannona64faf02012-04-21 18:52:52 -040089# XXX deprecate
Brett Cannon64befe92012-04-17 19:14:26 -040090def load_compiled(name, pathname, file=None):
91 return _LoadCompiledCompatibility(name, pathname, file).load_module(name)
92
93
Brett Cannona64faf02012-04-21 18:52:52 -040094# XXX deprecate
Brett Cannon2ee61422012-04-15 22:28:28 -040095def 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 Cannon01a76172012-04-15 20:25:23 -0400107
Brett Cannona64faf02012-04-21 18:52:52 -0400108# XXX deprecate
Brett Cannon01a76172012-04-15 20:25:23 -0400109def 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 Cannonde10bf42012-04-16 20:44:21 -0400116 if mode and (not mode.startswith(('r', 'U')) or '+' in mode):
Brett Cannon01a76172012-04-15 20:25:23 -0400117 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)