Mercurial > cpython-lru_cache-weakref
annotate Lib/imp.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 | 70bd6f7e013b |
| children |
| rev | line source |
|---|---|
|
76331
d777f854a66e
Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
1 """This module provides the components needed to build your own __import__ |
|
d777f854a66e
Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
2 function. Undocumented functions are obsolete. |
|
d777f854a66e
Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
3 |
|
d777f854a66e
Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
4 In most cases it is preferred you consider using the importlib module's |
|
d777f854a66e
Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
5 functionality over this module. |
|
d777f854a66e
Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
6 |
|
d777f854a66e
Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
7 """ |
|
d777f854a66e
Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
8 # (Probably) need to stay in _imp |
|
76640
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
9 from _imp import (lock_held, acquire_lock, release_lock, |
|
82719
d5aa922f97f9
Issue #16880: _imp.load_dynamic() is not defined on a platform that
Brett Cannon <brett@python.org>
parents:
78816
diff
changeset
|
10 get_frozen_object, is_frozen_package, |
|
76336
60acd04d5613
Consider load_dynamic() something to keep in _imp.
Brett Cannon <brett@python.org>
parents:
76331
diff
changeset
|
11 init_builtin, init_frozen, is_builtin, is_frozen, |
|
78486
1db6553f3f8c
Issue #15576: Allow extension modules to be a package's __init__
Brett Cannon <brett@python.org>
parents:
78078
diff
changeset
|
12 _fix_co_filename) |
|
82719
d5aa922f97f9
Issue #16880: _imp.load_dynamic() is not defined on a platform that
Brett Cannon <brett@python.org>
parents:
78816
diff
changeset
|
13 try: |
|
d5aa922f97f9
Issue #16880: _imp.load_dynamic() is not defined on a platform that
Brett Cannon <brett@python.org>
parents:
78816
diff
changeset
|
14 from _imp import load_dynamic |
|
d5aa922f97f9
Issue #16880: _imp.load_dynamic() is not defined on a platform that
Brett Cannon <brett@python.org>
parents:
78816
diff
changeset
|
15 except ImportError: |
|
d5aa922f97f9
Issue #16880: _imp.load_dynamic() is not defined on a platform that
Brett Cannon <brett@python.org>
parents:
78816
diff
changeset
|
16 # Platform doesn't support dynamic loading. |
|
d5aa922f97f9
Issue #16880: _imp.load_dynamic() is not defined on a platform that
Brett Cannon <brett@python.org>
parents:
78816
diff
changeset
|
17 load_dynamic = None |
|
76331
d777f854a66e
Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents:
diff
changeset
|
18 |
|
87347
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
19 from importlib._bootstrap import SourcelessFileLoader, _ERR_MSG, _SpecMethods |
|
76338
2df37938b8e1
Issue #13959: Re-implement imp.load_module() in imp.py.
Brett Cannon <brett@python.org>
parents:
76336
diff
changeset
|
20 |
|
76874
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
21 from importlib import machinery |
|
84128
5619bc2d8207
Issue #18192: Introduce importlib.util.MAGIC_NUMBER and document the
Brett Cannon <brett@python.org>
parents:
84123
diff
changeset
|
22 from importlib import util |
|
84123
01da7bf11ca1
Issue #18193: Add importlib.reload(), documenting (but not
Brett Cannon <brett@python.org>
parents:
84114
diff
changeset
|
23 import importlib |
|
76342
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
24 import os |
|
76455
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
25 import sys |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
26 import tokenize |
|
84134
9cacdb9d0c59
Issue #17907: touch up the code for imp.new_module().
Brett Cannon <brett@python.org>
parents:
84128
diff
changeset
|
27 import types |
|
76874
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
28 import warnings |
|
76455
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
29 |
|
84167
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
30 warnings.warn("the imp module is deprecated in favour of importlib; " |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
31 "see the module's documentation for alternative uses", |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
32 PendingDeprecationWarning) |
|
76455
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
33 |
| 76877 | 34 # DEPRECATED |
|
76455
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
35 SEARCH_ERROR = 0 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
36 PY_SOURCE = 1 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
37 PY_COMPILED = 2 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
38 C_EXTENSION = 3 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
39 PY_RESOURCE = 4 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
40 PKG_DIRECTORY = 5 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
41 C_BUILTIN = 6 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
42 PY_FROZEN = 7 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
43 PY_CODERESOURCE = 8 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
44 IMP_HOOK = 9 |
|
76342
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
45 |
|
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
46 |
|
84134
9cacdb9d0c59
Issue #17907: touch up the code for imp.new_module().
Brett Cannon <brett@python.org>
parents:
84128
diff
changeset
|
47 def new_module(name): |
|
9cacdb9d0c59
Issue #17907: touch up the code for imp.new_module().
Brett Cannon <brett@python.org>
parents:
84128
diff
changeset
|
48 """**DEPRECATED** |
|
9cacdb9d0c59
Issue #17907: touch up the code for imp.new_module().
Brett Cannon <brett@python.org>
parents:
84128
diff
changeset
|
49 |
|
9cacdb9d0c59
Issue #17907: touch up the code for imp.new_module().
Brett Cannon <brett@python.org>
parents:
84128
diff
changeset
|
50 Create a new module. |
|
9cacdb9d0c59
Issue #17907: touch up the code for imp.new_module().
Brett Cannon <brett@python.org>
parents:
84128
diff
changeset
|
51 |
|
9cacdb9d0c59
Issue #17907: touch up the code for imp.new_module().
Brett Cannon <brett@python.org>
parents:
84128
diff
changeset
|
52 The module is not entered into sys.modules. |
|
9cacdb9d0c59
Issue #17907: touch up the code for imp.new_module().
Brett Cannon <brett@python.org>
parents:
84128
diff
changeset
|
53 |
|
9cacdb9d0c59
Issue #17907: touch up the code for imp.new_module().
Brett Cannon <brett@python.org>
parents:
84128
diff
changeset
|
54 """ |
|
9cacdb9d0c59
Issue #17907: touch up the code for imp.new_module().
Brett Cannon <brett@python.org>
parents:
84128
diff
changeset
|
55 return types.ModuleType(name) |
|
9cacdb9d0c59
Issue #17907: touch up the code for imp.new_module().
Brett Cannon <brett@python.org>
parents:
84128
diff
changeset
|
56 |
|
9cacdb9d0c59
Issue #17907: touch up the code for imp.new_module().
Brett Cannon <brett@python.org>
parents:
84128
diff
changeset
|
57 |
|
78042
efb5e6ab10f4
Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents:
78026
diff
changeset
|
58 def get_magic(): |
|
84128
5619bc2d8207
Issue #18192: Introduce importlib.util.MAGIC_NUMBER and document the
Brett Cannon <brett@python.org>
parents:
84123
diff
changeset
|
59 """**DEPRECATED** |
|
5619bc2d8207
Issue #18192: Introduce importlib.util.MAGIC_NUMBER and document the
Brett Cannon <brett@python.org>
parents:
84123
diff
changeset
|
60 |
|
5619bc2d8207
Issue #18192: Introduce importlib.util.MAGIC_NUMBER and document the
Brett Cannon <brett@python.org>
parents:
84123
diff
changeset
|
61 Return the magic number for .pyc or .pyo files. |
|
5619bc2d8207
Issue #18192: Introduce importlib.util.MAGIC_NUMBER and document the
Brett Cannon <brett@python.org>
parents:
84123
diff
changeset
|
62 """ |
|
5619bc2d8207
Issue #18192: Introduce importlib.util.MAGIC_NUMBER and document the
Brett Cannon <brett@python.org>
parents:
84123
diff
changeset
|
63 return util.MAGIC_NUMBER |
|
78042
efb5e6ab10f4
Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents:
78026
diff
changeset
|
64 |
|
efb5e6ab10f4
Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents:
78026
diff
changeset
|
65 |
|
77917
b36bed82c9d0
Issue #15166: Re-implement imp.get_tag() using sys.implementation.
Brett Cannon <brett@python.org>
parents:
77456
diff
changeset
|
66 def get_tag(): |
|
b36bed82c9d0
Issue #15166: Re-implement imp.get_tag() using sys.implementation.
Brett Cannon <brett@python.org>
parents:
77456
diff
changeset
|
67 """Return the magic tag for .pyc or .pyo files.""" |
|
b36bed82c9d0
Issue #15166: Re-implement imp.get_tag() using sys.implementation.
Brett Cannon <brett@python.org>
parents:
77456
diff
changeset
|
68 return sys.implementation.cache_tag |
|
b36bed82c9d0
Issue #15166: Re-implement imp.get_tag() using sys.implementation.
Brett Cannon <brett@python.org>
parents:
77456
diff
changeset
|
69 |
|
b36bed82c9d0
Issue #15166: Re-implement imp.get_tag() using sys.implementation.
Brett Cannon <brett@python.org>
parents:
77456
diff
changeset
|
70 |
|
84136
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
71 def cache_from_source(path, debug_override=None): |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
72 """**DEPRECATED** |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
73 |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
74 Given the path to a .py file, return the path to its .pyc/.pyo file. |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
75 |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
76 The .py file does not need to exist; this simply returns the path to the |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
77 .pyc/.pyo file calculated as if the .py file were imported. The extension |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
78 will be .pyc unless sys.flags.optimize is non-zero, then it will be .pyo. |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
79 |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
80 If debug_override is not None, then it must be a boolean and is used in |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
81 place of sys.flags.optimize. |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
82 |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
83 If sys.implementation.cache_tag is None then NotImplementedError is raised. |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
84 |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
85 """ |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
86 return util.cache_from_source(path, debug_override) |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
87 |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
88 |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
89 def source_from_cache(path): |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
90 """**DEPRECATED** |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
91 |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
92 Given the path to a .pyc./.pyo file, return the path to its .py file. |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
93 |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
94 The .pyc/.pyo file does not need to exist; this simply returns the path to |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
95 the .py file calculated to correspond to the .pyc/.pyo file. If path does |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
96 not conform to PEP 3147 format, ValueError will be raised. If |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
97 sys.implementation.cache_tag is None then NotImplementedError is raised. |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
98 |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
99 """ |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
100 return util.source_from_cache(path) |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
101 |
|
32067804942e
Issue #18194: Introduce importlib.util.cache_from_source() and
Brett Cannon <brett@python.org>
parents:
84135
diff
changeset
|
102 |
|
76754
257cbd2fac38
Issue #13959: Re-implement imp.get_suffixes() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76753
diff
changeset
|
103 def get_suffixes(): |
|
84167
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
104 """**DEPRECATED**""" |
|
78486
1db6553f3f8c
Issue #15576: Allow extension modules to be a package's __init__
Brett Cannon <brett@python.org>
parents:
78078
diff
changeset
|
105 extensions = [(s, 'rb', C_EXTENSION) for s in machinery.EXTENSION_SUFFIXES] |
|
87448
70bd6f7e013b
Issue #15204: Deprecated the 'U' mode in file-like objects.
Serhiy Storchaka <storchaka@gmail.com>
parents:
87347
diff
changeset
|
106 source = [(s, 'r', PY_SOURCE) for s in machinery.SOURCE_SUFFIXES] |
|
76874
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
107 bytecode = [(s, 'rb', PY_COMPILED) for s in machinery.BYTECODE_SUFFIXES] |
|
76754
257cbd2fac38
Issue #13959: Re-implement imp.get_suffixes() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76753
diff
changeset
|
108 |
|
257cbd2fac38
Issue #13959: Re-implement imp.get_suffixes() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76753
diff
changeset
|
109 return extensions + source + bytecode |
|
257cbd2fac38
Issue #13959: Re-implement imp.get_suffixes() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76753
diff
changeset
|
110 |
|
257cbd2fac38
Issue #13959: Re-implement imp.get_suffixes() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76753
diff
changeset
|
111 |
|
76633
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
112 class NullImporter: |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
113 |
|
84167
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
114 """**DEPRECATED** |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
115 |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
116 Null import object. |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
117 |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
118 """ |
|
76633
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
119 |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
120 def __init__(self, path): |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
121 if path == '': |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
122 raise ImportError('empty pathname', path='') |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
123 elif os.path.isdir(path): |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
124 raise ImportError('existing directory', path=path) |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
125 |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
126 def find_module(self, fullname): |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
127 """Always returns None.""" |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
128 return None |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
129 |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
130 |
|
76381
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
131 class _HackedGetData: |
|
76371
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
132 |
|
76381
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
133 """Compatibiilty support for 'file' arguments of various load_*() |
|
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
134 functions.""" |
|
76371
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
135 |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
136 def __init__(self, fullname, path, file=None): |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
137 super().__init__(fullname, path) |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
138 self.file = file |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
139 |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
140 def get_data(self, path): |
|
76381
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
141 """Gross hack to contort loader to deal w/ load_*()'s bad API.""" |
|
76478
1da623513b26
Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents:
76456
diff
changeset
|
142 if self.file and path == self.path: |
|
85337
ddd610cb65ef
Issue #18755: Allow imp.load_*() loaders to have get_data() called
Brett Cannon <brett@python.org>
parents:
85173
diff
changeset
|
143 if not self.file.closed: |
|
ddd610cb65ef
Issue #18755: Allow imp.load_*() loaders to have get_data() called
Brett Cannon <brett@python.org>
parents:
85173
diff
changeset
|
144 file = self.file |
|
ddd610cb65ef
Issue #18755: Allow imp.load_*() loaders to have get_data() called
Brett Cannon <brett@python.org>
parents:
85173
diff
changeset
|
145 else: |
|
ddd610cb65ef
Issue #18755: Allow imp.load_*() loaders to have get_data() called
Brett Cannon <brett@python.org>
parents:
85173
diff
changeset
|
146 self.file = file = open(self.path, 'r') |
|
ddd610cb65ef
Issue #18755: Allow imp.load_*() loaders to have get_data() called
Brett Cannon <brett@python.org>
parents:
85173
diff
changeset
|
147 |
|
ddd610cb65ef
Issue #18755: Allow imp.load_*() loaders to have get_data() called
Brett Cannon <brett@python.org>
parents:
85173
diff
changeset
|
148 with file: |
|
76371
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
149 # Technically should be returning bytes, but |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
150 # SourceLoader.get_code() just passed what is returned to |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
151 # compile() which can handle str. And converting to bytes would |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
152 # require figuring out the encoding to decode to and |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
153 # tokenize.detect_encoding() only accepts bytes. |
|
85337
ddd610cb65ef
Issue #18755: Allow imp.load_*() loaders to have get_data() called
Brett Cannon <brett@python.org>
parents:
85173
diff
changeset
|
154 return file.read() |
|
76371
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
155 else: |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
156 return super().get_data(path) |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
157 |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
158 |
|
84135
7bb54aa762e1
Make it more obvious what things used in imp are snuck in through private APIs
Brett Cannon <brett@python.org>
parents:
84134
diff
changeset
|
159 class _LoadSourceCompatibility(_HackedGetData, machinery.SourceFileLoader): |
|
76381
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
160 |
|
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
161 """Compatibility support for implementing load_source().""" |
|
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
162 |
|
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
163 |
|
76371
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
164 def load_source(name, pathname, file=None): |
|
87347
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
165 loader = _LoadSourceCompatibility(name, pathname, file) |
|
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
166 spec = util.spec_from_file_location(name, pathname, loader=loader) |
|
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
167 methods = _SpecMethods(spec) |
|
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
168 if name in sys.modules: |
|
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
169 module = methods.exec(sys.modules[name]) |
|
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
170 else: |
|
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
171 module = methods.load() |
|
83541
3dcc81c2eef5
Issue #17358: imp.load_source() and load_compiled() should now return
Brett Cannon <brett@python.org>
parents:
82719
diff
changeset
|
172 # To allow reloading to potentially work, use a non-hacked loader which |
|
3dcc81c2eef5
Issue #17358: imp.load_source() and load_compiled() should now return
Brett Cannon <brett@python.org>
parents:
82719
diff
changeset
|
173 # won't rely on a now-closed file object. |
|
84135
7bb54aa762e1
Make it more obvious what things used in imp are snuck in through private APIs
Brett Cannon <brett@python.org>
parents:
84134
diff
changeset
|
174 module.__loader__ = machinery.SourceFileLoader(name, pathname) |
|
87347
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
175 module.__spec__.loader = module.__loader__ |
|
83541
3dcc81c2eef5
Issue #17358: imp.load_source() and load_compiled() should now return
Brett Cannon <brett@python.org>
parents:
82719
diff
changeset
|
176 return module |
|
76371
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
177 |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
178 |
|
84135
7bb54aa762e1
Make it more obvious what things used in imp are snuck in through private APIs
Brett Cannon <brett@python.org>
parents:
84134
diff
changeset
|
179 class _LoadCompiledCompatibility(_HackedGetData, SourcelessFileLoader): |
|
76381
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
180 |
|
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
181 """Compatibility support for implementing load_compiled().""" |
|
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
182 |
|
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
183 |
|
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
184 def load_compiled(name, pathname, file=None): |
|
84167
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
185 """**DEPRECATED**""" |
|
87347
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
186 loader = _LoadCompiledCompatibility(name, pathname, file) |
|
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
187 spec = util.spec_from_file_location(name, pathname, loader=loader) |
|
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
188 methods = _SpecMethods(spec) |
|
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
189 if name in sys.modules: |
|
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
190 module = methods.exec(sys.modules[name]) |
|
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
191 else: |
|
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
192 module = methods.load() |
|
83541
3dcc81c2eef5
Issue #17358: imp.load_source() and load_compiled() should now return
Brett Cannon <brett@python.org>
parents:
82719
diff
changeset
|
193 # To allow reloading to potentially work, use a non-hacked loader which |
|
3dcc81c2eef5
Issue #17358: imp.load_source() and load_compiled() should now return
Brett Cannon <brett@python.org>
parents:
82719
diff
changeset
|
194 # won't rely on a now-closed file object. |
|
84135
7bb54aa762e1
Make it more obvious what things used in imp are snuck in through private APIs
Brett Cannon <brett@python.org>
parents:
84134
diff
changeset
|
195 module.__loader__ = SourcelessFileLoader(name, pathname) |
|
87347
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
196 module.__spec__.loader = module.__loader__ |
|
83541
3dcc81c2eef5
Issue #17358: imp.load_source() and load_compiled() should now return
Brett Cannon <brett@python.org>
parents:
82719
diff
changeset
|
197 return module |
|
76381
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
198 |
|
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
199 |
|
76342
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
200 def load_package(name, path): |
|
84167
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
201 """**DEPRECATED**""" |
|
76342
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
202 if os.path.isdir(path): |
| 76877 | 203 extensions = (machinery.SOURCE_SUFFIXES[:] + |
| 204 machinery.BYTECODE_SUFFIXES[:]) | |
|
76342
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
205 for extension in extensions: |
|
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
206 path = os.path.join(path, '__init__'+extension) |
|
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
207 if os.path.exists(path): |
|
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
208 break |
|
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
209 else: |
|
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
210 raise ValueError('{!r} is not a package'.format(path)) |
|
87347
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
211 spec = util.spec_from_file_location(name, path, |
|
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
212 submodule_search_locations=[]) |
|
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
213 methods = _SpecMethods(spec) |
|
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
214 if name in sys.modules: |
|
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
215 return methods.exec(sys.modules[name]) |
|
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
216 else: |
|
07229c6104b1
Implement PEP 451 (ModuleSpec).
Eric Snow <ericsnowcurrently@gmail.com>
parents:
85339
diff
changeset
|
217 return methods.load() |
|
76342
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
218 |
|
76338
2df37938b8e1
Issue #13959: Re-implement imp.load_module() in imp.py.
Brett Cannon <brett@python.org>
parents:
76336
diff
changeset
|
219 |
|
2df37938b8e1
Issue #13959: Re-implement imp.load_module() in imp.py.
Brett Cannon <brett@python.org>
parents:
76336
diff
changeset
|
220 def load_module(name, file, filename, details): |
|
77456
034c814eb187
Issue #13959: Add to imp.find_module() and load_module's docstrings
Brett Cannon <brett@python.org>
parents:
76877
diff
changeset
|
221 """**DEPRECATED** |
|
034c814eb187
Issue #13959: Add to imp.find_module() and load_module's docstrings
Brett Cannon <brett@python.org>
parents:
76877
diff
changeset
|
222 |
|
034c814eb187
Issue #13959: Add to imp.find_module() and load_module's docstrings
Brett Cannon <brett@python.org>
parents:
76877
diff
changeset
|
223 Load a module, given information returned by find_module(). |
|
76338
2df37938b8e1
Issue #13959: Re-implement imp.load_module() in imp.py.
Brett Cannon <brett@python.org>
parents:
76336
diff
changeset
|
224 |
|
2df37938b8e1
Issue #13959: Re-implement imp.load_module() in imp.py.
Brett Cannon <brett@python.org>
parents:
76336
diff
changeset
|
225 The module name must include the full package name, if any. |
|
2df37938b8e1
Issue #13959: Re-implement imp.load_module() in imp.py.
Brett Cannon <brett@python.org>
parents:
76336
diff
changeset
|
226 |
|
2df37938b8e1
Issue #13959: Re-implement imp.load_module() in imp.py.
Brett Cannon <brett@python.org>
parents:
76336
diff
changeset
|
227 """ |
|
2df37938b8e1
Issue #13959: Re-implement imp.load_module() in imp.py.
Brett Cannon <brett@python.org>
parents:
76336
diff
changeset
|
228 suffix, mode, type_ = details |
|
84167
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
229 if mode and (not mode.startswith(('r', 'U')) or '+' in mode): |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
230 raise ValueError('invalid file open mode {!r}'.format(mode)) |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
231 elif file is None and type_ in {PY_SOURCE, PY_COMPILED}: |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
232 msg = 'file object required for import (type code {})'.format(type_) |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
233 raise ValueError(msg) |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
234 elif type_ == PY_SOURCE: |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
235 return load_source(name, filename, file) |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
236 elif type_ == PY_COMPILED: |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
237 return load_compiled(name, filename, file) |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
238 elif type_ == C_EXTENSION and load_dynamic is not None: |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
239 if file is None: |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
240 with open(filename, 'rb') as opened_file: |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
241 return load_dynamic(name, filename, opened_file) |
| 76877 | 242 else: |
|
84167
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
243 return load_dynamic(name, filename, file) |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
244 elif type_ == PKG_DIRECTORY: |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
245 return load_package(name, filename) |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
246 elif type_ == C_BUILTIN: |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
247 return init_builtin(name) |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
248 elif type_ == PY_FROZEN: |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
249 return init_frozen(name) |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
250 else: |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
251 msg = "Don't know how to import {} (type code {})".format(name, type_) |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
252 raise ImportError(msg, name=name) |
|
76455
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
253 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
254 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
255 def find_module(name, path=None): |
|
77456
034c814eb187
Issue #13959: Add to imp.find_module() and load_module's docstrings
Brett Cannon <brett@python.org>
parents:
76877
diff
changeset
|
256 """**DEPRECATED** |
|
034c814eb187
Issue #13959: Add to imp.find_module() and load_module's docstrings
Brett Cannon <brett@python.org>
parents:
76877
diff
changeset
|
257 |
|
034c814eb187
Issue #13959: Add to imp.find_module() and load_module's docstrings
Brett Cannon <brett@python.org>
parents:
76877
diff
changeset
|
258 Search for a module. |
|
76455
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
259 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
260 If path is omitted or None, search for a built-in, frozen or special |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
261 module and continue search in sys.path. The module name cannot |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
262 contain '.'; to search for a submodule of a package, pass the |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
263 submodule name and the package's __path__. |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
264 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
265 """ |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
266 if not isinstance(name, str): |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
267 raise TypeError("'name' must be a str, not {}".format(type(name))) |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
268 elif not isinstance(path, (type(None), list)): |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
269 # Backwards-compatibility |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
270 raise RuntimeError("'list' must be None or a list, " |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
271 "not {}".format(type(name))) |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
272 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
273 if path is None: |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
274 if is_builtin(name): |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
275 return None, None, ('', '', C_BUILTIN) |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
276 elif is_frozen(name): |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
277 return None, None, ('', '', PY_FROZEN) |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
278 else: |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
279 path = sys.path |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
280 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
281 for entry in path: |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
282 package_directory = os.path.join(entry, name) |
|
76874
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
283 for suffix in ['.py', machinery.BYTECODE_SUFFIXES[0]]: |
|
76455
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
284 package_file_name = '__init__' + suffix |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
285 file_path = os.path.join(package_directory, package_file_name) |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
286 if os.path.isfile(file_path): |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
287 return None, package_directory, ('', '', PKG_DIRECTORY) |
|
84167
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
288 for suffix, mode, type_ in get_suffixes(): |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
289 file_name = name + suffix |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
290 file_path = os.path.join(entry, file_name) |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
291 if os.path.isfile(file_path): |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
292 break |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
293 else: |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
294 continue |
|
1b8f08c4efd5
Issue #17177: The imp module is pending deprecation.
Brett Cannon <brett@python.org>
parents:
84136
diff
changeset
|
295 break # Break out of outer loop when breaking out of inner loop. |
|
76455
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
296 else: |
|
84135
7bb54aa762e1
Make it more obvious what things used in imp are snuck in through private APIs
Brett Cannon <brett@python.org>
parents:
84134
diff
changeset
|
297 raise ImportError(_ERR_MSG.format(name), name=name) |
|
76455
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
298 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
299 encoding = None |
|
87448
70bd6f7e013b
Issue #15204: Deprecated the 'U' mode in file-like objects.
Serhiy Storchaka <storchaka@gmail.com>
parents:
87347
diff
changeset
|
300 if 'b' not in mode: |
|
76455
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
301 with open(file_path, 'rb') as file: |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
302 encoding = tokenize.detect_encoding(file.readline)[0] |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
303 file = open(file_path, mode, encoding=encoding) |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
304 return file, file_path, (suffix, mode, type_) |
|
76640
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
305 |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
306 |
|
84123
01da7bf11ca1
Issue #18193: Add importlib.reload(), documenting (but not
Brett Cannon <brett@python.org>
parents:
84114
diff
changeset
|
307 def reload(module): |
|
01da7bf11ca1
Issue #18193: Add importlib.reload(), documenting (but not
Brett Cannon <brett@python.org>
parents:
84114
diff
changeset
|
308 """**DEPRECATED** |
|
76640
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
309 |
|
84123
01da7bf11ca1
Issue #18193: Add importlib.reload(), documenting (but not
Brett Cannon <brett@python.org>
parents:
84114
diff
changeset
|
310 Reload the module and return it. |
|
76640
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
311 |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
312 The module must have been successfully imported before. |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
313 |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
314 """ |
|
84123
01da7bf11ca1
Issue #18193: Add importlib.reload(), documenting (but not
Brett Cannon <brett@python.org>
parents:
84114
diff
changeset
|
315 return importlib.reload(module) |
