Skip to content

Commit afdf8ce

Browse files
CPython Developersyouknowone
authored andcommitted
Update importlib from v3.14.2
1 parent 648223a commit afdf8ce

70 files changed

Lines changed: 2851 additions & 940 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Lib/importlib/_bootstrap.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,9 @@ def release(self):
382382
self.waiters.pop()
383383
self.wakeup.release()
384384

385+
def locked(self):
386+
return bool(self.count)
387+
385388
def __repr__(self):
386389
return f'_ModuleLock({self.name!r}) at {id(self)}'
387390

@@ -490,8 +493,7 @@ def _call_with_frames_removed(f, *args, **kwds):
490493

491494
def _verbose_message(message, *args, verbosity=1):
492495
"""Print the message to stderr if -v/PYTHONVERBOSE is turned on."""
493-
# XXX RUSTPYTHON: hasattr check because we might be bootstrapping and we wouldn't have stderr yet
494-
if sys.flags.verbose >= verbosity and hasattr(sys, "stderr"):
496+
if sys.flags.verbose >= verbosity:
495497
if not message.startswith(('#', 'import ')):
496498
message = '# ' + message
497499
print(message.format(*args), file=sys.stderr)
@@ -1242,10 +1244,12 @@ def _find_spec(name, path, target=None):
12421244
"""Find a module's spec."""
12431245
meta_path = sys.meta_path
12441246
if meta_path is None:
1245-
# PyImport_Cleanup() is running or has been called.
12461247
raise ImportError("sys.meta_path is None, Python is likely "
12471248
"shutting down")
12481249

1250+
# gh-130094: Copy sys.meta_path so that we have a consistent view of the
1251+
# list while iterating over it.
1252+
meta_path = list(meta_path)
12491253
if not meta_path:
12501254
_warnings.warn('sys.meta_path is empty', ImportWarning)
12511255

@@ -1300,7 +1304,6 @@ def _sanity_check(name, package, level):
13001304

13011305

13021306
_ERR_MSG_PREFIX = 'No module named '
1303-
_ERR_MSG = _ERR_MSG_PREFIX + '{!r}'
13041307

13051308
def _find_and_load_unlocked(name, import_):
13061309
path = None
@@ -1310,15 +1313,22 @@ def _find_and_load_unlocked(name, import_):
13101313
if parent not in sys.modules:
13111314
_call_with_frames_removed(import_, parent)
13121315
# Crazy side-effects!
1313-
if name in sys.modules:
1314-
return sys.modules[name]
1316+
module = sys.modules.get(name)
1317+
if module is not None:
1318+
return module
13151319
parent_module = sys.modules[parent]
13161320
try:
13171321
path = parent_module.__path__
13181322
except AttributeError:
13191323
msg = f'{_ERR_MSG_PREFIX}{name!r}; {parent!r} is not a package'
13201324
raise ModuleNotFoundError(msg, name=name) from None
13211325
parent_spec = parent_module.__spec__
1326+
if getattr(parent_spec, '_initializing', False):
1327+
_call_with_frames_removed(import_, parent)
1328+
# Crazy side-effects (again)!
1329+
module = sys.modules.get(name)
1330+
if module is not None:
1331+
return module
13221332
child = name.rpartition('.')[2]
13231333
spec = _find_spec(name, path)
13241334
if spec is None:

Lib/importlib/_bootstrap_external.py

Lines changed: 10 additions & 274 deletions
Large diffs are not rendered by default.

Lib/importlib/abc.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
_frozen_importlib_external = _bootstrap_external
1414
from ._abc import Loader
1515
import abc
16-
import warnings
17-
18-
from .resources import abc as _resources_abc
1916

2017

2118
__all__ = [
@@ -25,19 +22,6 @@
2522
]
2623

2724

28-
def __getattr__(name):
29-
"""
30-
For backwards compatibility, continue to make names
31-
from _resources_abc available through this module. #93963
32-
"""
33-
if name in _resources_abc.__all__:
34-
obj = getattr(_resources_abc, name)
35-
warnings._deprecated(f"{__name__}.{name}", remove=(3, 14))
36-
globals()[name] = obj
37-
return obj
38-
raise AttributeError(f'module {__name__!r} has no attribute {name!r}')
39-
40-
4125
def _register(abstract_cls, *classes):
4226
for cls in classes:
4327
abstract_cls.register(cls)
@@ -80,10 +64,13 @@ def invalidate_caches(self):
8064
class ResourceLoader(Loader):
8165

8266
"""Abstract base class for loaders which can return data from their
83-
back-end storage.
67+
back-end storage to facilitate reading data to perform an import.
8468
8569
This ABC represents one of the optional protocols specified by PEP 302.
8670
71+
For directly loading resources, use TraversableResources instead. This class
72+
primarily exists for backwards compatibility with other ABCs in this module.
73+
8774
"""
8875

8976
@abc.abstractmethod
@@ -215,6 +202,10 @@ class SourceLoader(_bootstrap_external.SourceLoader, ResourceLoader, ExecutionLo
215202

216203
def path_mtime(self, path):
217204
"""Return the (int) modification time for the path (str)."""
205+
import warnings
206+
warnings.warn('SourceLoader.path_mtime is deprecated in favour of '
207+
'SourceLoader.path_stats().',
208+
DeprecationWarning, stacklevel=2)
218209
if self.path_stats.__func__ is SourceLoader.path_stats:
219210
raise OSError
220211
return int(self.path_stats(path)['mtime'])

Lib/importlib/machinery.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
from ._bootstrap import ModuleSpec
44
from ._bootstrap import BuiltinImporter
55
from ._bootstrap import FrozenImporter
6-
from ._bootstrap_external import (SOURCE_SUFFIXES, DEBUG_BYTECODE_SUFFIXES,
7-
OPTIMIZED_BYTECODE_SUFFIXES, BYTECODE_SUFFIXES,
8-
EXTENSION_SUFFIXES)
6+
from ._bootstrap_external import (
7+
SOURCE_SUFFIXES, BYTECODE_SUFFIXES, EXTENSION_SUFFIXES,
8+
DEBUG_BYTECODE_SUFFIXES as _DEBUG_BYTECODE_SUFFIXES,
9+
OPTIMIZED_BYTECODE_SUFFIXES as _OPTIMIZED_BYTECODE_SUFFIXES
10+
)
911
from ._bootstrap_external import WindowsRegistryFinder
1012
from ._bootstrap_external import PathFinder
1113
from ._bootstrap_external import FileFinder
@@ -19,3 +21,30 @@
1921
def all_suffixes():
2022
"""Returns a list of all recognized module suffixes for this process"""
2123
return SOURCE_SUFFIXES + BYTECODE_SUFFIXES + EXTENSION_SUFFIXES
24+
25+
26+
__all__ = ['AppleFrameworkLoader', 'BYTECODE_SUFFIXES', 'BuiltinImporter',
27+
'DEBUG_BYTECODE_SUFFIXES', 'EXTENSION_SUFFIXES',
28+
'ExtensionFileLoader', 'FileFinder', 'FrozenImporter', 'ModuleSpec',
29+
'NamespaceLoader', 'OPTIMIZED_BYTECODE_SUFFIXES', 'PathFinder',
30+
'SOURCE_SUFFIXES', 'SourceFileLoader', 'SourcelessFileLoader',
31+
'WindowsRegistryFinder', 'all_suffixes']
32+
33+
34+
def __getattr__(name):
35+
import warnings
36+
37+
if name == 'DEBUG_BYTECODE_SUFFIXES':
38+
warnings.warn('importlib.machinery.DEBUG_BYTECODE_SUFFIXES is '
39+
'deprecated; use importlib.machinery.BYTECODE_SUFFIXES '
40+
'instead.',
41+
DeprecationWarning, stacklevel=2)
42+
return _DEBUG_BYTECODE_SUFFIXES
43+
elif name == 'OPTIMIZED_BYTECODE_SUFFIXES':
44+
warnings.warn('importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES is '
45+
'deprecated; use importlib.machinery.BYTECODE_SUFFIXES '
46+
'instead.',
47+
DeprecationWarning, stacklevel=2)
48+
return _OPTIMIZED_BYTECODE_SUFFIXES
49+
50+
raise AttributeError(f'module {__name__!r} has no attribute {name!r}')

Lib/importlib/resources/_common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ def resolve(cand: Optional[Anchor]) -> types.ModuleType:
7777
return cast(types.ModuleType, cand)
7878

7979

80-
@resolve.register(str) # TODO: RUSTPYTHON; manual type annotation
80+
@resolve.register
8181
def _(cand: str) -> types.ModuleType:
8282
return importlib.import_module(cand)
8383

8484

85-
@resolve.register(type(None)) # TODO: RUSTPYTHON; manual type annotation
85+
@resolve.register
8686
def _(cand: None) -> types.ModuleType:
8787
return resolve(_infer_caller().f_globals['__name__'])
8888

@@ -183,7 +183,7 @@ def _(path):
183183
@contextlib.contextmanager
184184
def _temp_path(dir: tempfile.TemporaryDirectory):
185185
"""
186-
Wrap tempfile.TemporyDirectory to return a pathlib object.
186+
Wrap tempfile.TemporaryDirectory to return a pathlib object.
187187
"""
188188
with dir as result:
189189
yield pathlib.Path(result)

Lib/importlib/resources/_legacy.py

Lines changed: 0 additions & 120 deletions
This file was deleted.

Lib/importlib/util.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from ._bootstrap import spec_from_loader
66
from ._bootstrap import _find_spec
77
from ._bootstrap_external import MAGIC_NUMBER
8-
from ._bootstrap_external import _RAW_MAGIC_NUMBER
98
from ._bootstrap_external import cache_from_source
109
from ._bootstrap_external import decode_source
1110
from ._bootstrap_external import source_from_cache
@@ -18,7 +17,7 @@
1817

1918
def source_hash(source_bytes):
2019
"Return the hash of *source_bytes* as used in hash-based pyc files."
21-
return _imp.source_hash(_RAW_MAGIC_NUMBER, source_bytes)
20+
return _imp.source_hash(_imp.pyc_magic_number_token, source_bytes)
2221

2322

2423
def resolve_name(name, package):
@@ -272,3 +271,9 @@ def exec_module(self, module):
272271
loader_state['is_loading'] = False
273272
module.__spec__.loader_state = loader_state
274273
module.__class__ = _LazyModule
274+
275+
276+
__all__ = ['LazyLoader', 'Loader', 'MAGIC_NUMBER',
277+
'cache_from_source', 'decode_source', 'find_spec',
278+
'module_from_spec', 'resolve_name', 'source_from_cache',
279+
'source_hash', 'spec_from_file_location', 'spec_from_loader']

0 commit comments

Comments
 (0)