Skip to content

Commit b7de6df

Browse files
committed
pythongh-97850: Deprecate find_module and get_module in pkgutil
1 parent 8367ca1 commit b7de6df

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

Doc/whatsnew/3.12.rst

+4
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ Deprecated
251251
:exc:`ImportWarning`).
252252
(Contributed by Brett Cannon in :gh:`65961`.)
253253

254+
* :func:`pkgutil.find_module` and :func:`pkgutil.get_module`
255+
now raise :exc:`DeprecationWarning`,
256+
use :func:`importlib.util.find_spec` instead.
257+
254258

255259
Pending Removal in Python 3.13
256260
------------------------------

Lib/pkgutil.py

+8
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,10 @@ def get_loader(module_or_name):
465465
If the named module is not already imported, its containing package
466466
(if any) is imported, in order to establish the package __path__.
467467
"""
468+
warnings.warn("`pkgutil.get_loader` is deprecated since Python 3.12; "
469+
"this function is slated for removal in Python 3.14, "
470+
"use `importlib.util.find_spec` instead",
471+
DeprecationWarning)
468472
if module_or_name in sys.modules:
469473
module_or_name = sys.modules[module_or_name]
470474
if module_or_name is None:
@@ -489,6 +493,10 @@ def find_loader(fullname):
489493
importlib.util.find_spec that converts most failures to ImportError
490494
and only returns the loader rather than the full spec
491495
"""
496+
warnings.warn("`pkgutil.find_loader` is deprecated since Python 3.12; "
497+
"this function is slated for removal in Python 3.14, "
498+
"use `importlib.util.find_spec` instead",
499+
DeprecationWarning)
492500
if fullname.startswith('.'):
493501
msg = "Relative module name {!r} not supported".format(fullname)
494502
raise ImportError(msg)

Lib/test/test_pkgutil.py

+23-14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os.path
1111
import tempfile
1212
import shutil
13+
import warnings
1314
import zipfile
1415

1516
# Note: pkgutil.walk_packages is currently tested in test_runpy. This is
@@ -549,22 +550,26 @@ def test_loader_deprecated(self):
549550
with self.check_deprecated():
550551
pkgutil.ImpLoader("", "", "", "")
551552

552-
def test_get_loader_avoids_emulation(self):
553-
with check_warnings() as w:
554-
self.assertIsNotNone(pkgutil.get_loader("sys"))
555-
self.assertIsNotNone(pkgutil.get_loader("os"))
556-
self.assertIsNotNone(pkgutil.get_loader("test.support"))
557-
self.assertEqual(len(w.warnings), 0)
553+
def test_get_loader_is_deprecated(self):
554+
for module in ["sys", "os", "test.support"]:
555+
with check_warnings((
556+
"`pkgutil.get_loader` is deprecated since Python 3.12; "
557+
"this function is slated for removal in Python 3.14, "
558+
"use `importlib.util.find_spec` instead",
559+
DeprecationWarning,
560+
)):
561+
res = pkgutil.get_loader(module)
562+
self.assertIsNotNone(res)
558563

559564
@unittest.skipIf(__name__ == '__main__', 'not compatible with __main__')
560565
def test_get_loader_handles_missing_loader_attribute(self):
561566
global __loader__
562567
this_loader = __loader__
563568
del __loader__
564569
try:
565-
with check_warnings() as w:
570+
with warnings.catch_warnings():
571+
warnings.simplefilter('ignore', DeprecationWarning)
566572
self.assertIsNotNone(pkgutil.get_loader(__name__))
567-
self.assertEqual(len(w.warnings), 0)
568573
finally:
569574
__loader__ = this_loader
570575

@@ -600,12 +605,16 @@ def test_find_loader_missing_module(self):
600605
loader = pkgutil.find_loader(name)
601606
self.assertIsNone(loader)
602607

603-
def test_find_loader_avoids_emulation(self):
604-
with check_warnings() as w:
605-
self.assertIsNotNone(pkgutil.find_loader("sys"))
606-
self.assertIsNotNone(pkgutil.find_loader("os"))
607-
self.assertIsNotNone(pkgutil.find_loader("test.support"))
608-
self.assertEqual(len(w.warnings), 0)
608+
def test_find_loader_is_deprecated(self):
609+
for module in ["sys", "os", "test.support"]:
610+
with check_warnings((
611+
"`pkgutil.find_loader` is deprecated since Python 3.12; "
612+
"this function is slated for removal in Python 3.14, "
613+
"use `importlib.util.find_spec` instead",
614+
DeprecationWarning,
615+
)):
616+
res = pkgutil.find_loader(module)
617+
self.assertIsNotNone(res)
609618

610619
def test_get_importer_avoids_emulation(self):
611620
# We use an illegal path so *none* of the path hooks should fire
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deprecate :func:`pkgutil.find_module` and :func:`pkgutil.get_module`.

0 commit comments

Comments
 (0)