blob: dc685d0e11b204657435e5e84794affd5314329d [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 Cannon6f44d662012-04-15 16:08:47 -040013# Can (probably) move to importlib
14from _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 Cannon16475ad2012-04-16 22:11:25 -040017from _imp import (find_module, load_compiled, NullImporter,
Brett Cannon6f44d662012-04-15 16:08:47 -040018 SEARCH_ERROR, PY_SOURCE, PY_COMPILED, C_EXTENSION,
19 PY_RESOURCE, PKG_DIRECTORY, C_BUILTIN, PY_FROZEN,
20 PY_CODERESOURCE, IMP_HOOK)
21
22from importlib._bootstrap import _new_module as new_module
Brett Cannon01a76172012-04-15 20:25:23 -040023
Brett Cannon2ee61422012-04-15 22:28:28 -040024from importlib import _bootstrap
25import os
26
27
Brett Cannon16475ad2012-04-16 22:11:25 -040028class _LoadSourceCompatibility(_bootstrap._SourceFileLoader):
29
30 """Compatibility support for implementing load_source()."""
31
32 def __init__(self, fullname, path, file=None):
33 super().__init__(fullname, path)
34 self.file = file
35
36 def get_data(self, path):
37 """Gross hack to contort SourceFileLoader to deal w/ load_source()'s bad
38 API."""
Brett Cannon578393b2012-04-16 23:11:28 -040039 if self.file and path == self._path:
Brett Cannon16475ad2012-04-16 22:11:25 -040040 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
51def load_source(name, pathname, file=None):
52 return _LoadSourceCompatibility(name, pathname, file).load_module(name)
53
54
Brett Cannon2ee61422012-04-15 22:28:28 -040055def load_package(name, path):
56 if os.path.isdir(path):
57 extensions = _bootstrap._suffix_list(PY_SOURCE)
58 extensions += _bootstrap._suffix_list(PY_COMPILED)
59 for extension in extensions:
60 path = os.path.join(path, '__init__'+extension)
61 if os.path.exists(path):
62 break
63 else:
64 raise ValueError('{!r} is not a package'.format(path))
65 return _bootstrap._SourceFileLoader(name, path).load_module(name)
66
Brett Cannon01a76172012-04-15 20:25:23 -040067
68def load_module(name, file, filename, details):
69 """Load a module, given information returned by find_module().
70
71 The module name must include the full package name, if any.
72
73 """
74 suffix, mode, type_ = details
Brett Cannonde10bf42012-04-16 20:44:21 -040075 if mode and (not mode.startswith(('r', 'U')) or '+' in mode):
Brett Cannon01a76172012-04-15 20:25:23 -040076 raise ValueError('invalid file open mode {!r}'.format(mode))
77 elif file is None and type_ in {PY_SOURCE, PY_COMPILED}:
78 msg = 'file object required for import (type code {})'.format(type_)
79 raise ValueError(msg)
80 elif type_ == PY_SOURCE:
81 return load_source(name, filename, file)
82 elif type_ == PY_COMPILED:
83 return load_compiled(name, filename, file)
84 elif type_ == PKG_DIRECTORY:
85 return load_package(name, filename)
86 elif type_ == C_BUILTIN:
87 return init_builtin(name)
88 elif type_ == PY_FROZEN:
89 return init_frozen(name)
90 else:
91 msg = "Don't know how to import {} (type code {}".format(name, type_)
92 raise ImportError(msg, name=name)