Mercurial > cpython-lru_cache-weakref
annotate Lib/importlib/_bootstrap.py @ 64166:d0e9febfa00a 3.1
Merged revisions 84172 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r84172 | martin.v.loewis | 2010-08-18 18:12:23 +0200 (Mi, 18 Aug 2010) | 2 lines
Restore GIL in nis_cat in case of error.
........
| author | Martin v. Löwis <martin@v.loewis.de> |
|---|---|
| date | Thu, 19 Aug 2010 09:03:03 +0000 |
| parents | 52823820dce1 |
| children | 0991b40e895d 3c9232a684ae |
| 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 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
25 for x in args) |
|
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.""" |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
56 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
|
57 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
58 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
59 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
|
60 """Replacement for os.path.splitext()[0].""" |
|
52139
2e8e897a1244
Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents:
52115
diff
changeset
|
61 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
|
62 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
|
63 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
|
64 else: |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
65 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
|
66 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
67 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
68 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
|
69 """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
|
70 if not path: |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
71 path = _os.getcwd() |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
72 try: |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
73 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
|
74 except AttributeError: |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
75 if path.startswith('/'): |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
76 return path |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
77 else: |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
78 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
|
79 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
80 |
|
52139
2e8e897a1244
Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents:
52115
diff
changeset
|
81 class _closing: |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
82 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
83 """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
|
84 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
85 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
|
86 self.obj = obj |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
87 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
88 def __enter__(self): |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
89 return self.obj |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
90 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
91 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
|
92 self.obj.close() |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
93 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
94 |
|
52139
2e8e897a1244
Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents:
52115
diff
changeset
|
95 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
|
96 """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
|
97 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
|
98 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
|
99 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
|
100 |
|
43d3a95ed662
Create a simple substitute for functools.wraps to use in importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
51430
diff
changeset
|
101 |
|
52140
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
102 # Finder/loader utility code ################################################## |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
103 |
|
52002
76a579b6bade
Rename importlib.util.set___package__ to set_package.
Brett Cannon <bcannon@gmail.com>
parents:
51995
diff
changeset
|
104 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
|
105 """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
|
106 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
|
107 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
|
108 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
|
109 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
|
110 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
|
111 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
|
112 return module |
|
52139
2e8e897a1244
Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents:
52115
diff
changeset
|
113 _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
|
114 return wrapper |
|
5cdc5f0c134c
Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents:
51429
diff
changeset
|
115 |
|
5cdc5f0c134c
Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents:
51429
diff
changeset
|
116 |
|
52115
afd7118c8ccf
Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents:
52112
diff
changeset
|
117 def set_loader(fxn): |
|
afd7118c8ccf
Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents:
52112
diff
changeset
|
118 """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
|
119 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
|
120 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
|
121 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
|
122 module.__loader__ = self |
|
afd7118c8ccf
Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents:
52112
diff
changeset
|
123 return module |
|
52139
2e8e897a1244
Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents:
52115
diff
changeset
|
124 _wrap(wrapper, fxn) |
|
52115
afd7118c8ccf
Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents:
52112
diff
changeset
|
125 return wrapper |
|
afd7118c8ccf
Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents:
52112
diff
changeset
|
126 |
|
afd7118c8ccf
Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents:
52112
diff
changeset
|
127 |
|
52140
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
128 def module_for_loader(fxn): |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
129 """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
|
130 |
|
52164
0b19ba318071
Clean up docstring from importlib.util.module_for_loader.
Brett Cannon <bcannon@gmail.com>
parents:
52142
diff
changeset
|
131 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
|
132 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
|
133 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
|
134 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
|
135 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
|
136 |
|
52164
0b19ba318071
Clean up docstring from importlib.util.module_for_loader.
Brett Cannon <bcannon@gmail.com>
parents:
52142
diff
changeset
|
137 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
|
138 the second argument. |
|
52140
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
139 |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
140 """ |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
141 def decorated(self, fullname): |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
142 module = sys.modules.get(fullname) |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
143 is_reload = bool(module) |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
144 if not is_reload: |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
145 # 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
|
146 # 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
|
147 # infinite loop. |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
148 module = imp.new_module(fullname) |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
149 sys.modules[fullname] = module |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
150 try: |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
151 return fxn(self, module) |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
152 except: |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
153 if not is_reload: |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
154 del sys.modules[fullname] |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
155 raise |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
156 _wrap(decorated, fxn) |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
157 return decorated |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
158 |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
159 |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
160 def _check_name(method): |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
161 """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
|
162 loader can handle. |
|
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 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
|
165 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
|
166 |
|
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 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
|
169 if self._name != name: |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
170 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
|
171 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
|
172 _wrap(inner, method) |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
173 return inner |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
174 |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
175 |
|
52166
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
176 def _requires_builtin(fxn): |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
177 """Decorator to verify the named module is built-in.""" |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
178 def wrapper(self, fullname): |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
179 if fullname not in sys.builtin_module_names: |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
180 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
|
181 return fxn(self, fullname) |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
182 _wrap(wrapper, fxn) |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
183 return wrapper |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
184 |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
185 |
|
52168
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
186 def _requires_frozen(fxn): |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
187 """Decorator to verify the named module is frozen.""" |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
188 def wrapper(self, fullname): |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
189 if not imp.is_frozen(fullname): |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
190 raise ImportError("{0} is not a frozen module".format(fullname)) |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
191 return fxn(self, fullname) |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
192 _wrap(wrapper, fxn) |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
193 return wrapper |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
194 |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
195 |
|
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
|
196 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
|
197 """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
|
198 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
|
199 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
|
200 |
|
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 |
|
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 # Loaders ##################################################################### |
|
52140
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
203 |
|
50983
0940a4f2506e
Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents:
50893
diff
changeset
|
204 class BuiltinImporter: |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
205 |
|
52165
62f9dad628de
A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
52164
diff
changeset
|
206 """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
|
207 |
|
52165
62f9dad628de
A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
52164
diff
changeset
|
208 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
|
209 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
|
210 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
211 """ |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
212 |
|
50983
0940a4f2506e
Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents:
50893
diff
changeset
|
213 @classmethod |
|
0940a4f2506e
Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents:
50893
diff
changeset
|
214 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
|
215 """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
|
216 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
217 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
|
218 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
219 """ |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
220 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
|
221 return None |
|
50983
0940a4f2506e
Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents:
50893
diff
changeset
|
222 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
|
223 |
|
51026
c26f1af26bfc
Document both importlib.machinery.BuiltinImporter and FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
50983
diff
changeset
|
224 @classmethod |
|
52002
76a579b6bade
Rename importlib.util.set___package__ to set_package.
Brett Cannon <bcannon@gmail.com>
parents:
51995
diff
changeset
|
225 @set_package |
|
52115
afd7118c8ccf
Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents:
52112
diff
changeset
|
226 @set_loader |
|
52166
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
227 @_requires_builtin |
|
51026
c26f1af26bfc
Document both importlib.machinery.BuiltinImporter and FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
50983
diff
changeset
|
228 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
|
229 """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
|
230 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
|
231 try: |
|
147fcebacb5b
Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents:
51639
diff
changeset
|
232 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
|
233 except: |
|
147fcebacb5b
Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents:
51639
diff
changeset
|
234 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
|
235 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
|
236 raise |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
237 |
|
52166
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
238 @classmethod |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
239 @_requires_builtin |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
240 def get_code(cls, fullname): |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
241 """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
|
242 return None |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
243 |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
244 @classmethod |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
245 @_requires_builtin |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
246 def get_source(cls, fullname): |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
247 """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
|
248 return None |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
249 |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
250 @classmethod |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
251 @_requires_builtin |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
252 def is_package(cls, fullname): |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
253 """Return None as built-in module are never packages.""" |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
254 return False |
|
69cd615b76fb
Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52165
diff
changeset
|
255 |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
256 |
|
50983
0940a4f2506e
Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents:
50893
diff
changeset
|
257 class FrozenImporter: |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
258 |
|
52165
62f9dad628de
A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
52164
diff
changeset
|
259 """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
|
260 |
|
52165
62f9dad628de
A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
52164
diff
changeset
|
261 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
|
262 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
|
263 |
|
50983
0940a4f2506e
Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents:
50893
diff
changeset
|
264 """ |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
265 |
|
50983
0940a4f2506e
Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents:
50893
diff
changeset
|
266 @classmethod |
|
0940a4f2506e
Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents:
50893
diff
changeset
|
267 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
|
268 """Find a frozen module.""" |
|
0940a4f2506e
Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents:
50893
diff
changeset
|
269 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
|
270 |
|
50983
0940a4f2506e
Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents:
50893
diff
changeset
|
271 @classmethod |
|
52002
76a579b6bade
Rename importlib.util.set___package__ to set_package.
Brett Cannon <bcannon@gmail.com>
parents:
51995
diff
changeset
|
272 @set_package |
|
52115
afd7118c8ccf
Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents:
52112
diff
changeset
|
273 @set_loader |
|
52168
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
274 @_requires_frozen |
|
50983
0940a4f2506e
Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents:
50893
diff
changeset
|
275 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
|
276 """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
|
277 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
|
278 try: |
|
147fcebacb5b
Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents:
51639
diff
changeset
|
279 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
|
280 except: |
|
147fcebacb5b
Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents:
51639
diff
changeset
|
281 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
|
282 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
|
283 raise |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
284 |
|
52168
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
285 @classmethod |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
286 @_requires_frozen |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
287 def get_code(cls, fullname): |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
288 """Return the code object for the frozen module.""" |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
289 return imp.get_frozen_object(fullname) |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
290 |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
291 @classmethod |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
292 @_requires_frozen |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
293 def get_source(cls, fullname): |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
294 """Return None as frozen modules do not have source code.""" |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
295 return None |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
296 |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
297 @classmethod |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
298 @_requires_frozen |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
299 def is_package(cls, fullname): |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
300 """Return if the frozen module is a package.""" |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
301 return imp.is_frozen_package(fullname) |
|
70f5f9113ae1
Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents:
52166
diff
changeset
|
302 |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
303 |
|
51771
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
304 class PyLoader: |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
305 |
|
52165
62f9dad628de
A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
52164
diff
changeset
|
306 """Loader base class for Python source code. |
|
62f9dad628de
A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
52164
diff
changeset
|
307 |
|
62f9dad628de
A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
52164
diff
changeset
|
308 Subclasses need to implement the methods: |
|
51771
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
309 |
|
52165
62f9dad628de
A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
52164
diff
changeset
|
310 - source_path |
|
62f9dad628de
A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
52164
diff
changeset
|
311 - get_data |
|
62f9dad628de
A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
52164
diff
changeset
|
312 - is_package |
|
51771
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
313 |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
314 """ |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
315 |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
316 @module_for_loader |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
317 def load_module(self, module): |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
318 """Load a source module.""" |
|
52075
2033d45a5b99
Minor changes to Python source base loader.
Brett Cannon <bcannon@gmail.com>
parents:
52002
diff
changeset
|
319 return self._load_module(module) |
|
51771
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
320 |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
321 def _load_module(self, module): |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
322 """Initialize a module from source.""" |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
323 name = module.__name__ |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
324 code_object = self.get_code(module.__name__) |
|
52075
2033d45a5b99
Minor changes to Python source base loader.
Brett Cannon <bcannon@gmail.com>
parents:
52002
diff
changeset
|
325 # __file__ may have been set by the caller, e.g. bytecode path. |
|
51771
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
326 if not hasattr(module, '__file__'): |
|
52075
2033d45a5b99
Minor changes to Python source base loader.
Brett Cannon <bcannon@gmail.com>
parents:
52002
diff
changeset
|
327 module.__file__ = self.source_path(name) |
|
51771
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
328 if self.is_package(name): |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
329 module.__path__ = [module.__file__.rsplit(path_sep, 1)[0]] |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
330 module.__package__ = module.__name__ |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
331 if not hasattr(module, '__path__'): |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
332 module.__package__ = module.__package__.rpartition('.')[0] |
|
52075
2033d45a5b99
Minor changes to Python source base loader.
Brett Cannon <bcannon@gmail.com>
parents:
52002
diff
changeset
|
333 module.__loader__ = self |
|
51771
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
334 exec(code_object, module.__dict__) |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
335 return module |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
336 |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
337 def get_code(self, fullname): |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
338 """Get a code object from source.""" |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
339 source_path = self.source_path(fullname) |
|
52075
2033d45a5b99
Minor changes to Python source base loader.
Brett Cannon <bcannon@gmail.com>
parents:
52002
diff
changeset
|
340 if source_path is None: |
|
2033d45a5b99
Minor changes to Python source base loader.
Brett Cannon <bcannon@gmail.com>
parents:
52002
diff
changeset
|
341 message = "a source path must exist to load {0}".format(fullname) |
|
2033d45a5b99
Minor changes to Python source base loader.
Brett Cannon <bcannon@gmail.com>
parents:
52002
diff
changeset
|
342 raise ImportError(message) |
|
51771
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
343 source = self.get_data(source_path) |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
344 # Convert to universal newlines. |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
345 line_endings = b'\n' |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
346 for index, c in enumerate(source): |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
347 if c == ord(b'\n'): |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
348 break |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
349 elif c == ord(b'\r'): |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
350 line_endings = b'\r' |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
351 try: |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
352 if source[index+1] == ord(b'\n'): |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
353 line_endings += b'\n' |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
354 except IndexError: |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
355 pass |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
356 break |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
357 if line_endings != b'\n': |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
358 source = source.replace(line_endings, b'\n') |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
359 return compile(source, source_path, 'exec', dont_inherit=True) |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
360 |
|
52112
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
361 # Never use in implementing import! Imports code within the method. |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
362 def get_source(self, fullname): |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
363 """Return the source code for a module. |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
364 |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
365 self.source_path() and self.get_data() are used to implement this |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
366 method. |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
367 |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
368 """ |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
369 path = self.source_path(fullname) |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
370 if path is None: |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
371 return None |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
372 try: |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
373 source_bytes = self.get_data(path) |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
374 except IOError: |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
375 return ImportError("source not available through get_data()") |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
376 import io |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
377 import tokenize |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
378 encoding = tokenize.detect_encoding(io.BytesIO(source_bytes).readline) |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
379 return source_bytes.decode(encoding[0]) |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
380 |
|
51771
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
381 |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
382 class PyPycLoader(PyLoader): |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
383 |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
384 """Loader base class for Python source and bytecode. |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
385 |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
386 Requires implementing the methods needed for PyLoader as well as |
|
51781
6a5bee1893ad
Relocate source_mtime in importlib to PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
51771
diff
changeset
|
387 source_mtime, bytecode_path, and write_bytecode. |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
388 |
|
51771
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
389 """ |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
390 |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
391 @module_for_loader |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
392 def load_module(self, module): |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
393 """Load a module from source or bytecode.""" |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
394 name = module.__name__ |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
52080
diff
changeset
|
395 source_path = self.source_path(name) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
52080
diff
changeset
|
396 bytecode_path = self.bytecode_path(name) |
|
52080
65371df41aca
Fix importlib._bootstrap.PyPycLoader.load_module() to better handle
Brett Cannon <bcannon@gmail.com>
parents:
52075
diff
changeset
|
397 # get_code can worry about no viable paths existing. |
|
65371df41aca
Fix importlib._bootstrap.PyPycLoader.load_module() to better handle
Brett Cannon <bcannon@gmail.com>
parents:
52075
diff
changeset
|
398 module.__file__ = source_path or bytecode_path |
|
51771
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
399 return self._load_module(module) |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
400 |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
401 def get_code(self, fullname): |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
402 """Get a code object from source or bytecode.""" |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
403 # XXX Care enough to make sure this call does not happen if the magic |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
404 # number is bad? |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
405 source_timestamp = self.source_mtime(fullname) |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
406 # Try to use bytecode if it is available. |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
407 bytecode_path = self.bytecode_path(fullname) |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
408 if bytecode_path: |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
409 data = self.get_data(bytecode_path) |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
410 try: |
|
58960
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
411 magic = data[:4] |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
412 if len(magic) < 4: |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
413 raise ImportError("bad magic number in {}".format(fullname)) |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
414 raw_timestamp = data[4:8] |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
415 if len(raw_timestamp) < 4: |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
416 raise EOFError("bad timestamp in {}".format(fullname)) |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
417 pyc_timestamp = marshal._r_long(raw_timestamp) |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
418 bytecode = data[8:] |
|
51771
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
419 # Verify that the magic number is valid. |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
420 if imp.get_magic() != magic: |
|
58960
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
421 raise ImportError("bad magic number in {}".format(fullname)) |
|
51771
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
422 # Verify that the bytecode is not stale (only matters when |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
423 # there is source to fall back on. |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
424 if source_timestamp: |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
425 if pyc_timestamp < source_timestamp: |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
426 raise ImportError("bytecode is stale") |
|
58960
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
427 except (ImportError, EOFError): |
|
51771
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
428 # If source is available give it a shot. |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
429 if source_timestamp is not None: |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
430 pass |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
431 else: |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
432 raise |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
433 else: |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
434 # Bytecode seems fine, so try to use it. |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
435 # XXX If the bytecode is ill-formed, would it be beneficial to |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
436 # try for using source if available and issue a warning? |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
437 return marshal.loads(bytecode) |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
438 elif source_timestamp is None: |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
439 raise ImportError("no source or bytecode available to create code " |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
440 "object for {0!r}".format(fullname)) |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
441 # Use the source. |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
442 code_object = super().get_code(fullname) |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
443 # Generate bytecode and write it out. |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
444 if not sys.dont_write_bytecode: |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
445 data = bytearray(imp.get_magic()) |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
446 data.extend(marshal._w_long(source_timestamp)) |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
447 data.extend(marshal.dumps(code_object)) |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
448 self.write_bytecode(fullname, data) |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
449 return code_object |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
450 |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
451 |
|
52142
04a5b6438526
Finish properly hiding importlib implementation code.
Brett Cannon <bcannon@gmail.com>
parents:
52141
diff
changeset
|
452 class _PyFileLoader(PyLoader): |
|
51771
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
453 |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
454 """Load a Python source file.""" |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
455 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
456 def __init__(self, name, path, is_pkg): |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
457 self._name = name |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
458 self._is_pkg = is_pkg |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
459 # Figure out the base path based on whether it was source or bytecode |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
460 # that was found. |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
461 try: |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
462 self._base_path = _path_without_ext(path, imp.PY_SOURCE) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
463 except ValueError: |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
464 self._base_path = _path_without_ext(path, imp.PY_COMPILED) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
465 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
466 def _find_path(self, ext_type): |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
467 """Find a path from the base path and the specified extension type that |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
468 exists, returning None if one is not found.""" |
|
52139
2e8e897a1244
Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents:
52115
diff
changeset
|
469 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
|
470 path = self._base_path + suffix |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
471 if _path_exists(path): |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
472 return path |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
473 else: |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
474 return None |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
475 |
|
52139
2e8e897a1244
Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents:
52115
diff
changeset
|
476 @_check_name |
|
51239
386eb4b5cbb3
Expose source_path and bytecode_path on _PyFileLoader.
Brett Cannon <bcannon@gmail.com>
parents:
51231
diff
changeset
|
477 def source_path(self, fullname): |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
478 """Return the path to an existing source file for the module, or None |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
479 if one cannot be found.""" |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
480 # Not a property so that it is easy to override. |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
481 return self._find_path(imp.PY_SOURCE) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
482 |
|
51771
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
483 def get_data(self, path): |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
484 """Return the data from path as raw bytes.""" |
|
51995
e37bb30255f3
Move importlib over to _io.
Brett Cannon <bcannon@gmail.com>
parents:
51991
diff
changeset
|
485 return _io.FileIO(path, 'r').read() # Assuming bytes. |
|
51771
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
486 |
|
52139
2e8e897a1244
Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents:
52115
diff
changeset
|
487 @_check_name |
|
51771
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
488 def is_package(self, fullname): |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
489 """Return a boolean based on whether the module is a package. |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
490 |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
491 Raises ImportError (like get_source) if the loader cannot handle the |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
492 package. |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
493 |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
494 """ |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
495 return self._is_pkg |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
496 |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
497 |
|
52142
04a5b6438526
Finish properly hiding importlib implementation code.
Brett Cannon <bcannon@gmail.com>
parents:
52141
diff
changeset
|
498 class _PyPycFileLoader(PyPycLoader, _PyFileLoader): |
|
51771
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
499 |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
500 """Load a module from a source or bytecode file.""" |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
501 |
|
52139
2e8e897a1244
Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents:
52115
diff
changeset
|
502 @_check_name |
|
51781
6a5bee1893ad
Relocate source_mtime in importlib to PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
51771
diff
changeset
|
503 def source_mtime(self, name): |
|
6a5bee1893ad
Relocate source_mtime in importlib to PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
51771
diff
changeset
|
504 """Return the modification time of the source for the specified |
|
6a5bee1893ad
Relocate source_mtime in importlib to PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
51771
diff
changeset
|
505 module.""" |
|
6a5bee1893ad
Relocate source_mtime in importlib to PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
51771
diff
changeset
|
506 source_path = self.source_path(name) |
|
6a5bee1893ad
Relocate source_mtime in importlib to PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
51771
diff
changeset
|
507 if not source_path: |
|
6a5bee1893ad
Relocate source_mtime in importlib to PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
51771
diff
changeset
|
508 return None |
|
6a5bee1893ad
Relocate source_mtime in importlib to PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
51771
diff
changeset
|
509 return int(_os.stat(source_path).st_mtime) |
|
6a5bee1893ad
Relocate source_mtime in importlib to PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
51771
diff
changeset
|
510 |
|
52139
2e8e897a1244
Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents:
52115
diff
changeset
|
511 @_check_name |
|
51771
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
512 def bytecode_path(self, fullname): |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
513 """Return the path to a bytecode file, or None if one does not |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
514 exist.""" |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
515 # Not a property for easy overriding. |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
516 return self._find_path(imp.PY_COMPILED) |
|
ee7029710c05
Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents:
51770
diff
changeset
|
517 |
|
52139
2e8e897a1244
Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents:
52115
diff
changeset
|
518 @_check_name |
|
51242
766405c04bdc
Simplify write_bytecode for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
51240
diff
changeset
|
519 def write_bytecode(self, name, data): |
|
766405c04bdc
Simplify write_bytecode for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
51240
diff
changeset
|
520 """Write out 'data' for the specified module, returning a boolean |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
521 signifying if the write-out actually occurred. |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
522 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
523 Raises ImportError (just like get_source) if the specified module |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
524 cannot be handled by the loader. |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
525 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
526 """ |
|
51239
386eb4b5cbb3
Expose source_path and bytecode_path on _PyFileLoader.
Brett Cannon <bcannon@gmail.com>
parents:
51231
diff
changeset
|
527 bytecode_path = self.bytecode_path(name) |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
528 if not bytecode_path: |
|
52139
2e8e897a1244
Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents:
52115
diff
changeset
|
529 bytecode_path = self._base_path + _suffix_list(imp.PY_COMPILED)[0] |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
530 try: |
|
57137
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55761
diff
changeset
|
531 # Assuming bytes. |
|
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55761
diff
changeset
|
532 with _closing(_io.FileIO(bytecode_path, 'w')) as bytecode_file: |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
533 bytecode_file.write(data) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
534 return True |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
535 except IOError as exc: |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
536 if exc.errno == errno.EACCES: |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
537 return False |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
538 else: |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
539 raise |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
540 |
|
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 class _ExtensionFileLoader: |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
543 |
|
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
|
544 """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
|
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 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
|
547 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
548 """ |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
549 |
|
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
|
550 def __init__(self, name, path, is_pkg): |
|
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 """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
|
552 |
|
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
|
553 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
|
554 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
|
555 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
556 """ |
|
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
|
557 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
|
558 self._path = 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
|
559 if is_pkg: |
|
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 ValueError("extension modules cannot be packages") |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
561 |
|
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
|
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 @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
|
564 @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
|
565 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
|
566 """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
|
567 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
|
568 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
|
569 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
|
570 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
|
571 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
|
572 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
|
573 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
|
574 |
|
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 @_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
|
576 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
|
577 """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
|
578 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
|
579 |
|
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
|
580 @_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
|
581 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
|
582 """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
|
583 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
|
584 |
|
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
|
585 @_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
|
586 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
|
587 """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
|
588 return None |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
589 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
590 |
|
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
|
591 # Finders ##################################################################### |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
592 |
|
51322
53d6445c6c5a
Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents:
51266
diff
changeset
|
593 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
|
594 |
|
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 """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
|
596 |
|
51639
ef2cba80c975
Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents:
51433
diff
changeset
|
597 @classmethod |
|
ef2cba80c975
Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents:
51433
diff
changeset
|
598 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
|
599 """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
|
600 |
|
51639
ef2cba80c975
Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents:
51433
diff
changeset
|
601 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
|
602 |
|
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 """ |
|
51639
ef2cba80c975
Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents:
51433
diff
changeset
|
604 if not hooks: |
|
ef2cba80c975
Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents:
51433
diff
changeset
|
605 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
|
606 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
|
607 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
|
608 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
|
609 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
|
610 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
|
611 else: |
|
51639
ef2cba80c975
Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents:
51433
diff
changeset
|
612 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
|
613 |
|
51322
53d6445c6c5a
Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents:
51266
diff
changeset
|
614 @classmethod |
|
51639
ef2cba80c975
Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents:
51433
diff
changeset
|
615 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
|
616 """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
|
617 |
|
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 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
|
619 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
|
620 (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
|
621 |
|
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
|
622 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
|
623 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
|
624 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
|
625 |
|
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
|
626 """ |
|
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
|
627 try: |
|
51322
53d6445c6c5a
Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents:
51266
diff
changeset
|
628 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
|
629 except KeyError: |
|
51322
53d6445c6c5a
Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents:
51266
diff
changeset
|
630 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
|
631 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
|
632 else: |
|
51639
ef2cba80c975
Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents:
51433
diff
changeset
|
633 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
|
634 # 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
|
635 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
|
636 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
|
637 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
|
638 |
|
51322
53d6445c6c5a
Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents:
51266
diff
changeset
|
639 @classmethod |
|
53d6445c6c5a
Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents:
51266
diff
changeset
|
640 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
|
641 """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
|
642 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
|
643 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
|
644 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
|
645 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
|
646 try: |
|
51322
53d6445c6c5a
Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents:
51266
diff
changeset
|
647 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
|
648 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
|
649 continue |
|
52464
3b5bcb93bd11
Fix importlib.machinery.PathFinder.find_module() to essentially skip over None
Brett Cannon <bcannon@gmail.com>
parents:
52168
diff
changeset
|
650 if finder: |
|
3b5bcb93bd11
Fix importlib.machinery.PathFinder.find_module() to essentially skip over None
Brett Cannon <bcannon@gmail.com>
parents:
52168
diff
changeset
|
651 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
|
652 if loader: |
|
3b5bcb93bd11
Fix importlib.machinery.PathFinder.find_module() to essentially skip over None
Brett Cannon <bcannon@gmail.com>
parents:
52168
diff
changeset
|
653 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
|
654 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
|
655 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
|
656 |
|
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
|
657 |
|
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
|
658 class _ChainedFinder: |
|
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
|
659 |
|
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
|
660 """Finder that sequentially calls other finders.""" |
|
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
|
661 |
|
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
|
662 def __init__(self, *finders): |
|
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
|
663 self._finders = finders |
|
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
|
664 |
|
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
|
665 def find_module(self, fullname, path=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
|
666 for finder in self._finders: |
|
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
|
667 result = finder.find_module(fullname, 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
|
668 if result: |
|
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 return result |
|
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
|
670 else: |
|
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
|
671 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
|
672 |
|
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
|
673 |
|
52142
04a5b6438526
Finish properly hiding importlib implementation code.
Brett Cannon <bcannon@gmail.com>
parents:
52141
diff
changeset
|
674 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
|
675 |
|
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
|
676 """Base class for file finders. |
|
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
|
677 |
|
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
|
678 Subclasses are expected to define the following attributes: |
|
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
|
679 |
|
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
|
680 * _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
|
681 Sequence of file suffixes whose order will be followed. |
|
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
|
682 |
|
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
|
683 * _possible_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
|
684 True if importer should check for packages. |
|
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
|
685 |
|
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
|
686 * _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
|
687 A callable that takes the module name, a file path, and whether |
|
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
|
688 the path points to a package and returns a loader for the 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
|
689 found at that 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
|
690 |
|
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
|
691 """ |
|
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
|
692 |
|
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
|
693 def __init__(self, path_entry): |
|
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
|
694 """Initialize an importer for the passed-in sys.path entry (which is |
|
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
|
695 assumed to have already been verified as an existing directory). |
|
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
|
696 |
|
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
|
697 Can be used as an entry on sys.path_hook. |
|
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
|
698 |
|
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
|
699 """ |
|
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
|
700 absolute_path = _path_absolute(path_entry) |
|
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
|
701 if not _path_isdir(absolute_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
|
702 raise ImportError("only directories are supported") |
|
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 self._path_entry = absolute_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
|
704 |
|
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
|
705 def find_module(self, fullname, path=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
|
706 tail_module = fullname.rpartition('.')[2] |
|
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
|
707 package_directory = 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
|
708 if self._possible_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
|
709 for ext in self._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
|
710 package_directory = _path_join(self._path_entry, tail_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
|
711 init_filename = '__init__' + ext |
|
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 package_init = _path_join(package_directory, init_filename) |
|
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 if (_path_isfile(package_init) and |
|
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
|
714 _case_ok(self._path_entry, tail_module) and |
|
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
|
715 _case_ok(package_directory, init_filename)): |
|
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
|
716 return self._loader(fullname, package_init, True) |
|
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
|
717 for ext in self._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
|
718 file_name = tail_module + ext |
|
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
|
719 file_path = _path_join(self._path_entry, file_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
|
720 if (_path_isfile(file_path) and |
|
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
|
721 _case_ok(self._path_entry, file_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
|
722 return self._loader(fullname, file_path, 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
|
723 else: |
|
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
|
724 # Raise a warning if it matches a directory w/o an __init__ file. |
|
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
|
725 if (package_directory is not None and |
|
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
|
726 _path_isdir(package_directory) and |
|
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
|
727 _case_ok(self._path_entry, tail_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
|
728 _warnings.warn("Not importing directory %s: missing __init__" |
|
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
|
729 % package_directory, ImportWarning) |
|
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
|
730 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
|
731 |
|
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
|
732 |
|
52142
04a5b6438526
Finish properly hiding importlib implementation code.
Brett Cannon <bcannon@gmail.com>
parents:
52141
diff
changeset
|
733 class _PyFileFinder(_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
|
734 |
|
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
|
735 """Importer for source/bytecode files.""" |
|
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
|
736 |
|
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
|
737 _possible_package = True |
|
52142
04a5b6438526
Finish properly hiding importlib implementation code.
Brett Cannon <bcannon@gmail.com>
parents:
52141
diff
changeset
|
738 _loader = _PyFileLoader |
|
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
|
739 |
|
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
|
740 def __init__(self, path_entry): |
|
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
|
741 # Lack of imp during class creation means _suffixes is set here. |
|
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
|
742 # Make sure that Python source files are listed first! Needed for an |
|
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
|
743 # optimization by the 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
|
744 self._suffixes = _suffix_list(imp.PY_SOURCE) |
|
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
|
745 super().__init__(path_entry) |
|
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
|
746 |
|
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
|
747 |
|
52142
04a5b6438526
Finish properly hiding importlib implementation code.
Brett Cannon <bcannon@gmail.com>
parents:
52141
diff
changeset
|
748 class _PyPycFileFinder(_PyFileFinder): |
|
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 |
|
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 """Finder for source and bytecode files.""" |
|
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 |
|
52142
04a5b6438526
Finish properly hiding importlib implementation code.
Brett Cannon <bcannon@gmail.com>
parents:
52141
diff
changeset
|
752 _loader = _PyPycFileLoader |
|
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
|
753 |
|
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 def __init__(self, path_entry): |
|
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 super().__init__(path_entry) |
|
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 self._suffixes += _suffix_list(imp.PY_COMPILED) |
|
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 |
|
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 |
|
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 |
|
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 |
|
52142
04a5b6438526
Finish properly hiding importlib implementation code.
Brett Cannon <bcannon@gmail.com>
parents:
52141
diff
changeset
|
761 class _ExtensionFileFinder(_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
|
762 |
|
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
|
763 """Importer for extension files.""" |
|
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
|
764 |
|
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
|
765 _possible_package = 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
|
766 _loader = _ExtensionFileLoader |
|
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
|
767 |
|
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
|
768 def __init__(self, path_entry): |
|
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
|
769 # Assigning to _suffixes here instead of at the class level because |
|
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
|
770 # imp is not imported at the time of class creation. |
|
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
|
771 self._suffixes = _suffix_list(imp.C_EXTENSION) |
|
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
|
772 super().__init__(path_entry) |
|
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
|
773 |
|
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
|
774 |
|
52140
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
775 # Import itself ############################################################### |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
776 |
|
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
|
777 def _chained_path_hook(*path_hooks): |
|
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
|
778 """Create a closure which sequentially checks path hooks to see which ones |
|
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
|
779 (if any) can work with a 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
|
780 def path_hook(entry): |
|
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
|
781 """Check to see if 'entry' matches any of the enclosed path hooks.""" |
|
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
|
782 finders = [] |
|
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
|
783 for hook in path_hooks: |
|
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
|
784 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
|
785 finder = hook(entry) |
|
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
|
786 except ImportError: |
|
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
|
787 continue |
|
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
|
788 else: |
|
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
|
789 finders.append(finder) |
|
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
|
790 if not finders: |
|
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
|
791 raise ImportError("no finder found") |
|
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
|
792 else: |
|
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
|
793 return _ChainedFinder(*finders) |
|
52140
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
794 |
|
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
|
795 return path_hook |
|
52140
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
796 |
|
1a8bbc5fb3ef
Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
52139
diff
changeset
|
797 |
|
52142
04a5b6438526
Finish properly hiding importlib implementation code.
Brett Cannon <bcannon@gmail.com>
parents:
52141
diff
changeset
|
798 _DEFAULT_PATH_HOOK = _chained_path_hook(_ExtensionFileFinder, _PyPycFileFinder) |
|
51768
a1faff74ff89
Do some cleanup in importlib:
Brett Cannon <bcannon@gmail.com>
parents:
51681
diff
changeset
|
799 |
|
51639
ef2cba80c975
Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents:
51433
diff
changeset
|
800 class _DefaultPathFinder(PathFinder): |
|
ef2cba80c975
Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents:
51433
diff
changeset
|
801 |
|
ef2cba80c975
Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents:
51433
diff
changeset
|
802 """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
|
803 __import__.""" |
|
ef2cba80c975
Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents:
51433
diff
changeset
|
804 |
|
ef2cba80c975
Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents:
51433
diff
changeset
|
805 @classmethod |
|
ef2cba80c975
Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents:
51433
diff
changeset
|
806 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
|
807 """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
|
808 try: |
|
ef2cba80c975
Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents:
51433
diff
changeset
|
809 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
|
810 except ImportError: |
|
51768
a1faff74ff89
Do some cleanup in importlib:
Brett Cannon <bcannon@gmail.com>
parents:
51681
diff
changeset
|
811 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
|
812 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
|
813 |
|
ef2cba80c975
Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents:
51433
diff
changeset
|
814 @classmethod |
|
ef2cba80c975
Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents:
51433
diff
changeset
|
815 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
|
816 """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
|
817 sys.path_importer_cache.""" |
|
51768
a1faff74ff89
Do some cleanup in importlib:
Brett Cannon <bcannon@gmail.com>
parents:
51681
diff
changeset
|
818 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
|
819 |
|
ef2cba80c975
Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents:
51433
diff
changeset
|
820 |
|
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
|
821 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
|
822 |
|
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
|
823 """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
|
824 |
|
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
|
825 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
|
826 """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
|
827 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
|
828 |
|
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
|
829 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
|
830 """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
|
831 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
|
832 |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
833 |
|
51639
ef2cba80c975
Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents:
51433
diff
changeset
|
834 _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
|
835 |
|
51383
a00e5dcea16c
Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents:
51356
diff
changeset
|
836 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
|
837 """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
|
838 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
|
839 |
|
a00e5dcea16c
Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents:
51356
diff
changeset
|
840 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
|
841 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
|
842 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
|
843 |
|
51383
a00e5dcea16c
Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents:
51356
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 if package: |
|
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51383
diff
changeset
|
846 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
|
847 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
|
848 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
|
849 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
|
850 "import") |
|
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51383
diff
changeset
|
851 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
|
852 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
|
853 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
|
854 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
|
855 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
|
856 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
|
857 try: |
|
a00e5dcea16c
Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents:
51356
diff
changeset
|
858 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
|
859 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
|
860 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
|
861 "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
|
862 if name: |
|
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51383
diff
changeset
|
863 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
|
864 else: |
|
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51383
diff
changeset
|
865 name = package[:dot] |
|
52139
2e8e897a1244
Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents:
52115
diff
changeset
|
866 with _ImportLockContext(): |
|
51383
a00e5dcea16c
Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents:
51356
diff
changeset
|
867 try: |
|
55761
d8280364c581
Merged revisions 74584 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
52633
diff
changeset
|
868 module = sys.modules[name] |
|
d8280364c581
Merged revisions 74584 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
52633
diff
changeset
|
869 if module is None: |
|
d8280364c581
Merged revisions 74584 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
52633
diff
changeset
|
870 message = ("import of {} halted; " |
|
d8280364c581
Merged revisions 74584 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
52633
diff
changeset
|
871 "None in sys.modules".format(name)) |
|
d8280364c581
Merged revisions 74584 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
52633
diff
changeset
|
872 raise ImportError(message) |
|
d8280364c581
Merged revisions 74584 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
52633
diff
changeset
|
873 return module |
|
51383
a00e5dcea16c
Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents:
51356
diff
changeset
|
874 except KeyError: |
|
a00e5dcea16c
Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents:
51356
diff
changeset
|
875 pass |
|
a00e5dcea16c
Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents:
51356
diff
changeset
|
876 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
|
877 path = None |
|
a00e5dcea16c
Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents:
51356
diff
changeset
|
878 if parent: |
|
a00e5dcea16c
Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents:
51356
diff
changeset
|
879 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
|
880 _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
|
881 # 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
|
882 parent_module = sys.modules[parent] |
|
51383
a00e5dcea16c
Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents:
51356
diff
changeset
|
883 path = parent_module.__path__ |
|
51639
ef2cba80c975
Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents:
51433
diff
changeset
|
884 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
|
885 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
|
886 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
|
887 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
|
888 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
|
889 break |
|
51383
a00e5dcea16c
Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents:
51356
diff
changeset
|
890 else: |
|
a00e5dcea16c
Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents:
51356
diff
changeset
|
891 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
|
892 # 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
|
893 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
|
894 if parent: |
|
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51383
diff
changeset
|
895 # 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
|
896 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
|
897 # 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
|
898 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
|
899 # 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
|
900 # 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
|
901 try: |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
902 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
|
903 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
|
904 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
|
905 except AttributeError: |
|
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51383
diff
changeset
|
906 pass |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
907 return module |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
908 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
909 |
|
52165
62f9dad628de
A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
52164
diff
changeset
|
910 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
|
911 """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
|
912 |
|
51426
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51383
diff
changeset
|
913 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
|
914 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
|
915 '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
|
916 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
|
917 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
|
918 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
|
919 |
|
51426
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51383
diff
changeset
|
920 """ |
|
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51383
diff
changeset
|
921 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
|
922 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
|
923 else: |
|
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51383
diff
changeset
|
924 # __package__ is not guaranteed to be defined. |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
925 try: |
|
51426
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51383
diff
changeset
|
926 package = globals['__package__'] |
|
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51383
diff
changeset
|
927 except KeyError: |
|
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51383
diff
changeset
|
928 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
|
929 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
|
930 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
|
931 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
|
932 # 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
|
933 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
|
934 # 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
|
935 # 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
|
936 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
|
937 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
|
938 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
|
939 return module |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
940 else: |
|
51426
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51383
diff
changeset
|
941 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
|
942 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
|
943 else: |
|
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51383
diff
changeset
|
944 # 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
|
945 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
|
946 if '*' in fromlist and hasattr(module, '__all__'): |
|
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51383
diff
changeset
|
947 fromlist.remove('*') |
|
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51383
diff
changeset
|
948 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
|
949 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
|
950 try: |
|
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51383
diff
changeset
|
951 _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
|
952 except ImportError: |
|
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51383
diff
changeset
|
953 pass |
|
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
51383
diff
changeset
|
954 return module |
