Mercurial > cpython-lru_cache-weakref
annotate Lib/imp.py @ 84007:dbfbdf2b5c19 3.3
Fix #17967: For ftp urls CWD to target instead of hopping to each directory
towards target. This fixes a bug where target is accessible, but parent
directories are restricted.
| author | Senthil Kumaran <senthil@uthcode.com> |
|---|---|
| date | Sat, 01 Jun 2013 08:27:06 -0700 |
| parents | c0a21617dbee |
| children | 80b65aa2d579 8d28d44f3a9a |
| 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 |
|
78042
efb5e6ab10f4
Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents:
78026
diff
changeset
|
19 # Directly exposed by this module |
|
76753
59b8a7fcb047
Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents:
76642
diff
changeset
|
20 from importlib._bootstrap import new_module |
|
78078
9e164b404983
Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents:
78042
diff
changeset
|
21 from importlib._bootstrap import cache_from_source, source_from_cache |
|
76338
2df37938b8e1
Issue #13959: Re-implement imp.load_module() in imp.py.
Brett Cannon <brett@python.org>
parents:
76336
diff
changeset
|
22 |
|
78042
efb5e6ab10f4
Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents:
78026
diff
changeset
|
23 |
|
76342
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
24 from importlib import _bootstrap |
|
76874
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
25 from importlib import machinery |
|
76342
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
26 import os |
|
76455
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
27 import sys |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
28 import tokenize |
|
76874
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
29 import warnings |
|
76455
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
30 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
31 |
| 76877 | 32 # DEPRECATED |
|
76455
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
33 SEARCH_ERROR = 0 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
34 PY_SOURCE = 1 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
35 PY_COMPILED = 2 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
36 C_EXTENSION = 3 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
37 PY_RESOURCE = 4 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
38 PKG_DIRECTORY = 5 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
39 C_BUILTIN = 6 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
40 PY_FROZEN = 7 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
41 PY_CODERESOURCE = 8 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
42 IMP_HOOK = 9 |
|
76342
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
43 |
|
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
44 |
|
78042
efb5e6ab10f4
Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents:
78026
diff
changeset
|
45 def get_magic(): |
|
efb5e6ab10f4
Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents:
78026
diff
changeset
|
46 """Return the magic number for .pyc or .pyo files.""" |
|
efb5e6ab10f4
Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents:
78026
diff
changeset
|
47 return _bootstrap._MAGIC_BYTES |
|
efb5e6ab10f4
Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents:
78026
diff
changeset
|
48 |
|
efb5e6ab10f4
Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents:
78026
diff
changeset
|
49 |
|
77917
b36bed82c9d0
Issue #15166: Re-implement imp.get_tag() using sys.implementation.
Brett Cannon <brett@python.org>
parents:
77456
diff
changeset
|
50 def get_tag(): |
|
b36bed82c9d0
Issue #15166: Re-implement imp.get_tag() using sys.implementation.
Brett Cannon <brett@python.org>
parents:
77456
diff
changeset
|
51 """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
|
52 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
|
53 |
|
b36bed82c9d0
Issue #15166: Re-implement imp.get_tag() using sys.implementation.
Brett Cannon <brett@python.org>
parents:
77456
diff
changeset
|
54 |
|
76754
257cbd2fac38
Issue #13959: Re-implement imp.get_suffixes() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76753
diff
changeset
|
55 def get_suffixes(): |
|
76874
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
56 warnings.warn('imp.get_suffixes() is deprecated; use the constants ' |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
57 'defined on importlib.machinery instead', |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
58 DeprecationWarning, 2) |
|
78486
1db6553f3f8c
Issue #15576: Allow extension modules to be a package's __init__
Brett Cannon <brett@python.org>
parents:
78078
diff
changeset
|
59 extensions = [(s, 'rb', C_EXTENSION) for s in machinery.EXTENSION_SUFFIXES] |
|
76874
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
60 source = [(s, 'U', PY_SOURCE) for s in machinery.SOURCE_SUFFIXES] |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
61 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
|
62 |
|
257cbd2fac38
Issue #13959: Re-implement imp.get_suffixes() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76753
diff
changeset
|
63 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
|
64 |
|
257cbd2fac38
Issue #13959: Re-implement imp.get_suffixes() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76753
diff
changeset
|
65 |
|
76633
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
66 class NullImporter: |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
67 |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
68 """Null import object.""" |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
69 |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
70 def __init__(self, path): |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
71 if path == '': |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
72 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
|
73 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
|
74 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
|
75 |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
76 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
|
77 """Always returns None.""" |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
78 return None |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
79 |
|
eb5c5c23ca9b
Issue #13959: Re-implement imp.NullImporter in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76548
diff
changeset
|
80 |
|
76381
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
81 class _HackedGetData: |
|
76371
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
82 |
|
76381
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
83 """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
|
84 functions.""" |
|
76371
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
85 |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
86 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
|
87 super().__init__(fullname, path) |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
88 self.file = file |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
89 |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
90 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
|
91 """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
|
92 if self.file and path == self.path: |
|
76371
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
93 with self.file: |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
94 # 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
|
95 # 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
|
96 # 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
|
97 # 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
|
98 # tokenize.detect_encoding() only accepts bytes. |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
99 return self.file.read() |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
100 else: |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
101 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
|
102 |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
103 |
|
76478
1da623513b26
Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents:
76456
diff
changeset
|
104 class _LoadSourceCompatibility(_HackedGetData, _bootstrap.SourceFileLoader): |
|
76381
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
105 |
|
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
106 """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
|
107 |
|
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
108 |
|
76371
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
109 def load_source(name, pathname, file=None): |
| 76877 | 110 msg = ('imp.load_source() is deprecated; use ' |
| 111 'importlib.machinery.SourceFileLoader(name, pathname).load_module()' | |
| 112 ' instead') | |
| 113 warnings.warn(msg, DeprecationWarning, 2) | |
|
83541
3dcc81c2eef5
Issue #17358: imp.load_source() and load_compiled() should now return
Brett Cannon <brett@python.org>
parents:
82719
diff
changeset
|
114 _LoadSourceCompatibility(name, pathname, file).load_module(name) |
|
3dcc81c2eef5
Issue #17358: imp.load_source() and load_compiled() should now return
Brett Cannon <brett@python.org>
parents:
82719
diff
changeset
|
115 module = sys.modules[name] |
|
3dcc81c2eef5
Issue #17358: imp.load_source() and load_compiled() should now return
Brett Cannon <brett@python.org>
parents:
82719
diff
changeset
|
116 # 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
|
117 # won't rely on a now-closed file object. |
|
3dcc81c2eef5
Issue #17358: imp.load_source() and load_compiled() should now return
Brett Cannon <brett@python.org>
parents:
82719
diff
changeset
|
118 module.__loader__ = _bootstrap.SourceFileLoader(name, pathname) |
|
3dcc81c2eef5
Issue #17358: imp.load_source() and load_compiled() should now return
Brett Cannon <brett@python.org>
parents:
82719
diff
changeset
|
119 return module |
|
76371
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
120 |
|
3b5b4b4bb43c
Issue #13959: Re-implement imp.load_source() in imp.py.
Brett Cannon <brett@python.org>
parents:
76368
diff
changeset
|
121 |
|
76381
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
122 class _LoadCompiledCompatibility(_HackedGetData, |
|
76548
a2cf07135e4f
Issue #14605: Rename _SourcelessFileLoader to SourcelessFileLoader.
Marc-Andre Lemburg <mal@egenix.com>
parents:
76547
diff
changeset
|
123 _bootstrap.SourcelessFileLoader): |
|
76381
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
124 |
|
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
125 """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
|
126 |
|
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
127 |
|
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
128 def load_compiled(name, pathname, file=None): |
| 76877 | 129 msg = ('imp.load_compiled() is deprecated; use ' |
| 130 'importlib.machinery.SourcelessFileLoader(name, pathname).' | |
| 131 'load_module() instead ') | |
| 132 warnings.warn(msg, DeprecationWarning, 2) | |
|
83541
3dcc81c2eef5
Issue #17358: imp.load_source() and load_compiled() should now return
Brett Cannon <brett@python.org>
parents:
82719
diff
changeset
|
133 _LoadCompiledCompatibility(name, pathname, file).load_module(name) |
|
3dcc81c2eef5
Issue #17358: imp.load_source() and load_compiled() should now return
Brett Cannon <brett@python.org>
parents:
82719
diff
changeset
|
134 module = sys.modules[name] |
|
3dcc81c2eef5
Issue #17358: imp.load_source() and load_compiled() should now return
Brett Cannon <brett@python.org>
parents:
82719
diff
changeset
|
135 # 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
|
136 # won't rely on a now-closed file object. |
|
3dcc81c2eef5
Issue #17358: imp.load_source() and load_compiled() should now return
Brett Cannon <brett@python.org>
parents:
82719
diff
changeset
|
137 module.__loader__ = _bootstrap.SourcelessFileLoader(name, pathname) |
|
3dcc81c2eef5
Issue #17358: imp.load_source() and load_compiled() should now return
Brett Cannon <brett@python.org>
parents:
82719
diff
changeset
|
138 return module |
|
76381
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
139 |
|
66bd85bcf916
Issue #13959: Re-implement imp.load_compiled() in imp.py.
Brett Cannon <brett@python.org>
parents:
76372
diff
changeset
|
140 |
|
76342
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
141 def load_package(name, path): |
| 76877 | 142 msg = ('imp.load_package() is deprecated; use either ' |
| 143 'importlib.machinery.SourceFileLoader() or ' | |
| 144 'importlib.machinery.SourcelessFileLoader() instead') | |
| 145 warnings.warn(msg, DeprecationWarning, 2) | |
|
76342
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
146 if os.path.isdir(path): |
| 76877 | 147 extensions = (machinery.SOURCE_SUFFIXES[:] + |
| 148 machinery.BYTECODE_SUFFIXES[:]) | |
|
76342
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
149 for extension in extensions: |
|
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
150 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
|
151 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
|
152 break |
|
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
153 else: |
|
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
154 raise ValueError('{!r} is not a package'.format(path)) |
|
76478
1da623513b26
Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents:
76456
diff
changeset
|
155 return _bootstrap.SourceFileLoader(name, path).load_module(name) |
|
76342
4256df44023b
Issue #13959: Re-implement imp.load_package() in imp.py.
Brett Cannon <brett@python.org>
parents:
76338
diff
changeset
|
156 |
|
76338
2df37938b8e1
Issue #13959: Re-implement imp.load_module() in imp.py.
Brett Cannon <brett@python.org>
parents:
76336
diff
changeset
|
157 |
|
2df37938b8e1
Issue #13959: Re-implement imp.load_module() in imp.py.
Brett Cannon <brett@python.org>
parents:
76336
diff
changeset
|
158 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
|
159 """**DEPRECATED** |
|
034c814eb187
Issue #13959: Add to imp.find_module() and load_module's docstrings
Brett Cannon <brett@python.org>
parents:
76877
diff
changeset
|
160 |
|
034c814eb187
Issue #13959: Add to imp.find_module() and load_module's docstrings
Brett Cannon <brett@python.org>
parents:
76877
diff
changeset
|
161 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
|
162 |
|
2df37938b8e1
Issue #13959: Re-implement imp.load_module() in imp.py.
Brett Cannon <brett@python.org>
parents:
76336
diff
changeset
|
163 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
|
164 |
|
2df37938b8e1
Issue #13959: Re-implement imp.load_module() in imp.py.
Brett Cannon <brett@python.org>
parents:
76336
diff
changeset
|
165 """ |
|
2df37938b8e1
Issue #13959: Re-implement imp.load_module() in imp.py.
Brett Cannon <brett@python.org>
parents:
76336
diff
changeset
|
166 suffix, mode, type_ = details |
| 76877 | 167 with warnings.catch_warnings(): |
| 168 warnings.simplefilter('ignore') | |
| 169 if mode and (not mode.startswith(('r', 'U')) or '+' in mode): | |
| 170 raise ValueError('invalid file open mode {!r}'.format(mode)) | |
|
83594
c0a21617dbee
Issue #15902: Fix imp.load_module() to accept None as a file when
Brett Cannon <brett@python.org>
parents:
83541
diff
changeset
|
171 elif file is None and type_ in {PY_SOURCE, PY_COMPILED}: |
| 76877 | 172 msg = 'file object required for import (type code {})'.format(type_) |
| 173 raise ValueError(msg) | |
| 174 elif type_ == PY_SOURCE: | |
| 175 return load_source(name, filename, file) | |
| 176 elif type_ == PY_COMPILED: | |
| 177 return load_compiled(name, filename, file) | |
|
82719
d5aa922f97f9
Issue #16880: _imp.load_dynamic() is not defined on a platform that
Brett Cannon <brett@python.org>
parents:
78816
diff
changeset
|
178 elif type_ == C_EXTENSION and load_dynamic is not None: |
|
83594
c0a21617dbee
Issue #15902: Fix imp.load_module() to accept None as a file when
Brett Cannon <brett@python.org>
parents:
83541
diff
changeset
|
179 if file is None: |
|
c0a21617dbee
Issue #15902: Fix imp.load_module() to accept None as a file when
Brett Cannon <brett@python.org>
parents:
83541
diff
changeset
|
180 with open(filename, 'rb') as opened_file: |
|
c0a21617dbee
Issue #15902: Fix imp.load_module() to accept None as a file when
Brett Cannon <brett@python.org>
parents:
83541
diff
changeset
|
181 return load_dynamic(name, filename, opened_file) |
|
c0a21617dbee
Issue #15902: Fix imp.load_module() to accept None as a file when
Brett Cannon <brett@python.org>
parents:
83541
diff
changeset
|
182 else: |
|
c0a21617dbee
Issue #15902: Fix imp.load_module() to accept None as a file when
Brett Cannon <brett@python.org>
parents:
83541
diff
changeset
|
183 return load_dynamic(name, filename, file) |
| 76877 | 184 elif type_ == PKG_DIRECTORY: |
| 185 return load_package(name, filename) | |
| 186 elif type_ == C_BUILTIN: | |
| 187 return init_builtin(name) | |
| 188 elif type_ == PY_FROZEN: | |
| 189 return init_frozen(name) | |
| 190 else: | |
|
78816
9ba57938f54d
Issue #15828: Restore support for C extension modules in imp.load_module()
Nick Coghlan <ncoghlan@gmail.com>
parents:
78486
diff
changeset
|
191 msg = "Don't know how to import {} (type code {})".format(name, type_) |
| 76877 | 192 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
|
193 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
194 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
195 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
|
196 """**DEPRECATED** |
|
034c814eb187
Issue #13959: Add to imp.find_module() and load_module's docstrings
Brett Cannon <brett@python.org>
parents:
76877
diff
changeset
|
197 |
|
034c814eb187
Issue #13959: Add to imp.find_module() and load_module's docstrings
Brett Cannon <brett@python.org>
parents:
76877
diff
changeset
|
198 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
|
199 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
200 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
|
201 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
|
202 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
|
203 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
|
204 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
205 """ |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
206 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
|
207 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
|
208 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
|
209 # Backwards-compatibility |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
210 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
|
211 "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
|
212 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
213 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
|
214 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
|
215 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
|
216 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
|
217 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
|
218 else: |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
219 path = sys.path |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
220 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
221 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
|
222 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
|
223 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
|
224 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
|
225 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
|
226 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
|
227 return None, package_directory, ('', '', PKG_DIRECTORY) |
|
76874
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
228 with warnings.catch_warnings(): |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
229 warnings.simplefilter('ignore') |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
230 for suffix, mode, type_ in get_suffixes(): |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
231 file_name = name + suffix |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
232 file_path = os.path.join(entry, file_name) |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
233 if os.path.isfile(file_path): |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
234 break |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
235 else: |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
236 continue |
|
b81ddaf0db47
Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents:
76769
diff
changeset
|
237 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
|
238 else: |
|
78025
75831951a6b5
Issue #15256: Re-use the ImportError exception message as defined by
Brett Cannon <brett@python.org>
parents:
77917
diff
changeset
|
239 raise ImportError(_bootstrap._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
|
240 |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
241 encoding = None |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
242 if mode == 'U': |
|
085cf1480cfe
Issue #13959: Re-implement imp.find_module() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76453
diff
changeset
|
243 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
|
244 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
|
245 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
|
246 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
|
247 |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
248 |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
249 _RELOADING = {} |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
250 |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
251 def reload(module): |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
252 """Reload the module and return it. |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
253 |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
254 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
|
255 |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
256 """ |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
257 if not module or type(module) != type(sys): |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
258 raise TypeError("reload() argument must be module") |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
259 name = module.__name__ |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
260 if name not in sys.modules: |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
261 msg = "module {} not in sys.modules" |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
262 raise ImportError(msg.format(name), name=name) |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
263 if name in _RELOADING: |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
264 return _RELOADING[name] |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
265 _RELOADING[name] = module |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
266 try: |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
267 parent_name = name.rpartition('.')[0] |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
268 if parent_name and parent_name not in sys.modules: |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
269 msg = "parent {!r} not in sys.modules" |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
270 raise ImportError(msg.format(parentname), name=parent_name) |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
271 return module.__loader__.load_module(name) |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
272 finally: |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
273 try: |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
274 del _RELOADING[name] |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
275 except KeyError: |
|
eb68502731dd
Issues #13959, 14647: Re-implement imp.reload() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents:
76633
diff
changeset
|
276 pass |
