X Tutup
Skip to content

Commit 1500d49

Browse files
Issue 19713: Add PEP 451-related deprecations.
1 parent 02b9f9d commit 1500d49

File tree

15 files changed

+4340
-4039
lines changed

15 files changed

+4340
-4039
lines changed

Lib/importlib/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import _imp # Just the builtin component, NOT the full Python module
1313
import sys
1414
import types
15+
import warnings
1516

1617
try:
1718
import _frozen_importlib as _bootstrap
@@ -77,13 +78,16 @@ def find_spec(name, path=None):
7778
return spec
7879

7980

80-
# XXX Deprecate...
8181
def find_loader(name, path=None):
8282
"""Return the loader for the specified module.
8383
8484
This is a backward-compatible wrapper around find_spec().
8585
86+
This function is deprecated in favor of importlib.find_spec().
87+
8688
"""
89+
warnings.warn('Use importlib.find_spec() instead.', DeprecationWarning,
90+
stacklevel=2)
8791
try:
8892
loader = sys.modules[name].__loader__
8993
if loader is None:

Lib/importlib/_bootstrap.py

Lines changed: 99 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,11 @@ def _requires_frozen_wrapper(self, fullname):
564564

565565
def _find_module_shim(self, fullname):
566566
"""Try to find a loader for the specified module by delegating to
567-
self.find_loader()."""
567+
self.find_loader().
568+
569+
This method is deprecated in favor of finder.find_spec().
570+
571+
"""
568572
# Call find_loader(). If it returns a string (indicating this
569573
# is a namespace package portion), generate a warning and
570574
# return None.
@@ -576,8 +580,11 @@ def _find_module_shim(self, fullname):
576580

577581

578582
def _load_module_shim(self, fullname):
579-
"""Load the specified module into sys.modules and return it."""
580-
# XXX Deprecation Warning here...
583+
"""Load the specified module into sys.modules and return it.
584+
585+
This method is deprecated. Use loader.exec_module instead.
586+
587+
"""
581588
spec = spec_from_loader(fullname, self)
582589
methods = _SpecMethods(spec)
583590
if fullname in sys.modules:
@@ -683,7 +690,9 @@ def _module_repr(module):
683690
# The implementation of ModuleType__repr__().
684691
loader = getattr(module, '__loader__', None)
685692
if hasattr(loader, 'module_repr'):
686-
# XXX Deprecation Warning here...
693+
# As soon as BuiltinImporter, FrozenImporter, and NamespaceLoader
694+
# drop their implementations for module_repr. we can add a
695+
# deprecation warning here.
687696
try:
688697
return loader.module_repr(module)
689698
except Exception:
@@ -1149,17 +1158,27 @@ def exec(self, module):
11491158
return module
11501159
self.init_module_attrs(module, _override=True)
11511160
if not hasattr(self.spec.loader, 'exec_module'):
1152-
# XXX DeprecationWarning goes here...
1161+
# (issue19713) Once BuiltinImporter and ExtensionFileLoader
1162+
# have exec_module() implemented, we can add a deprecation
1163+
# warning here.
11531164
self.spec.loader.load_module(name)
11541165
else:
11551166
self._exec(module)
11561167
return sys.modules[name]
11571168

11581169
def _load_backward_compatible(self):
1159-
# XXX DeprecationWarning goes here...
1170+
# (issue19713) Once BuiltinImporter and ExtensionFileLoader
1171+
# have exec_module() implemented, we can add a deprecation
1172+
# warning here.
11601173
spec = self.spec
11611174
# The module must be in sys.modules!
1162-
spec.loader.load_module(spec.name)
1175+
try:
1176+
_warnings
1177+
except NameError:
1178+
# We must be importing builtins in setup().
1179+
spec.loader.load_module(spec.name)
1180+
else:
1181+
spec.loader.load_module(spec.name)
11631182
module = sys.modules[spec.name]
11641183
if getattr(module, '__loader__', None) is None:
11651184
try:
@@ -1233,7 +1252,11 @@ class BuiltinImporter:
12331252

12341253
@staticmethod
12351254
def module_repr(module):
1236-
# XXX deprecate
1255+
"""Return repr for the module.
1256+
1257+
The method is deprecated. The import machinery does the job itself.
1258+
1259+
"""
12371260
return '<module {!r} (built-in)>'.format(module.__name__)
12381261

12391262
@classmethod
@@ -1251,6 +1274,8 @@ def find_module(cls, fullname, path=None):
12511274
12521275
If 'path' is ever specified then the search is considered a failure.
12531276
1277+
This method is deprecated. Use find_spec() instead.
1278+
12541279
"""
12551280
spec = cls.find_spec(fullname, path)
12561281
return spec.loader if spec is not None else None
@@ -1259,6 +1284,8 @@ def find_module(cls, fullname, path=None):
12591284
@_requires_builtin
12601285
def load_module(cls, fullname):
12611286
"""Load a built-in module."""
1287+
# Once an exec_module() implementation is added we can also
1288+
# add a deprecation warning here.
12621289
with _ManageReload(fullname):
12631290
module = _call_with_frames_removed(_imp.init_builtin, fullname)
12641291
module.__loader__ = cls
@@ -1281,7 +1308,6 @@ def get_source(cls, fullname):
12811308
@_requires_builtin
12821309
def is_package(cls, fullname):
12831310
"""Return False as built-in modules are never packages."""
1284-
# XXX DeprecationWarning here...
12851311
return False
12861312

12871313

@@ -1296,7 +1322,11 @@ class FrozenImporter:
12961322

12971323
@staticmethod
12981324
def module_repr(m):
1299-
# XXX deprecate
1325+
"""Return repr for the module.
1326+
1327+
The method is deprecated. The import machinery does the job itself.
1328+
1329+
"""
13001330
return '<module {!r} (frozen)>'.format(m.__name__)
13011331

13021332
@classmethod
@@ -1308,7 +1338,11 @@ def find_spec(cls, fullname, path=None, target=None):
13081338

13091339
@classmethod
13101340
def find_module(cls, fullname, path=None):
1311-
"""Find a frozen module."""
1341+
"""Find a frozen module.
1342+
1343+
This method is deprecated. Use find_spec() instead.
1344+
1345+
"""
13121346
return cls if _imp.is_frozen(fullname) else None
13131347

13141348
@staticmethod
@@ -1322,7 +1356,11 @@ def exec_module(module):
13221356

13231357
@classmethod
13241358
def load_module(cls, fullname):
1325-
"""Load a frozen module."""
1359+
"""Load a frozen module.
1360+
1361+
This method is deprecated. Use exec_module() instead.
1362+
1363+
"""
13261364
return _load_module_shim(cls, fullname)
13271365

13281366
@classmethod
@@ -1395,7 +1433,11 @@ def find_spec(cls, fullname, path=None, target=None):
13951433

13961434
@classmethod
13971435
def find_module(cls, fullname, path=None):
1398-
"""Find module named in the registry."""
1436+
"""Find module named in the registry.
1437+
1438+
This method is deprecated. Use exec_module() instead.
1439+
1440+
"""
13991441
spec = cls.find_spec(fullname, path)
14001442
if spec is not None:
14011443
return spec.loader
@@ -1408,7 +1450,6 @@ class _LoaderBasics:
14081450
"""Base class of common code needed by both SourceLoader and
14091451
SourcelessFileLoader."""
14101452

1411-
# XXX deprecate?
14121453
def is_package(self, fullname):
14131454
"""Concrete implementation of InspectLoader.is_package by checking if
14141455
the path returned by get_filename has a filename of '__init__.py'."""
@@ -1558,9 +1599,12 @@ def __hash__(self):
15581599

15591600
@_check_name
15601601
def load_module(self, fullname):
1561-
"""Load a module from a file."""
1562-
# The only reason for this method is for the name check.
1602+
"""Load a module from a file.
15631603
1604+
This method is deprecated. Use exec_module() instead.
1605+
1606+
"""
1607+
# The only reason for this method is for the name check.
15641608
# Issue #14857: Avoid the zero-argument form of super so the implementation
15651609
# of that form can be updated without breaking the frozen module
15661610
return super(FileLoader, self).load_module(fullname)
@@ -1660,6 +1704,8 @@ def __hash__(self):
16601704
@_check_name
16611705
def load_module(self, fullname):
16621706
"""Load an extension module."""
1707+
# Once an exec_module() implementation is added we can also
1708+
# add a deprecation warning here.
16631709
with _ManageReload(fullname):
16641710
module = _call_with_frames_removed(_imp.load_dynamic,
16651711
fullname, self.path)
@@ -1754,9 +1800,13 @@ class _NamespaceLoader:
17541800
def __init__(self, name, path, path_finder):
17551801
self._path = _NamespacePath(name, path, path_finder)
17561802

1757-
# XXX Deprecate
17581803
@classmethod
17591804
def module_repr(cls, module):
1805+
"""Return repr for the module.
1806+
1807+
The method is deprecated. The import machinery does the job itself.
1808+
1809+
"""
17601810
return '<module {!r} (namespace)>'.format(module.__name__)
17611811

17621812
def is_package(self, fullname):
@@ -1768,9 +1818,16 @@ def get_source(self, fullname):
17681818
def get_code(self, fullname):
17691819
return compile('', '<string>', 'exec', dont_inherit=True)
17701820

1771-
# XXX Deprecate
1821+
def exec_module(self, module):
1822+
pass
1823+
17721824
def load_module(self, fullname):
1773-
"""Load a namespace module."""
1825+
"""Load a namespace module.
1826+
1827+
This method is deprecated. Use exec_module() instead.
1828+
1829+
"""
1830+
# The import system never calls this method.
17741831
_verbose_message('namespace module loaded with path {!r}', self._path)
17751832
return _load_module_shim(self, fullname)
17761833

@@ -1825,6 +1882,8 @@ def _path_importer_cache(cls, path):
18251882

18261883
@classmethod
18271884
def _legacy_get_spec(cls, fullname, finder):
1885+
# This would be a good place for a DeprecationWarning if
1886+
# we ended up going that route.
18281887
if hasattr(finder, 'find_loader'):
18291888
loader, portions = finder.find_loader(fullname)
18301889
else:
@@ -1893,8 +1952,11 @@ def find_spec(cls, fullname, path=None, target=None):
18931952
@classmethod
18941953
def find_module(cls, fullname, path=None):
18951954
"""find the module on sys.path or 'path' based on sys.path_hooks and
1896-
sys.path_importer_cache."""
1897-
# XXX Deprecation warning here.
1955+
sys.path_importer_cache.
1956+
1957+
This method is deprecated. Use find_spec() instead.
1958+
1959+
"""
18981960
spec = cls.find_spec(fullname, path)
18991961
if spec is None:
19001962
return None
@@ -1932,7 +1994,11 @@ def invalidate_caches(self):
19321994

19331995
def find_loader(self, fullname):
19341996
"""Try to find a loader for the specified module, or the namespace
1935-
package portions. Returns (loader, list-of-portions)."""
1997+
package portions. Returns (loader, list-of-portions).
1998+
1999+
This method is deprecated. Use find_spec() instead.
2000+
2001+
"""
19362002
spec = self.find_spec(fullname)
19372003
if spec is None:
19382004
return None, []
@@ -2065,6 +2131,15 @@ def _resolve_name(name, package, level):
20652131
return '{}.{}'.format(base, name) if name else base
20662132

20672133

2134+
def _find_spec_legacy(finder, name, path):
2135+
# This would be a good place for a DeprecationWarning if
2136+
# we ended up going that route.
2137+
loader = finder.find_module(name, path)
2138+
if loader is None:
2139+
return None
2140+
return spec_from_loader(name, loader)
2141+
2142+
20682143
def _find_spec(name, path, target=None):
20692144
"""Find a module's loader."""
20702145
if not sys.meta_path:
@@ -2078,10 +2153,9 @@ def _find_spec(name, path, target=None):
20782153
try:
20792154
find_spec = finder.find_spec
20802155
except AttributeError:
2081-
loader = finder.find_module(name, path)
2082-
if loader is None:
2156+
spec = _find_spec_legacy(finder, name, path)
2157+
if spec is None:
20832158
continue
2084-
spec = spec_from_loader(name, loader)
20852159
else:
20862160
spec = find_spec(name, path, target)
20872161
if spec is not None:

Lib/importlib/abc.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ class MetaPathFinder(Finder):
4343
# We don't define find_spec() here since that would break
4444
# hasattr checks we do to support backward compatibility.
4545

46-
# XXX Deprecate
4746
def find_module(self, fullname, path):
4847
"""Return a loader for the module.
4948
5049
If no module is found, return None. The fullname is a str and
5150
the path is a list of strings or None.
5251
52+
This method is deprecated in favor of finder.find_spec().
53+
5354
"""
5455
return None
5556

@@ -69,7 +70,6 @@ class PathEntryFinder(Finder):
6970
# We don't define find_spec() here since that would break
7071
# hasattr checks we do to support backward compatibility.
7172

72-
# XXX Deprecate.
7373
def find_loader(self, fullname):
7474
"""Return (loader, namespace portion) for the path entry.
7575
@@ -81,10 +81,11 @@ def find_loader(self, fullname):
8181
The portion will be discarded if another path entry finder
8282
locates the module as a normal module or package.
8383
84+
This method is deprecated in favor of finder.find_spec().
85+
8486
"""
8587
return None, []
8688

87-
# XXX Deprecate.
8889
find_module = _bootstrap._find_module_shim
8990

9091
def invalidate_caches(self):
@@ -115,7 +116,6 @@ def create_module(self, spec):
115116
# We don't define exec_module() here since that would break
116117
# hasattr checks we do to support backward compatibility.
117118

118-
# XXX Deprecate.
119119
def load_module(self, fullname):
120120
"""Return the loaded module.
121121
@@ -124,16 +124,19 @@ def load_module(self, fullname):
124124
125125
ImportError is raised on failure.
126126
127+
This method is deprecated in favor of loader.exec_module().
128+
127129
"""
128130
raise ImportError
129131

130-
# XXX Deprecate.
131132
def module_repr(self, module):
132133
"""Return a module's repr.
133134
134135
Used by the module type when the method does not raise
135136
NotImplementedError.
136137
138+
This method is deprecated.
139+
137140
"""
138141
# The exception will cause ModuleType.__repr__ to ignore this method.
139142
raise NotImplementedError

0 commit comments

Comments
 (0)
X Tutup