annotate Lib/importlib/_bootstrap.py @ 62799:dcc2e8bf64cf

Fix: Issue9091 Minor documentation clarification.
author Senthil Kumaran <orsenthil@gmail.com>
date Mon, 05 Jul 2010 11:41:42 +0000
parents 32682b253ffc
children cf399de12b54
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1 """Core implementation of import.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
2
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
3 This module is NOT meant to be directly imported! It has been designed such
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
4 that it can be bootstrapped into Python as the implementation of import. As
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
5 such it requires the injection of specific modules and attributes in order to
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
6 work. One should use importlib as the public-facing version of this module.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
7
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
8 """
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
9
51995
e37bb30255f3 Move importlib over to _io.
Brett Cannon <bcannon@gmail.com>
parents: 51991
diff changeset
10 # Injected modules are '_warnings', 'imp', 'sys', 'marshal', 'errno', '_io',
e37bb30255f3 Move importlib over to _io.
Brett Cannon <bcannon@gmail.com>
parents: 51991
diff changeset
11 # and '_os' (a.k.a. 'posix', 'nt' or 'os2').
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
12 # Injected attribute is path_sep.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
13 #
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
14 # When editing this code be aware that code executed at import time CANNOT
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
15 # reference any injected objects! This includes not only global code but also
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
16 # anything specified at the class level.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
17
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
18
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
19 # Bootstrap-related code ######################################################
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
20
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
21 # XXX Could also expose Modules/getpath.c:joinpath()
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
22 def _path_join(*args):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
23 """Replacement for os.path.join."""
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
24 return path_sep.join(x[:-len(path_sep)] if x.endswith(path_sep) else x
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
25 for x in args if x)
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
26
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
27
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
28 def _path_exists(path):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
29 """Replacement for os.path.exists."""
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
30 try:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
31 _os.stat(path)
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
32 except OSError:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
33 return False
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
34 else:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
35 return True
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
36
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
37
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
38 def _path_is_mode_type(path, mode):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
39 """Test whether the path is the specified mode type."""
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
40 try:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
41 stat_info = _os.stat(path)
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
42 except OSError:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
43 return False
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
44 return (stat_info.st_mode & 0o170000) == mode
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
45
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
46
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
47 # XXX Could also expose Modules/getpath.c:isfile()
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
48 def _path_isfile(path):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
49 """Replacement for os.path.isfile."""
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
50 return _path_is_mode_type(path, 0o100000)
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
51
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
52
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
53 # XXX Could also expose Modules/getpath.c:isdir()
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
54 def _path_isdir(path):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
55 """Replacement for os.path.isdir."""
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
56 if not path:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
57 path = _os.getcwd()
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
58 return _path_is_mode_type(path, 0o040000)
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
59
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
60
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
61 def _path_without_ext(path, ext_type):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
62 """Replacement for os.path.splitext()[0]."""
52139
2e8e897a1244 Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents: 52115
diff changeset
63 for suffix in _suffix_list(ext_type):
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
64 if path.endswith(suffix):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
65 return path[:-len(suffix)]
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
66 else:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
67 raise ValueError("path is not of the specified type")
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
68
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
69
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
70 def _path_absolute(path):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
71 """Replacement for os.path.abspath."""
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
72 if not path:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
73 path = _os.getcwd()
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
74 try:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
75 return _os._getfullpathname(path)
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
76 except AttributeError:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
77 if path.startswith('/'):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
78 return path
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
79 else:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
80 return _path_join(_os.getcwd(), path)
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
81
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
82
52139
2e8e897a1244 Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents: 52115
diff changeset
83 class _closing:
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
84
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
85 """Simple replacement for contextlib.closing."""
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
86
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
87 def __init__(self, obj):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
88 self.obj = obj
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
89
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
90 def __enter__(self):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
91 return self.obj
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
92
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
93 def __exit__(self, *args):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
94 self.obj.close()
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
95
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
96
52139
2e8e897a1244 Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents: 52115
diff changeset
97 def _wrap(new, old):
51433
43d3a95ed662 Create a simple substitute for functools.wraps to use in importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 51430
diff changeset
98 """Simple substitute for functools.wraps."""
43d3a95ed662 Create a simple substitute for functools.wraps to use in importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 51430
diff changeset
99 for replace in ['__module__', '__name__', '__doc__']:
43d3a95ed662 Create a simple substitute for functools.wraps to use in importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 51430
diff changeset
100 setattr(new, replace, getattr(old, replace))
43d3a95ed662 Create a simple substitute for functools.wraps to use in importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 51430
diff changeset
101 new.__dict__.update(old.__dict__)
43d3a95ed662 Create a simple substitute for functools.wraps to use in importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 51430
diff changeset
102
43d3a95ed662 Create a simple substitute for functools.wraps to use in importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 51430
diff changeset
103
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
104 code_type = type(_wrap.__code__)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
105
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
106 # Finder/loader utility code ##################################################
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
107
52002
76a579b6bade Rename importlib.util.set___package__ to set_package.
Brett Cannon <bcannon@gmail.com>
parents: 51995
diff changeset
108 def set_package(fxn):
51430
5cdc5f0c134c Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents: 51429
diff changeset
109 """Set __package__ on the returned module."""
5cdc5f0c134c Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents: 51429
diff changeset
110 def wrapper(*args, **kwargs):
5cdc5f0c134c Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents: 51429
diff changeset
111 module = fxn(*args, **kwargs)
5cdc5f0c134c Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents: 51429
diff changeset
112 if not hasattr(module, '__package__') or module.__package__ is None:
5cdc5f0c134c Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents: 51429
diff changeset
113 module.__package__ = module.__name__
5cdc5f0c134c Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents: 51429
diff changeset
114 if not hasattr(module, '__path__'):
5cdc5f0c134c Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents: 51429
diff changeset
115 module.__package__ = module.__package__.rpartition('.')[0]
5cdc5f0c134c Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents: 51429
diff changeset
116 return module
52139
2e8e897a1244 Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents: 52115
diff changeset
117 _wrap(wrapper, fxn)
51430
5cdc5f0c134c Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents: 51429
diff changeset
118 return wrapper
5cdc5f0c134c Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents: 51429
diff changeset
119
5cdc5f0c134c Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents: 51429
diff changeset
120
52115
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
121 def set_loader(fxn):
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
122 """Set __loader__ on the returned module."""
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
123 def wrapper(self, *args, **kwargs):
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
124 module = fxn(self, *args, **kwargs)
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
125 if not hasattr(module, '__loader__'):
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
126 module.__loader__ = self
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
127 return module
52139
2e8e897a1244 Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents: 52115
diff changeset
128 _wrap(wrapper, fxn)
52115
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
129 return wrapper
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
130
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
131
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
132 def module_for_loader(fxn):
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
133 """Decorator to handle selecting the proper module for loaders.
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
134
52164
0b19ba318071 Clean up docstring from importlib.util.module_for_loader.
Brett Cannon <bcannon@gmail.com>
parents: 52142
diff changeset
135 The decorated function is passed the module to use instead of the module
0b19ba318071 Clean up docstring from importlib.util.module_for_loader.
Brett Cannon <bcannon@gmail.com>
parents: 52142
diff changeset
136 name. The module passed in to the function is either from sys.modules if
0b19ba318071 Clean up docstring from importlib.util.module_for_loader.
Brett Cannon <bcannon@gmail.com>
parents: 52142
diff changeset
137 it already exists or is a new module which has __name__ set and is inserted
0b19ba318071 Clean up docstring from importlib.util.module_for_loader.
Brett Cannon <bcannon@gmail.com>
parents: 52142
diff changeset
138 into sys.modules. If an exception is raised and the decorator created the
0b19ba318071 Clean up docstring from importlib.util.module_for_loader.
Brett Cannon <bcannon@gmail.com>
parents: 52142
diff changeset
139 module it is subsequently removed from sys.modules.
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
140
52164
0b19ba318071 Clean up docstring from importlib.util.module_for_loader.
Brett Cannon <bcannon@gmail.com>
parents: 52142
diff changeset
141 The decorator assumes that the decorated function takes the module name as
0b19ba318071 Clean up docstring from importlib.util.module_for_loader.
Brett Cannon <bcannon@gmail.com>
parents: 52142
diff changeset
142 the second argument.
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
143
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
144 """
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
145 def decorated(self, fullname, *args, **kwargs):
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
146 module = sys.modules.get(fullname)
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
147 is_reload = bool(module)
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
148 if not is_reload:
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
149 # This must be done before open() is called as the 'io' module
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
150 # implicitly imports 'locale' and would otherwise trigger an
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
151 # infinite loop.
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
152 module = imp.new_module(fullname)
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
153 sys.modules[fullname] = module
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
154 try:
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
155 return fxn(self, module, *args, **kwargs)
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
156 except:
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
157 if not is_reload:
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
158 del sys.modules[fullname]
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
159 raise
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
160 _wrap(decorated, fxn)
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
161 return decorated
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
162
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
163
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
164 def _check_name(method):
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
165 """Decorator to verify that the module being requested matches the one the
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
166 loader can handle.
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
167
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
168 The first argument (self) must define _name which the second argument is
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
169 comapred against. If the comparison fails then ImportError is raised.
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
170
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
171 """
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
172 def inner(self, name, *args, **kwargs):
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
173 if self._name != name:
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
174 raise ImportError("loader cannot handle %s" % name)
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
175 return method(self, name, *args, **kwargs)
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
176 _wrap(inner, method)
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
177 return inner
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
178
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
179
52166
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
180 def _requires_builtin(fxn):
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
181 """Decorator to verify the named module is built-in."""
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
182 def wrapper(self, fullname):
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
183 if fullname not in sys.builtin_module_names:
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
184 raise ImportError("{0} is not a built-in module".format(fullname))
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
185 return fxn(self, fullname)
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
186 _wrap(wrapper, fxn)
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
187 return wrapper
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
188
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
189
52168
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
190 def _requires_frozen(fxn):
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
191 """Decorator to verify the named module is frozen."""
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
192 def wrapper(self, fullname):
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
193 if not imp.is_frozen(fullname):
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
194 raise ImportError("{0} is not a frozen module".format(fullname))
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
195 return fxn(self, fullname)
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
196 _wrap(wrapper, fxn)
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
197 return wrapper
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
198
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
199
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
200 def _suffix_list(suffix_type):
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
201 """Return a list of file suffixes based on the imp file type."""
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
202 return [suffix[0] for suffix in imp.get_suffixes()
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
203 if suffix[2] == suffix_type]
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
204
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
205
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
206 # Loaders #####################################################################
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
207
50983
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
208 class BuiltinImporter:
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
209
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52164
diff changeset
210 """Meta path import for built-in modules.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
211
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52164
diff changeset
212 All methods are either class or static methods to avoid the need to
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52164
diff changeset
213 instantiate the class.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
214
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
215 """
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
216
50983
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
217 @classmethod
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
218 def find_module(cls, fullname, path=None):
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52164
diff changeset
219 """Find the built-in module.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
220
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
221 If 'path' is ever specified then the search is considered a failure.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
222
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
223 """
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
224 if path is not None:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
225 return None
50983
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
226 return cls if imp.is_builtin(fullname) else None
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
227
51026
c26f1af26bfc Document both importlib.machinery.BuiltinImporter and FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 50983
diff changeset
228 @classmethod
52002
76a579b6bade Rename importlib.util.set___package__ to set_package.
Brett Cannon <bcannon@gmail.com>
parents: 51995
diff changeset
229 @set_package
52115
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
230 @set_loader
52166
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
231 @_requires_builtin
51026
c26f1af26bfc Document both importlib.machinery.BuiltinImporter and FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 50983
diff changeset
232 def load_module(cls, fullname):
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
233 """Load a built-in module."""
51681
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
234 is_reload = fullname in sys.modules
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
235 try:
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
236 return imp.init_builtin(fullname)
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
237 except:
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
238 if not is_reload and fullname in sys.modules:
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
239 del sys.modules[fullname]
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
240 raise
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
241
52166
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
242 @classmethod
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
243 @_requires_builtin
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
244 def get_code(cls, fullname):
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
245 """Return None as built-in modules do not have code objects."""
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
246 return None
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
247
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
248 @classmethod
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
249 @_requires_builtin
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
250 def get_source(cls, fullname):
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
251 """Return None as built-in modules do not have source code."""
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
252 return None
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
253
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
254 @classmethod
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
255 @_requires_builtin
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
256 def is_package(cls, fullname):
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
257 """Return None as built-in module are never packages."""
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
258 return False
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
259
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
260
50983
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
261 class FrozenImporter:
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
262
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52164
diff changeset
263 """Meta path import for frozen modules.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
264
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52164
diff changeset
265 All methods are either class or static methods to avoid the need to
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52164
diff changeset
266 instantiate the class.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
267
50983
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
268 """
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
269
50983
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
270 @classmethod
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
271 def find_module(cls, fullname, path=None):
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
272 """Find a frozen module."""
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
273 return cls if imp.is_frozen(fullname) else None
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
274
50983
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
275 @classmethod
52002
76a579b6bade Rename importlib.util.set___package__ to set_package.
Brett Cannon <bcannon@gmail.com>
parents: 51995
diff changeset
276 @set_package
52115
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
277 @set_loader
52168
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
278 @_requires_frozen
50983
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
279 def load_module(cls, fullname):
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
280 """Load a frozen module."""
51681
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
281 is_reload = fullname in sys.modules
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
282 try:
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
283 return imp.init_frozen(fullname)
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
284 except:
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
285 if not is_reload and fullname in sys.modules:
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
286 del sys.modules[fullname]
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
287 raise
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
288
52168
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
289 @classmethod
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
290 @_requires_frozen
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
291 def get_code(cls, fullname):
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
292 """Return the code object for the frozen module."""
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
293 return imp.get_frozen_object(fullname)
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
294
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
295 @classmethod
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
296 @_requires_frozen
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
297 def get_source(cls, fullname):
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
298 """Return None as frozen modules do not have source code."""
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
299 return None
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
300
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
301 @classmethod
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
302 @_requires_frozen
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
303 def is_package(cls, fullname):
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
304 """Return if the frozen module is a package."""
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
305 return imp.is_frozen_package(fullname)
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
306
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
307
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
308 class _LoaderBasics:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
309
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
310 """Base class of common code needed by both SourceLoader and
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
311 _SourcelessFileLoader."""
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
312
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
313 def is_package(self, fullname):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
314 """Concrete implementation of InspectLoader.is_package by checking if
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
315 the path returned by get_filename has a filename of '__init__.py'."""
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
316 filename = self.get_filename(fullname).rpartition(path_sep)[2]
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
317 return filename.rsplit('.', 1)[0] == '__init__'
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
318
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
319 def _bytes_from_bytecode(self, fullname, data, source_mtime):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
320 """Return the marshalled bytes from bytecode, verifying the magic
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
321 number and timestamp alon the way.
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
322
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
323 If source_mtime is None then skip the timestamp check.
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
324
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
325 """
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
326 magic = data[:4]
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
327 raw_timestamp = data[4:8]
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
328 if len(magic) != 4 or magic != imp.get_magic():
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
329 raise ImportError("bad magic number in {}".format(fullname))
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
330 elif len(raw_timestamp) != 4:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
331 raise EOFError("bad timestamp in {}".format(fullname))
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
332 elif source_mtime is not None:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
333 if marshal._r_long(raw_timestamp) != source_mtime:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
334 raise ImportError("bytecode is stale for {}".format(fullname))
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
335 # Can't return the code object as errors from marshal loading need to
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
336 # propagate even when source is available.
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
337 return data[8:]
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
338
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
339 @module_for_loader
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
340 def _load_module(self, module, *, sourceless=False):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
341 """Helper for load_module able to handle either source or sourceless
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
342 loading."""
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
343 name = module.__name__
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
344 code_object = self.get_code(name)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
345 module.__file__ = self.get_filename(name)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
346 if not sourceless:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
347 module.__cached__ = imp.cache_from_source(module.__file__)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
348 else:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
349 module.__cached__ = module.__file__
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
350 module.__package__ = name
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
351 if self.is_package(name):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
352 module.__path__ = [module.__file__.rsplit(path_sep, 1)[0]]
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
353 else:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
354 module.__package__ = module.__package__.rpartition('.')[0]
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
355 module.__loader__ = self
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
356 exec(code_object, module.__dict__)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
357 return module
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
358
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
359
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
360 class SourceLoader(_LoaderBasics):
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
361
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
362 def path_mtime(self, path:str) -> int:
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
363 """Optional method that returns the modification time for the specified
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
364 path.
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
365
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
366 Implementing this method allows the loader to read bytecode files.
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
367
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
368 """
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
369 raise NotImplementedError
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
370
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
371 def set_data(self, path:str, data:bytes) -> None:
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
372 """Optional method which writes data to a file path.
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
373
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
374 Implementing this method allows for the writing of bytecode files.
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
375
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
376 """
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
377 raise NotImplementedError
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
378
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
379
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
380 def get_source(self, fullname):
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
381 """Concrete implementation of InspectLoader.get_source."""
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
382 import tokenize
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
383 path = self.get_filename(fullname)
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
384 try:
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
385 source_bytes = self.get_data(path)
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
386 except IOError:
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
387 raise ImportError("source not available through get_data()")
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
388 encoding = tokenize.detect_encoding(_io.BytesIO(source_bytes).readline)
62761
32682b253ffc Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
389 newline_decoder = _io.IncrementalNewlineDecoder(None, True)
32682b253ffc Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
390 return newline_decoder.decode(source_bytes.decode(encoding[0]))
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
391
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
392 def get_code(self, fullname):
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
393 """Concrete implementation of InspectLoader.get_code.
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
394
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
395 Reading of bytecode requires path_mtime to be implemented. To write
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
396 bytecode, set_data must also be implemented.
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
397
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
398 """
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
399 source_path = self.get_filename(fullname)
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
400 bytecode_path = imp.cache_from_source(source_path)
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
401 source_mtime = None
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
402 if bytecode_path is not None:
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
403 try:
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
404 source_mtime = self.path_mtime(source_path)
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
405 except NotImplementedError:
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
406 pass
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
407 else:
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
408 try:
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
409 data = self.get_data(bytecode_path)
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
410 except IOError:
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
411 pass
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
412 else:
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
413 try:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
414 bytes_data = self._bytes_from_bytecode(fullname, data,
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
415 source_mtime)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
416 except (ImportError, EOFError):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
417 pass
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
418 else:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
419 found = marshal.loads(bytes_data)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
420 if isinstance(found, code_type):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
421 return found
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
422 else:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
423 msg = "Non-code object in {}"
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
424 raise ImportError(msg.format(bytecode_path))
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
425 source_bytes = self.get_data(source_path)
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
426 code_object = compile(source_bytes, source_path, 'exec',
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
427 dont_inherit=True)
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
428 if (not sys.dont_write_bytecode and bytecode_path is not None and
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
429 source_mtime is not None):
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
430 # If e.g. Jython ever implements imp.cache_from_source to have
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
431 # their own cached file format, this block of code will most likely
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
432 # throw an exception.
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
433 data = bytearray(imp.get_magic())
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
434 data.extend(marshal._w_long(source_mtime))
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
435 data.extend(marshal.dumps(code_object))
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
436 try:
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
437 self.set_data(bytecode_path, data)
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
438 except NotImplementedError:
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
439 pass
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
440 return code_object
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
441
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
442 def load_module(self, fullname):
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
443 """Concrete implementation of Loader.load_module.
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
444
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
445 Requires ExecutionLoader.get_filename and ResourceLoader.get_data to be
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
446 implemented to load source code. Use of bytecode is dictated by whether
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
447 get_code uses/writes bytecode.
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
448
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
449 """
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
450 return self._load_module(fullname)
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
451
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
452
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
453 class _FileLoader:
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
454
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
455 """Base file loader class which implements the loader protocol methods that
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
456 require file system usage."""
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
457
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
458 def __init__(self, fullname, path):
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
459 """Cache the module name and the path to the file found by the
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
460 finder."""
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
461 self._name = fullname
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
462 self._path = path
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
463
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
464 @_check_name
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
465 def get_filename(self, fullname):
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
466 """Return the path to the source file as found by the finder."""
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
467 return self._path
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
468
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
469 def get_data(self, path):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
470 """Return the data from path as raw bytes."""
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
471 with _closing(_io.FileIO(path, 'r')) as file:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
472 return file.read()
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
473
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
474
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
475 class _SourceFileLoader(_FileLoader, SourceLoader):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
476
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
477 """Concrete implementation of SourceLoader using the file system."""
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
478
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
479 def path_mtime(self, path):
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
480 """Return the modification time for the path."""
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
481 return int(_os.stat(path).st_mtime)
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
482
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
483 def set_data(self, path, data):
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
484 """Write bytes data to a file."""
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
485 try:
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
486 with _closing(_io.FileIO(path, 'wb')) as file:
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
487 file.write(data)
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
488 except IOError as exc:
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
489 if exc.errno == errno.ENOENT:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
490 directory, _, filename = path.rpartition(path_sep)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
491 sub_directories = []
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
492 while not _path_isdir(directory):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
493 directory, _, sub_dir = directory.rpartition(path_sep)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
494 sub_directories.append(sub_dir)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
495 for part in reversed(sub_directories):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
496 directory = _path_join(directory, part)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
497 try:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
498 _os.mkdir(directory)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
499 except IOError as exc:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
500 if exc.errno != errno.EACCES:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
501 raise
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
502 else:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
503 return
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
504 return self.set_data(path, data)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
505 elif exc.errno != errno.EACCES:
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
506 raise
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
507
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
508
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
509 class _SourcelessFileLoader(_FileLoader, _LoaderBasics):
55355
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 52633
diff changeset
510
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
511 """Loader which handles sourceless file imports."""
52112
03080fa08dfe Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
512
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
513 def load_module(self, fullname):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
514 return self._load_module(fullname, sourceless=True)
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
515
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
516 def get_code(self, fullname):
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
517 path = self.get_filename(fullname)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
518 data = self.get_data(path)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
519 bytes_data = self._bytes_from_bytecode(fullname, data, None)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
520 found = marshal.loads(bytes_data)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
521 if isinstance(found, code_type):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
522 return found
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
523 else:
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
524 raise ImportError("Non-code object in {}".format(path))
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
525
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
526 def get_source(self, fullname):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
527 """Return None as there is no source code."""
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
528 return None
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
529
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
530
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
531 class _ExtensionFileLoader:
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
532
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
533 """Loader for extension modules.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
534
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
535 The constructor is designed to work with FileFinder.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
536
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
537 """
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
538
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
539 def __init__(self, name, path):
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
540 """Initialize the loader.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
541
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
542 If is_pkg is True then an exception is raised as extension modules
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
543 cannot be the __init__ module for an extension module.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
544
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
545 """
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
546 self._name = name
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
547 self._path = path
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
548
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
549 @_check_name
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
550 @set_package
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
551 @set_loader
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
552 def load_module(self, fullname):
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
553 """Load an extension module."""
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
554 is_reload = fullname in sys.modules
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
555 try:
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
556 return imp.load_dynamic(fullname, self._path)
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
557 except:
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
558 if not is_reload and fullname in sys.modules:
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
559 del sys.modules[fullname]
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
560 raise
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
561
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
562 @_check_name
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
563 def is_package(self, fullname):
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
564 """Return False as an extension module can never be a package."""
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
565 return False
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
566
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
567 @_check_name
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
568 def get_code(self, fullname):
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
569 """Return None as an extension module cannot create a code object."""
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
570 return None
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
571
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
572 @_check_name
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
573 def get_source(self, fullname):
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
574 """Return None as extension modules have no source code."""
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
575 return None
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
576
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
577
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
578 # Finders #####################################################################
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
579
51322
53d6445c6c5a Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents: 51266
diff changeset
580 class PathFinder:
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
581
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
582 """Meta path finder for sys.(path|path_hooks|path_importer_cache)."""
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
583
51639
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
584 @classmethod
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
585 def _path_hooks(cls, path, hooks=None):
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
586 """Search sequence of hooks for a finder for 'path'.
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
587
51639
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
588 If 'hooks' is false then use sys.path_hooks.
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
589
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
590 """
51639
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
591 if not hooks:
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
592 hooks = sys.path_hooks
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
593 for hook in hooks:
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
594 try:
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
595 return hook(path)
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
596 except ImportError:
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
597 continue
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
598 else:
51639
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
599 raise ImportError("no path hook found for {0}".format(path))
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
600
51322
53d6445c6c5a Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents: 51266
diff changeset
601 @classmethod
51639
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
602 def _path_importer_cache(cls, path, default=None):
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
603 """Get the finder for the path from sys.path_importer_cache.
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
604
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
605 If the path is not in the cache, find the appropriate finder and cache
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
606 it. If None is cached, get the default finder and cache that
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
607 (if applicable).
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
608
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
609 Because of NullImporter, some finder should be returned. The only
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
610 explicit fail case is if None is cached but the path cannot be used for
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
611 the default hook, for which ImportError is raised.
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
612
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
613 """
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
614 try:
51322
53d6445c6c5a Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents: 51266
diff changeset
615 finder = sys.path_importer_cache[path]
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
616 except KeyError:
51322
53d6445c6c5a Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents: 51266
diff changeset
617 finder = cls._path_hooks(path)
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
618 sys.path_importer_cache[path] = finder
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
619 else:
51639
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
620 if finder is None and default:
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
621 # Raises ImportError on failure.
51639
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
622 finder = default(path)
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
623 sys.path_importer_cache[path] = finder
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
624 return finder
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
625
51322
53d6445c6c5a Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents: 51266
diff changeset
626 @classmethod
53d6445c6c5a Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents: 51266
diff changeset
627 def find_module(cls, fullname, path=None):
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52164
diff changeset
628 """Find the module on sys.path or 'path' based on sys.path_hooks and
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52164
diff changeset
629 sys.path_importer_cache."""
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
630 if not path:
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
631 path = sys.path
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
632 for entry in path:
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
633 try:
51322
53d6445c6c5a Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents: 51266
diff changeset
634 finder = cls._path_importer_cache(entry)
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
635 except ImportError:
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
636 continue
52464
3b5bcb93bd11 Fix importlib.machinery.PathFinder.find_module() to essentially skip over None
Brett Cannon <bcannon@gmail.com>
parents: 52168
diff changeset
637 if finder:
3b5bcb93bd11 Fix importlib.machinery.PathFinder.find_module() to essentially skip over None
Brett Cannon <bcannon@gmail.com>
parents: 52168
diff changeset
638 loader = finder.find_module(fullname)
3b5bcb93bd11 Fix importlib.machinery.PathFinder.find_module() to essentially skip over None
Brett Cannon <bcannon@gmail.com>
parents: 52168
diff changeset
639 if loader:
3b5bcb93bd11 Fix importlib.machinery.PathFinder.find_module() to essentially skip over None
Brett Cannon <bcannon@gmail.com>
parents: 52168
diff changeset
640 return loader
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
641 else:
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
642 return None
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
643
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
644
52142
04a5b6438526 Finish properly hiding importlib implementation code.
Brett Cannon <bcannon@gmail.com>
parents: 52141
diff changeset
645 class _FileFinder:
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
646
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
647 """File-based finder.
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
648
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
649 Constructor takes a list of objects detailing what file extensions their
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
650 loader supports along with whether it can be used for a package.
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
651
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
652 """
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
653
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
654 def __init__(self, path, *details):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
655 """Initialize with finder details."""
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
656 packages = []
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
657 modules = []
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
658 for detail in details:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
659 modules.extend((suffix, detail.loader) for suffix in detail.suffixes)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
660 if detail.supports_packages:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
661 packages.extend((suffix, detail.loader)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
662 for suffix in detail.suffixes)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
663 self.packages = packages
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
664 self.modules = modules
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
665 self.path = path
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
666
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
667 def find_module(self, fullname):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
668 """Try to find a loader for the specified module."""
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
669 tail_module = fullname.rpartition('.')[2]
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
670 base_path = _path_join(self.path, tail_module)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
671 if _path_isdir(base_path) and _case_ok(self.path, tail_module):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
672 for suffix, loader in self.packages:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
673 init_filename = '__init__' + suffix
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
674 full_path = _path_join(base_path, init_filename)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
675 if (_path_isfile(full_path) and
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
676 _case_ok(base_path, init_filename)):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
677 return loader(fullname, full_path)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
678 else:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
679 msg = "Not importing directory {}: missing __init__"
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
680 _warnings.warn(msg.format(base_path), ImportWarning)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
681 for suffix, loader in self.modules:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
682 mod_filename = tail_module + suffix
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
683 full_path = _path_join(self.path, mod_filename)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
684 if _path_isfile(full_path) and _case_ok(self.path, mod_filename):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
685 return loader(fullname, full_path)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
686 return None
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
687
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
688 class _SourceFinderDetails:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
689
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
690 loader = _SourceFileLoader
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
691 supports_packages = True
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
692
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
693 def __init__(self):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
694 self.suffixes = _suffix_list(imp.PY_SOURCE)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
695
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
696 class _SourcelessFinderDetails:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
697
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
698 loader = _SourcelessFileLoader
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
699 supports_packages = True
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
700
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
701 def __init__(self):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
702 self.suffixes = _suffix_list(imp.PY_COMPILED)
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
703
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
704
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
705 class _ExtensionFinderDetails:
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
706
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
707 loader = _ExtensionFileLoader
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
708 supports_packages = False
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
709
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
710 def __init__(self):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
711 self.suffixes = _suffix_list(imp.C_EXTENSION)
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
712
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
713
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
714 # Import itself ###############################################################
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
715
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
716 def _file_path_hook(path):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
717 """If the path is a directory, return a file-based finder."""
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
718 if _path_isdir(path):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
719 return _FileFinder(path, _ExtensionFinderDetails(),
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
720 _SourceFinderDetails(),
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
721 _SourcelessFinderDetails())
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
722 else:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
723 raise ImportError("only directories are supported")
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
724
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
725
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
726 _DEFAULT_PATH_HOOK = _file_path_hook
51768
a1faff74ff89 Do some cleanup in importlib:
Brett Cannon <bcannon@gmail.com>
parents: 51681
diff changeset
727
51639
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
728 class _DefaultPathFinder(PathFinder):
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
729
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
730 """Subclass of PathFinder that implements implicit semantics for
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
731 __import__."""
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
732
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
733 @classmethod
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
734 def _path_hooks(cls, path):
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
735 """Search sys.path_hooks as well as implicit path hooks."""
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
736 try:
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
737 return super()._path_hooks(path)
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
738 except ImportError:
51768
a1faff74ff89 Do some cleanup in importlib:
Brett Cannon <bcannon@gmail.com>
parents: 51681
diff changeset
739 implicit_hooks = [_DEFAULT_PATH_HOOK, imp.NullImporter]
51639
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
740 return super()._path_hooks(path, implicit_hooks)
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
741
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
742 @classmethod
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
743 def _path_importer_cache(cls, path):
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
744 """Use the default path hook when None is stored in
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
745 sys.path_importer_cache."""
51768
a1faff74ff89 Do some cleanup in importlib:
Brett Cannon <bcannon@gmail.com>
parents: 51681
diff changeset
746 return super()._path_importer_cache(path, _DEFAULT_PATH_HOOK)
51639
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
747
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
748
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
749 class _ImportLockContext:
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
750
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
751 """Context manager for the import lock."""
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
752
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
753 def __enter__(self):
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
754 """Acquire the import lock."""
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
755 imp.acquire_lock()
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
756
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
757 def __exit__(self, exc_type, exc_value, exc_traceback):
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
758 """Release the import lock regardless of any raised exceptions."""
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
759 imp.release_lock()
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
760
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
761
51639
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
762 _IMPLICIT_META_PATH = [BuiltinImporter, FrozenImporter, _DefaultPathFinder]
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
763
51383
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
764 def _gcd_import(name, package=None, level=0):
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
765 """Import and return the module based on its name, the package the call is
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
766 being made from, and the level adjustment.
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
767
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
768 This function represents the greatest common denominator of functionality
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
769 between import_module and __import__. This includes settting __package__ if
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
770 the loader did not.
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
771
51383
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
772 """
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
773 if package:
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
774 if not hasattr(package, 'rindex'):
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
775 raise ValueError("__package__ not set to a string")
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
776 elif package not in sys.modules:
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
777 msg = ("Parent module {0!r} not loaded, cannot perform relative "
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
778 "import")
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
779 raise SystemError(msg.format(package))
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
780 if not name and level == 0:
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
781 raise ValueError("Empty module name")
51383
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
782 if level > 0:
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
783 dot = len(package)
51383
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
784 for x in range(level, 1, -1):
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
785 try:
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
786 dot = package.rindex('.', 0, dot)
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
787 except ValueError:
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
788 raise ValueError("attempted relative import beyond "
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
789 "top-level package")
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
790 if name:
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
791 name = "{0}.{1}".format(package[:dot], name)
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
792 else:
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
793 name = package[:dot]
52139
2e8e897a1244 Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents: 52115
diff changeset
794 with _ImportLockContext():
51383
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
795 try:
55759
e4c2cbd3bf93 Have importlib raise ImportError if None is found in sys.modules. This matches
Brett Cannon <bcannon@gmail.com>
parents: 55355
diff changeset
796 module = sys.modules[name]
e4c2cbd3bf93 Have importlib raise ImportError if None is found in sys.modules. This matches
Brett Cannon <bcannon@gmail.com>
parents: 55355
diff changeset
797 if module is None:
e4c2cbd3bf93 Have importlib raise ImportError if None is found in sys.modules. This matches
Brett Cannon <bcannon@gmail.com>
parents: 55355
diff changeset
798 message = ("import of {} halted; "
e4c2cbd3bf93 Have importlib raise ImportError if None is found in sys.modules. This matches
Brett Cannon <bcannon@gmail.com>
parents: 55355
diff changeset
799 "None in sys.modules".format(name))
e4c2cbd3bf93 Have importlib raise ImportError if None is found in sys.modules. This matches
Brett Cannon <bcannon@gmail.com>
parents: 55355
diff changeset
800 raise ImportError(message)
e4c2cbd3bf93 Have importlib raise ImportError if None is found in sys.modules. This matches
Brett Cannon <bcannon@gmail.com>
parents: 55355
diff changeset
801 return module
51383
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
802 except KeyError:
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
803 pass
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
804 parent = name.rpartition('.')[0]
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
805 path = None
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
806 if parent:
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
807 if parent not in sys.modules:
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
808 _gcd_import(parent)
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
809 # Backwards-compatibility; be nicer to skip the dict lookup.
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
810 parent_module = sys.modules[parent]
55774
99dc22ef1df0 Trying to import a submodule from another module and not a package was raising
Brett Cannon <bcannon@gmail.com>
parents: 55771
diff changeset
811 try:
99dc22ef1df0 Trying to import a submodule from another module and not a package was raising
Brett Cannon <bcannon@gmail.com>
parents: 55771
diff changeset
812 path = parent_module.__path__
99dc22ef1df0 Trying to import a submodule from another module and not a package was raising
Brett Cannon <bcannon@gmail.com>
parents: 55771
diff changeset
813 except AttributeError:
99dc22ef1df0 Trying to import a submodule from another module and not a package was raising
Brett Cannon <bcannon@gmail.com>
parents: 55771
diff changeset
814 raise ImportError("no module named {}; "
99dc22ef1df0 Trying to import a submodule from another module and not a package was raising
Brett Cannon <bcannon@gmail.com>
parents: 55771
diff changeset
815 "{} is not a package".format(name, parent))
51639
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
816 meta_path = sys.meta_path + _IMPLICIT_META_PATH
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
817 for finder in meta_path:
51383
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
818 loader = finder.find_module(name, path)
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
819 if loader is not None:
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
820 loader.load_module(name)
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
821 break
51383
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
822 else:
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
823 raise ImportError("No module named {0}".format(name))
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
824 # Backwards-compatibility; be nicer to skip the dict lookup.
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
825 module = sys.modules[name]
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
826 if parent:
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
827 # Set the module as an attribute on its parent.
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
828 setattr(parent_module, name.rpartition('.')[2], module)
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
829 # Set __package__ if the loader did not.
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
830 if not hasattr(module, '__package__') or module.__package__ is None:
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
831 # Watch out for what comes out of sys.modules to not be a module,
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
832 # e.g. an int.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
833 try:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
834 module.__package__ = module.__name__
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
835 if not hasattr(module, '__path__'):
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
836 module.__package__ = module.__package__.rpartition('.')[0]
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
837 except AttributeError:
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
838 pass
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
839 return module
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
840
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
841
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52164
diff changeset
842 def __import__(name, globals={}, locals={}, fromlist=[], level=0):
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
843 """Import a module.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
844
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
845 The 'globals' argument is used to infer where the import is occuring from
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
846 to handle relative imports. The 'locals' argument is ignored. The
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
847 'fromlist' argument specifies what should exist as attributes on the module
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
848 being imported (e.g. ``from module import <fromlist>``). The 'level'
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
849 argument represents the package location to import from in a relative
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
850 import (e.g. ``from ..pkg import mod`` would have a 'level' of 2).
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
851
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
852 """
55769
368bfc18a7a1 Raise TypeError if the name given to importlib.__import__() lacks an rpartition
Brett Cannon <bcannon@gmail.com>
parents: 55766
diff changeset
853 if not hasattr(name, 'rpartition'):
368bfc18a7a1 Raise TypeError if the name given to importlib.__import__() lacks an rpartition
Brett Cannon <bcannon@gmail.com>
parents: 55766
diff changeset
854 raise TypeError("module name must be str, not {}".format(type(name)))
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
855 if level == 0:
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
856 module = _gcd_import(name)
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
857 else:
55771
560008d098dd When the globals argument to importlib.__import__() contained any value for
Brett Cannon <bcannon@gmail.com>
parents: 55769
diff changeset
858 # __package__ is not guaranteed to be defined or could be set to None
560008d098dd When the globals argument to importlib.__import__() contained any value for
Brett Cannon <bcannon@gmail.com>
parents: 55769
diff changeset
859 # to represent that it's proper value is unknown
560008d098dd When the globals argument to importlib.__import__() contained any value for
Brett Cannon <bcannon@gmail.com>
parents: 55769
diff changeset
860 package = globals.get('__package__')
560008d098dd When the globals argument to importlib.__import__() contained any value for
Brett Cannon <bcannon@gmail.com>
parents: 55769
diff changeset
861 if package is None:
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
862 package = globals['__name__']
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
863 if '__path__' not in globals:
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
864 package = package.rpartition('.')[0]
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
865 module = _gcd_import(name, package, level)
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
866 # The hell that is fromlist ...
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
867 if not fromlist:
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
868 # Return up to the first dot in 'name'. This is complicated by the fact
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
869 # that 'name' may be relative.
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
870 if level == 0:
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
871 return sys.modules[name.partition('.')[0]]
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
872 elif not name:
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
873 return module
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
874 else:
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
875 cut_off = len(name) - len(name.partition('.')[0])
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
876 return sys.modules[module.__name__[:-cut_off]]
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
877 else:
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
878 # If a package was imported, try to import stuff from fromlist.
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
879 if hasattr(module, '__path__'):
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
880 if '*' in fromlist and hasattr(module, '__all__'):
55766
08090b5e96c2 Allow importlib.__import__ to accept any iterable for fromlist. Discovered when
Brett Cannon <bcannon@gmail.com>
parents: 55759
diff changeset
881 fromlist = list(fromlist)
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
882 fromlist.remove('*')
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
883 fromlist.extend(module.__all__)
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
884 for x in (y for y in fromlist if not hasattr(module,y)):
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
885 try:
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
886 _gcd_import('{0}.{1}'.format(module.__name__, x))
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
887 except ImportError:
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
888 pass
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
889 return module