Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9e09849

Browse files
authored
bpo-41006: importlib.util no longer imports typing (GH-20938)
Create importlib._abc submodule to avoid importing typing when importlib.util is imported. Move Loader ABC into importlib._abc.
1 parent 236a0f5 commit 9e09849

4 files changed

Lines changed: 55 additions & 51 deletions

File tree

Lib/importlib/_abc.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""Subset of importlib.abc used to reduce importlib.util imports."""
2+
from . import _bootstrap
3+
import abc
4+
5+
6+
class Loader(metaclass=abc.ABCMeta):
7+
8+
"""Abstract base class for import loaders."""
9+
10+
def create_module(self, spec):
11+
"""Return a module to initialize and into which to load.
12+
13+
This method should raise ImportError if anything prevents it
14+
from creating a new module. It may return None to indicate
15+
that the spec should create the new module.
16+
"""
17+
# By default, defer to default semantics for the new module.
18+
return None
19+
20+
# We don't define exec_module() here since that would break
21+
# hasattr checks we do to support backward compatibility.
22+
23+
def load_module(self, fullname):
24+
"""Return the loaded module.
25+
26+
The module must be added to sys.modules and have import-related
27+
attributes set properly. The fullname is a str.
28+
29+
ImportError is raised on failure.
30+
31+
This method is deprecated in favor of loader.exec_module(). If
32+
exec_module() exists then it is used to provide a backwards-compatible
33+
functionality for this method.
34+
35+
"""
36+
if not hasattr(self, 'exec_module'):
37+
raise ImportError
38+
return _bootstrap._load_module_shim(self, fullname)
39+
40+
def module_repr(self, module):
41+
"""Return a module's repr.
42+
43+
Used by the module type when the method does not raise
44+
NotImplementedError.
45+
46+
This method is deprecated.
47+
48+
"""
49+
# The exception will cause ModuleType.__repr__ to ignore this method.
50+
raise NotImplementedError

Lib/importlib/abc.py

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import _frozen_importlib_external
1313
except ImportError:
1414
_frozen_importlib_external = _bootstrap_external
15+
from ._abc import Loader
1516
import abc
1617
import warnings
1718
from typing import Protocol, runtime_checkable
@@ -134,53 +135,6 @@ def invalidate_caches(self):
134135
_register(PathEntryFinder, machinery.FileFinder)
135136

136137

137-
class Loader(metaclass=abc.ABCMeta):
138-
139-
"""Abstract base class for import loaders."""
140-
141-
def create_module(self, spec):
142-
"""Return a module to initialize and into which to load.
143-
144-
This method should raise ImportError if anything prevents it
145-
from creating a new module. It may return None to indicate
146-
that the spec should create the new module.
147-
"""
148-
# By default, defer to default semantics for the new module.
149-
return None
150-
151-
# We don't define exec_module() here since that would break
152-
# hasattr checks we do to support backward compatibility.
153-
154-
def load_module(self, fullname):
155-
"""Return the loaded module.
156-
157-
The module must be added to sys.modules and have import-related
158-
attributes set properly. The fullname is a str.
159-
160-
ImportError is raised on failure.
161-
162-
This method is deprecated in favor of loader.exec_module(). If
163-
exec_module() exists then it is used to provide a backwards-compatible
164-
functionality for this method.
165-
166-
"""
167-
if not hasattr(self, 'exec_module'):
168-
raise ImportError
169-
return _bootstrap._load_module_shim(self, fullname)
170-
171-
def module_repr(self, module):
172-
"""Return a module's repr.
173-
174-
Used by the module type when the method does not raise
175-
NotImplementedError.
176-
177-
This method is deprecated.
178-
179-
"""
180-
# The exception will cause ModuleType.__repr__ to ignore this method.
181-
raise NotImplementedError
182-
183-
184138
class ResourceLoader(Loader):
185139

186140
"""Abstract base class for loaders which can return data from their

Lib/importlib/util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Utility code for constructing importers, etc."""
2-
from . import abc
2+
from ._abc import Loader
33
from ._bootstrap import module_from_spec
44
from ._bootstrap import _resolve_name
55
from ._bootstrap import spec_from_loader
@@ -263,7 +263,7 @@ def __delattr__(self, attr):
263263
delattr(self, attr)
264264

265265

266-
class LazyLoader(abc.Loader):
266+
class LazyLoader(Loader):
267267

268268
"""A loader that creates a module which defers loading until attribute access."""
269269

Lib/test/test_importlib/test_spec.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,8 @@ def test_spec_from_file_location_default(self):
650650
# Need to use a circuitous route to get at importlib.machinery to make
651651
# sure the same class object is used in the isinstance() check as
652652
# would have been used to create the loader.
653-
self.assertIsInstance(spec.loader,
654-
self.util.abc.machinery.SourceFileLoader)
653+
SourceFileLoader = self.util.spec_from_file_location.__globals__['SourceFileLoader']
654+
self.assertIsInstance(spec.loader, SourceFileLoader)
655655
self.assertEqual(spec.loader.name, self.name)
656656
self.assertEqual(spec.loader.path, self.path)
657657
self.assertEqual(spec.origin, self.path)

0 commit comments

Comments
 (0)