Mercurial > cpython-lru_cache-weakref
annotate Lib/importlib/__init__.py @ 79691:05c8928362ee
Merge __next__ method link fixes with 3.3.
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Fri, 12 Oct 2012 13:46:37 +0300 |
| parents | 0a75ce232f56 |
| children | 358be7742377 |
| rev | line source |
|---|---|
|
75324
de125ee32897
Update importlib.invalidate_caches() to be more general.
Brett Cannon <brett@python.org>
parents:
75048
diff
changeset
|
1 """A pure Python implementation of import.""" |
|
75048
bbaab666e6c7
Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74839
diff
changeset
|
2 __all__ = ['__import__', 'import_module', 'invalidate_caches'] |
|
52138
6b024ff5b58f
Define importlib.__init__.__all__.
Brett Cannon <bcannon@gmail.com>
parents:
51991
diff
changeset
|
3 |
|
50984
dd638b7e747e
Add some comment markers to more clearly delineate what different parts of the
Brett Cannon <bcannon@gmail.com>
parents:
50922
diff
changeset
|
4 # Bootstrap help ##################################################### |
|
78195
4431dc4bb770
Close #15386: There was a loophole that meant importlib.machinery and imp would sometimes reference an uninitialised copy of importlib._bootstrap
Nick Coghlan <ncoghlan@gmail.com>
parents:
77945
diff
changeset
|
5 |
|
4431dc4bb770
Close #15386: There was a loophole that meant importlib.machinery and imp would sometimes reference an uninitialised copy of importlib._bootstrap
Nick Coghlan <ncoghlan@gmail.com>
parents:
77945
diff
changeset
|
6 # Until bootstrapping is complete, DO NOT import any modules that attempt |
|
4431dc4bb770
Close #15386: There was a loophole that meant importlib.machinery and imp would sometimes reference an uninitialised copy of importlib._bootstrap
Nick Coghlan <ncoghlan@gmail.com>
parents:
77945
diff
changeset
|
7 # to import importlib._bootstrap (directly or indirectly). Since this |
|
4431dc4bb770
Close #15386: There was a loophole that meant importlib.machinery and imp would sometimes reference an uninitialised copy of importlib._bootstrap
Nick Coghlan <ncoghlan@gmail.com>
parents:
77945
diff
changeset
|
8 # partially initialised package would be present in sys.modules, those |
|
4431dc4bb770
Close #15386: There was a loophole that meant importlib.machinery and imp would sometimes reference an uninitialised copy of importlib._bootstrap
Nick Coghlan <ncoghlan@gmail.com>
parents:
77945
diff
changeset
|
9 # modules would get an uninitialised copy of the source version, instead |
|
4431dc4bb770
Close #15386: There was a loophole that meant importlib.machinery and imp would sometimes reference an uninitialised copy of importlib._bootstrap
Nick Coghlan <ncoghlan@gmail.com>
parents:
77945
diff
changeset
|
10 # of a fully initialised version (either the frozen one or the one |
|
4431dc4bb770
Close #15386: There was a loophole that meant importlib.machinery and imp would sometimes reference an uninitialised copy of importlib._bootstrap
Nick Coghlan <ncoghlan@gmail.com>
parents:
77945
diff
changeset
|
11 # initialised below if the frozen one is not available). |
|
4431dc4bb770
Close #15386: There was a loophole that meant importlib.machinery and imp would sometimes reference an uninitialised copy of importlib._bootstrap
Nick Coghlan <ncoghlan@gmail.com>
parents:
77945
diff
changeset
|
12 import _imp # Just the builtin component, NOT the full Python module |
|
74839
322e6f818cd2
Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents:
74643
diff
changeset
|
13 import sys |
|
50984
dd638b7e747e
Add some comment markers to more clearly delineate what different parts of the
Brett Cannon <bcannon@gmail.com>
parents:
50922
diff
changeset
|
14 |
|
77500
e3a984076837
Issue #14657: The frozen instance of importlib used for bootstrap is now also the module imported as importlib._bootstrap.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76891
diff
changeset
|
15 try: |
|
77945
906c69928049
Issue #15210: Greatly simplify the test for supporting importlib
Brett Cannon <brett@python.org>
parents:
77916
diff
changeset
|
16 import _frozen_importlib as _bootstrap |
|
906c69928049
Issue #15210: Greatly simplify the test for supporting importlib
Brett Cannon <brett@python.org>
parents:
77916
diff
changeset
|
17 except ImportError: |
|
77500
e3a984076837
Issue #14657: The frozen instance of importlib used for bootstrap is now also the module imported as importlib._bootstrap.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76891
diff
changeset
|
18 from . import _bootstrap |
|
78195
4431dc4bb770
Close #15386: There was a loophole that meant importlib.machinery and imp would sometimes reference an uninitialised copy of importlib._bootstrap
Nick Coghlan <ncoghlan@gmail.com>
parents:
77945
diff
changeset
|
19 _bootstrap._setup(sys, _imp) |
|
77500
e3a984076837
Issue #14657: The frozen instance of importlib used for bootstrap is now also the module imported as importlib._bootstrap.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76891
diff
changeset
|
20 else: |
|
e3a984076837
Issue #14657: The frozen instance of importlib used for bootstrap is now also the module imported as importlib._bootstrap.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76891
diff
changeset
|
21 # importlib._bootstrap is the built-in import, ensure we don't create |
|
e3a984076837
Issue #14657: The frozen instance of importlib used for bootstrap is now also the module imported as importlib._bootstrap.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76891
diff
changeset
|
22 # a second copy of the module. |
|
e3a984076837
Issue #14657: The frozen instance of importlib used for bootstrap is now also the module imported as importlib._bootstrap.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76891
diff
changeset
|
23 _bootstrap.__name__ = 'importlib._bootstrap' |
|
e3a984076837
Issue #14657: The frozen instance of importlib used for bootstrap is now also the module imported as importlib._bootstrap.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76891
diff
changeset
|
24 _bootstrap.__package__ = 'importlib' |
|
e3a984076837
Issue #14657: The frozen instance of importlib used for bootstrap is now also the module imported as importlib._bootstrap.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76891
diff
changeset
|
25 _bootstrap.__file__ = __file__.replace('__init__.py', '_bootstrap.py') |
|
e3a984076837
Issue #14657: The frozen instance of importlib used for bootstrap is now also the module imported as importlib._bootstrap.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76891
diff
changeset
|
26 sys.modules['importlib._bootstrap'] = _bootstrap |
|
e3a984076837
Issue #14657: The frozen instance of importlib used for bootstrap is now also the module imported as importlib._bootstrap.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76891
diff
changeset
|
27 |
|
e3a984076837
Issue #14657: The frozen instance of importlib used for bootstrap is now also the module imported as importlib._bootstrap.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76891
diff
changeset
|
28 # To simplify imports in test code |
|
e3a984076837
Issue #14657: The frozen instance of importlib used for bootstrap is now also the module imported as importlib._bootstrap.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76891
diff
changeset
|
29 _w_long = _bootstrap._w_long |
|
e3a984076837
Issue #14657: The frozen instance of importlib used for bootstrap is now also the module imported as importlib._bootstrap.
Antoine Pitrou <solipsis@pitrou.net>
parents:
76891
diff
changeset
|
30 _r_long = _bootstrap._r_long |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
31 |
|
78195
4431dc4bb770
Close #15386: There was a loophole that meant importlib.machinery and imp would sometimes reference an uninitialised copy of importlib._bootstrap
Nick Coghlan <ncoghlan@gmail.com>
parents:
77945
diff
changeset
|
32 # Fully bootstrapped at this point, import whatever you like, circular |
|
4431dc4bb770
Close #15386: There was a loophole that meant importlib.machinery and imp would sometimes reference an uninitialised copy of importlib._bootstrap
Nick Coghlan <ncoghlan@gmail.com>
parents:
77945
diff
changeset
|
33 # dependencies and startup overhead minimisation permitting :) |
|
50922
a63f7cf3793a
Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
34 |
|
50984
dd638b7e747e
Add some comment markers to more clearly delineate what different parts of the
Brett Cannon <bcannon@gmail.com>
parents:
50922
diff
changeset
|
35 # Public API ######################################################### |
|
dd638b7e747e
Add some comment markers to more clearly delineate what different parts of the
Brett Cannon <bcannon@gmail.com>
parents:
50922
diff
changeset
|
36 |
|
75324
de125ee32897
Update importlib.invalidate_caches() to be more general.
Brett Cannon <brett@python.org>
parents:
75048
diff
changeset
|
37 from ._bootstrap import __import__ |
|
de125ee32897
Update importlib.invalidate_caches() to be more general.
Brett Cannon <brett@python.org>
parents:
75048
diff
changeset
|
38 |
|
de125ee32897
Update importlib.invalidate_caches() to be more general.
Brett Cannon <brett@python.org>
parents:
75048
diff
changeset
|
39 |
|
de125ee32897
Update importlib.invalidate_caches() to be more general.
Brett Cannon <brett@python.org>
parents:
75048
diff
changeset
|
40 def invalidate_caches(): |
|
78485
0a75ce232f56
Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents:
78195
diff
changeset
|
41 """Call the invalidate_caches() method on all meta path finders stored in |
|
0a75ce232f56
Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents:
78195
diff
changeset
|
42 sys.meta_path (where implemented).""" |
|
0a75ce232f56
Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents:
78195
diff
changeset
|
43 for finder in sys.meta_path: |
|
75324
de125ee32897
Update importlib.invalidate_caches() to be more general.
Brett Cannon <brett@python.org>
parents:
75048
diff
changeset
|
44 if hasattr(finder, 'invalidate_caches'): |
|
de125ee32897
Update importlib.invalidate_caches() to be more general.
Brett Cannon <brett@python.org>
parents:
75048
diff
changeset
|
45 finder.invalidate_caches() |
|
50922
a63f7cf3793a
Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
46 |
|
a63f7cf3793a
Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
47 |
|
76891
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
48 def find_loader(name, path=None): |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
49 """Find the loader for the specified module. |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
50 |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
51 First, sys.modules is checked to see if the module was already imported. If |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
52 so, then sys.modules[name].__loader__ is returned. If that happens to be |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
53 set to None, then ValueError is raised. If the module is not in |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
54 sys.modules, then sys.meta_path is searched for a suitable loader with the |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
55 value of 'path' given to the finders. None is returned if no loader could |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
56 be found. |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
57 |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
58 Dotted names do not have their parent packages implicitly imported. |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
59 |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
60 """ |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
61 try: |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
62 loader = sys.modules[name].__loader__ |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
63 if loader is None: |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
64 raise ValueError('{}.__loader__ is None'.format(name)) |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
65 else: |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
66 return loader |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
67 except KeyError: |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
68 pass |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
69 return _bootstrap._find_module(name, path) |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
70 |
|
7bf8ac742d2f
Issue #13959: Introduce importlib.find_loader().
Brett Cannon <brett@python.org>
parents:
75324
diff
changeset
|
71 |
|
50922
a63f7cf3793a
Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
72 def import_module(name, package=None): |
|
a63f7cf3793a
Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
73 """Import a module. |
|
a63f7cf3793a
Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
74 |
|
a63f7cf3793a
Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
75 The 'package' argument is required when performing a relative import. It |
|
a63f7cf3793a
Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
76 specifies the package to use as the anchor point from which to resolve the |
|
a63f7cf3793a
Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
77 relative import to an absolute import. |
|
a63f7cf3793a
Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
78 |
|
a63f7cf3793a
Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
79 """ |
|
51426
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
50984
diff
changeset
|
80 level = 0 |
|
50922
a63f7cf3793a
Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
81 if name.startswith('.'): |
|
a63f7cf3793a
Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
82 if not package: |
|
a63f7cf3793a
Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
83 raise TypeError("relative imports require the 'package' argument") |
|
a63f7cf3793a
Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
84 for character in name: |
|
a63f7cf3793a
Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
85 if character != '.': |
|
a63f7cf3793a
Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
86 break |
|
a63f7cf3793a
Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
87 level += 1 |
|
51426
7a1356d2aeef
Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents:
50984
diff
changeset
|
88 return _bootstrap._gcd_import(name[level:], package, level) |
