]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-121604: Make sure all deprecated items in importlib raise DeprecationWarning ...
authorTomas R. <tomas.roun8@gmail.com>
Wed, 15 Jan 2025 00:48:46 +0000 (01:48 +0100)
committerGitHub <noreply@github.com>
Wed, 15 Jan 2025 00:48:46 +0000 (16:48 -0800)
Co-authored-by: rashansmith <smith.rashan@gmail.com>
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Co-authored-by: Brett Cannon <brett@python.org>
Lib/importlib/_bootstrap_external.py
Lib/importlib/abc.py
Lib/importlib/machinery.py
Lib/inspect.py
Lib/test/test_importlib/test_abc.py
Lib/test/test_importlib/test_api.py
Lib/test/test_importlib/test_windows.py
Misc/NEWS.d/next/Library/2024-12-16-22-20-38.gh-issue-121604.m3Xn4G.rst [new file with mode: 0644]

index fa36159711846fc5d0dc28709da6a31cddab7020..697f7c55218a8fcc71e1cbf22a6879a3a7adb150 100644 (file)
@@ -716,6 +716,12 @@ class WindowsRegistryFinder:
 
     @classmethod
     def find_spec(cls, fullname, path=None, target=None):
+        _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)
+
         filepath = cls._search_registry(fullname)
         if filepath is None:
             return None
index eea6b38af6fa1348278f6f6639bf272300c93068..bb2837d38d83f10758f3d432b3802d8cbbf06631 100644 (file)
@@ -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
@@ -199,6 +208,10 @@ 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'])
index 6e294d59bfdcb9681590857c217841132e846feb..63d726445c3d96d8dda46aed39897a10b8d05db1 100644 (file)
@@ -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
@@ -27,3 +29,22 @@ __all__ = ['AppleFrameworkLoader', 'BYTECODE_SUFFIXES', 'BuiltinImporter',
            '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}')
index 5b7c4df8927c878a8bb30ad50dd36d20f1576e30..facad478103668c4051065c620a968fb772d6e3f 100644 (file)
@@ -858,8 +858,7 @@ def getsourcefile(object):
     Return None if no way can be identified to get the source.
     """
     filename = getfile(object)
-    all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
-    all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]
+    all_bytecode_suffixes = importlib.machinery.BYTECODE_SUFFIXES[:]
     if any(filename.endswith(s) for s in all_bytecode_suffixes):
         filename = (os.path.splitext(filename)[0] +
                     importlib.machinery.SOURCE_SUFFIXES[0])
index 603125f6d926f672f0e0fa1c831bf99fc2d83d6e..00af2dd712425a5b378662d22cdfa3687900ca6d 100644 (file)
@@ -913,5 +913,37 @@ class SourceLoaderGetSourceTests:
                          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()
index 51ea5270b1a928f8222ece33a1f997a27fd08aac..6035b2ca72efb987e5976f50df29c3475cbfebb2 100644 (file)
@@ -492,5 +492,18 @@ class TestModuleAll(unittest.TestCase):
         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)
+
+
 if __name__ == '__main__':
     unittest.main()
index 8a9a8fffcd10d49b01f915933ff8d10daa3a0a8e..f32680bdbeb9e377cfdd1cc90d8b79d220e3c83b 100644 (file)
@@ -104,6 +104,12 @@ class WindowsRegistryFinderTests:
             spec = self.machinery.WindowsRegistryFinder.find_spec(self.test_module)
             self.assertIsNone(spec)
 
+    def test_raises_deprecation_warning(self):
+        # WindowsRegistryFinder is not meant to be instantiated, so the
+        # deprecation warning is raised in the 'find_spec' method instead.
+        with self.assertWarns(DeprecationWarning):
+            self.machinery.WindowsRegistryFinder.find_spec('spam')
+
 (Frozen_WindowsRegistryFinderTests,
  Source_WindowsRegistryFinderTests
  ) = test_util.test_both(WindowsRegistryFinderTests, machinery=machinery)
diff --git a/Misc/NEWS.d/next/Library/2024-12-16-22-20-38.gh-issue-121604.m3Xn4G.rst b/Misc/NEWS.d/next/Library/2024-12-16-22-20-38.gh-issue-121604.m3Xn4G.rst
new file mode 100644 (file)
index 0000000..9a6fce8
--- /dev/null
@@ -0,0 +1 @@
+Add missing Deprecation warnings for :attr:`importlib.machinery.DEBUG_BYTECODE_SUFFIXES`, :attr:`importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES`, :class:`importlib.machinery.WindowsRegistryFinder`, :class:`importlib.abc.ResourceLoader`, :meth:`importlib.abc.SourceLoader.path_mtime`.