Skip to content

Commit d6e83fb

Browse files
sobolevnCAM-Gerlachbrettcannonarhadthedev
authored
gh-97850: Deprecate find_loader and get_loader in pkgutil (GH-98520)
Co-authored-by: C.A.M. Gerlach <[email protected]> Co-authored-by: Brett Cannon <[email protected]> Co-authored-by: Oleg Iarygin <[email protected]>
1 parent 9f9e001 commit d6e83fb

File tree

5 files changed

+43
-18
lines changed

5 files changed

+43
-18
lines changed

Doc/library/pkgutil.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ support.
6464
.. versionchanged:: 3.4
6565
Updated to be based on :pep:`451`
6666

67+
.. deprecated-removed:: 3.12 3.14
68+
Use :func:`importlib.util.find_spec` instead.
69+
70+
6771
.. function:: get_importer(path_item)
6872

6973
Retrieve a :term:`finder` for the given *path_item*.
@@ -96,6 +100,9 @@ support.
96100
.. versionchanged:: 3.4
97101
Updated to be based on :pep:`451`
98102

103+
.. deprecated-removed:: 3.12 3.14
104+
Use :func:`importlib.util.find_spec` instead.
105+
99106

100107
.. function:: iter_importers(fullname='')
101108

Doc/whatsnew/3.12.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,11 @@ Pending Removal in Python 3.14
813813
* The *onerror* argument of :func:`shutil.rmtree` is deprecated in 3.12,
814814
and will be removed in 3.14.
815815

816+
* :func:`pkgutil.find_loader` and :func:`pkgutil.get_loader`
817+
now raise :exc:`DeprecationWarning`;
818+
use :func:`importlib.util.find_spec` instead.
819+
(Contributed by Nikita Sobolev in :gh:`97850`.)
820+
816821
Pending Removal in Future Versions
817822
----------------------------------
818823

Lib/pkgutil.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ def get_loader(module_or_name):
270270
If the named module is not already imported, its containing package
271271
(if any) is imported, in order to establish the package __path__.
272272
"""
273+
warnings._deprecated("pkgutil.get_loader",
274+
f"{warnings._DEPRECATED_MSG}; "
275+
"use importlib.util.find_spec() instead",
276+
remove=(3, 14))
273277
if module_or_name in sys.modules:
274278
module_or_name = sys.modules[module_or_name]
275279
if module_or_name is None:
@@ -294,6 +298,10 @@ def find_loader(fullname):
294298
importlib.util.find_spec that converts most failures to ImportError
295299
and only returns the loader rather than the full spec
296300
"""
301+
warnings._deprecated("pkgutil.find_loader",
302+
f"{warnings._DEPRECATED_MSG}; "
303+
"use importlib.util.find_spec() instead",
304+
remove=(3, 14))
297305
if fullname.startswith('.'):
298306
msg = "Relative module name {!r} not supported".format(fullname)
299307
raise ImportError(msg)

Lib/test/test_pkgutil.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from pathlib import Path
22
from test.support.import_helper import unload, CleanImport
3-
from test.support.warnings_helper import check_warnings
3+
from test.support.warnings_helper import check_warnings, ignore_warnings
44
import unittest
55
import sys
66
import importlib
@@ -535,25 +535,18 @@ class ImportlibMigrationTests(unittest.TestCase):
535535
# PEP 302 emulation in this module is in the process of being
536536
# deprecated in favour of importlib proper
537537

538-
def test_get_loader_avoids_emulation(self):
539-
with check_warnings() as w:
540-
self.assertIsNotNone(pkgutil.get_loader("sys"))
541-
self.assertIsNotNone(pkgutil.get_loader("os"))
542-
self.assertIsNotNone(pkgutil.get_loader("test.support"))
543-
self.assertEqual(len(w.warnings), 0)
544-
545538
@unittest.skipIf(__name__ == '__main__', 'not compatible with __main__')
539+
@ignore_warnings(category=DeprecationWarning)
546540
def test_get_loader_handles_missing_loader_attribute(self):
547541
global __loader__
548542
this_loader = __loader__
549543
del __loader__
550544
try:
551-
with check_warnings() as w:
552-
self.assertIsNotNone(pkgutil.get_loader(__name__))
553-
self.assertEqual(len(w.warnings), 0)
545+
self.assertIsNotNone(pkgutil.get_loader(__name__))
554546
finally:
555547
__loader__ = this_loader
556548

549+
@ignore_warnings(category=DeprecationWarning)
557550
def test_get_loader_handles_missing_spec_attribute(self):
558551
name = 'spam'
559552
mod = type(sys)(name)
@@ -563,6 +556,7 @@ def test_get_loader_handles_missing_spec_attribute(self):
563556
loader = pkgutil.get_loader(name)
564557
self.assertIsNone(loader)
565558

559+
@ignore_warnings(category=DeprecationWarning)
566560
def test_get_loader_handles_spec_attribute_none(self):
567561
name = 'spam'
568562
mod = type(sys)(name)
@@ -572,6 +566,7 @@ def test_get_loader_handles_spec_attribute_none(self):
572566
loader = pkgutil.get_loader(name)
573567
self.assertIsNone(loader)
574568

569+
@ignore_warnings(category=DeprecationWarning)
575570
def test_get_loader_None_in_sys_modules(self):
576571
name = 'totally bogus'
577572
sys.modules[name] = None
@@ -581,18 +576,26 @@ def test_get_loader_None_in_sys_modules(self):
581576
del sys.modules[name]
582577
self.assertIsNone(loader)
583578

579+
def test_get_loader_is_deprecated(self):
580+
with check_warnings(
581+
(r".*\bpkgutil.get_loader\b.*", DeprecationWarning),
582+
):
583+
res = pkgutil.get_loader("sys")
584+
self.assertIsNotNone(res)
585+
586+
def test_find_loader_is_deprecated(self):
587+
with check_warnings(
588+
(r".*\bpkgutil.find_loader\b.*", DeprecationWarning),
589+
):
590+
res = pkgutil.find_loader("sys")
591+
self.assertIsNotNone(res)
592+
593+
@ignore_warnings(category=DeprecationWarning)
584594
def test_find_loader_missing_module(self):
585595
name = 'totally bogus'
586596
loader = pkgutil.find_loader(name)
587597
self.assertIsNone(loader)
588598

589-
def test_find_loader_avoids_emulation(self):
590-
with check_warnings() as w:
591-
self.assertIsNotNone(pkgutil.find_loader("sys"))
592-
self.assertIsNotNone(pkgutil.find_loader("os"))
593-
self.assertIsNotNone(pkgutil.find_loader("test.support"))
594-
self.assertEqual(len(w.warnings), 0)
595-
596599
def test_get_importer_avoids_emulation(self):
597600
# We use an illegal path so *none* of the path hooks should fire
598601
with check_warnings() as w:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Deprecate :func:`pkgutil.find_loader` and :func:`pkgutil.get_loader`
2+
in favor of :func:`importlib.util.find_spec`.

0 commit comments

Comments
 (0)