annotate Lib/importlib/abc.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 07229c6104b1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1 """Abstract base classes related to import."""
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
2 from . import _bootstrap
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
3 from . import machinery
76478
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
4 try:
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
5 import _frozen_importlib
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
6 except ImportError as exc:
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
7 if exc.name != '_frozen_importlib':
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
8 raise
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
9 _frozen_importlib = None
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
10 import abc
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
11
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
12
76478
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
13 def _register(abstract_cls, *classes):
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
14 for cls in classes:
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
15 abstract_cls.register(cls)
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
16 if _frozen_importlib is not None:
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
17 frozen_cls = getattr(_frozen_importlib, cls.__name__)
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
18 abstract_cls.register(frozen_cls)
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
19
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
20
78379
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
21 class Finder(metaclass=abc.ABCMeta):
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
22
78485
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78380
diff changeset
23 """Legacy abstract base class for import finders.
78379
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
24
78485
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78380
diff changeset
25 It may be subclassed for compatibility with legacy third party
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78380
diff changeset
26 reimplementations of the import system. Otherwise, finder
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78380
diff changeset
27 implementations should derive from the more specific MetaPathFinder
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78380
diff changeset
28 or PathEntryFinder ABCs.
78379
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
29 """
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
30
78485
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78380
diff changeset
31 @abc.abstractmethod
78379
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
32 def find_module(self, fullname, path=None):
78485
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78380
diff changeset
33 """An abstract method that should find a module.
78379
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
34 The fullname is a str and the optional path is a str or None.
83225
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
35 Returns a Loader object or None.
78379
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
36 """
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
37
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
38
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
39 class MetaPathFinder(Finder):
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
40
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
41 """Abstract base class for import finders on sys.meta_path."""
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
42
87347
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
43 # We don't define find_spec() here since that would break
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
44 # hasattr checks we do to support backward compatibility.
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
45
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
46 # XXX Deprecate
78379
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
47 def find_module(self, fullname, path):
87347
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
48 """Return a loader for the module.
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
49
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
50 If no module is found, return None. The fullname is a str and
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
51 the path is a list of strings or None.
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
52
78379
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
53 """
87347
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
54 return None
78379
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
55
78485
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78380
diff changeset
56 def invalidate_caches(self):
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78380
diff changeset
57 """An optional method for clearing the finder's cache, if any.
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78380
diff changeset
58 This method is used by importlib.invalidate_caches().
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78380
diff changeset
59 """
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78380
diff changeset
60
78379
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
61 _register(MetaPathFinder, machinery.BuiltinImporter, machinery.FrozenImporter,
78380
a1ac1e13c5a0 Close #15519: Properly expose WindowsRegistryFinder in importlib and bring the name into line with normal import terminology. Original patch by Eric Snow
Nick Coghlan <ncoghlan@gmail.com>
parents: 78379
diff changeset
62 machinery.PathFinder, machinery.WindowsRegistryFinder)
78379
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
63
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
64
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
65 class PathEntryFinder(Finder):
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
66
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
67 """Abstract base class for path entry finders used by PathFinder."""
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
68
87347
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
69 # We don't define find_spec() here since that would break
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
70 # hasattr checks we do to support backward compatibility.
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
71
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
72 # XXX Deprecate.
78379
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
73 def find_loader(self, fullname):
87347
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
74 """Return (loader, namespace portion) for the path entry.
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
75
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
76 The fullname is a str. The namespace portion is a sequence of
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
77 path entries contributing to part of a namespace package. The
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
78 sequence may be empty. If loader is not None, the portion will
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
79 be ignored.
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
80
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
81 The portion will be discarded if another path entry finder
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
82 locates the module as a normal module or package.
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
83
78379
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
84 """
83225
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
85 return None, []
78379
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
86
87347
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
87 # XXX Deprecate.
78489
e7a67f1bf604 Issue #15502: Refactor some code.
Brett Cannon <brett@python.org>
parents: 78485
diff changeset
88 find_module = _bootstrap._find_module_shim
78485
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78380
diff changeset
89
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78380
diff changeset
90 def invalidate_caches(self):
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78380
diff changeset
91 """An optional method for clearing the finder's cache, if any.
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78380
diff changeset
92 This method is used by PathFinder.invalidate_caches().
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78380
diff changeset
93 """
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78380
diff changeset
94
78379
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
95 _register(PathEntryFinder, machinery.FileFinder)
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
96
184700df5b6a Issue #15502: Bring the importlib ABCs into line with the current state of the import protocols given PEP 420. Original patch by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78360
diff changeset
97
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
98 class Loader(metaclass=abc.ABCMeta):
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
99
87347
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
100 """Abstract base class for import loaders."""
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
101
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
102 def create_module(self, spec):
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
103 """Return a module to initialize and into which to load.
83225
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
104
87347
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
105 This method should raise ImportError if anything prevents it
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
106 from creating a new module. It may return None to indicate
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
107 that the spec should create the new module.
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
108
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
109 create_module() is optional.
83225
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
110
87347
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
111 """
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
112 # By default, defer to _SpecMethods.create() for the new module.
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
113 return None
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
114
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
115 # We don't define exec_module() here since that would break
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
116 # hasattr checks we do to support backward compatibility.
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
117
87347
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
118 # XXX Deprecate.
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
119 def load_module(self, fullname):
87347
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
120 """Return the loaded module.
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
121
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
122 The module must be added to sys.modules and have import-related
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
123 attributes set properly. The fullname is a str.
83225
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
124
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
125 ImportError is raised on failure.
87347
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
126
83225
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
127 """
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
128 raise ImportError
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
129
87347
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
130 # XXX Deprecate.
78351
d64f84bd1a02 Integration of importdocs from the features/pep-420 repo.
Barry Warsaw <barry@python.org>
parents: 77915
diff changeset
131 def module_repr(self, module):
83225
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
132 """Return a module's repr.
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
133
84000
42a3d182d906 Docstring cleanup
Brett Cannon <brett@python.org>
parents: 83939
diff changeset
134 Used by the module type when the method does not raise
42a3d182d906 Docstring cleanup
Brett Cannon <brett@python.org>
parents: 83939
diff changeset
135 NotImplementedError.
87347
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
136
83225
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
137 """
87347
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
138 # The exception will cause ModuleType.__repr__ to ignore this method.
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
139 raise NotImplementedError
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
140
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
141
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
142 class ResourceLoader(Loader):
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
143
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
144 """Abstract base class for loaders which can return data from their
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
145 back-end storage.
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
146
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
147 This ABC represents one of the optional protocols specified by PEP 302.
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
148
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
149 """
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
150
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
151 @abc.abstractmethod
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
152 def get_data(self, path):
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
153 """Abstract method which when implemented should return the bytes for
67581
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
154 the specified path. The path must be a str."""
83225
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
155 raise IOError
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
156
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
157
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
158 class InspectLoader(Loader):
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
159
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
160 """Abstract base class for loaders which support inspection about the
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
161 modules they can load.
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
162
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
163 This ABC represents one of the optional protocols specified by PEP 302.
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
164
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
165 """
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
166
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
167 def is_package(self, fullname):
87347
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
168 """Optional method which when implemented should return whether the
83225
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
169 module is a package. The fullname is a str. Returns a bool.
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
170
87347
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
171 Raises ImportError if the module cannot be found.
83225
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
172 """
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
173 raise ImportError
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
174
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
175 def get_code(self, fullname):
83939
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
176 """Method which returns the code object for the module.
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
177
83939
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
178 The fullname is a str. Returns a types.CodeType if possible, else
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
179 returns None if a code object does not make sense
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
180 (e.g. built-in module). Raises ImportError if the module cannot be
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
181 found.
83225
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
182 """
83939
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
183 source = self.get_source(fullname)
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
184 if source is None:
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
185 return None
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
186 return self.source_to_code(source)
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
187
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
188 @abc.abstractmethod
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
189 def get_source(self, fullname):
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
190 """Abstract method which should return the source code for the
83225
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
191 module. The fullname is a str. Returns a str.
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
192
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
193 Raises ImportError if the module cannot be found.
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
194 """
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
195 raise ImportError
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
196
83933
0a7d237c0919 Move importlib.abc.SourceLoader.source_to_code() to InspectLoader.
Brett Cannon <brett@python.org>
parents: 83225
diff changeset
197 def source_to_code(self, data, path='<string>'):
0a7d237c0919 Move importlib.abc.SourceLoader.source_to_code() to InspectLoader.
Brett Cannon <brett@python.org>
parents: 83225
diff changeset
198 """Compile 'data' into a code object.
0a7d237c0919 Move importlib.abc.SourceLoader.source_to_code() to InspectLoader.
Brett Cannon <brett@python.org>
parents: 83225
diff changeset
199
0a7d237c0919 Move importlib.abc.SourceLoader.source_to_code() to InspectLoader.
Brett Cannon <brett@python.org>
parents: 83225
diff changeset
200 The 'data' argument can be anything that compile() can handle. The'path'
0a7d237c0919 Move importlib.abc.SourceLoader.source_to_code() to InspectLoader.
Brett Cannon <brett@python.org>
parents: 83225
diff changeset
201 argument should be where the data was retrieved (when applicable)."""
0a7d237c0919 Move importlib.abc.SourceLoader.source_to_code() to InspectLoader.
Brett Cannon <brett@python.org>
parents: 83225
diff changeset
202 return compile(data, path, 'exec', dont_inherit=True)
0a7d237c0919 Move importlib.abc.SourceLoader.source_to_code() to InspectLoader.
Brett Cannon <brett@python.org>
parents: 83225
diff changeset
203
87347
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
204 exec_module = _bootstrap._LoaderBasics.exec_module
84001
e873f2e67353 Issues #18088, 18089: Introduce
Brett Cannon <brett@python.org>
parents: 84000
diff changeset
205 load_module = _bootstrap._LoaderBasics.load_module
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
206
87347
07229c6104b1 Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85967
diff changeset
207 _register(InspectLoader, machinery.BuiltinImporter, machinery.FrozenImporter)
52166
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
208
52083
c06f02a5e962 Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
209
55355
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
210 class ExecutionLoader(InspectLoader):
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
211
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
212 """Abstract base class for loaders that wish to support the execution of
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
213 modules as scripts.
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
214
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
215 This ABC represents one of the optional protocols specified in PEP 302.
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
216
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
217 """
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
218
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
219 @abc.abstractmethod
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
220 def get_filename(self, fullname):
55355
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
221 """Abstract method which should return the value that __file__ is to be
83225
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
222 set to.
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
223
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
224 Raises ImportError if the module cannot be found.
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
225 """
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
226 raise ImportError
55355
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
227
83939
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
228 def get_code(self, fullname):
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
229 """Method to return the code object for fullname.
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
230
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
231 Should return None if not applicable (e.g. built-in module).
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
232 Raise ImportError if the module cannot be found.
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
233 """
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
234 source = self.get_source(fullname)
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
235 if source is None:
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
236 return None
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
237 try:
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
238 path = self.get_filename(fullname)
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
239 except ImportError:
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
240 return self.source_to_code(source)
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
241 else:
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
242 return self.source_to_code(source, path)
11510db74223 Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
Brett Cannon <brett@python.org>
parents: 83933
diff changeset
243
85967
62d045a873bb [issue 19152] Ensure we have actually registered ExtensionFileLoader as an ExecutionLoader.
Eric Snow <ericsnowcurrently@gmail.com>
parents: 85952
diff changeset
244 _register(ExecutionLoader, machinery.ExtensionFileLoader)
55355
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
245
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 55353
diff changeset
246
76478
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
247 class FileLoader(_bootstrap.FileLoader, ResourceLoader, ExecutionLoader):
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
248
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
249 """Abstract base class partially implementing the ResourceLoader and
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
250 ExecutionLoader ABCs."""
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
251
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
252 _register(FileLoader, machinery.SourceFileLoader,
76548
a2cf07135e4f Issue #14605: Rename _SourcelessFileLoader to SourcelessFileLoader.
Marc-Andre Lemburg <mal@egenix.com>
parents: 76547
diff changeset
253 machinery.SourcelessFileLoader)
76478
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
254
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
255
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 62581
diff changeset
256 class SourceLoader(_bootstrap.SourceLoader, ResourceLoader, ExecutionLoader):
62581
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
257
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
258 """Abstract base class for loading source code (and optionally any
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
259 corresponding bytecode).
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
260
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
261 To support loading from source code, the abstractmethods inherited from
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
262 ResourceLoader and ExecutionLoader need to be implemented. To also support
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
263 loading from bytecode, the optional methods specified directly by this ABC
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
264 is required.
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
265
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
266 Inherited abstractmethods not implemented in this ABC:
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
267
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
268 * ResourceLoader.get_data
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
269 * ExecutionLoader.get_filename
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
270
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
271 """
dd30bc5d184e Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents: 57669
diff changeset
272
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
273 def path_mtime(self, path):
67581
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
274 """Return the (int) modification time for the path (str)."""
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73914
diff changeset
275 if self.path_stats.__func__ is SourceLoader.path_stats:
83225
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
276 raise IOError
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73914
diff changeset
277 return int(self.path_stats(path)['mtime'])
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73914
diff changeset
278
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73914
diff changeset
279 def path_stats(self, path):
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73914
diff changeset
280 """Return a metadata dict for the source pointed to by the path (str).
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73914
diff changeset
281 Possible keys:
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73914
diff changeset
282 - 'mtime' (mandatory) is the numeric timestamp of last source
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73914
diff changeset
283 code modification;
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73914
diff changeset
284 - 'size' (optional) is the size in bytes of the source code.
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73914
diff changeset
285 """
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73914
diff changeset
286 if self.path_mtime.__func__ is SourceLoader.path_mtime:
83225
8e733e30edf6 Issue #17093,17566,17567: Methods from classes in importlib.abc now raise/return
Brett Cannon <brett@python.org>
parents: 80265
diff changeset
287 raise IOError
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73914
diff changeset
288 return {'mtime': self.path_mtime(path)}
64236
8f5faf5696e7 While not strictly necessary thanks to the odd ABC inheritance done through
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
289
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 64236
diff changeset
290 def set_data(self, path, data):
64236
8f5faf5696e7 While not strictly necessary thanks to the odd ABC inheritance done through
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
291 """Write the bytes to the path (if possible).
8f5faf5696e7 While not strictly necessary thanks to the odd ABC inheritance done through
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
292
67581
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
293 Accepts a str path and data as bytes.
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
294
64236
8f5faf5696e7 While not strictly necessary thanks to the odd ABC inheritance done through
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
295 Any needed intermediary directories are to be created. If for some
8f5faf5696e7 While not strictly necessary thanks to the odd ABC inheritance done through
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
296 reason the file cannot be written because of permissions, fail
8f5faf5696e7 While not strictly necessary thanks to the odd ABC inheritance done through
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
297 silently.
8f5faf5696e7 While not strictly necessary thanks to the odd ABC inheritance done through
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
298 """
8f5faf5696e7 While not strictly necessary thanks to the odd ABC inheritance done through
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
299
76478
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
300 _register(SourceLoader, machinery.SourceFileLoader)