Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,14 @@ class WindowsRegistryFinder:
'\\Modules\\{fullname}\\Debug')
DEBUG_BUILD = (_MS_WINDOWS and '_d.pyd' in EXTENSION_SUFFIXES)

def __init__(self):
import warnings
warnings.warn('importlib.machinery.WindowsRegistryFinder is '
'deprecated. Use site configuration instead. '
'Future versions of Python may not enable this '
'finder by default.',
DeprecationWarning, stacklevel=2)

@staticmethod
def _open_registry(key):
try:
Expand Down
14 changes: 14 additions & 0 deletions Lib/importlib/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ class ResourceLoader(Loader):

"""

def __init__(self):
import warnings
warnings.warn('importlib.abc.ResourceLoader is deprecated in '
'favour of supporting resource loading through '
'importlib.resources.abc.ResourceReader.',
DeprecationWarning, stacklevel=2)
super().__init__()


@abc.abstractmethod
def get_data(self, path):
"""Abstract method which when implemented should return the bytes for
Expand Down Expand Up @@ -199,6 +208,11 @@ class SourceLoader(_bootstrap_external.SourceLoader, ResourceLoader, ExecutionLo

def path_mtime(self, path):
"""Return the (int) modification time for the path (str)."""
import warnings
warnings.warn('SourceLoader.path_mtime is deprecated in favour of '
'SourceLoader.path_stats().',
DeprecationWarning,
stacklevel=2)
if self.path_stats.__func__ is SourceLoader.path_stats:
raise OSError
return int(self.path_stats(path)['mtime'])
Expand Down
27 changes: 24 additions & 3 deletions Lib/importlib/machinery.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from ._bootstrap import ModuleSpec
from ._bootstrap import BuiltinImporter
from ._bootstrap import FrozenImporter
from ._bootstrap_external import (SOURCE_SUFFIXES, DEBUG_BYTECODE_SUFFIXES,
OPTIMIZED_BYTECODE_SUFFIXES, BYTECODE_SUFFIXES,
EXTENSION_SUFFIXES)
from ._bootstrap_external import (
SOURCE_SUFFIXES, BYTECODE_SUFFIXES, EXTENSION_SUFFIXES,
DEBUG_BYTECODE_SUFFIXES as _DEBUG_BYTECODE_SUFFIXES,
OPTIMIZED_BYTECODE_SUFFIXES as _OPTIMIZED_BYTECODE_SUFFIXES
)
from ._bootstrap_external import WindowsRegistryFinder
from ._bootstrap_external import PathFinder
from ._bootstrap_external import FileFinder
Expand All @@ -27,3 +29,22 @@ def all_suffixes():
'NamespaceLoader', 'OPTIMIZED_BYTECODE_SUFFIXES', 'PathFinder',
'SOURCE_SUFFIXES', 'SourceFileLoader', 'SourcelessFileLoader',
'WindowsRegistryFinder', 'all_suffixes']


def __getattr__(name):
import warnings

if name == 'DEBUG_BYTECODE_SUFFIXES':
warnings.warn('importlib.machinery.DEBUG_BYTECODE_SUFFIXES is '
'deprecated. Use importlib.machinery.BYTECODE_SUFFIXES '
'instead.',
DeprecationWarning, stacklevel=2)
return _DEBUG_BYTECODE_SUFFIXES
elif name == 'OPTIMIZED_BYTECODE_SUFFIXES':
warnings.warn('importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES is '
'deprecated. Use importlib.machinery.BYTECODE_SUFFIXES '
'instead.',
DeprecationWarning, stacklevel=2)
return _OPTIMIZED_BYTECODE_SUFFIXES

raise AttributeError(f'module {__name__!r} has no attribute {name!r}')
32 changes: 32 additions & 0 deletions Lib/test/test_importlib/test_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,5 +913,37 @@ def test_universal_newlines(self):
SourceOnlyLoaderMock=SPLIT_SOL)


class SourceLoaderDeprecationWarningsTests(unittest.TestCase):
"""Tests SourceLoader deprecation warnings."""

def test_deprecated_path_mtime(self):
from importlib.abc import SourceLoader
class DummySourceLoader(SourceLoader):
def get_data(self, path):
return b''

def get_filename(self, fullname):
return 'foo.py'

def path_stats(self, path):
return {'mtime': 1}

loader = DummySourceLoader()
with self.assertWarns(DeprecationWarning):
loader.path_mtime('foo.py')


class ResourceLoaderDeprecationWarningsTests(unittest.TestCase):
"""Tests ResourceLoader deprecation warnings."""

def test_deprecated_resource_loader(self):
from importlib.abc import ResourceLoader
class DummyLoader(ResourceLoader):
def get_data(self, path):
return b''

with self.assertWarns(DeprecationWarning):
DummyLoader()

if __name__ == '__main__':
unittest.main()
18 changes: 18 additions & 0 deletions Lib/test/test_importlib/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,5 +492,23 @@ def test_util(self):
support.check__all__(self, util['Source'], extra=extra)


class TestDeprecations(unittest.TestCase):
def test_machinery_deprecated_attributes(self):
from importlib import machinery
attributes = (
'DEBUG_BYTECODE_SUFFIXES',
'OPTIMIZED_BYTECODE_SUFFIXES',
)
for attr in attributes:
with self.subTest(attr=attr):
with self.assertWarns(DeprecationWarning):
getattr(machinery, attr)

def test_deprecated_windows_registry_finder(self):
from importlib.machinery import WindowsRegistryFinder
with self.assertWarns(DeprecationWarning):
WindowsRegistryFinder()


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add missing Deprecation warnings for several :mod:`importlib` members.
Loading