annotate Lib/importlib/_bootstrap.py @ 81343:3a7cd8efc19f

#16852: merge with 3.3.
author Ezio Melotti <ezio.melotti@gmail.com>
date Thu, 10 Jan 2013 03:29:45 +0200
parents 7d69d04522e3
children b94f308e9b47
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1 """Core implementation of import.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
2
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
3 This module is NOT meant to be directly imported! It has been designed such
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
4 that it can be bootstrapped into Python as the implementation of import. As
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
5 such it requires the injection of specific modules and attributes in order to
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
6 work. One should use importlib as the public-facing version of this module.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
7
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
8 """
76549
acfdf46b8de1 Issue #14605 and #14642:
Marc-Andre Lemburg <mal@egenix.com>
parents: 76548
diff changeset
9 #
acfdf46b8de1 Issue #14605 and #14642:
Marc-Andre Lemburg <mal@egenix.com>
parents: 76548
diff changeset
10 # IMPORTANT: Whenever making changes to this module, be sure to run
acfdf46b8de1 Issue #14605 and #14642:
Marc-Andre Lemburg <mal@egenix.com>
parents: 76548
diff changeset
11 # a top-level make in order to get the frozen version of the module
acfdf46b8de1 Issue #14605 and #14642:
Marc-Andre Lemburg <mal@egenix.com>
parents: 76548
diff changeset
12 # update. Not doing so, will result in the Makefile to fail for
acfdf46b8de1 Issue #14605 and #14642:
Marc-Andre Lemburg <mal@egenix.com>
parents: 76548
diff changeset
13 # all others who don't have a ./python around to freeze the module
acfdf46b8de1 Issue #14605 and #14642:
Marc-Andre Lemburg <mal@egenix.com>
parents: 76548
diff changeset
14 # in the early stages of compilation.
acfdf46b8de1 Issue #14605 and #14642:
Marc-Andre Lemburg <mal@egenix.com>
parents: 76548
diff changeset
15 #
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
16
76449
163360370ad2 Make path manipulation more robust for platforms with alternative path
Brett Cannon <brett@python.org>
parents: 76445
diff changeset
17 # See importlib._setup() for what is injected into the global namespace.
163360370ad2 Make path manipulation more robust for platforms with alternative path
Brett Cannon <brett@python.org>
parents: 76445
diff changeset
18
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
19 # When editing this code be aware that code executed at import time CANNOT
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
20 # reference any injected objects! This includes not only global code but also
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
21 # anything specified at the class level.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
22
76478
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
23 # XXX Make sure all public names have no single leading underscore and all
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
24 # others do.
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
25
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
26
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
27 # Bootstrap-related code ######################################################
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
28
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
29 _CASE_INSENSITIVE_PLATFORMS = 'win', 'cygwin', 'darwin'
74643
ecf4a7bb8807 Relocate importlib._case_ok to importlib._bootstrap.
Brett Cannon <brett@python.org>
parents: 74630
diff changeset
30
ecf4a7bb8807 Relocate importlib._case_ok to importlib._bootstrap.
Brett Cannon <brett@python.org>
parents: 74630
diff changeset
31
75055
b8576719fe71 _relax_case -> _make_relax_case
Antoine Pitrou <solipsis@pitrou.net>
parents: 75048
diff changeset
32 def _make_relax_case():
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
33 if sys.platform.startswith(_CASE_INSENSITIVE_PLATFORMS):
75048
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
34 def _relax_case():
75070
defd944af91b put docstrings on functions
Benjamin Peterson <benjamin@python.org>
parents: 75055
diff changeset
35 """True if filenames must be checked case-insensitively."""
75048
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
36 return b'PYTHONCASEOK' in _os.environ
74643
ecf4a7bb8807 Relocate importlib._case_ok to importlib._bootstrap.
Brett Cannon <brett@python.org>
parents: 74630
diff changeset
37 else:
75048
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
38 def _relax_case():
75070
defd944af91b put docstrings on functions
Benjamin Peterson <benjamin@python.org>
parents: 75055
diff changeset
39 """True if filenames must be checked case-insensitively."""
75048
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
40 return False
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
41 return _relax_case
74643
ecf4a7bb8807 Relocate importlib._case_ok to importlib._bootstrap.
Brett Cannon <brett@python.org>
parents: 74630
diff changeset
42
ecf4a7bb8807 Relocate importlib._case_ok to importlib._bootstrap.
Brett Cannon <brett@python.org>
parents: 74630
diff changeset
43
74630
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
44 # TODO: Expose from marshal
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
45 def _w_long(x):
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
46 """Convert a 32-bit integer to little-endian.
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
47
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
48 XXX Temporary until marshal's long functions are exposed.
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
49
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
50 """
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
51 x = int(x)
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
52 int_bytes = []
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
53 int_bytes.append(x & 0xFF)
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
54 int_bytes.append((x >> 8) & 0xFF)
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
55 int_bytes.append((x >> 16) & 0xFF)
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
56 int_bytes.append((x >> 24) & 0xFF)
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
57 return bytearray(int_bytes)
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
58
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
59
74630
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
60 # TODO: Expose from marshal
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
61 def _r_long(int_bytes):
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
62 """Convert 4 bytes in little-endian to an integer.
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
63
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
64 XXX Temporary until marshal's long function are exposed.
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
65
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
66 """
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
67 x = int_bytes[0]
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
68 x |= int_bytes[1] << 8
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
69 x |= int_bytes[2] << 16
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
70 x |= int_bytes[3] << 24
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
71 return x
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
72
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
73
76472
4e853913054c Issue #13959: Continue to try to accomodate altsep in importlib by not
Brett Cannon <brett@python.org>
parents: 76466
diff changeset
74 def _path_join(*path_parts):
76449
163360370ad2 Make path manipulation more robust for platforms with alternative path
Brett Cannon <brett@python.org>
parents: 76445
diff changeset
75 """Replacement for os.path.join()."""
76472
4e853913054c Issue #13959: Continue to try to accomodate altsep in importlib by not
Brett Cannon <brett@python.org>
parents: 76466
diff changeset
76 new_parts = []
4e853913054c Issue #13959: Continue to try to accomodate altsep in importlib by not
Brett Cannon <brett@python.org>
parents: 76466
diff changeset
77 for part in path_parts:
4e853913054c Issue #13959: Continue to try to accomodate altsep in importlib by not
Brett Cannon <brett@python.org>
parents: 76466
diff changeset
78 if not part:
4e853913054c Issue #13959: Continue to try to accomodate altsep in importlib by not
Brett Cannon <brett@python.org>
parents: 76466
diff changeset
79 continue
4e853913054c Issue #13959: Continue to try to accomodate altsep in importlib by not
Brett Cannon <brett@python.org>
parents: 76466
diff changeset
80 new_parts.append(part)
4e853913054c Issue #13959: Continue to try to accomodate altsep in importlib by not
Brett Cannon <brett@python.org>
parents: 76466
diff changeset
81 if part[-1] not in path_separators:
4e853913054c Issue #13959: Continue to try to accomodate altsep in importlib by not
Brett Cannon <brett@python.org>
parents: 76466
diff changeset
82 new_parts.append(path_sep)
4e853913054c Issue #13959: Continue to try to accomodate altsep in importlib by not
Brett Cannon <brett@python.org>
parents: 76466
diff changeset
83 return ''.join(new_parts[:-1]) # Drop superfluous path separator.
74630
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
84
76449
163360370ad2 Make path manipulation more robust for platforms with alternative path
Brett Cannon <brett@python.org>
parents: 76445
diff changeset
85
163360370ad2 Make path manipulation more robust for platforms with alternative path
Brett Cannon <brett@python.org>
parents: 76445
diff changeset
86 def _path_split(path):
163360370ad2 Make path manipulation more robust for platforms with alternative path
Brett Cannon <brett@python.org>
parents: 76445
diff changeset
87 """Replacement for os.path.split()."""
163360370ad2 Make path manipulation more robust for platforms with alternative path
Brett Cannon <brett@python.org>
parents: 76445
diff changeset
88 for x in reversed(path):
163360370ad2 Make path manipulation more robust for platforms with alternative path
Brett Cannon <brett@python.org>
parents: 76445
diff changeset
89 if x in path_separators:
163360370ad2 Make path manipulation more robust for platforms with alternative path
Brett Cannon <brett@python.org>
parents: 76445
diff changeset
90 sep = x
163360370ad2 Make path manipulation more robust for platforms with alternative path
Brett Cannon <brett@python.org>
parents: 76445
diff changeset
91 break
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
92 else:
76449
163360370ad2 Make path manipulation more robust for platforms with alternative path
Brett Cannon <brett@python.org>
parents: 76445
diff changeset
93 sep = path_sep
163360370ad2 Make path manipulation more robust for platforms with alternative path
Brett Cannon <brett@python.org>
parents: 76445
diff changeset
94 front, _, tail = path.rpartition(sep)
163360370ad2 Make path manipulation more robust for platforms with alternative path
Brett Cannon <brett@python.org>
parents: 76445
diff changeset
95 return front, tail
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
96
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
97
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
98 def _path_is_mode_type(path, mode):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
99 """Test whether the path is the specified mode type."""
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
100 try:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
101 stat_info = _os.stat(path)
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
102 except OSError:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
103 return False
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
104 return (stat_info.st_mode & 0o170000) == mode
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
105
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
106
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
107 # XXX Could also expose Modules/getpath.c:isfile()
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
108 def _path_isfile(path):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
109 """Replacement for os.path.isfile."""
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
110 return _path_is_mode_type(path, 0o100000)
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
111
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
112
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
113 # XXX Could also expose Modules/getpath.c:isdir()
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
114 def _path_isdir(path):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
115 """Replacement for os.path.isdir."""
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
116 if not path:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
117 path = _os.getcwd()
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
118 return _path_is_mode_type(path, 0o040000)
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
119
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
120
78722
3a831a0a29c4 Close #2501: Permission bits are once again correctly copied from the source file to the cached bytecode file. Test by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78663
diff changeset
121 def _write_atomic(path, data, mode=0o666):
75185
27d31f0c4ad5 Issue #14077: importlib: Fix regression introduced by de6703671386.
Charles-François Natali <neologix@free.fr>
parents: 75082
diff changeset
122 """Best-effort function to write data to a path atomically.
27d31f0c4ad5 Issue #14077: importlib: Fix regression introduced by de6703671386.
Charles-François Natali <neologix@free.fr>
parents: 75082
diff changeset
123 Be prepared to handle a FileExistsError if concurrent writing of the
27d31f0c4ad5 Issue #14077: importlib: Fix regression introduced by de6703671386.
Charles-François Natali <neologix@free.fr>
parents: 75082
diff changeset
124 temporary file is attempted."""
73573
b75b41237380 Issue #13392: Writing a pyc file should now be atomic under Windows as well.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73477
diff changeset
125 # id() is used to generate a pseudo-random filename.
b75b41237380 Issue #13392: Writing a pyc file should now be atomic under Windows as well.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73477
diff changeset
126 path_tmp = '{}.{}'.format(path, id(path))
78722
3a831a0a29c4 Close #2501: Permission bits are once again correctly copied from the source file to the cached bytecode file. Test by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78663
diff changeset
127 fd = _os.open(path_tmp,
3a831a0a29c4 Close #2501: Permission bits are once again correctly copied from the source file to the cached bytecode file. Test by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78663
diff changeset
128 _os.O_EXCL | _os.O_CREAT | _os.O_WRONLY, mode & 0o666)
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
129 try:
74998
de6703671386 Have importlib use os.replace() for atomic renaming.
Brett Cannon <brett@python.org>
parents: 74994
diff changeset
130 # We first write data to a temporary file, and then use os.replace() to
de6703671386 Have importlib use os.replace() for atomic renaming.
Brett Cannon <brett@python.org>
parents: 74994
diff changeset
131 # perform an atomic rename.
73573
b75b41237380 Issue #13392: Writing a pyc file should now be atomic under Windows as well.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73477
diff changeset
132 with _io.FileIO(fd, 'wb') as file:
b75b41237380 Issue #13392: Writing a pyc file should now be atomic under Windows as well.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73477
diff changeset
133 file.write(data)
74998
de6703671386 Have importlib use os.replace() for atomic renaming.
Brett Cannon <brett@python.org>
parents: 74994
diff changeset
134 _os.replace(path_tmp, path)
73573
b75b41237380 Issue #13392: Writing a pyc file should now be atomic under Windows as well.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73477
diff changeset
135 except OSError:
b75b41237380 Issue #13392: Writing a pyc file should now be atomic under Windows as well.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73477
diff changeset
136 try:
b75b41237380 Issue #13392: Writing a pyc file should now be atomic under Windows as well.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73477
diff changeset
137 _os.unlink(path_tmp)
72960
c16063765d3a Issue #13146: Writing a pyc file is now atomic under POSIX.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68887
diff changeset
138 except OSError:
73573
b75b41237380 Issue #13392: Writing a pyc file should now be atomic under Windows as well.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73477
diff changeset
139 pass
b75b41237380 Issue #13392: Writing a pyc file should now be atomic under Windows as well.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73477
diff changeset
140 raise
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
141
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
142
52139
2e8e897a1244 Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents: 52115
diff changeset
143 def _wrap(new, old):
77964
37850f083af5 Minor refactoring in importlib._bootstrap, and fix the '_wrap' docstring.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 77917
diff changeset
144 """Simple substitute for functools.update_wrapper."""
73977
54a77c556d9a Issue #13593: updating the importlib utility decorators for __qualname__.
Meador Inge <meadori@gmail.com>
parents: 73974
diff changeset
145 for replace in ['__module__', '__name__', '__qualname__', '__doc__']:
74838
7a6fd7cd16e9 Don't fail in the face of a lacking attribute when wrapping a
Brett Cannon <brett@python.org>
parents: 74643
diff changeset
146 if hasattr(old, replace):
7a6fd7cd16e9 Don't fail in the face of a lacking attribute when wrapping a
Brett Cannon <brett@python.org>
parents: 74643
diff changeset
147 setattr(new, replace, getattr(old, replace))
51433
43d3a95ed662 Create a simple substitute for functools.wraps to use in importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 51430
diff changeset
148 new.__dict__.update(old.__dict__)
43d3a95ed662 Create a simple substitute for functools.wraps to use in importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 51430
diff changeset
149
43d3a95ed662 Create a simple substitute for functools.wraps to use in importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 51430
diff changeset
150
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
151 _code_type = type(_wrap.__code__)
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
152
76331
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
153
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
154 def new_module(name):
76331
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
155 """Create a new module.
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
156
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
157 The module is not entered into sys.modules.
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
158
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
159 """
76755
c6a97506b2ee Jython-friendly tweak.
Brett Cannon <brett@python.org>
parents: 76754
diff changeset
160 return type(_io)(name)
76331
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
161
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
162
77013
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
163 # Module-level locking ########################################################
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
164
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
165 # A dict mapping module names to weakrefs of _ModuleLock instances
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
166 _module_locks = {}
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
167 # A dict mapping thread ids to _ModuleLock instances
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
168 _blocking_on = {}
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
169
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
170
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
171 class _DeadlockError(RuntimeError):
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
172 pass
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
173
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
174
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
175 class _ModuleLock:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
176 """A recursive lock implementation which is able to detect deadlocks
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
177 (e.g. thread 1 trying to take locks A then B, and thread 2 trying to
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
178 take locks B then A).
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
179 """
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
180
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
181 def __init__(self, name):
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
182 self.lock = _thread.allocate_lock()
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
183 self.wakeup = _thread.allocate_lock()
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
184 self.name = name
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
185 self.owner = None
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
186 self.count = 0
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
187 self.waiters = 0
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
188
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
189 def has_deadlock(self):
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
190 # Deadlock avoidance for concurrent circular imports.
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
191 me = _thread.get_ident()
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
192 tid = self.owner
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
193 while True:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
194 lock = _blocking_on.get(tid)
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
195 if lock is None:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
196 return False
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
197 tid = lock.owner
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
198 if tid == me:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
199 return True
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
200
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
201 def acquire(self):
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
202 """
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
203 Acquire the module lock. If a potential deadlock is detected,
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
204 a _DeadlockError is raised.
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
205 Otherwise, the lock is always acquired and True is returned.
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
206 """
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
207 tid = _thread.get_ident()
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
208 _blocking_on[tid] = self
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
209 try:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
210 while True:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
211 with self.lock:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
212 if self.count == 0 or self.owner == tid:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
213 self.owner = tid
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
214 self.count += 1
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
215 return True
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
216 if self.has_deadlock():
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
217 raise _DeadlockError("deadlock detected by %r" % self)
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
218 if self.wakeup.acquire(False):
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
219 self.waiters += 1
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
220 # Wait for a release() call
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
221 self.wakeup.acquire()
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
222 self.wakeup.release()
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
223 finally:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
224 del _blocking_on[tid]
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
225
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
226 def release(self):
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
227 tid = _thread.get_ident()
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
228 with self.lock:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
229 if self.owner != tid:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
230 raise RuntimeError("cannot release un-acquired lock")
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
231 assert self.count > 0
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
232 self.count -= 1
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
233 if self.count == 0:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
234 self.owner = None
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
235 if self.waiters:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
236 self.waiters -= 1
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
237 self.wakeup.release()
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
238
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
239 def __repr__(self):
79692
987a8724b220 Remove uses of % with str.format.
Brett Cannon <brett@python.org>
parents: 79653
diff changeset
240 return "_ModuleLock({!r}) at {}".format(self.name, id(self))
77013
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
241
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
242
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
243 class _DummyModuleLock:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
244 """A simple _ModuleLock equivalent for Python builds without
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
245 multi-threading support."""
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
246
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
247 def __init__(self, name):
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
248 self.name = name
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
249 self.count = 0
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
250
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
251 def acquire(self):
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
252 self.count += 1
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
253 return True
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
254
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
255 def release(self):
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
256 if self.count == 0:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
257 raise RuntimeError("cannot release un-acquired lock")
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
258 self.count -= 1
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
259
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
260 def __repr__(self):
79692
987a8724b220 Remove uses of % with str.format.
Brett Cannon <brett@python.org>
parents: 79653
diff changeset
261 return "_DummyModuleLock({!r}) at {}".format(self.name, id(self))
77013
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
262
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
263
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
264 # The following two functions are for consumption by Python/import.c.
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
265
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
266 def _get_module_lock(name):
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
267 """Get or create the module lock for a given module name.
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
268
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
269 Should only be called with the import lock taken."""
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
270 lock = None
78779
7fa6336e9864 Issue #15781: Fix two small race conditions in import's module locking.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78758
diff changeset
271 try:
77013
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
272 lock = _module_locks[name]()
78779
7fa6336e9864 Issue #15781: Fix two small race conditions in import's module locking.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78758
diff changeset
273 except KeyError:
7fa6336e9864 Issue #15781: Fix two small race conditions in import's module locking.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78758
diff changeset
274 pass
77013
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
275 if lock is None:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
276 if _thread is None:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
277 lock = _DummyModuleLock(name)
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
278 else:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
279 lock = _ModuleLock(name)
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
280 def cb(_):
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
281 del _module_locks[name]
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
282 _module_locks[name] = _weakref.ref(lock, cb)
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
283 return lock
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
284
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
285 def _lock_unlock_module(name):
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
286 """Release the global import lock, and acquires then release the
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
287 module lock for a given module name.
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
288 This is used to ensure a module is completely initialized, in the
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
289 event it is being imported by another thread.
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
290
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
291 Should only be called with the import lock taken."""
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
292 lock = _get_module_lock(name)
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
293 _imp.release_lock()
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
294 try:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
295 lock.acquire()
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
296 except _DeadlockError:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
297 # Concurrent circular import, we'll accept a partially initialized
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
298 # module object.
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
299 pass
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
300 else:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
301 lock.release()
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
302
78344
62033490ca0f Close #15486: Simplify the mechanism used to remove importlib frames from tracebacks when they just introduce irrelevant noise
Nick Coghlan <ncoghlan@gmail.com>
parents: 78314
diff changeset
303 # Frame stripping magic ###############################################
77013
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
304
78344
62033490ca0f Close #15486: Simplify the mechanism used to remove importlib frames from tracebacks when they just introduce irrelevant noise
Nick Coghlan <ncoghlan@gmail.com>
parents: 78314
diff changeset
305 def _call_with_frames_removed(f, *args, **kwds):
62033490ca0f Close #15486: Simplify the mechanism used to remove importlib frames from tracebacks when they just introduce irrelevant noise
Nick Coghlan <ncoghlan@gmail.com>
parents: 78314
diff changeset
306 """remove_importlib_frames in import.c will always remove sequences
62033490ca0f Close #15486: Simplify the mechanism used to remove importlib frames from tracebacks when they just introduce irrelevant noise
Nick Coghlan <ncoghlan@gmail.com>
parents: 78314
diff changeset
307 of importlib frames that end with a call to this function
62033490ca0f Close #15486: Simplify the mechanism used to remove importlib frames from tracebacks when they just introduce irrelevant noise
Nick Coghlan <ncoghlan@gmail.com>
parents: 78314
diff changeset
308
62033490ca0f Close #15486: Simplify the mechanism used to remove importlib frames from tracebacks when they just introduce irrelevant noise
Nick Coghlan <ncoghlan@gmail.com>
parents: 78314
diff changeset
309 Use it instead of a normal call in places where including the importlib
62033490ca0f Close #15486: Simplify the mechanism used to remove importlib frames from tracebacks when they just introduce irrelevant noise
Nick Coghlan <ncoghlan@gmail.com>
parents: 78314
diff changeset
310 frames introduces unwanted noise into the traceback (e.g. when executing
62033490ca0f Close #15486: Simplify the mechanism used to remove importlib frames from tracebacks when they just introduce irrelevant noise
Nick Coghlan <ncoghlan@gmail.com>
parents: 78314
diff changeset
311 module code)
62033490ca0f Close #15486: Simplify the mechanism used to remove importlib frames from tracebacks when they just introduce irrelevant noise
Nick Coghlan <ncoghlan@gmail.com>
parents: 78314
diff changeset
312 """
62033490ca0f Close #15486: Simplify the mechanism used to remove importlib frames from tracebacks when they just introduce irrelevant noise
Nick Coghlan <ncoghlan@gmail.com>
parents: 78314
diff changeset
313 return f(*args, **kwds)
62033490ca0f Close #15486: Simplify the mechanism used to remove importlib frames from tracebacks when they just introduce irrelevant noise
Nick Coghlan <ncoghlan@gmail.com>
parents: 78314
diff changeset
314
62033490ca0f Close #15486: Simplify the mechanism used to remove importlib frames from tracebacks when they just introduce irrelevant noise
Nick Coghlan <ncoghlan@gmail.com>
parents: 78314
diff changeset
315
62033490ca0f Close #15486: Simplify the mechanism used to remove importlib frames from tracebacks when they just introduce irrelevant noise
Nick Coghlan <ncoghlan@gmail.com>
parents: 78314
diff changeset
316 # Finder/loader utility code ###############################################
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
317
78042
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
318 """Magic word to reject .pyc files generated by other Python versions.
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
319 It should change for each incompatible change to the bytecode.
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
320
78042
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
321 The value of CR and LF is incorporated so if you ever read or write
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
322 a .pyc file in text mode the magic number will be wrong; also, the
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
323 Apple MPW compiler swaps their values, botching string constants.
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
324
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
325 The magic numbers must be spaced apart at least 2 values, as the
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
326 -U interpeter flag will cause MAGIC+1 being used. They have been
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
327 odd numbers for some time now.
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
328
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
329 There were a variety of old schemes for setting the magic number.
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
330 The current working scheme is to increment the previous value by
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
331 10.
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
332
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
333 Starting with the adoption of PEP 3147 in Python 3.2, every bump in magic
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
334 number also includes a new "magic tag", i.e. a human readable string used
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
335 to represent the magic number in __pycache__ directories. When you change
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
336 the magic number, you must also set a new unique magic tag. Generally this
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
337 can be named after the Python major version of the magic number bump, but
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
338 it can really be anything, as long as it's different than anything else
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
339 that's come before. The tags are included in the following table, starting
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
340 with Python 3.2a0.
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
341
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
342 Known values:
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
343 Python 1.5: 20121
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
344 Python 1.5.1: 20121
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
345 Python 1.5.2: 20121
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
346 Python 1.6: 50428
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
347 Python 2.0: 50823
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
348 Python 2.0.1: 50823
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
349 Python 2.1: 60202
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
350 Python 2.1.1: 60202
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
351 Python 2.1.2: 60202
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
352 Python 2.2: 60717
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
353 Python 2.3a0: 62011
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
354 Python 2.3a0: 62021
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
355 Python 2.3a0: 62011 (!)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
356 Python 2.4a0: 62041
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
357 Python 2.4a3: 62051
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
358 Python 2.4b1: 62061
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
359 Python 2.5a0: 62071
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
360 Python 2.5a0: 62081 (ast-branch)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
361 Python 2.5a0: 62091 (with)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
362 Python 2.5a0: 62092 (changed WITH_CLEANUP opcode)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
363 Python 2.5b3: 62101 (fix wrong code: for x, in ...)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
364 Python 2.5b3: 62111 (fix wrong code: x += yield)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
365 Python 2.5c1: 62121 (fix wrong lnotab with for loops and
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
366 storing constants that should have been removed)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
367 Python 2.5c2: 62131 (fix wrong code: for x, in ... in listcomp/genexp)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
368 Python 2.6a0: 62151 (peephole optimizations and STORE_MAP opcode)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
369 Python 2.6a1: 62161 (WITH_CLEANUP optimization)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
370 Python 3000: 3000
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
371 3010 (removed UNARY_CONVERT)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
372 3020 (added BUILD_SET)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
373 3030 (added keyword-only parameters)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
374 3040 (added signature annotations)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
375 3050 (print becomes a function)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
376 3060 (PEP 3115 metaclass syntax)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
377 3061 (string literals become unicode)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
378 3071 (PEP 3109 raise changes)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
379 3081 (PEP 3137 make __file__ and __name__ unicode)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
380 3091 (kill str8 interning)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
381 3101 (merge from 2.6a0, see 62151)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
382 3103 (__file__ points to source file)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
383 Python 3.0a4: 3111 (WITH_CLEANUP optimization).
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
384 Python 3.0a5: 3131 (lexical exception stacking, including POP_EXCEPT)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
385 Python 3.1a0: 3141 (optimize list, set and dict comprehensions:
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
386 change LIST_APPEND and SET_ADD, add MAP_ADD)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
387 Python 3.1a0: 3151 (optimize conditional branches:
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
388 introduce POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
389 Python 3.2a0: 3160 (add SETUP_WITH)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
390 tag: cpython-32
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
391 Python 3.2a1: 3170 (add DUP_TOP_TWO, remove DUP_TOPX and ROT_FOUR)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
392 tag: cpython-32
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
393 Python 3.2a2 3180 (add DELETE_DEREF)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
394 Python 3.3a0 3190 __class__ super closure changed
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
395 Python 3.3a0 3200 (__qualname__ added)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
396 3210 (added size modulo 2**32 to the pyc header)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
397 Python 3.3a1 3220 (changed PEP 380 implementation)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
398 Python 3.3a4 3230 (revert changes to implicit __class__ closure)
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
399
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
400 MAGIC must change whenever the bytecode emitted by the compiler may no
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
401 longer be understood by older implementations of the eval loop (usually
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
402 due to the addition of new opcodes).
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
403
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
404 """
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
405 _RAW_MAGIC_NUMBER = 3230 | ord('\r') << 16 | ord('\n') << 24
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
406 _MAGIC_BYTES = bytes(_RAW_MAGIC_NUMBER >> n & 0xff for n in range(0, 25, 8))
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
407
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
408 _PYCACHE = '__pycache__'
76450
b773a751c2e7 Issue #13959: Re-implement imp.cache_from_source() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents: 76449
diff changeset
409
76874
b81ddaf0db47 Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents: 76826
diff changeset
410 SOURCE_SUFFIXES = ['.py'] # _setup() adds .pyw as needed.
76451
ea46ebba8a0f Issue #13959: Re-implement imp.source_from_cache() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents: 76450
diff changeset
411
76874
b81ddaf0db47 Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents: 76826
diff changeset
412 DEBUG_BYTECODE_SUFFIXES = ['.pyc']
b81ddaf0db47 Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents: 76826
diff changeset
413 OPTIMIZED_BYTECODE_SUFFIXES = ['.pyo']
76450
b773a751c2e7 Issue #13959: Re-implement imp.cache_from_source() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents: 76449
diff changeset
414
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
415 def cache_from_source(path, debug_override=None):
76450
b773a751c2e7 Issue #13959: Re-implement imp.cache_from_source() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents: 76449
diff changeset
416 """Given the path to a .py file, return the path to its .pyc/.pyo file.
b773a751c2e7 Issue #13959: Re-implement imp.cache_from_source() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents: 76449
diff changeset
417
b773a751c2e7 Issue #13959: Re-implement imp.cache_from_source() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents: 76449
diff changeset
418 The .py file does not need to exist; this simply returns the path to the
b773a751c2e7 Issue #13959: Re-implement imp.cache_from_source() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents: 76449
diff changeset
419 .pyc/.pyo file calculated as if the .py file were imported. The extension
79173
4de5e4ec3cff don't depend on __debug__ because it's baked in at freeze time (issue #16046)
Benjamin Peterson <benjamin@python.org>
parents: 78779
diff changeset
420 will be .pyc unless sys.flags.optimize is non-zero, then it will be .pyo.
76450
b773a751c2e7 Issue #13959: Re-implement imp.cache_from_source() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents: 76449
diff changeset
421
79191
9b0582477aad rephrase
Benjamin Peterson <benjamin@python.org>
parents: 79173
diff changeset
422 If debug_override is not None, then it must be a boolean and is used in
9b0582477aad rephrase
Benjamin Peterson <benjamin@python.org>
parents: 79173
diff changeset
423 place of sys.flags.optimize.
76450
b773a751c2e7 Issue #13959: Re-implement imp.cache_from_source() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents: 76449
diff changeset
424
78026
e86330669bb5 Issue #15056: imp.cache_from_source() and source_from_cache() raise
Brett Cannon <brett@python.org>
parents: 78010
diff changeset
425 If sys.implementation.cache_tag is None then NotImplementedError is raised.
e86330669bb5 Issue #15056: imp.cache_from_source() and source_from_cache() raise
Brett Cannon <brett@python.org>
parents: 78010
diff changeset
426
76450
b773a751c2e7 Issue #13959: Re-implement imp.cache_from_source() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents: 76449
diff changeset
427 """
79173
4de5e4ec3cff don't depend on __debug__ because it's baked in at freeze time (issue #16046)
Benjamin Peterson <benjamin@python.org>
parents: 78779
diff changeset
428 debug = not sys.flags.optimize if debug_override is None else debug_override
76874
b81ddaf0db47 Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents: 76826
diff changeset
429 if debug:
b81ddaf0db47 Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents: 76826
diff changeset
430 suffixes = DEBUG_BYTECODE_SUFFIXES
b81ddaf0db47 Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents: 76826
diff changeset
431 else:
b81ddaf0db47 Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents: 76826
diff changeset
432 suffixes = OPTIMIZED_BYTECODE_SUFFIXES
76450
b773a751c2e7 Issue #13959: Re-implement imp.cache_from_source() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents: 76449
diff changeset
433 head, tail = _path_split(path)
b773a751c2e7 Issue #13959: Re-implement imp.cache_from_source() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents: 76449
diff changeset
434 base_filename, sep, _ = tail.partition('.')
78026
e86330669bb5 Issue #15056: imp.cache_from_source() and source_from_cache() raise
Brett Cannon <brett@python.org>
parents: 78010
diff changeset
435 tag = sys.implementation.cache_tag
e86330669bb5 Issue #15056: imp.cache_from_source() and source_from_cache() raise
Brett Cannon <brett@python.org>
parents: 78010
diff changeset
436 if tag is None:
e86330669bb5 Issue #15056: imp.cache_from_source() and source_from_cache() raise
Brett Cannon <brett@python.org>
parents: 78010
diff changeset
437 raise NotImplementedError('sys.implementation.cache_tag is None')
e86330669bb5 Issue #15056: imp.cache_from_source() and source_from_cache() raise
Brett Cannon <brett@python.org>
parents: 78010
diff changeset
438 filename = ''.join([base_filename, sep, tag, suffixes[0]])
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
439 return _path_join(head, _PYCACHE, filename)
76450
b773a751c2e7 Issue #13959: Re-implement imp.cache_from_source() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents: 76449
diff changeset
440
b773a751c2e7 Issue #13959: Re-implement imp.cache_from_source() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents: 76449
diff changeset
441
78078
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
442 def source_from_cache(path):
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
443 """Given the path to a .pyc./.pyo file, return the path to its .py file.
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
444
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
445 The .pyc/.pyo file does not need to exist; this simply returns the path to
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
446 the .py file calculated to correspond to the .pyc/.pyo file. If path does
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
447 not conform to PEP 3147 format, ValueError will be raised. If
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
448 sys.implementation.cache_tag is None then NotImplementedError is raised.
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
449
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
450 """
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
451 if sys.implementation.cache_tag is None:
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
452 raise NotImplementedError('sys.implementation.cache_tag is None')
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
453 head, pycache_filename = _path_split(path)
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
454 head, pycache = _path_split(head)
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
455 if pycache != _PYCACHE:
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
456 raise ValueError('{} not bottom-level directory in '
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
457 '{!r}'.format(_PYCACHE, path))
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
458 if pycache_filename.count('.') != 2:
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
459 raise ValueError('expected only 2 dots in '
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
460 '{!r}'.format(pycache_filename))
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
461 base_filename = pycache_filename.partition('.')[0]
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
462 return _path_join(head, base_filename + SOURCE_SUFFIXES[0])
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
463
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
464
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
465 def _get_sourcefile(bytecode_path):
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
466 """Convert a bytecode file path to a source path (if possible).
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
467
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
468 This function exists purely for backwards-compatibility for
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
469 PyImport_ExecCodeModuleWithFilenames() in the C API.
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
470
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
471 """
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
472 if len(bytecode_path) == 0:
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
473 return None
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
474 rest, _, extension = bytecode_path.rparition('.')
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
475 if not rest or extension.lower()[-3:-1] != '.py':
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
476 return bytecode_path
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
477
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
478 try:
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
479 source_path = source_from_cache(bytecode_path)
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
480 except (NotImplementedError, ValueError):
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
481 source_path = bytcode_path[-1:]
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
482
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
483 return source_path if _path_isfile(source_stats) else bytecode_path
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
484
9e164b404983 Issues #15169, #14599: Make PyImport_ExecCodeModuleWithPathnames() use
Brett Cannon <brett@python.org>
parents: 78051
diff changeset
485
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
486 def _verbose_message(message, *args):
76303
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
487 """Print the message to stderr if -v/PYTHONVERBOSE is turned on."""
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
488 if sys.flags.verbose:
76327
1ca10eb58483 utilize startswith(tuple)
Philip Jenvey <pjenvey@underboss.org>
parents: 76312
diff changeset
489 if not message.startswith(('#', 'import ')):
76303
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
490 message = '# ' + message
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
491 print(message.format(*args), file=sys.stderr)
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
492
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
493
52002
76a579b6bade Rename importlib.util.set___package__ to set_package.
Brett Cannon <bcannon@gmail.com>
parents: 51995
diff changeset
494 def set_package(fxn):
51430
5cdc5f0c134c Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents: 51429
diff changeset
495 """Set __package__ on the returned module."""
74449
9935f7837c4b Issue #13588: Rename decorators in importlib.
Brett Cannon <brett@python.org>
parents: 74358
diff changeset
496 def set_package_wrapper(*args, **kwargs):
51430
5cdc5f0c134c Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents: 51429
diff changeset
497 module = fxn(*args, **kwargs)
77964
37850f083af5 Minor refactoring in importlib._bootstrap, and fix the '_wrap' docstring.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 77917
diff changeset
498 if getattr(module, '__package__', None) is None:
51430
5cdc5f0c134c Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents: 51429
diff changeset
499 module.__package__ = module.__name__
5cdc5f0c134c Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents: 51429
diff changeset
500 if not hasattr(module, '__path__'):
5cdc5f0c134c Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents: 51429
diff changeset
501 module.__package__ = module.__package__.rpartition('.')[0]
5cdc5f0c134c Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents: 51429
diff changeset
502 return module
74449
9935f7837c4b Issue #13588: Rename decorators in importlib.
Brett Cannon <brett@python.org>
parents: 74358
diff changeset
503 _wrap(set_package_wrapper, fxn)
9935f7837c4b Issue #13588: Rename decorators in importlib.
Brett Cannon <brett@python.org>
parents: 74358
diff changeset
504 return set_package_wrapper
51430
5cdc5f0c134c Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents: 51429
diff changeset
505
5cdc5f0c134c Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents: 51429
diff changeset
506
52115
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
507 def set_loader(fxn):
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
508 """Set __loader__ on the returned module."""
74449
9935f7837c4b Issue #13588: Rename decorators in importlib.
Brett Cannon <brett@python.org>
parents: 74358
diff changeset
509 def set_loader_wrapper(self, *args, **kwargs):
52115
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
510 module = fxn(self, *args, **kwargs)
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
511 if not hasattr(module, '__loader__'):
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
512 module.__loader__ = self
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
513 return module
74449
9935f7837c4b Issue #13588: Rename decorators in importlib.
Brett Cannon <brett@python.org>
parents: 74358
diff changeset
514 _wrap(set_loader_wrapper, fxn)
9935f7837c4b Issue #13588: Rename decorators in importlib.
Brett Cannon <brett@python.org>
parents: 74358
diff changeset
515 return set_loader_wrapper
52115
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
516
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
517
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
518 def module_for_loader(fxn):
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
519 """Decorator to handle selecting the proper module for loaders.
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
520
52164
0b19ba318071 Clean up docstring from importlib.util.module_for_loader.
Brett Cannon <bcannon@gmail.com>
parents: 52142
diff changeset
521 The decorated function is passed the module to use instead of the module
0b19ba318071 Clean up docstring from importlib.util.module_for_loader.
Brett Cannon <bcannon@gmail.com>
parents: 52142
diff changeset
522 name. The module passed in to the function is either from sys.modules if
76586
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
523 it already exists or is a new module. If the module is new, then __name__
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
524 is set the first argument to the method, __loader__ is set to self, and
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
525 __package__ is set accordingly (if self.is_package() is defined) will be set
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
526 before it is passed to the decorated function (if self.is_package() does
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
527 not work for the module it will be set post-load).
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
528
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
529 If an exception is raised and the decorator created the module it is
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
530 subsequently removed from sys.modules.
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
531
52164
0b19ba318071 Clean up docstring from importlib.util.module_for_loader.
Brett Cannon <bcannon@gmail.com>
parents: 52142
diff changeset
532 The decorator assumes that the decorated function takes the module name as
0b19ba318071 Clean up docstring from importlib.util.module_for_loader.
Brett Cannon <bcannon@gmail.com>
parents: 52142
diff changeset
533 the second argument.
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
534
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
535 """
74449
9935f7837c4b Issue #13588: Rename decorators in importlib.
Brett Cannon <brett@python.org>
parents: 74358
diff changeset
536 def module_for_loader_wrapper(self, fullname, *args, **kwargs):
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
537 module = sys.modules.get(fullname)
76384
c1399cf7bd6a Issue #12599: Be more strict in accepting None vs. a false-like object
Brett Cannon <brett@python.org>
parents: 76331
diff changeset
538 is_reload = module is not None
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
539 if not is_reload:
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
540 # This must be done before open() is called as the 'io' module
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
541 # implicitly imports 'locale' and would otherwise trigger an
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
542 # infinite loop.
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
543 module = new_module(fullname)
78779
7fa6336e9864 Issue #15781: Fix two small race conditions in import's module locking.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78758
diff changeset
544 # This must be done before putting the module in sys.modules
7fa6336e9864 Issue #15781: Fix two small race conditions in import's module locking.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78758
diff changeset
545 # (otherwise an optimization shortcut in import.c becomes wrong)
7fa6336e9864 Issue #15781: Fix two small race conditions in import's module locking.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78758
diff changeset
546 module.__initializing__ = True
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
547 sys.modules[fullname] = module
76586
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
548 module.__loader__ = self
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
549 try:
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
550 is_package = self.is_package(fullname)
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
551 except (ImportError, AttributeError):
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
552 pass
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
553 else:
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
554 if is_package:
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
555 module.__package__ = fullname
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
556 else:
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
557 module.__package__ = fullname.rpartition('.')[0]
78779
7fa6336e9864 Issue #15781: Fix two small race conditions in import's module locking.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78758
diff changeset
558 else:
7fa6336e9864 Issue #15781: Fix two small race conditions in import's module locking.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78758
diff changeset
559 module.__initializing__ = True
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
560 try:
76586
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
561 # If __package__ was not set above, __import__() will do it later.
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
562 return fxn(self, module, *args, **kwargs)
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
563 except:
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
564 if not is_reload:
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
565 del sys.modules[fullname]
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
566 raise
77013
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
567 finally:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
568 module.__initializing__ = False
74449
9935f7837c4b Issue #13588: Rename decorators in importlib.
Brett Cannon <brett@python.org>
parents: 74358
diff changeset
569 _wrap(module_for_loader_wrapper, fxn)
9935f7837c4b Issue #13588: Rename decorators in importlib.
Brett Cannon <brett@python.org>
parents: 74358
diff changeset
570 return module_for_loader_wrapper
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
571
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
572
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
573 def _check_name(method):
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
574 """Decorator to verify that the module being requested matches the one the
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
575 loader can handle.
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
576
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
577 The first argument (self) must define _name which the second argument is
68488
0991b40e895d #11515: fix several typos. Patch by Piotr Kasprzyk.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58960
diff changeset
578 compared against. If the comparison fails then ImportError is raised.
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
579
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
580 """
76877
626d5c6fbd95 Issue #13959: Have
Brett Cannon <brett@python.org>
parents: 76874
diff changeset
581 def _check_name_wrapper(self, name=None, *args, **kwargs):
626d5c6fbd95 Issue #13959: Have
Brett Cannon <brett@python.org>
parents: 76874
diff changeset
582 if name is None:
626d5c6fbd95 Issue #13959: Have
Brett Cannon <brett@python.org>
parents: 76874
diff changeset
583 name = self.name
626d5c6fbd95 Issue #13959: Have
Brett Cannon <brett@python.org>
parents: 76874
diff changeset
584 elif self.name != name:
76280
c071af95772a Have importlib take advantage of ImportError's new 'name' and 'path'
Brett Cannon <brett@python.org>
parents: 76081
diff changeset
585 raise ImportError("loader cannot handle %s" % name, name=name)
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
586 return method(self, name, *args, **kwargs)
74449
9935f7837c4b Issue #13588: Rename decorators in importlib.
Brett Cannon <brett@python.org>
parents: 74358
diff changeset
587 _wrap(_check_name_wrapper, method)
9935f7837c4b Issue #13588: Rename decorators in importlib.
Brett Cannon <brett@python.org>
parents: 74358
diff changeset
588 return _check_name_wrapper
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
589
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
590
52166
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
591 def _requires_builtin(fxn):
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
592 """Decorator to verify the named module is built-in."""
74449
9935f7837c4b Issue #13588: Rename decorators in importlib.
Brett Cannon <brett@python.org>
parents: 74358
diff changeset
593 def _requires_builtin_wrapper(self, fullname):
52166
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
594 if fullname not in sys.builtin_module_names:
77964
37850f083af5 Minor refactoring in importlib._bootstrap, and fix the '_wrap' docstring.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 77917
diff changeset
595 raise ImportError("{} is not a built-in module".format(fullname),
76280
c071af95772a Have importlib take advantage of ImportError's new 'name' and 'path'
Brett Cannon <brett@python.org>
parents: 76081
diff changeset
596 name=fullname)
52166
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
597 return fxn(self, fullname)
74449
9935f7837c4b Issue #13588: Rename decorators in importlib.
Brett Cannon <brett@python.org>
parents: 74358
diff changeset
598 _wrap(_requires_builtin_wrapper, fxn)
9935f7837c4b Issue #13588: Rename decorators in importlib.
Brett Cannon <brett@python.org>
parents: 74358
diff changeset
599 return _requires_builtin_wrapper
52166
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
600
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
601
52168
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
602 def _requires_frozen(fxn):
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
603 """Decorator to verify the named module is frozen."""
74449
9935f7837c4b Issue #13588: Rename decorators in importlib.
Brett Cannon <brett@python.org>
parents: 74358
diff changeset
604 def _requires_frozen_wrapper(self, fullname):
76331
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
605 if not _imp.is_frozen(fullname):
77964
37850f083af5 Minor refactoring in importlib._bootstrap, and fix the '_wrap' docstring.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 77917
diff changeset
606 raise ImportError("{} is not a frozen module".format(fullname),
76280
c071af95772a Have importlib take advantage of ImportError's new 'name' and 'path'
Brett Cannon <brett@python.org>
parents: 76081
diff changeset
607 name=fullname)
52168
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
608 return fxn(self, fullname)
74449
9935f7837c4b Issue #13588: Rename decorators in importlib.
Brett Cannon <brett@python.org>
parents: 74358
diff changeset
609 _wrap(_requires_frozen_wrapper, fxn)
9935f7837c4b Issue #13588: Rename decorators in importlib.
Brett Cannon <brett@python.org>
parents: 74358
diff changeset
610 return _requires_frozen_wrapper
52168
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
611
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
612
78489
e7a67f1bf604 Issue #15502: Refactor some code.
Brett Cannon <brett@python.org>
parents: 78487
diff changeset
613 def _find_module_shim(self, fullname):
e7a67f1bf604 Issue #15502: Refactor some code.
Brett Cannon <brett@python.org>
parents: 78487
diff changeset
614 """Try to find a loader for the specified module by delegating to
e7a67f1bf604 Issue #15502: Refactor some code.
Brett Cannon <brett@python.org>
parents: 78487
diff changeset
615 self.find_loader()."""
e7a67f1bf604 Issue #15502: Refactor some code.
Brett Cannon <brett@python.org>
parents: 78487
diff changeset
616 # Call find_loader(). If it returns a string (indicating this
e7a67f1bf604 Issue #15502: Refactor some code.
Brett Cannon <brett@python.org>
parents: 78487
diff changeset
617 # is a namespace package portion), generate a warning and
e7a67f1bf604 Issue #15502: Refactor some code.
Brett Cannon <brett@python.org>
parents: 78487
diff changeset
618 # return None.
e7a67f1bf604 Issue #15502: Refactor some code.
Brett Cannon <brett@python.org>
parents: 78487
diff changeset
619 loader, portions = self.find_loader(fullname)
e7a67f1bf604 Issue #15502: Refactor some code.
Brett Cannon <brett@python.org>
parents: 78487
diff changeset
620 if loader is None and len(portions):
e7a67f1bf604 Issue #15502: Refactor some code.
Brett Cannon <brett@python.org>
parents: 78487
diff changeset
621 msg = "Not importing directory {}: missing __init__"
e7a67f1bf604 Issue #15502: Refactor some code.
Brett Cannon <brett@python.org>
parents: 78487
diff changeset
622 _warnings.warn(msg.format(portions[0]), ImportWarning)
e7a67f1bf604 Issue #15502: Refactor some code.
Brett Cannon <brett@python.org>
parents: 78487
diff changeset
623 return loader
e7a67f1bf604 Issue #15502: Refactor some code.
Brett Cannon <brett@python.org>
parents: 78487
diff changeset
624
e7a67f1bf604 Issue #15502: Refactor some code.
Brett Cannon <brett@python.org>
parents: 78487
diff changeset
625
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
626
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
627
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
628 # Loaders #####################################################################
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
629
50983
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
630 class BuiltinImporter:
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
631
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52164
diff changeset
632 """Meta path import for built-in modules.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
633
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52164
diff changeset
634 All methods are either class or static methods to avoid the need to
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52164
diff changeset
635 instantiate the class.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
636
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
637 """
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
638
50983
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
639 @classmethod
77126
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
640 def module_repr(cls, module):
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
641 return "<module '{}' (built-in)>".format(module.__name__)
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
642
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
643 @classmethod
50983
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
644 def find_module(cls, fullname, path=None):
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52164
diff changeset
645 """Find the built-in module.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
646
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
647 If 'path' is ever specified then the search is considered a failure.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
648
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
649 """
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
650 if path is not None:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
651 return None
76331
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
652 return cls if _imp.is_builtin(fullname) else None
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
653
51026
c26f1af26bfc Document both importlib.machinery.BuiltinImporter and FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 50983
diff changeset
654 @classmethod
52002
76a579b6bade Rename importlib.util.set___package__ to set_package.
Brett Cannon <bcannon@gmail.com>
parents: 51995
diff changeset
655 @set_package
52115
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
656 @set_loader
52166
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
657 @_requires_builtin
51026
c26f1af26bfc Document both importlib.machinery.BuiltinImporter and FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 50983
diff changeset
658 def load_module(cls, fullname):
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
659 """Load a built-in module."""
51681
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
660 is_reload = fullname in sys.modules
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
661 try:
78344
62033490ca0f Close #15486: Simplify the mechanism used to remove importlib frames from tracebacks when they just introduce irrelevant noise
Nick Coghlan <ncoghlan@gmail.com>
parents: 78314
diff changeset
662 return _call_with_frames_removed(_imp.init_builtin, fullname)
51681
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
663 except:
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
664 if not is_reload and fullname in sys.modules:
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
665 del sys.modules[fullname]
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
666 raise
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
667
52166
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
668 @classmethod
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
669 @_requires_builtin
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
670 def get_code(cls, fullname):
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
671 """Return None as built-in modules do not have code objects."""
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
672 return None
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
673
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
674 @classmethod
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
675 @_requires_builtin
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
676 def get_source(cls, fullname):
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
677 """Return None as built-in modules do not have source code."""
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
678 return None
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
679
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
680 @classmethod
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
681 @_requires_builtin
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
682 def is_package(cls, fullname):
77964
37850f083af5 Minor refactoring in importlib._bootstrap, and fix the '_wrap' docstring.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 77917
diff changeset
683 """Return False as built-in modules are never packages."""
52166
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
684 return False
69cd615b76fb Implement InspectLoader for BuiltinImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
685
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
686
50983
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
687 class FrozenImporter:
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
688
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52164
diff changeset
689 """Meta path import for frozen modules.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
690
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52164
diff changeset
691 All methods are either class or static methods to avoid the need to
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52164
diff changeset
692 instantiate the class.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
693
50983
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
694 """
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
695
50983
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
696 @classmethod
77126
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
697 def module_repr(cls, m):
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
698 return "<module '{}' (frozen)>".format(m.__name__)
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
699
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
700 @classmethod
50983
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
701 def find_module(cls, fullname, path=None):
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
702 """Find a frozen module."""
76331
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
703 return cls if _imp.is_frozen(fullname) else None
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
704
50983
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
705 @classmethod
52002
76a579b6bade Rename importlib.util.set___package__ to set_package.
Brett Cannon <bcannon@gmail.com>
parents: 51995
diff changeset
706 @set_package
52115
afd7118c8ccf Implement importlib.util.set_loader: a decorator to automatically set
Brett Cannon <bcannon@gmail.com>
parents: 52112
diff changeset
707 @set_loader
52168
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
708 @_requires_frozen
50983
0940a4f2506e Add importlib.machinery with its first tenants, BuitinImporter and
Brett Cannon <bcannon@gmail.com>
parents: 50893
diff changeset
709 def load_module(cls, fullname):
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
710 """Load a frozen module."""
51681
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
711 is_reload = fullname in sys.modules
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
712 try:
78344
62033490ca0f Close #15486: Simplify the mechanism used to remove importlib frames from tracebacks when they just introduce irrelevant noise
Nick Coghlan <ncoghlan@gmail.com>
parents: 78314
diff changeset
713 m = _call_with_frames_removed(_imp.init_frozen, fullname)
77126
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
714 # Let our own module_repr() method produce a suitable repr.
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
715 del m.__file__
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
716 return m
51681
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
717 except:
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
718 if not is_reload and fullname in sys.modules:
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
719 del sys.modules[fullname]
147fcebacb5b Implement the more specific PEP 302 semantics for loaders and what happens upon
Brett Cannon <bcannon@gmail.com>
parents: 51639
diff changeset
720 raise
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
721
52168
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
722 @classmethod
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
723 @_requires_frozen
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
724 def get_code(cls, fullname):
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
725 """Return the code object for the frozen module."""
76331
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
726 return _imp.get_frozen_object(fullname)
52168
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
727
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
728 @classmethod
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
729 @_requires_frozen
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
730 def get_source(cls, fullname):
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
731 """Return None as frozen modules do not have source code."""
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
732 return None
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
733
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
734 @classmethod
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
735 @_requires_frozen
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
736 def is_package(cls, fullname):
78491
f7b59e890e30 fix docstring wording
Philip Jenvey <pjenvey@underboss.org>
parents: 78489
diff changeset
737 """Return True if the frozen module is a package."""
76331
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
738 return _imp.is_frozen_package(fullname)
52168
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
739
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
740
78380
a1ac1e13c5a0 Close #15519: Properly expose WindowsRegistryFinder in importlib and bring the name into line with normal import terminology. Original patch by Eric Snow
Nick Coghlan <ncoghlan@gmail.com>
parents: 78344
diff changeset
741 class WindowsRegistryFinder:
78308
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
742
78380
a1ac1e13c5a0 Close #15519: Properly expose WindowsRegistryFinder in importlib and bring the name into line with normal import terminology. Original patch by Eric Snow
Nick Coghlan <ncoghlan@gmail.com>
parents: 78344
diff changeset
743 """Meta path finder for modules declared in the Windows registry.
78308
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
744 """
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
745
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
746 REGISTRY_KEY = (
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
747 "Software\\Python\\PythonCore\\{sys_version}"
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
748 "\\Modules\\{fullname}")
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
749 REGISTRY_KEY_DEBUG = (
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
750 "Software\\Python\\PythonCore\\{sys_version}"
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
751 "\\Modules\\{fullname}\\Debug")
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
752 DEBUG_BUILD = False # Changed in _setup()
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
753
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
754 @classmethod
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
755 def _open_registry(cls, key):
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
756 try:
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
757 return _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, key)
80944
bb94f6222fef Issue #16719: Get rid of WindowsError. Use OSError instead
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80930
diff changeset
758 except OSError:
78308
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
759 return _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, key)
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
760
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
761 @classmethod
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
762 def _search_registry(cls, fullname):
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
763 if cls.DEBUG_BUILD:
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
764 registry_key = cls.REGISTRY_KEY_DEBUG
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
765 else:
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
766 registry_key = cls.REGISTRY_KEY
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
767 key = registry_key.format(fullname=fullname,
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
768 sys_version=sys.version[:3])
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
769 try:
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
770 with cls._open_registry(key) as hkey:
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
771 filepath = _winreg.QueryValue(hkey, "")
80944
bb94f6222fef Issue #16719: Get rid of WindowsError. Use OSError instead
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80930
diff changeset
772 except OSError:
78308
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
773 return None
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
774 return filepath
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
775
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
776 @classmethod
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
777 def find_module(cls, fullname, path=None):
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
778 """Find module named in the registry."""
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
779 filepath = cls._search_registry(fullname)
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
780 if filepath is None:
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
781 return None
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
782 try:
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
783 _os.stat(filepath)
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
784 except OSError:
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
785 return None
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
786 for loader, suffixes, _ in _get_supported_file_loaders():
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
787 if filepath.endswith(tuple(suffixes)):
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
788 return loader(fullname, filepath)
52168
70f5f9113ae1 Implement InspectLoader for FrozenImporter.
Brett Cannon <bcannon@gmail.com>
parents: 52166
diff changeset
789
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
790
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
791 class _LoaderBasics:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
792
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
793 """Base class of common code needed by both SourceLoader and
76478
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
794 SourcelessFileLoader."""
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
795
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
796 def is_package(self, fullname):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
797 """Concrete implementation of InspectLoader.is_package by checking if
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
798 the path returned by get_filename has a filename of '__init__.py'."""
76449
163360370ad2 Make path manipulation more robust for platforms with alternative path
Brett Cannon <brett@python.org>
parents: 76445
diff changeset
799 filename = _path_split(self.get_filename(fullname))[1]
77457
240b7467e65c Issue #14938: importlib.abc.SourceLoader.is_package() now takes the
Brett Cannon <brett@python.org>
parents: 77180
diff changeset
800 filename_base = filename.rsplit('.', 1)[0]
240b7467e65c Issue #14938: importlib.abc.SourceLoader.is_package() now takes the
Brett Cannon <brett@python.org>
parents: 77180
diff changeset
801 tail_name = fullname.rpartition('.')[2]
240b7467e65c Issue #14938: importlib.abc.SourceLoader.is_package() now takes the
Brett Cannon <brett@python.org>
parents: 77180
diff changeset
802 return filename_base == '__init__' and tail_name != '__init__'
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52164
diff changeset
803
76280
c071af95772a Have importlib take advantage of ImportError's new 'name' and 'path'
Brett Cannon <brett@python.org>
parents: 76081
diff changeset
804 def _bytes_from_bytecode(self, fullname, data, bytecode_path, source_stats):
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
805 """Return the marshalled bytes from bytecode, verifying the magic
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
806 number, timestamp and source size along the way.
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
807
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
808 If source_stats is None then skip the timestamp check.
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
809
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
810 """
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
811 magic = data[:4]
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
812 raw_timestamp = data[4:8]
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
813 raw_size = data[8:12]
78042
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
814 if magic != _MAGIC_BYTES:
76445
c820aa9c0c00 Issue #13959: Keep imp.get_magic() in C code, but cache in importlib
Brett Cannon <brett@python.org>
parents: 76428
diff changeset
815 msg = 'bad magic number in {!r}: {!r}'.format(fullname, magic)
c820aa9c0c00 Issue #13959: Keep imp.get_magic() in C code, but cache in importlib
Brett Cannon <brett@python.org>
parents: 76428
diff changeset
816 raise ImportError(msg, name=fullname, path=bytecode_path)
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
817 elif len(raw_timestamp) != 4:
76303
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
818 message = 'bad timestamp in {}'.format(fullname)
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
819 _verbose_message(message)
76303
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
820 raise EOFError(message)
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
821 elif len(raw_size) != 4:
76303
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
822 message = 'bad size in {}'.format(fullname)
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
823 _verbose_message(message)
76303
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
824 raise EOFError(message)
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
825 if source_stats is not None:
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
826 try:
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
827 source_mtime = int(source_stats['mtime'])
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
828 except KeyError:
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
829 pass
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
830 else:
74630
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
831 if _r_long(raw_timestamp) != source_mtime:
76303
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
832 message = 'bytecode is stale for {}'.format(fullname)
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
833 _verbose_message(message)
76303
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
834 raise ImportError(message, name=fullname,
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
835 path=bytecode_path)
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
836 try:
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
837 source_size = source_stats['size'] & 0xFFFFFFFF
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
838 except KeyError:
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
839 pass
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
840 else:
74630
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
841 if _r_long(raw_size) != source_size:
75250
8840037a9c53 unused imports, pep8
Philip Jenvey <pjenvey@underboss.org>
parents: 75239
diff changeset
842 raise ImportError(
76280
c071af95772a Have importlib take advantage of ImportError's new 'name' and 'path'
Brett Cannon <brett@python.org>
parents: 76081
diff changeset
843 "bytecode is stale for {}".format(fullname),
c071af95772a Have importlib take advantage of ImportError's new 'name' and 'path'
Brett Cannon <brett@python.org>
parents: 76081
diff changeset
844 name=fullname, path=bytecode_path)
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
845 # Can't return the code object as errors from marshal loading need to
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
846 # propagate even when source is available.
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
847 return data[12:]
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
848
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
849 @module_for_loader
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
850 def _load_module(self, module, *, sourceless=False):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
851 """Helper for load_module able to handle either source or sourceless
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
852 loading."""
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
853 name = module.__name__
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
854 code_object = self.get_code(name)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
855 module.__file__ = self.get_filename(name)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
856 if not sourceless:
78026
e86330669bb5 Issue #15056: imp.cache_from_source() and source_from_cache() raise
Brett Cannon <brett@python.org>
parents: 78010
diff changeset
857 try:
e86330669bb5 Issue #15056: imp.cache_from_source() and source_from_cache() raise
Brett Cannon <brett@python.org>
parents: 78010
diff changeset
858 module.__cached__ = cache_from_source(module.__file__)
e86330669bb5 Issue #15056: imp.cache_from_source() and source_from_cache() raise
Brett Cannon <brett@python.org>
parents: 78010
diff changeset
859 except NotImplementedError:
e86330669bb5 Issue #15056: imp.cache_from_source() and source_from_cache() raise
Brett Cannon <brett@python.org>
parents: 78010
diff changeset
860 module.__cached__ = module.__file__
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
861 else:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
862 module.__cached__ = module.__file__
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
863 module.__package__ = name
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
864 if self.is_package(name):
76449
163360370ad2 Make path manipulation more robust for platforms with alternative path
Brett Cannon <brett@python.org>
parents: 76445
diff changeset
865 module.__path__ = [_path_split(module.__file__)[0]]
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
866 else:
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
867 module.__package__ = module.__package__.rpartition('.')[0]
52075
2033d45a5b99 Minor changes to Python source base loader.
Brett Cannon <bcannon@gmail.com>
parents: 52002
diff changeset
868 module.__loader__ = self
78344
62033490ca0f Close #15486: Simplify the mechanism used to remove importlib frames from tracebacks when they just introduce irrelevant noise
Nick Coghlan <ncoghlan@gmail.com>
parents: 78314
diff changeset
869 _call_with_frames_removed(exec, code_object, module.__dict__)
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
870 return module
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
871
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
872
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
873 class SourceLoader(_LoaderBasics):
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
874
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 66226
diff changeset
875 def path_mtime(self, path):
67581
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
876 """Optional method that returns the modification time (an int) for the
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
877 specified path, where path is a str.
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
878 """
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
879 raise NotImplementedError
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
880
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
881 def path_stats(self, path):
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
882 """Optional method returning a metadata dict for the specified path
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
883 to by the path (str).
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
884 Possible keys:
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
885 - 'mtime' (mandatory) is the numeric timestamp of last source
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
886 code modification;
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
887 - 'size' (optional) is the size in bytes of the source code.
52112
03080fa08dfe Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
888
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
889 Implementing this method allows the loader to read bytecode files.
52112
03080fa08dfe Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
890 """
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
891 return {'mtime': self.path_mtime(path)}
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
892
78722
3a831a0a29c4 Close #2501: Permission bits are once again correctly copied from the source file to the cached bytecode file. Test by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78663
diff changeset
893 def _cache_bytecode(self, source_path, cache_path, data):
3a831a0a29c4 Close #2501: Permission bits are once again correctly copied from the source file to the cached bytecode file. Test by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78663
diff changeset
894 """Optional method which writes data (bytes) to a file path (a str).
3a831a0a29c4 Close #2501: Permission bits are once again correctly copied from the source file to the cached bytecode file. Test by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78663
diff changeset
895
3a831a0a29c4 Close #2501: Permission bits are once again correctly copied from the source file to the cached bytecode file. Test by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78663
diff changeset
896 Implementing this method allows for the writing of bytecode files.
3a831a0a29c4 Close #2501: Permission bits are once again correctly copied from the source file to the cached bytecode file. Test by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78663
diff changeset
897
3a831a0a29c4 Close #2501: Permission bits are once again correctly copied from the source file to the cached bytecode file. Test by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78663
diff changeset
898 The source path is needed in order to correctly transfer permissions
3a831a0a29c4 Close #2501: Permission bits are once again correctly copied from the source file to the cached bytecode file. Test by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78663
diff changeset
899 """
3a831a0a29c4 Close #2501: Permission bits are once again correctly copied from the source file to the cached bytecode file. Test by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78663
diff changeset
900 # For backwards compatibility, we delegate to set_data()
3a831a0a29c4 Close #2501: Permission bits are once again correctly copied from the source file to the cached bytecode file. Test by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78663
diff changeset
901 return self.set_data(cache_path, data)
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
902
67573
e125d92f7438 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 66226
diff changeset
903 def set_data(self, path, data):
67581
7d658b49b3e1 Issue 10899: Remove function type annotations from the stdlib
Raymond Hettinger <python@rcn.com>
parents: 67573
diff changeset
904 """Optional method which writes data (bytes) to a file path (a str).
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
905
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
906 Implementing this method allows for the writing of bytecode files.
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
907
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
908 """
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
909 raise NotImplementedError
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
910
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
911
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
912 def get_source(self, fullname):
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
913 """Concrete implementation of InspectLoader.get_source."""
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
914 import tokenize
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
915 path = self.get_filename(fullname)
52112
03080fa08dfe Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
916 try:
03080fa08dfe Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
917 source_bytes = self.get_data(path)
81039
7d69d04522e3 Replace IOError with OSError (#16715)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80944
diff changeset
918 except OSError as exc:
76280
c071af95772a Have importlib take advantage of ImportError's new 'name' and 'path'
Brett Cannon <brett@python.org>
parents: 76081
diff changeset
919 raise ImportError("source not available through get_data()",
78112
7d202353a728 Issue #15343: A lot more than just unicode decoding can go wrong when retrieving a source file
Nick Coghlan <ncoghlan@gmail.com>
parents: 78078
diff changeset
920 name=fullname) from exc
7d202353a728 Issue #15343: A lot more than just unicode decoding can go wrong when retrieving a source file
Nick Coghlan <ncoghlan@gmail.com>
parents: 78078
diff changeset
921 readsource = _io.BytesIO(source_bytes).readline
7d202353a728 Issue #15343: A lot more than just unicode decoding can go wrong when retrieving a source file
Nick Coghlan <ncoghlan@gmail.com>
parents: 78078
diff changeset
922 try:
7d202353a728 Issue #15343: A lot more than just unicode decoding can go wrong when retrieving a source file
Nick Coghlan <ncoghlan@gmail.com>
parents: 78078
diff changeset
923 encoding = tokenize.detect_encoding(readsource)
7d202353a728 Issue #15343: A lot more than just unicode decoding can go wrong when retrieving a source file
Nick Coghlan <ncoghlan@gmail.com>
parents: 78078
diff changeset
924 except SyntaxError as exc:
7d202353a728 Issue #15343: A lot more than just unicode decoding can go wrong when retrieving a source file
Nick Coghlan <ncoghlan@gmail.com>
parents: 78078
diff changeset
925 raise ImportError("Failed to detect encoding",
7d202353a728 Issue #15343: A lot more than just unicode decoding can go wrong when retrieving a source file
Nick Coghlan <ncoghlan@gmail.com>
parents: 78078
diff changeset
926 name=fullname) from exc
62761
32682b253ffc Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62756
diff changeset
927 newline_decoder = _io.IncrementalNewlineDecoder(None, True)
78112
7d202353a728 Issue #15343: A lot more than just unicode decoding can go wrong when retrieving a source file
Nick Coghlan <ncoghlan@gmail.com>
parents: 78078
diff changeset
928 try:
7d202353a728 Issue #15343: A lot more than just unicode decoding can go wrong when retrieving a source file
Nick Coghlan <ncoghlan@gmail.com>
parents: 78078
diff changeset
929 return newline_decoder.decode(source_bytes.decode(encoding[0]))
7d202353a728 Issue #15343: A lot more than just unicode decoding can go wrong when retrieving a source file
Nick Coghlan <ncoghlan@gmail.com>
parents: 78078
diff changeset
930 except UnicodeDecodeError as exc:
7d202353a728 Issue #15343: A lot more than just unicode decoding can go wrong when retrieving a source file
Nick Coghlan <ncoghlan@gmail.com>
parents: 78078
diff changeset
931 raise ImportError("Failed to decode source file",
7d202353a728 Issue #15343: A lot more than just unicode decoding can go wrong when retrieving a source file
Nick Coghlan <ncoghlan@gmail.com>
parents: 78078
diff changeset
932 name=fullname) from exc
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
933
80725
7caab48f171e Issue #15627: This is simply an update to the name of a new method recently added
Eric Snow <ericsnowcurrently@gmail.com>
parents: 80530
diff changeset
934 def source_to_code(self, data, path):
80507
e30bcce5c634 Issue #15627: Add the compile_source() method to
Brett Cannon <brett@python.org>
parents: 80410
diff changeset
935 """Return the code object compiled from source.
e30bcce5c634 Issue #15627: Add the compile_source() method to
Brett Cannon <brett@python.org>
parents: 80410
diff changeset
936
e30bcce5c634 Issue #15627: Add the compile_source() method to
Brett Cannon <brett@python.org>
parents: 80410
diff changeset
937 The 'data' argument can be any object type that compile() supports.
e30bcce5c634 Issue #15627: Add the compile_source() method to
Brett Cannon <brett@python.org>
parents: 80410
diff changeset
938 """
e30bcce5c634 Issue #15627: Add the compile_source() method to
Brett Cannon <brett@python.org>
parents: 80410
diff changeset
939 return _call_with_frames_removed(compile, data, path, 'exec',
e30bcce5c634 Issue #15627: Add the compile_source() method to
Brett Cannon <brett@python.org>
parents: 80410
diff changeset
940 dont_inherit=True)
e30bcce5c634 Issue #15627: Add the compile_source() method to
Brett Cannon <brett@python.org>
parents: 80410
diff changeset
941
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
942 def get_code(self, fullname):
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
943 """Concrete implementation of InspectLoader.get_code.
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
944
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
945 Reading of bytecode requires path_stats to be implemented. To write
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
946 bytecode, set_data must also be implemented.
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
947
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
948 """
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
949 source_path = self.get_filename(fullname)
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
950 source_mtime = None
78026
e86330669bb5 Issue #15056: imp.cache_from_source() and source_from_cache() raise
Brett Cannon <brett@python.org>
parents: 78010
diff changeset
951 try:
e86330669bb5 Issue #15056: imp.cache_from_source() and source_from_cache() raise
Brett Cannon <brett@python.org>
parents: 78010
diff changeset
952 bytecode_path = cache_from_source(source_path)
e86330669bb5 Issue #15056: imp.cache_from_source() and source_from_cache() raise
Brett Cannon <brett@python.org>
parents: 78010
diff changeset
953 except NotImplementedError:
e86330669bb5 Issue #15056: imp.cache_from_source() and source_from_cache() raise
Brett Cannon <brett@python.org>
parents: 78010
diff changeset
954 bytecode_path = None
e86330669bb5 Issue #15056: imp.cache_from_source() and source_from_cache() raise
Brett Cannon <brett@python.org>
parents: 78010
diff changeset
955 else:
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
956 try:
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
957 st = self.path_stats(source_path)
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
958 except NotImplementedError:
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
959 pass
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
960 else:
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
961 source_mtime = int(st['mtime'])
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
962 try:
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
963 data = self.get_data(bytecode_path)
81039
7d69d04522e3 Replace IOError with OSError (#16715)
Andrew Svetlov <andrew.svetlov@gmail.com>
parents: 80944
diff changeset
964 except OSError:
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
965 pass
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
966 else:
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
967 try:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
968 bytes_data = self._bytes_from_bytecode(fullname, data,
76280
c071af95772a Have importlib take advantage of ImportError's new 'name' and 'path'
Brett Cannon <brett@python.org>
parents: 76081
diff changeset
969 bytecode_path,
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
970 st)
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
971 except (ImportError, EOFError):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
972 pass
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
973 else:
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
974 _verbose_message('{} matches {}', bytecode_path,
76303
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
975 source_path)
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
976 found = marshal.loads(bytes_data)
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
977 if isinstance(found, _code_type):
76331
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
978 _imp._fix_co_filename(found, source_path)
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
979 _verbose_message('code object from {}',
76303
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
980 bytecode_path)
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
981 return found
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
982 else:
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
983 msg = "Non-code object in {}"
76280
c071af95772a Have importlib take advantage of ImportError's new 'name' and 'path'
Brett Cannon <brett@python.org>
parents: 76081
diff changeset
984 raise ImportError(msg.format(bytecode_path),
c071af95772a Have importlib take advantage of ImportError's new 'name' and 'path'
Brett Cannon <brett@python.org>
parents: 76081
diff changeset
985 name=fullname, path=bytecode_path)
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
986 source_bytes = self.get_data(source_path)
80725
7caab48f171e Issue #15627: This is simply an update to the name of a new method recently added
Eric Snow <ericsnowcurrently@gmail.com>
parents: 80530
diff changeset
987 code_object = self.source_to_code(source_bytes, source_path)
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
988 _verbose_message('code object from {}', source_path)
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
989 if (not sys.dont_write_bytecode and bytecode_path is not None and
75250
8840037a9c53 unused imports, pep8
Philip Jenvey <pjenvey@underboss.org>
parents: 75239
diff changeset
990 source_mtime is not None):
78042
efb5e6ab10f4 Issue #15167 (as part of #13959): imp.get_magic() is no implemented in
Brett Cannon <brett@python.org>
parents: 78026
diff changeset
991 data = bytearray(_MAGIC_BYTES)
74630
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
992 data.extend(_w_long(source_mtime))
7d8f016784cd Move some code from importlib.__init__ to importlib._bootstrap that
Brett Cannon <brett@python.org>
parents: 74449
diff changeset
993 data.extend(_w_long(len(source_bytes)))
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
994 data.extend(marshal.dumps(code_object))
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
995 try:
78722
3a831a0a29c4 Close #2501: Permission bits are once again correctly copied from the source file to the cached bytecode file. Test by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78663
diff changeset
996 self._cache_bytecode(source_path, bytecode_path, data)
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
997 _verbose_message('wrote {!r}', bytecode_path)
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
998 except NotImplementedError:
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
999 pass
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
1000 return code_object
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
1001
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1002 def load_module(self, fullname):
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
1003 """Concrete implementation of Loader.load_module.
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
1004
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
1005 Requires ExecutionLoader.get_filename and ResourceLoader.get_data to be
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
1006 implemented to load source code. Use of bytecode is dictated by whether
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
1007 get_code uses/writes bytecode.
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
1008
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
1009 """
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1010 return self._load_module(fullname)
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
1011
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
1012
76478
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1013 class FileLoader:
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
1014
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1015 """Base file loader class which implements the loader protocol methods that
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1016 require file system usage."""
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1017
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
1018 def __init__(self, fullname, path):
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1019 """Cache the module name and the path to the file found by the
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1020 finder."""
76478
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1021 self.name = fullname
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1022 self.path = path
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1023
52139
2e8e897a1244 Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents: 52115
diff changeset
1024 @_check_name
76877
626d5c6fbd95 Issue #13959: Have
Brett Cannon <brett@python.org>
parents: 76874
diff changeset
1025 def load_module(self, fullname):
626d5c6fbd95 Issue #13959: Have
Brett Cannon <brett@python.org>
parents: 76874
diff changeset
1026 """Load a module from a file."""
77180
bcb3b81853cc Tweak importlib._bootstrap to avoid zero-argument super so I can work on issue #14857 without breaking imports
Nick Coghlan <ncoghlan@gmail.com>
parents: 77164
diff changeset
1027 # Issue #14857: Avoid the zero-argument form so the implementation
bcb3b81853cc Tweak importlib._bootstrap to avoid zero-argument super so I can work on issue #14857 without breaking imports
Nick Coghlan <ncoghlan@gmail.com>
parents: 77164
diff changeset
1028 # of that form can be updated without breaking the frozen module
bcb3b81853cc Tweak importlib._bootstrap to avoid zero-argument super so I can work on issue #14857 without breaking imports
Nick Coghlan <ncoghlan@gmail.com>
parents: 77164
diff changeset
1029 return super(FileLoader, self).load_module(fullname)
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1030
52139
2e8e897a1244 Make utility code in importlib._bootstrap private.
Brett Cannon <bcannon@gmail.com>
parents: 52115
diff changeset
1031 @_check_name
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
1032 def get_filename(self, fullname):
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
1033 """Return the path to the source file as found by the finder."""
76478
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1034 return self.path
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1035
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
1036 def get_data(self, path):
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
1037 """Return the data from path as raw bytes."""
64407
f8ba9558adbf Remove redundant context manager.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 64291
diff changeset
1038 with _io.FileIO(path, 'r') as file:
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1039 return file.read()
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
1040
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
1041
76478
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1042 class SourceFileLoader(FileLoader, SourceLoader):
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
1043
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1044 """Concrete implementation of SourceLoader using the file system."""
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
1045
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
1046 def path_stats(self, path):
78491
f7b59e890e30 fix docstring wording
Philip Jenvey <pjenvey@underboss.org>
parents: 78489
diff changeset
1047 """Return the metadata for the path."""
74358
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
1048 st = _os.stat(path)
87331661042b Issue #13645: pyc files now contain the size of the corresponding source
Antoine Pitrou <solipsis@pitrou.net>
parents: 73977
diff changeset
1049 return {'mtime': st.st_mtime, 'size': st.st_size}
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
1050
78722
3a831a0a29c4 Close #2501: Permission bits are once again correctly copied from the source file to the cached bytecode file. Test by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78663
diff changeset
1051 def _cache_bytecode(self, source_path, bytecode_path, data):
3a831a0a29c4 Close #2501: Permission bits are once again correctly copied from the source file to the cached bytecode file. Test by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78663
diff changeset
1052 # Adapt between the two APIs
78738
0c661c5632e0 Issue #2051: Tweak last commit for this issue to pass in mode instead
Brett Cannon <brett@python.org>
parents: 78722
diff changeset
1053 try:
0c661c5632e0 Issue #2051: Tweak last commit for this issue to pass in mode instead
Brett Cannon <brett@python.org>
parents: 78722
diff changeset
1054 mode = _os.stat(source_path).st_mode
0c661c5632e0 Issue #2051: Tweak last commit for this issue to pass in mode instead
Brett Cannon <brett@python.org>
parents: 78722
diff changeset
1055 except OSError:
0c661c5632e0 Issue #2051: Tweak last commit for this issue to pass in mode instead
Brett Cannon <brett@python.org>
parents: 78722
diff changeset
1056 mode = 0o666
79832
bbb3459fbcb8 Issue #6074: Apply an appropriate fix for importlib based imports
Nick Coghlan <ncoghlan@gmail.com>
parents: 79741
diff changeset
1057 # We always ensure write access so we can update cached files
bbb3459fbcb8 Issue #6074: Apply an appropriate fix for importlib based imports
Nick Coghlan <ncoghlan@gmail.com>
parents: 79741
diff changeset
1058 # later even when the source files are read-only on Windows (#6074)
bbb3459fbcb8 Issue #6074: Apply an appropriate fix for importlib based imports
Nick Coghlan <ncoghlan@gmail.com>
parents: 79741
diff changeset
1059 mode |= 0o200
78738
0c661c5632e0 Issue #2051: Tweak last commit for this issue to pass in mode instead
Brett Cannon <brett@python.org>
parents: 78722
diff changeset
1060 return self.set_data(bytecode_path, data, _mode=mode)
78722
3a831a0a29c4 Close #2501: Permission bits are once again correctly copied from the source file to the cached bytecode file. Test by Eric Snow.
Nick Coghlan <ncoghlan@gmail.com>
parents: 78663
diff changeset
1061
78738
0c661c5632e0 Issue #2051: Tweak last commit for this issue to pass in mode instead
Brett Cannon <brett@python.org>
parents: 78722
diff changeset
1062 def set_data(self, path, data, *, _mode=0o666):
62587
b07b2c954a80 Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 61692
diff changeset
1063 """Write bytes data to a file."""
76449
163360370ad2 Make path manipulation more robust for platforms with alternative path
Brett Cannon <brett@python.org>
parents: 76445
diff changeset
1064 parent, filename = _path_split(path)
64240
cf399de12b54 One of the joys of having test_multiprocessing occasionally execute after
Brett Cannon <bcannon@gmail.com>
parents: 62761
diff changeset
1065 path_parts = []
cf399de12b54 One of the joys of having test_multiprocessing occasionally execute after
Brett Cannon <bcannon@gmail.com>
parents: 62761
diff changeset
1066 # Figure out what directories are missing.
cf399de12b54 One of the joys of having test_multiprocessing occasionally execute after
Brett Cannon <bcannon@gmail.com>
parents: 62761
diff changeset
1067 while parent and not _path_isdir(parent):
76449
163360370ad2 Make path manipulation more robust for platforms with alternative path
Brett Cannon <brett@python.org>
parents: 76445
diff changeset
1068 parent, part = _path_split(parent)
64240
cf399de12b54 One of the joys of having test_multiprocessing occasionally execute after
Brett Cannon <bcannon@gmail.com>
parents: 62761
diff changeset
1069 path_parts.append(part)
cf399de12b54 One of the joys of having test_multiprocessing occasionally execute after
Brett Cannon <bcannon@gmail.com>
parents: 62761
diff changeset
1070 # Create needed directories.
cf399de12b54 One of the joys of having test_multiprocessing occasionally execute after
Brett Cannon <bcannon@gmail.com>
parents: 62761
diff changeset
1071 for part in reversed(path_parts):
cf399de12b54 One of the joys of having test_multiprocessing occasionally execute after
Brett Cannon <bcannon@gmail.com>
parents: 62761
diff changeset
1072 parent = _path_join(parent, part)
cf399de12b54 One of the joys of having test_multiprocessing occasionally execute after
Brett Cannon <bcannon@gmail.com>
parents: 62761
diff changeset
1073 try:
cf399de12b54 One of the joys of having test_multiprocessing occasionally execute after
Brett Cannon <bcannon@gmail.com>
parents: 62761
diff changeset
1074 _os.mkdir(parent)
73169
e4d44c2e8e81 Simplify and remove few dependencies on 'errno', thanks to PEP 3151.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73016
diff changeset
1075 except FileExistsError:
64240
cf399de12b54 One of the joys of having test_multiprocessing occasionally execute after
Brett Cannon <bcannon@gmail.com>
parents: 62761
diff changeset
1076 # Probably another Python process already created the dir.
73169
e4d44c2e8e81 Simplify and remove few dependencies on 'errno', thanks to PEP 3151.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73016
diff changeset
1077 continue
79741
53ce30b62de2 Issue #15833: don't raise an exception if importlib can't write byte-compiled
Trent Nelson <trent@trent.me>
parents: 79652
diff changeset
1078 except OSError as exc:
53ce30b62de2 Issue #15833: don't raise an exception if importlib can't write byte-compiled
Trent Nelson <trent@trent.me>
parents: 79652
diff changeset
1079 # Could be a permission error, read-only filesystem: just forget
53ce30b62de2 Issue #15833: don't raise an exception if importlib can't write byte-compiled
Trent Nelson <trent@trent.me>
parents: 79652
diff changeset
1080 # about writing the data.
53ce30b62de2 Issue #15833: don't raise an exception if importlib can't write byte-compiled
Trent Nelson <trent@trent.me>
parents: 79652
diff changeset
1081 _verbose_message('could not create {!r}: {!r}', parent, exc)
73169
e4d44c2e8e81 Simplify and remove few dependencies on 'errno', thanks to PEP 3151.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 73016
diff changeset
1082 return
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1083 try:
78738
0c661c5632e0 Issue #2051: Tweak last commit for this issue to pass in mode instead
Brett Cannon <brett@python.org>
parents: 78722
diff changeset
1084 _write_atomic(path, data, _mode)
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
1085 _verbose_message('created {!r}', path)
79741
53ce30b62de2 Issue #15833: don't raise an exception if importlib can't write byte-compiled
Trent Nelson <trent@trent.me>
parents: 79652
diff changeset
1086 except OSError as exc:
53ce30b62de2 Issue #15833: don't raise an exception if importlib can't write byte-compiled
Trent Nelson <trent@trent.me>
parents: 79652
diff changeset
1087 # Same as above: just don't write the bytecode.
53ce30b62de2 Issue #15833: don't raise an exception if importlib can't write byte-compiled
Trent Nelson <trent@trent.me>
parents: 79652
diff changeset
1088 _verbose_message('could not create {!r}: {!r}', path, exc)
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1089
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1090
76548
a2cf07135e4f Issue #14605: Rename _SourcelessFileLoader to SourcelessFileLoader.
Marc-Andre Lemburg <mal@egenix.com>
parents: 76547
diff changeset
1091 class SourcelessFileLoader(FileLoader, _LoaderBasics):
55355
dfaf6de2c8ce Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents: 52633
diff changeset
1092
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1093 """Loader which handles sourceless file imports."""
52112
03080fa08dfe Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents: 52083
diff changeset
1094
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1095 def load_module(self, fullname):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1096 return self._load_module(fullname, sourceless=True)
51771
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
1097
ee7029710c05 Refactor source and bytecode file loaders in importlib so that there
Brett Cannon <bcannon@gmail.com>
parents: 51770
diff changeset
1098 def get_code(self, fullname):
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1099 path = self.get_filename(fullname)
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1100 data = self.get_data(path)
76280
c071af95772a Have importlib take advantage of ImportError's new 'name' and 'path'
Brett Cannon <brett@python.org>
parents: 76081
diff changeset
1101 bytes_data = self._bytes_from_bytecode(fullname, data, path, None)
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1102 found = marshal.loads(bytes_data)
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
1103 if isinstance(found, _code_type):
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
1104 _verbose_message('code object from {!r}', path)
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1105 return found
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1106 else:
76280
c071af95772a Have importlib take advantage of ImportError's new 'name' and 'path'
Brett Cannon <brett@python.org>
parents: 76081
diff changeset
1107 raise ImportError("Non-code object in {}".format(path),
c071af95772a Have importlib take advantage of ImportError's new 'name' and 'path'
Brett Cannon <brett@python.org>
parents: 76081
diff changeset
1108 name=fullname, path=path)
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1109
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1110 def get_source(self, fullname):
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1111 """Return None as there is no source code."""
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1112 return None
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1113
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1114
78486
1db6553f3f8c Issue #15576: Allow extension modules to be a package's __init__
Brett Cannon <brett@python.org>
parents: 78485
diff changeset
1115 # Filled in by _setup().
1db6553f3f8c Issue #15576: Allow extension modules to be a package's __init__
Brett Cannon <brett@python.org>
parents: 78485
diff changeset
1116 EXTENSION_SUFFIXES = []
1db6553f3f8c Issue #15576: Allow extension modules to be a package's __init__
Brett Cannon <brett@python.org>
parents: 78485
diff changeset
1117
1db6553f3f8c Issue #15576: Allow extension modules to be a package's __init__
Brett Cannon <brett@python.org>
parents: 78485
diff changeset
1118
76478
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1119 class ExtensionFileLoader:
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1120
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1121 """Loader for extension modules.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1122
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1123 The constructor is designed to work with FileFinder.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1124
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1125 """
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1126
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1127 def __init__(self, name, path):
76478
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1128 self.name = name
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1129 self.path = path
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1130
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1131 @_check_name
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1132 @set_package
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1133 @set_loader
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1134 def load_module(self, fullname):
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1135 """Load an extension module."""
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1136 is_reload = fullname in sys.modules
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1137 try:
78344
62033490ca0f Close #15486: Simplify the mechanism used to remove importlib frames from tracebacks when they just introduce irrelevant noise
Nick Coghlan <ncoghlan@gmail.com>
parents: 78314
diff changeset
1138 module = _call_with_frames_removed(_imp.load_dynamic,
62033490ca0f Close #15486: Simplify the mechanism used to remove importlib frames from tracebacks when they just introduce irrelevant noise
Nick Coghlan <ncoghlan@gmail.com>
parents: 78314
diff changeset
1139 fullname, self.path)
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
1140 _verbose_message('extension module loaded from {!r}', self.path)
78511
34d5ec8a1019 Don't overwrite a __path__ value from extension modules if already
Brett Cannon <brett@python.org>
parents: 78491
diff changeset
1141 if self.is_package(fullname) and not hasattr(module, '__path__'):
78486
1db6553f3f8c Issue #15576: Allow extension modules to be a package's __init__
Brett Cannon <brett@python.org>
parents: 78485
diff changeset
1142 module.__path__ = [_path_split(self.path)[0]]
76303
2dd046be2c88 Issue #2377: Make importlib the implementation of __import__().
Brett Cannon <brett@python.org>
parents: 76280
diff changeset
1143 return module
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1144 except:
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1145 if not is_reload and fullname in sys.modules:
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1146 del sys.modules[fullname]
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1147 raise
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1148
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1149 def is_package(self, fullname):
78491
f7b59e890e30 fix docstring wording
Philip Jenvey <pjenvey@underboss.org>
parents: 78489
diff changeset
1150 """Return True if the extension module is a package."""
78486
1db6553f3f8c Issue #15576: Allow extension modules to be a package's __init__
Brett Cannon <brett@python.org>
parents: 78485
diff changeset
1151 file_name = _path_split(self.path)[1]
78487
e024f6ba5ed8 update docstring per the extension package fix, refactor
Philip Jenvey <pjenvey@underboss.org>
parents: 78486
diff changeset
1152 return any(file_name == '__init__' + suffix
e024f6ba5ed8 update docstring per the extension package fix, refactor
Philip Jenvey <pjenvey@underboss.org>
parents: 78486
diff changeset
1153 for suffix in EXTENSION_SUFFIXES)
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1154
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1155 def get_code(self, fullname):
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1156 """Return None as an extension module cannot create a code object."""
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1157 return None
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1158
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1159 def get_source(self, fullname):
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1160 """Return None as extension modules have no source code."""
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1161 return None
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1162
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1163
77126
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1164 class _NamespacePath:
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1165 """Represents a namespace package's path. It uses the module name
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1166 to find its parent module, and from there it looks up the parent's
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1167 __path__. When this changes, the module's own path is recomputed,
80409
58d6fea0c0c7 missing letter
Benjamin Peterson <benjamin@python.org>
parents: 79832
diff changeset
1168 using path_finder. For top-level modules, the parent module's path
77126
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1169 is sys.path."""
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1170
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1171 def __init__(self, name, path, path_finder):
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1172 self._name = name
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1173 self._path = path
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1174 self._last_parent_path = tuple(self._get_parent_path())
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1175 self._path_finder = path_finder
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1176
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1177 def _find_parent_path_names(self):
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1178 """Returns a tuple of (parent-module-name, parent-path-attr-name)"""
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1179 parent, dot, me = self._name.rpartition('.')
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1180 if dot == '':
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1181 # This is a top-level module. sys.path contains the parent path.
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1182 return 'sys', 'path'
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1183 # Not a top-level module. parent-module.__path__ contains the
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1184 # parent path.
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1185 return parent, '__path__'
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1186
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1187 def _get_parent_path(self):
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1188 parent_module_name, path_attr_name = self._find_parent_path_names()
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1189 return getattr(sys.modules[parent_module_name], path_attr_name)
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1190
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1191 def _recalculate(self):
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1192 # If the parent's path has changed, recalculate _path
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1193 parent_path = tuple(self._get_parent_path()) # Make a copy
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1194 if parent_path != self._last_parent_path:
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1195 loader, new_path = self._path_finder(self._name, parent_path)
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1196 # Note that no changes are made if a loader is returned, but we
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1197 # do remember the new parent path
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1198 if loader is None:
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1199 self._path = new_path
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1200 self._last_parent_path = parent_path # Save the copy
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1201 return self._path
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1202
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1203 def __iter__(self):
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1204 return iter(self._recalculate())
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1205
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1206 def __len__(self):
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1207 return len(self._recalculate())
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1208
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1209 def __repr__(self):
77964
37850f083af5 Minor refactoring in importlib._bootstrap, and fix the '_wrap' docstring.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 77917
diff changeset
1210 return "_NamespacePath({!r})".format(self._path)
77126
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1211
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1212 def __contains__(self, item):
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1213 return item in self._recalculate()
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1214
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1215 def append(self, item):
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1216 self._path.append(item)
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1217
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1218
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1219 class NamespaceLoader:
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1220 def __init__(self, name, path, path_finder):
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1221 self._path = _NamespacePath(name, path, path_finder)
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1222
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1223 @classmethod
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1224 def module_repr(cls, module):
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1225 return "<module '{}' (namespace)>".format(module.__name__)
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1226
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1227 @module_for_loader
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1228 def load_module(self, module):
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1229 """Load a namespace module."""
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1230 _verbose_message('namespace module loaded with path {!r}', self._path)
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1231 module.__path__ = self._path
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1232 return module
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1233
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1234
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1235 # Finders #####################################################################
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1236
51322
53d6445c6c5a Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents: 51266
diff changeset
1237 class PathFinder:
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1238
78382
1f8351cf00f3 Issue #15502: Bring the importlib.PathFinder docs and docstring more in line with the new import system documentation, and fix various parts of the new docs that weren't quite right given PEP 420 or were otherwise a bit misleading. Also note the key terminology problem still being discussed in the issue
Nick Coghlan <ncoghlan@gmail.com>
parents: 78380
diff changeset
1239 """Meta path finder for sys.path and package __path__ attributes."""
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1240
51639
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
1241 @classmethod
78485
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78456
diff changeset
1242 def invalidate_caches(cls):
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78456
diff changeset
1243 """Call the invalidate_caches() method on all path entry finders
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78456
diff changeset
1244 stored in sys.path_importer_caches (where implemented)."""
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78456
diff changeset
1245 for finder in sys.path_importer_cache.values():
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78456
diff changeset
1246 if hasattr(finder, 'invalidate_caches'):
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78456
diff changeset
1247 finder.invalidate_caches()
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78456
diff changeset
1248
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78456
diff changeset
1249 @classmethod
76566
57d558f1904d Issue #14605: Make explicit the entries on sys.path_hooks that used to
Brett Cannon <brett@python.org>
parents: 76549
diff changeset
1250 def _path_hooks(cls, path):
51639
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
1251 """Search sequence of hooks for a finder for 'path'.
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1252
51639
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
1253 If 'hooks' is false then use sys.path_hooks.
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1254
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1255 """
76566
57d558f1904d Issue #14605: Make explicit the entries on sys.path_hooks that used to
Brett Cannon <brett@python.org>
parents: 76549
diff changeset
1256 if not sys.path_hooks:
57d558f1904d Issue #14605: Make explicit the entries on sys.path_hooks that used to
Brett Cannon <brett@python.org>
parents: 76549
diff changeset
1257 _warnings.warn('sys.path_hooks is empty', ImportWarning)
57d558f1904d Issue #14605: Make explicit the entries on sys.path_hooks that used to
Brett Cannon <brett@python.org>
parents: 76549
diff changeset
1258 for hook in sys.path_hooks:
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1259 try:
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1260 return hook(path)
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1261 except ImportError:
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1262 continue
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1263 else:
76582
7025ee00dbf6 Issue #14605: Use None in sys.path_importer_cache to represent no
Brett Cannon <brett@python.org>
parents: 76579
diff changeset
1264 return None
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1265
51322
53d6445c6c5a Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents: 51266
diff changeset
1266 @classmethod
76566
57d558f1904d Issue #14605: Make explicit the entries on sys.path_hooks that used to
Brett Cannon <brett@python.org>
parents: 76549
diff changeset
1267 def _path_importer_cache(cls, path):
78382
1f8351cf00f3 Issue #15502: Bring the importlib.PathFinder docs and docstring more in line with the new import system documentation, and fix various parts of the new docs that weren't quite right given PEP 420 or were otherwise a bit misleading. Also note the key terminology problem still being discussed in the issue
Nick Coghlan <ncoghlan@gmail.com>
parents: 78380
diff changeset
1268 """Get the finder for the path entry from sys.path_importer_cache.
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1269
78382
1f8351cf00f3 Issue #15502: Bring the importlib.PathFinder docs and docstring more in line with the new import system documentation, and fix various parts of the new docs that weren't quite right given PEP 420 or were otherwise a bit misleading. Also note the key terminology problem still being discussed in the issue
Nick Coghlan <ncoghlan@gmail.com>
parents: 78380
diff changeset
1270 If the path entry is not in the cache, find the appropriate finder
1f8351cf00f3 Issue #15502: Bring the importlib.PathFinder docs and docstring more in line with the new import system documentation, and fix various parts of the new docs that weren't quite right given PEP 420 or were otherwise a bit misleading. Also note the key terminology problem still being discussed in the issue
Nick Coghlan <ncoghlan@gmail.com>
parents: 78380
diff changeset
1271 and cache it. If no finder is available, store None.
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1272
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1273 """
74994
b8593ec7e8c5 Tweak the handling of the empty string in sys.path for importlib.
Brett Cannon <brett@python.org>
parents: 74992
diff changeset
1274 if path == '':
75048
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1275 path = '.'
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1276 try:
51322
53d6445c6c5a Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents: 51266
diff changeset
1277 finder = sys.path_importer_cache[path]
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1278 except KeyError:
51322
53d6445c6c5a Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents: 51266
diff changeset
1279 finder = cls._path_hooks(path)
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1280 sys.path_importer_cache[path] = finder
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1281 return finder
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1282
51322
53d6445c6c5a Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents: 51266
diff changeset
1283 @classmethod
77126
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1284 def _get_loader(cls, fullname, path):
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1285 """Find the loader or namespace_path for this module/package name."""
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1286 # If this ends up being a namespace package, namespace_path is
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1287 # the list of paths that will become its __path__
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1288 namespace_path = []
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1289 for entry in path:
80529
291406748217 - Issue #16514: Fix regression causing a traceback when sys.path[0] is None
Barry Warsaw <barry@python.org>
parents: 80409
diff changeset
1290 if not isinstance(entry, (str, bytes)):
291406748217 - Issue #16514: Fix regression causing a traceback when sys.path[0] is None
Barry Warsaw <barry@python.org>
parents: 80409
diff changeset
1291 continue
77126
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1292 finder = cls._path_importer_cache(entry)
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1293 if finder is not None:
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1294 if hasattr(finder, 'find_loader'):
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1295 loader, portions = finder.find_loader(fullname)
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1296 else:
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1297 loader = finder.find_module(fullname)
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1298 portions = []
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1299 if loader is not None:
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1300 # We found a loader: return it immediately.
78485
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78456
diff changeset
1301 return loader, namespace_path
77126
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1302 # This is possibly part of a namespace package.
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1303 # Remember these path entries (if any) for when we
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1304 # create a namespace package, and continue iterating
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1305 # on path.
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1306 namespace_path.extend(portions)
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1307 else:
78485
0a75ce232f56 Issue #15502: Finish bringing importlib.abc in line with the current
Brett Cannon <brett@python.org>
parents: 78456
diff changeset
1308 return None, namespace_path
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1309
51322
53d6445c6c5a Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents: 51266
diff changeset
1310 @classmethod
53d6445c6c5a Rename importlib._bootstrap.SysPathFinder to PathFinder and expose off of
Brett Cannon <bcannon@gmail.com>
parents: 51266
diff changeset
1311 def find_module(cls, fullname, path=None):
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52164
diff changeset
1312 """Find the module on sys.path or 'path' based on sys.path_hooks and
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52164
diff changeset
1313 sys.path_importer_cache."""
76384
c1399cf7bd6a Issue #12599: Be more strict in accepting None vs. a false-like object
Brett Cannon <brett@python.org>
parents: 76331
diff changeset
1314 if path is None:
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1315 path = sys.path
77126
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1316 loader, namespace_path = cls._get_loader(fullname, path)
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1317 if loader is not None:
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1318 return loader
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1319 else:
77126
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1320 if namespace_path:
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1321 # We found at least one namespace path. Return a
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1322 # loader which can create the namespace package.
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1323 return NamespaceLoader(fullname, namespace_path, cls._get_loader)
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1324 else:
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1325 return None
51266
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1326
63386ae77f79 To prevent another screw-up on my part where my prototype gets lost thanks to
Brett Cannon <bcannon@gmail.com>
parents: 51242
diff changeset
1327
76478
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1328 class FileFinder:
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1329
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1330 """File-based finder.
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1331
76478
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1332 Interactions with the file system are cached for performance, being
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1333 refreshed when the directory the finder is handling has been modified.
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1334
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1335 """
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1336
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1337 def __init__(self, path, *details):
76478
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1338 """Initialize with the path to search on and a variable number of
77126
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1339 3-tuples containing the loader, file suffixes the loader recognizes,
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1340 and a boolean of whether the loader handles packages."""
78486
1db6553f3f8c Issue #15576: Allow extension modules to be a package's __init__
Brett Cannon <brett@python.org>
parents: 78485
diff changeset
1341 loaders = []
1db6553f3f8c Issue #15576: Allow extension modules to be a package's __init__
Brett Cannon <brett@python.org>
parents: 78485
diff changeset
1342 for loader, suffixes in details:
1db6553f3f8c Issue #15576: Allow extension modules to be a package's __init__
Brett Cannon <brett@python.org>
parents: 78485
diff changeset
1343 loaders.extend((suffix, loader) for suffix in suffixes)
1db6553f3f8c Issue #15576: Allow extension modules to be a package's __init__
Brett Cannon <brett@python.org>
parents: 78485
diff changeset
1344 self._loaders = loaders
75048
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1345 # Base (directory) path
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1346 self.path = path or '.'
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1347 self._path_mtime = -1
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1348 self._path_cache = set()
75082
3297dcdad196 Issue #14063: fix test_importlib failure under OS X case-insensitive filesystems
Antoine Pitrou <solipsis@pitrou.net>
parents: 75070
diff changeset
1349 self._relaxed_path_cache = set()
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1350
75324
de125ee32897 Update importlib.invalidate_caches() to be more general.
Brett Cannon <brett@python.org>
parents: 75250
diff changeset
1351 def invalidate_caches(self):
de125ee32897 Update importlib.invalidate_caches() to be more general.
Brett Cannon <brett@python.org>
parents: 75250
diff changeset
1352 """Invalidate the directory mtime."""
de125ee32897 Update importlib.invalidate_caches() to be more general.
Brett Cannon <brett@python.org>
parents: 75250
diff changeset
1353 self._path_mtime = -1
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1354
78489
e7a67f1bf604 Issue #15502: Refactor some code.
Brett Cannon <brett@python.org>
parents: 78487
diff changeset
1355 find_module = _find_module_shim
77126
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1356
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1357 def find_loader(self, fullname):
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1358 """Try to find a loader for the specified module, or the namespace
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1359 package portions. Returns (loader, list-of-portions)."""
77757
2051fead1933 Fixes issue 15039: namespace packages are no longer imported in preference to modules of the same name.
Eric V. Smith <eric@trueblade.com>
parents: 77600
diff changeset
1360 is_namespace = False
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1361 tail_module = fullname.rpartition('.')[2]
75048
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1362 try:
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1363 mtime = _os.stat(self.path).st_mtime
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1364 except OSError:
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1365 mtime = -1
75324
de125ee32897 Update importlib.invalidate_caches() to be more general.
Brett Cannon <brett@python.org>
parents: 75250
diff changeset
1366 if mtime != self._path_mtime:
75048
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1367 self._fill_cache()
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1368 self._path_mtime = mtime
75082
3297dcdad196 Issue #14063: fix test_importlib failure under OS X case-insensitive filesystems
Antoine Pitrou <solipsis@pitrou.net>
parents: 75070
diff changeset
1369 # tail_module keeps the original casing, for __file__ and friends
3297dcdad196 Issue #14063: fix test_importlib failure under OS X case-insensitive filesystems
Antoine Pitrou <solipsis@pitrou.net>
parents: 75070
diff changeset
1370 if _relax_case():
3297dcdad196 Issue #14063: fix test_importlib failure under OS X case-insensitive filesystems
Antoine Pitrou <solipsis@pitrou.net>
parents: 75070
diff changeset
1371 cache = self._relaxed_path_cache
3297dcdad196 Issue #14063: fix test_importlib failure under OS X case-insensitive filesystems
Antoine Pitrou <solipsis@pitrou.net>
parents: 75070
diff changeset
1372 cache_module = tail_module.lower()
3297dcdad196 Issue #14063: fix test_importlib failure under OS X case-insensitive filesystems
Antoine Pitrou <solipsis@pitrou.net>
parents: 75070
diff changeset
1373 else:
3297dcdad196 Issue #14063: fix test_importlib failure under OS X case-insensitive filesystems
Antoine Pitrou <solipsis@pitrou.net>
parents: 75070
diff changeset
1374 cache = self._path_cache
3297dcdad196 Issue #14063: fix test_importlib failure under OS X case-insensitive filesystems
Antoine Pitrou <solipsis@pitrou.net>
parents: 75070
diff changeset
1375 cache_module = tail_module
76309
e2f48b076012 Add some comments.
Brett Cannon <brett@python.org>
parents: 76303
diff changeset
1376 # Check if the module is the name of a directory (and thus a package).
75082
3297dcdad196 Issue #14063: fix test_importlib failure under OS X case-insensitive filesystems
Antoine Pitrou <solipsis@pitrou.net>
parents: 75070
diff changeset
1377 if cache_module in cache:
75048
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1378 base_path = _path_join(self.path, tail_module)
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1379 if _path_isdir(base_path):
78486
1db6553f3f8c Issue #15576: Allow extension modules to be a package's __init__
Brett Cannon <brett@python.org>
parents: 78485
diff changeset
1380 for suffix, loader in self._loaders:
75048
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1381 init_filename = '__init__' + suffix
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1382 full_path = _path_join(base_path, init_filename)
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1383 if _path_isfile(full_path):
77126
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1384 return (loader(fullname, full_path), [base_path])
75048
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1385 else:
77757
2051fead1933 Fixes issue 15039: namespace packages are no longer imported in preference to modules of the same name.
Eric V. Smith <eric@trueblade.com>
parents: 77600
diff changeset
1386 # A namespace package, return the path if we don't also
2051fead1933 Fixes issue 15039: namespace packages are no longer imported in preference to modules of the same name.
Eric V. Smith <eric@trueblade.com>
parents: 77600
diff changeset
1387 # find a module in the next section.
2051fead1933 Fixes issue 15039: namespace packages are no longer imported in preference to modules of the same name.
Eric V. Smith <eric@trueblade.com>
parents: 77600
diff changeset
1388 is_namespace = True
76309
e2f48b076012 Add some comments.
Brett Cannon <brett@python.org>
parents: 76303
diff changeset
1389 # Check for a file w/ a proper suffix exists.
78486
1db6553f3f8c Issue #15576: Allow extension modules to be a package's __init__
Brett Cannon <brett@python.org>
parents: 78485
diff changeset
1390 for suffix, loader in self._loaders:
75082
3297dcdad196 Issue #14063: fix test_importlib failure under OS X case-insensitive filesystems
Antoine Pitrou <solipsis@pitrou.net>
parents: 75070
diff changeset
1391 if cache_module + suffix in cache:
3297dcdad196 Issue #14063: fix test_importlib failure under OS X case-insensitive filesystems
Antoine Pitrou <solipsis@pitrou.net>
parents: 75070
diff changeset
1392 full_path = _path_join(self.path, tail_module + suffix)
75048
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1393 if _path_isfile(full_path):
77126
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1394 return (loader(fullname, full_path), [])
77757
2051fead1933 Fixes issue 15039: namespace packages are no longer imported in preference to modules of the same name.
Eric V. Smith <eric@trueblade.com>
parents: 77600
diff changeset
1395 if is_namespace:
2051fead1933 Fixes issue 15039: namespace packages are no longer imported in preference to modules of the same name.
Eric V. Smith <eric@trueblade.com>
parents: 77600
diff changeset
1396 return (None, [base_path])
77126
702009f3c0b1 issue 14660: Implement PEP 420, namespace packages.
Eric V. Smith <eric@trueblade.com>
parents: 77013
diff changeset
1397 return (None, [])
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1398
75048
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1399 def _fill_cache(self):
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1400 """Fill the cache of potential modules and packages for this directory."""
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1401 path = self.path
78663
bfd04bfb55c5 Close #14846: Handle a sys.path entry going away
Nick Coghlan <ncoghlan@gmail.com>
parents: 78619
diff changeset
1402 try:
bfd04bfb55c5 Close #14846: Handle a sys.path entry going away
Nick Coghlan <ncoghlan@gmail.com>
parents: 78619
diff changeset
1403 contents = _os.listdir(path)
bfd04bfb55c5 Close #14846: Handle a sys.path entry going away
Nick Coghlan <ncoghlan@gmail.com>
parents: 78619
diff changeset
1404 except FileNotFoundError:
bfd04bfb55c5 Close #14846: Handle a sys.path entry going away
Nick Coghlan <ncoghlan@gmail.com>
parents: 78619
diff changeset
1405 # Directory has been removed since last import
bfd04bfb55c5 Close #14846: Handle a sys.path entry going away
Nick Coghlan <ncoghlan@gmail.com>
parents: 78619
diff changeset
1406 contents = []
75082
3297dcdad196 Issue #14063: fix test_importlib failure under OS X case-insensitive filesystems
Antoine Pitrou <solipsis@pitrou.net>
parents: 75070
diff changeset
1407 # We store two cached versions, to handle runtime changes of the
3297dcdad196 Issue #14063: fix test_importlib failure under OS X case-insensitive filesystems
Antoine Pitrou <solipsis@pitrou.net>
parents: 75070
diff changeset
1408 # PYTHONCASEOK environment variable.
76428
a32be109bd86 Issue #14581: Windows users are allowed to import modules w/o taking
Brett Cannon <brett@python.org>
parents: 76406
diff changeset
1409 if not sys.platform.startswith('win'):
a32be109bd86 Issue #14581: Windows users are allowed to import modules w/o taking
Brett Cannon <brett@python.org>
parents: 76406
diff changeset
1410 self._path_cache = set(contents)
a32be109bd86 Issue #14581: Windows users are allowed to import modules w/o taking
Brett Cannon <brett@python.org>
parents: 76406
diff changeset
1411 else:
a32be109bd86 Issue #14581: Windows users are allowed to import modules w/o taking
Brett Cannon <brett@python.org>
parents: 76406
diff changeset
1412 # Windows users can import modules with case-insensitive file
a32be109bd86 Issue #14581: Windows users are allowed to import modules w/o taking
Brett Cannon <brett@python.org>
parents: 76406
diff changeset
1413 # suffixes (for legacy reasons). Make the suffix lowercase here
a32be109bd86 Issue #14581: Windows users are allowed to import modules w/o taking
Brett Cannon <brett@python.org>
parents: 76406
diff changeset
1414 # so it's done once instead of for every import. This is safe as
a32be109bd86 Issue #14581: Windows users are allowed to import modules w/o taking
Brett Cannon <brett@python.org>
parents: 76406
diff changeset
1415 # the specified suffixes to check against are always specified in a
a32be109bd86 Issue #14581: Windows users are allowed to import modules w/o taking
Brett Cannon <brett@python.org>
parents: 76406
diff changeset
1416 # case-sensitive manner.
a32be109bd86 Issue #14581: Windows users are allowed to import modules w/o taking
Brett Cannon <brett@python.org>
parents: 76406
diff changeset
1417 lower_suffix_contents = set()
a32be109bd86 Issue #14581: Windows users are allowed to import modules w/o taking
Brett Cannon <brett@python.org>
parents: 76406
diff changeset
1418 for item in contents:
a32be109bd86 Issue #14581: Windows users are allowed to import modules w/o taking
Brett Cannon <brett@python.org>
parents: 76406
diff changeset
1419 name, dot, suffix = item.partition('.')
a32be109bd86 Issue #14581: Windows users are allowed to import modules w/o taking
Brett Cannon <brett@python.org>
parents: 76406
diff changeset
1420 if dot:
a32be109bd86 Issue #14581: Windows users are allowed to import modules w/o taking
Brett Cannon <brett@python.org>
parents: 76406
diff changeset
1421 new_name = '{}.{}'.format(name, suffix.lower())
a32be109bd86 Issue #14581: Windows users are allowed to import modules w/o taking
Brett Cannon <brett@python.org>
parents: 76406
diff changeset
1422 else:
a32be109bd86 Issue #14581: Windows users are allowed to import modules w/o taking
Brett Cannon <brett@python.org>
parents: 76406
diff changeset
1423 new_name = name
a32be109bd86 Issue #14581: Windows users are allowed to import modules w/o taking
Brett Cannon <brett@python.org>
parents: 76406
diff changeset
1424 lower_suffix_contents.add(new_name)
a32be109bd86 Issue #14581: Windows users are allowed to import modules w/o taking
Brett Cannon <brett@python.org>
parents: 76406
diff changeset
1425 self._path_cache = lower_suffix_contents
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
1426 if sys.platform.startswith(_CASE_INSENSITIVE_PLATFORMS):
76406
052732bd3c10 don't bother keeping a set we'll never use
Benjamin Peterson <benjamin@python.org>
parents: 76390
diff changeset
1427 self._relaxed_path_cache = set(fn.lower() for fn in contents)
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 62587
diff changeset
1428
76478
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1429 @classmethod
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1430 def path_hook(cls, *loader_details):
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1431 """A class method which returns a closure to use on sys.path_hook
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1432 which will return an instance using the specified loaders and the path
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1433 called on the closure.
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1434
76478
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1435 If the path called on the closure is not a directory, ImportError is
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1436 raised.
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1437
76478
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1438 """
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1439 def path_hook_for_FileFinder(path):
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1440 """Path hook for importlib.machinery.FileFinder."""
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1441 if not _path_isdir(path):
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1442 raise ImportError("only directories are supported", path=path)
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1443 return cls(path, *loader_details)
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1444
76478
1da623513b26 Issue #14605: Expose importlib.abc.FileLoader and
Brett Cannon <brett@python.org>
parents: 76472
diff changeset
1445 return path_hook_for_FileFinder
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1446
77600
df856963fc6c A better repr() for FileFinder
Antoine Pitrou <solipsis@pitrou.net>
parents: 77457
diff changeset
1447 def __repr__(self):
79692
987a8724b220 Remove uses of % with str.format.
Brett Cannon <brett@python.org>
parents: 79653
diff changeset
1448 return "FileFinder({!r})".format(self.path)
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1449
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1450
52140
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
1451 # Import itself ###############################################################
1a8bbc5fb3ef Do a little bit of reorganization on importlib._bootstrap.
Brett Cannon <bcannon@gmail.com>
parents: 52139
diff changeset
1452
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1453 class _ImportLockContext:
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1454
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1455 """Context manager for the import lock."""
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1456
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1457 def __enter__(self):
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1458 """Acquire the import lock."""
76331
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
1459 _imp.acquire_lock()
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1460
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1461 def __exit__(self, exc_type, exc_value, exc_traceback):
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1462 """Release the import lock regardless of any raised exceptions."""
76331
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
1463 _imp.release_lock()
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1464
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1465
74983
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1466 def _resolve_name(name, package, level):
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1467 """Resolve a relative module name to an absolute one."""
75250
8840037a9c53 unused imports, pep8
Philip Jenvey <pjenvey@underboss.org>
parents: 75239
diff changeset
1468 bits = package.rsplit('.', level - 1)
75239
5cfc9c97af23 Simplify importib._resolve_name().
Brett Cannon <brett@python.org>
parents: 75235
diff changeset
1469 if len(bits) < level:
5cfc9c97af23 Simplify importib._resolve_name().
Brett Cannon <brett@python.org>
parents: 75235
diff changeset
1470 raise ValueError('attempted relative import beyond top-level package')
5cfc9c97af23 Simplify importib._resolve_name().
Brett Cannon <brett@python.org>
parents: 75235
diff changeset
1471 base = bits[0]
77964
37850f083af5 Minor refactoring in importlib._bootstrap, and fix the '_wrap' docstring.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 77917
diff changeset
1472 return '{}.{}'.format(base, name) if name else base
74983
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1473
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1474
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1475 def _find_module(name, path):
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1476 """Find a module's loader."""
76579
3bd60cc27664 Issue #14605: Stop having implicit entries for sys.meta_path.
Brett Cannon <brett@python.org>
parents: 76566
diff changeset
1477 if not sys.meta_path:
3bd60cc27664 Issue #14605: Stop having implicit entries for sys.meta_path.
Brett Cannon <brett@python.org>
parents: 76566
diff changeset
1478 _warnings.warn('sys.meta_path is empty', ImportWarning)
3bd60cc27664 Issue #14605: Stop having implicit entries for sys.meta_path.
Brett Cannon <brett@python.org>
parents: 76566
diff changeset
1479 for finder in sys.meta_path:
77013
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1480 with _ImportLockContext():
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1481 loader = finder.find_module(name, path)
74983
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1482 if loader is not None:
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1483 # The parent import may have already imported this module.
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1484 if name not in sys.modules:
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1485 return loader
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1486 else:
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1487 return sys.modules[name].__loader__
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1488 else:
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1489 return None
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1490
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1491
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1492 def _sanity_check(name, package, level):
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1493 """Verify arguments are "sane"."""
75228
909935a236e3 Do a type check instead of an interface check.
Brett Cannon <brett@python.org>
parents: 75193
diff changeset
1494 if not isinstance(name, str):
75193
f95faebf5dea Refactor importlib to make it easier to re-implement in C.
Brett Cannon <brett@python.org>
parents: 75185
diff changeset
1495 raise TypeError("module name must be str, not {}".format(type(name)))
f95faebf5dea Refactor importlib to make it easier to re-implement in C.
Brett Cannon <brett@python.org>
parents: 75185
diff changeset
1496 if level < 0:
f95faebf5dea Refactor importlib to make it easier to re-implement in C.
Brett Cannon <brett@python.org>
parents: 75185
diff changeset
1497 raise ValueError('level must be >= 0')
74983
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1498 if package:
75228
909935a236e3 Do a type check instead of an interface check.
Brett Cannon <brett@python.org>
parents: 75193
diff changeset
1499 if not isinstance(package, str):
75229
8d1040fdac60 Improper type for __package__ should raise TypeError, not ValueError.
Brett Cannon <brett@python.org>
parents: 75228
diff changeset
1500 raise TypeError("__package__ not set to a string")
74983
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1501 elif package not in sys.modules:
77964
37850f083af5 Minor refactoring in importlib._bootstrap, and fix the '_wrap' docstring.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 77917
diff changeset
1502 msg = ("Parent module {!r} not loaded, cannot perform relative "
74983
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1503 "import")
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1504 raise SystemError(msg.format(package))
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1505 if not name and level == 0:
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1506 raise ValueError("Empty module name")
52141
f188dd68620e Last big re-organization of importlib._bootstrap. Should actually be able to find something in the file now.
Brett Cannon <bcannon@gmail.com>
parents: 52140
diff changeset
1507
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1508
75193
f95faebf5dea Refactor importlib to make it easier to re-implement in C.
Brett Cannon <brett@python.org>
parents: 75185
diff changeset
1509 _ERR_MSG = 'No module named {!r}'
51639
ef2cba80c975 Change importlib.machinery.PathFinder to not have implicit semantics (that's
Brett Cannon <bcannon@gmail.com>
parents: 51433
diff changeset
1510
77013
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1511 def _find_and_load_unlocked(name, import_):
74983
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1512 path = None
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1513 parent = name.rpartition('.')[0]
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1514 if parent:
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1515 if parent not in sys.modules:
78344
62033490ca0f Close #15486: Simplify the mechanism used to remove importlib frames from tracebacks when they just introduce irrelevant noise
Nick Coghlan <ncoghlan@gmail.com>
parents: 78314
diff changeset
1516 _call_with_frames_removed(import_, parent)
76081
a40cd5976215 If a module injects something into sys.modules as a side-effect of
Brett Cannon <brett@python.org>
parents: 75463
diff changeset
1517 # Crazy side-effects!
a40cd5976215 If a module injects something into sys.modules as a side-effect of
Brett Cannon <brett@python.org>
parents: 75463
diff changeset
1518 if name in sys.modules:
a40cd5976215 If a module injects something into sys.modules as a side-effect of
Brett Cannon <brett@python.org>
parents: 75463
diff changeset
1519 return sys.modules[name]
74983
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1520 # Backwards-compatibility; be nicer to skip the dict lookup.
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1521 parent_module = sys.modules[parent]
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1522 try:
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1523 path = parent_module.__path__
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1524 except AttributeError:
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1525 msg = (_ERR_MSG + '; {} is not a package').format(name, parent)
76280
c071af95772a Have importlib take advantage of ImportError's new 'name' and 'path'
Brett Cannon <brett@python.org>
parents: 76081
diff changeset
1526 raise ImportError(msg, name=name)
75193
f95faebf5dea Refactor importlib to make it easier to re-implement in C.
Brett Cannon <brett@python.org>
parents: 75185
diff changeset
1527 loader = _find_module(name, path)
f95faebf5dea Refactor importlib to make it easier to re-implement in C.
Brett Cannon <brett@python.org>
parents: 75185
diff changeset
1528 if loader is None:
78758
ca4bf8e10bc0 Issue #15316: Let exceptions raised during imports triggered by the
Brett Cannon <brett@python.org>
parents: 78738
diff changeset
1529 exc = ImportError(_ERR_MSG.format(name), name=name)
ca4bf8e10bc0 Issue #15316: Let exceptions raised during imports triggered by the
Brett Cannon <brett@python.org>
parents: 78738
diff changeset
1530 # TODO(brett): switch to a proper ModuleNotFound exception in Python
ca4bf8e10bc0 Issue #15316: Let exceptions raised during imports triggered by the
Brett Cannon <brett@python.org>
parents: 78738
diff changeset
1531 # 3.4.
ca4bf8e10bc0 Issue #15316: Let exceptions raised during imports triggered by the
Brett Cannon <brett@python.org>
parents: 78738
diff changeset
1532 exc._not_found = True
ca4bf8e10bc0 Issue #15316: Let exceptions raised during imports triggered by the
Brett Cannon <brett@python.org>
parents: 78738
diff changeset
1533 raise exc
76390
db5e3431ee4c rollback 005fd1fe31ab (see #14609 and #14582)
Benjamin Peterson <benjamin@python.org>
parents: 76384
diff changeset
1534 elif name not in sys.modules:
db5e3431ee4c rollback 005fd1fe31ab (see #14609 and #14582)
Benjamin Peterson <benjamin@python.org>
parents: 76384
diff changeset
1535 # The parent import may have already imported this module.
db5e3431ee4c rollback 005fd1fe31ab (see #14609 and #14582)
Benjamin Peterson <benjamin@python.org>
parents: 76384
diff changeset
1536 loader.load_module(name)
76753
59b8a7fcb047 Properly mark names in importlib._bootstrap as private.
Brett Cannon <brett@python.org>
parents: 76586
diff changeset
1537 _verbose_message('import {!r} # {!r}', name, loader)
76390
db5e3431ee4c rollback 005fd1fe31ab (see #14609 and #14582)
Benjamin Peterson <benjamin@python.org>
parents: 76384
diff changeset
1538 # Backwards-compatibility; be nicer to skip the dict lookup.
db5e3431ee4c rollback 005fd1fe31ab (see #14609 and #14582)
Benjamin Peterson <benjamin@python.org>
parents: 76384
diff changeset
1539 module = sys.modules[name]
75193
f95faebf5dea Refactor importlib to make it easier to re-implement in C.
Brett Cannon <brett@python.org>
parents: 75185
diff changeset
1540 if parent:
f95faebf5dea Refactor importlib to make it easier to re-implement in C.
Brett Cannon <brett@python.org>
parents: 75185
diff changeset
1541 # Set the module as an attribute on its parent.
f95faebf5dea Refactor importlib to make it easier to re-implement in C.
Brett Cannon <brett@python.org>
parents: 75185
diff changeset
1542 parent_module = sys.modules[parent]
f95faebf5dea Refactor importlib to make it easier to re-implement in C.
Brett Cannon <brett@python.org>
parents: 75185
diff changeset
1543 setattr(parent_module, name.rpartition('.')[2], module)
f95faebf5dea Refactor importlib to make it easier to re-implement in C.
Brett Cannon <brett@python.org>
parents: 75185
diff changeset
1544 # Set __package__ if the loader did not.
77964
37850f083af5 Minor refactoring in importlib._bootstrap, and fix the '_wrap' docstring.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 77917
diff changeset
1545 if getattr(module, '__package__', None) is None:
75193
f95faebf5dea Refactor importlib to make it easier to re-implement in C.
Brett Cannon <brett@python.org>
parents: 75185
diff changeset
1546 try:
f95faebf5dea Refactor importlib to make it easier to re-implement in C.
Brett Cannon <brett@python.org>
parents: 75185
diff changeset
1547 module.__package__ = module.__name__
f95faebf5dea Refactor importlib to make it easier to re-implement in C.
Brett Cannon <brett@python.org>
parents: 75185
diff changeset
1548 if not hasattr(module, '__path__'):
f95faebf5dea Refactor importlib to make it easier to re-implement in C.
Brett Cannon <brett@python.org>
parents: 75185
diff changeset
1549 module.__package__ = module.__package__.rpartition('.')[0]
f95faebf5dea Refactor importlib to make it easier to re-implement in C.
Brett Cannon <brett@python.org>
parents: 75185
diff changeset
1550 except AttributeError:
f95faebf5dea Refactor importlib to make it easier to re-implement in C.
Brett Cannon <brett@python.org>
parents: 75185
diff changeset
1551 pass
76586
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
1552 # Set loader if need be.
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
1553 if not hasattr(module, '__loader__'):
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
1554 try:
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
1555 module.__loader__ = loader
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
1556 except AttributeError:
496c68f90a03 Issue #14646: __import__() now sets __loader__ if need be.
Brett Cannon <brett@python.org>
parents: 76582
diff changeset
1557 pass
75193
f95faebf5dea Refactor importlib to make it easier to re-implement in C.
Brett Cannon <brett@python.org>
parents: 75185
diff changeset
1558 return module
74983
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1559
66226
af13fb6fb3df Fix a minor inconsistency in capitalization for the 'No module named' exception
Brett Cannon <bcannon@gmail.com>
parents: 64407
diff changeset
1560
77013
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1561 def _find_and_load(name, import_):
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1562 """Find and load the module, and release the import lock."""
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1563 try:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1564 lock = _get_module_lock(name)
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1565 finally:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1566 _imp.release_lock()
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1567 lock.acquire()
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1568 try:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1569 return _find_and_load_unlocked(name, import_)
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1570 finally:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1571 lock.release()
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1572
66226
af13fb6fb3df Fix a minor inconsistency in capitalization for the 'No module named' exception
Brett Cannon <bcannon@gmail.com>
parents: 64407
diff changeset
1573
51383
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
1574 def _gcd_import(name, package=None, level=0):
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
1575 """Import and return the module based on its name, the package the call is
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
1576 being made from, and the level adjustment.
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
1577
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
1578 This function represents the greatest common denominator of functionality
68447
Eric V. Smith <eric@trueblade.com>
parents: 68257
diff changeset
1579 between import_module and __import__. This includes setting __package__ if
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1580 the loader did not.
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1581
51383
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
1582 """
74983
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1583 _sanity_check(name, package, level)
51383
a00e5dcea16c Initial, untested stab at writing a common denominator function for __import__
Brett Cannon <bcannon@gmail.com>
parents: 51356
diff changeset
1584 if level > 0:
74983
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1585 name = _resolve_name(name, package, level)
77013
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1586 _imp.acquire_lock()
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1587 if name not in sys.modules:
75193
f95faebf5dea Refactor importlib to make it easier to re-implement in C.
Brett Cannon <brett@python.org>
parents: 75185
diff changeset
1588 return _find_and_load(name, _gcd_import)
77013
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1589 module = sys.modules[name]
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1590 if module is None:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1591 _imp.release_lock()
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1592 message = ("import of {} halted; "
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1593 "None in sys.modules".format(name))
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1594 raise ImportError(message, name=name)
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1595 _lock_unlock_module(name)
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1596 return module
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1597
75235
3670df23081d Turn _return_module() into _handle_fromlist().
Brett Cannon <brett@python.org>
parents: 75229
diff changeset
1598 def _handle_fromlist(module, fromlist, import_):
74983
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1599 """Figure out what __import__ should return.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1600
74983
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1601 The import_ parameter is a callable which takes the name of module to
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1602 import. It is required to decouple the function from assuming importlib's
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1603 import implementation is desired.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1604
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1605 """
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1606 # The hell that is fromlist ...
75235
3670df23081d Turn _return_module() into _handle_fromlist().
Brett Cannon <brett@python.org>
parents: 75229
diff changeset
1607 # If a package was imported, try to import stuff from fromlist.
3670df23081d Turn _return_module() into _handle_fromlist().
Brett Cannon <brett@python.org>
parents: 75229
diff changeset
1608 if hasattr(module, '__path__'):
78051
dc18a2a66d16 Issue #15111: When a module was imported using a 'from import'
Brett Cannon <brett@python.org>
parents: 78042
diff changeset
1609 if '*' in fromlist:
75235
3670df23081d Turn _return_module() into _handle_fromlist().
Brett Cannon <brett@python.org>
parents: 75229
diff changeset
1610 fromlist = list(fromlist)
3670df23081d Turn _return_module() into _handle_fromlist().
Brett Cannon <brett@python.org>
parents: 75229
diff changeset
1611 fromlist.remove('*')
78051
dc18a2a66d16 Issue #15111: When a module was imported using a 'from import'
Brett Cannon <brett@python.org>
parents: 78042
diff changeset
1612 if hasattr(module, '__all__'):
dc18a2a66d16 Issue #15111: When a module was imported using a 'from import'
Brett Cannon <brett@python.org>
parents: 78042
diff changeset
1613 fromlist.extend(module.__all__)
77964
37850f083af5 Minor refactoring in importlib._bootstrap, and fix the '_wrap' docstring.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 77917
diff changeset
1614 for x in fromlist:
37850f083af5 Minor refactoring in importlib._bootstrap, and fix the '_wrap' docstring.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 77917
diff changeset
1615 if not hasattr(module, x):
79652
09b5158d5284 Closes issue #15111: Calling __import__ with a module specified in
Brett Cannon <brett@python.org>
parents: 79228
diff changeset
1616 from_name = '{}.{}'.format(module.__name__, x)
78619
0d52f125dd32 Issue #15715: Ignore failed imports triggered by the use of fromlist.
Brett Cannon <brett@python.org>
parents: 78511
diff changeset
1617 try:
79652
09b5158d5284 Closes issue #15111: Calling __import__ with a module specified in
Brett Cannon <brett@python.org>
parents: 79228
diff changeset
1618 _call_with_frames_removed(import_, from_name)
78758
ca4bf8e10bc0 Issue #15316: Let exceptions raised during imports triggered by the
Brett Cannon <brett@python.org>
parents: 78738
diff changeset
1619 except ImportError as exc:
78619
0d52f125dd32 Issue #15715: Ignore failed imports triggered by the use of fromlist.
Brett Cannon <brett@python.org>
parents: 78511
diff changeset
1620 # Backwards-compatibility dictates we ignore failed
78758
ca4bf8e10bc0 Issue #15316: Let exceptions raised during imports triggered by the
Brett Cannon <brett@python.org>
parents: 78738
diff changeset
1621 # imports triggered by fromlist for modules that don't
ca4bf8e10bc0 Issue #15316: Let exceptions raised during imports triggered by the
Brett Cannon <brett@python.org>
parents: 78738
diff changeset
1622 # exist.
ca4bf8e10bc0 Issue #15316: Let exceptions raised during imports triggered by the
Brett Cannon <brett@python.org>
parents: 78738
diff changeset
1623 # TODO(brett): In Python 3.4, have import raise
ca4bf8e10bc0 Issue #15316: Let exceptions raised during imports triggered by the
Brett Cannon <brett@python.org>
parents: 78738
diff changeset
1624 # ModuleNotFound and catch that.
79652
09b5158d5284 Closes issue #15111: Calling __import__ with a module specified in
Brett Cannon <brett@python.org>
parents: 79228
diff changeset
1625 if getattr(exc, '_not_found', False):
09b5158d5284 Closes issue #15111: Calling __import__ with a module specified in
Brett Cannon <brett@python.org>
parents: 79228
diff changeset
1626 if exc.name == from_name:
09b5158d5284 Closes issue #15111: Calling __import__ with a module specified in
Brett Cannon <brett@python.org>
parents: 79228
diff changeset
1627 continue
09b5158d5284 Closes issue #15111: Calling __import__ with a module specified in
Brett Cannon <brett@python.org>
parents: 79228
diff changeset
1628 raise
75235
3670df23081d Turn _return_module() into _handle_fromlist().
Brett Cannon <brett@python.org>
parents: 75229
diff changeset
1629 return module
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1630
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1631
74983
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1632 def _calc___package__(globals):
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1633 """Calculate what __package__ should be.
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1634
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1635 __package__ is not guaranteed to be defined or could be set to None
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1636 to represent that its proper value is unknown.
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1637
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1638 """
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1639 package = globals.get('__package__')
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1640 if package is None:
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1641 package = globals['__name__']
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1642 if '__path__' not in globals:
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1643 package = package.rpartition('.')[0]
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1644 return package
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1645
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1646
78308
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
1647 def _get_supported_file_loaders():
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
1648 """Returns a list of file-based module loaders.
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
1649
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
1650 Each item is a tuple (loader, suffixes, allow_packages).
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
1651 """
78486
1db6553f3f8c Issue #15576: Allow extension modules to be a package's __init__
Brett Cannon <brett@python.org>
parents: 78485
diff changeset
1652 extensions = ExtensionFileLoader, _imp.extension_suffixes()
1db6553f3f8c Issue #15576: Allow extension modules to be a package's __init__
Brett Cannon <brett@python.org>
parents: 78485
diff changeset
1653 source = SourceFileLoader, SOURCE_SUFFIXES
1db6553f3f8c Issue #15576: Allow extension modules to be a package's __init__
Brett Cannon <brett@python.org>
parents: 78485
diff changeset
1654 bytecode = SourcelessFileLoader, BYTECODE_SUFFIXES
78308
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
1655 return [extensions, source, bytecode]
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
1656
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
1657
78456
4240282a9f4a Issue #15471: Don't use mutable object as default values for the
Brett Cannon <brett@python.org>
parents: 78382
diff changeset
1658 def __import__(name, globals=None, locals=None, fromlist=(), level=0):
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1659 """Import a module.
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1660
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1661 The 'globals' argument is used to infer where the import is occuring from
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1662 to handle relative imports. The 'locals' argument is ignored. The
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1663 'fromlist' argument specifies what should exist as attributes on the module
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1664 being imported (e.g. ``from module import <fromlist>``). The 'level'
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1665 argument represents the package location to import from in a relative
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1666 import (e.g. ``from ..pkg import mod`` would have a 'level' of 2).
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1667
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1668 """
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1669 if level == 0:
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1670 module = _gcd_import(name)
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1671 else:
78456
4240282a9f4a Issue #15471: Don't use mutable object as default values for the
Brett Cannon <brett@python.org>
parents: 78382
diff changeset
1672 globals_ = globals if globals is not None else {}
4240282a9f4a Issue #15471: Don't use mutable object as default values for the
Brett Cannon <brett@python.org>
parents: 78382
diff changeset
1673 package = _calc___package__(globals_)
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1674 module = _gcd_import(name, package, level)
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1675 if not fromlist:
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1676 # Return up to the first dot in 'name'. This is complicated by the fact
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1677 # that 'name' may be relative.
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1678 if level == 0:
76826
d6324941b739 Issue #14583: Fix importlib bug when a package's __init__.py would first import one of its modules then raise an error.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76765
diff changeset
1679 return _gcd_import(name.partition('.')[0])
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1680 elif not name:
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1681 return module
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1682 else:
79228
4a979326e6d6 Add some comments.
Brett Cannon <brett@python.org>
parents: 79191
diff changeset
1683 # Figure out where to slice the module's name up to the first dot
4a979326e6d6 Add some comments.
Brett Cannon <brett@python.org>
parents: 79191
diff changeset
1684 # in 'name'.
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1685 cut_off = len(name) - len(name.partition('.')[0])
79228
4a979326e6d6 Add some comments.
Brett Cannon <brett@python.org>
parents: 79191
diff changeset
1686 # Slice end needs to be positive to alleviate need to special-case
4a979326e6d6 Add some comments.
Brett Cannon <brett@python.org>
parents: 79191
diff changeset
1687 # when ``'.' not in name``.
76312
9e8cbf07068a Handle importing pkg.mod by executing
Brett Cannon <brett@python.org>
parents: 76309
diff changeset
1688 return sys.modules[module.__name__[:len(module.__name__)-cut_off]]
75235
3670df23081d Turn _return_module() into _handle_fromlist().
Brett Cannon <brett@python.org>
parents: 75229
diff changeset
1689 else:
3670df23081d Turn _return_module() into _handle_fromlist().
Brett Cannon <brett@python.org>
parents: 75229
diff changeset
1690 return _handle_fromlist(module, fromlist, _gcd_import)
74983
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1691
03140936913c Refactor importlib.__import__() and _gcd_import() to facilitate using
Brett Cannon <brett@python.org>
parents: 74935
diff changeset
1692
76445
c820aa9c0c00 Issue #13959: Keep imp.get_magic() in C code, but cache in importlib
Brett Cannon <brett@python.org>
parents: 76428
diff changeset
1693
76331
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
1694 def _setup(sys_module, _imp_module):
74839
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1695 """Setup importlib by importing needed built-in modules and injecting them
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1696 into the global namespace.
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1697
76331
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
1698 As sys is needed for sys.modules access and _imp is needed to load built-in
74935
81f3bb5efa40 Bring importlib in line w/ changes made in my personal bootstrap branch in the sandbox.
Brett Cannon <brett@python.org>
parents: 74867
diff changeset
1699 modules, those two modules must be explicitly passed in.
74839
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1700
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1701 """
79173
4de5e4ec3cff don't depend on __debug__ because it's baked in at freeze time (issue #16046)
Benjamin Peterson <benjamin@python.org>
parents: 78779
diff changeset
1702 global _imp, sys, BYTECODE_SUFFIXES
76331
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
1703 _imp = _imp_module
74839
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1704 sys = sys_module
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1705
79173
4de5e4ec3cff don't depend on __debug__ because it's baked in at freeze time (issue #16046)
Benjamin Peterson <benjamin@python.org>
parents: 78779
diff changeset
1706 if sys.flags.optimize:
4de5e4ec3cff don't depend on __debug__ because it's baked in at freeze time (issue #16046)
Benjamin Peterson <benjamin@python.org>
parents: 78779
diff changeset
1707 BYTECODE_SUFFIXES = OPTIMIZED_BYTECODE_SUFFIXES
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 51383
diff changeset
1708 else:
79173
4de5e4ec3cff don't depend on __debug__ because it's baked in at freeze time (issue #16046)
Benjamin Peterson <benjamin@python.org>
parents: 78779
diff changeset
1709 BYTECODE_SUFFIXES = DEBUG_BYTECODE_SUFFIXES
4de5e4ec3cff don't depend on __debug__ because it's baked in at freeze time (issue #16046)
Benjamin Peterson <benjamin@python.org>
parents: 78779
diff changeset
1710
76331
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
1711 for module in (_imp, sys):
74839
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1712 if not hasattr(module, '__loader__'):
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1713 module.__loader__ = BuiltinImporter
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1714
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1715 self_module = sys.modules[__name__]
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1716 for builtin_name in ('_io', '_warnings', 'builtins', 'marshal'):
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1717 if builtin_name not in sys.modules:
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1718 builtin_module = BuiltinImporter.load_module(builtin_name)
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1719 else:
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1720 builtin_module = sys.modules[builtin_name]
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1721 setattr(self_module, builtin_name, builtin_module)
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1722
79480
15949f34e08f #16135: Removal of OS/2 support (Python code partial cleanup)
Jesus Cea <jcea@jcea.es>
parents: 79228
diff changeset
1723 os_details = ('posix', ['/']), ('nt', ['\\', '/'])
76449
163360370ad2 Make path manipulation more robust for platforms with alternative path
Brett Cannon <brett@python.org>
parents: 76445
diff changeset
1724 for builtin_os, path_separators in os_details:
76472
4e853913054c Issue #13959: Continue to try to accomodate altsep in importlib by not
Brett Cannon <brett@python.org>
parents: 76466
diff changeset
1725 # Assumption made in _path_join()
4e853913054c Issue #13959: Continue to try to accomodate altsep in importlib by not
Brett Cannon <brett@python.org>
parents: 76466
diff changeset
1726 assert all(len(sep) == 1 for sep in path_separators)
76449
163360370ad2 Make path manipulation more robust for platforms with alternative path
Brett Cannon <brett@python.org>
parents: 76445
diff changeset
1727 path_sep = path_separators[0]
74839
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1728 if builtin_os in sys.modules:
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1729 os_module = sys.modules[builtin_os]
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1730 break
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1731 else:
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1732 try:
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1733 os_module = BuiltinImporter.load_module(builtin_os)
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1734 break
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1735 except ImportError:
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1736 continue
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1737 else:
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1738 raise ImportError('importlib requires posix or nt')
77013
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1739
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1740 try:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1741 thread_module = BuiltinImporter.load_module('_thread')
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1742 except ImportError:
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1743 # Python was built without threads
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1744 thread_module = None
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1745 weakref_module = BuiltinImporter.load_module('_weakref')
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1746
78308
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
1747 if builtin_os == 'nt':
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
1748 winreg_module = BuiltinImporter.load_module('winreg')
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
1749 setattr(self_module, '_winreg', winreg_module)
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
1750
74839
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1751 setattr(self_module, '_os', os_module)
77013
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1752 setattr(self_module, '_thread', thread_module)
edb9ce3a6c2e Issue #9260: A finer-grained import lock.
Antoine Pitrou <solipsis@pitrou.net>
parents: 76877
diff changeset
1753 setattr(self_module, '_weakref', weakref_module)
74839
322e6f818cd2 Move setup code from importlib.__init__ to
Brett Cannon <brett@python.org>
parents: 74838
diff changeset
1754 setattr(self_module, 'path_sep', path_sep)
76449
163360370ad2 Make path manipulation more robust for platforms with alternative path
Brett Cannon <brett@python.org>
parents: 76445
diff changeset
1755 setattr(self_module, 'path_separators', set(path_separators))
75048
bbaab666e6c7 Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75045
diff changeset
1756 # Constants
75055
b8576719fe71 _relax_case -> _make_relax_case
Antoine Pitrou <solipsis@pitrou.net>
parents: 75048
diff changeset
1757 setattr(self_module, '_relax_case', _make_relax_case())
78486
1db6553f3f8c Issue #15576: Allow extension modules to be a package's __init__
Brett Cannon <brett@python.org>
parents: 78485
diff changeset
1758 EXTENSION_SUFFIXES.extend(_imp.extension_suffixes())
76451
ea46ebba8a0f Issue #13959: Re-implement imp.source_from_cache() in Lib/imp.py.
Brett Cannon <brett@python.org>
parents: 76450
diff changeset
1759 if builtin_os == 'nt':
76874
b81ddaf0db47 Issue #13959: Deprecate imp.get_suffixes() for new attributes on
Brett Cannon <brett@python.org>
parents: 76826
diff changeset
1760 SOURCE_SUFFIXES.append('.pyw')
78486
1db6553f3f8c Issue #15576: Allow extension modules to be a package's __init__
Brett Cannon <brett@python.org>
parents: 78485
diff changeset
1761 if '_d.pyd' in EXTENSION_SUFFIXES:
78380
a1ac1e13c5a0 Close #15519: Properly expose WindowsRegistryFinder in importlib and bring the name into line with normal import terminology. Original patch by Eric Snow
Nick Coghlan <ncoghlan@gmail.com>
parents: 78344
diff changeset
1762 WindowsRegistryFinder.DEBUG_BUILD = True
75000
e477832212b9 Optimize importlib's case-sensitivity check by wasting as little time as possible under case-sensitive OSs.
Brett Cannon <brett@python.org>
parents: 74998
diff changeset
1763
74935
81f3bb5efa40 Bring importlib in line w/ changes made in my personal bootstrap branch in the sandbox.
Brett Cannon <brett@python.org>
parents: 74867
diff changeset
1764
76331
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
1765 def _install(sys_module, _imp_module):
76765
146001e3364d Clean up a docstring.
Brett Cannon <brett@python.org>
parents: 76755
diff changeset
1766 """Install importlib as the implementation of import."""
76331
d777f854a66e Issue #13959: Rename imp to _imp and add Lib/imp.py and begin
Brett Cannon <brett@python.org>
parents: 76330
diff changeset
1767 _setup(sys_module, _imp_module)
78308
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
1768 supported_loaders = _get_supported_file_loaders()
76582
7025ee00dbf6 Issue #14605: Use None in sys.path_importer_cache to represent no
Brett Cannon <brett@python.org>
parents: 76579
diff changeset
1769 sys.path_hooks.extend([FileFinder.path_hook(*supported_loaders)])
78308
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
1770 sys.meta_path.append(BuiltinImporter)
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
1771 sys.meta_path.append(FrozenImporter)
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
1772 if _os.__name__ == 'nt':
78380
a1ac1e13c5a0 Close #15519: Properly expose WindowsRegistryFinder in importlib and bring the name into line with normal import terminology. Original patch by Eric Snow
Nick Coghlan <ncoghlan@gmail.com>
parents: 78344
diff changeset
1773 sys.meta_path.append(WindowsRegistryFinder)
78308
bd58c421057c Issue #14578: Support modules registered in the Windows registry again.
Martin v. Löwis <martin@v.loewis.de>
parents: 78112
diff changeset
1774 sys.meta_path.append(PathFinder)