Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd6e83fb

Browse files
sobolevnCAM-Gerlachbrettcannonarhadthedev
authored
gh-97850: Deprecatefind_loader andget_loader inpkgutil (GH-98520)
Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>Co-authored-by: Brett Cannon <brett@python.org>Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
1 parent9f9e001 commitd6e83fb

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
ifmodule_or_nameinsys.modules:
274278
module_or_name=sys.modules[module_or_name]
275279
ifmodule_or_nameisNone:
@@ -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
iffullname.startswith('.'):
298306
msg="Relative module name {!r} not supported".format(fullname)
299307
raiseImportError(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
frompathlibimportPath
22
fromtest.support.import_helperimportunload,CleanImport
3-
fromtest.support.warnings_helperimportcheck_warnings
3+
fromtest.support.warnings_helperimportcheck_warnings,ignore_warnings
44
importunittest
55
importsys
66
importimportlib
@@ -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-
deftest_get_loader_avoids_emulation(self):
539-
withcheck_warnings()asw:
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
deftest_get_loader_handles_missing_loader_attribute(self):
547541
global__loader__
548542
this_loader=__loader__
549543
del__loader__
550544
try:
551-
withcheck_warnings()asw:
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
deftest_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
deftest_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
deftest_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
delsys.modules[name]
582577
self.assertIsNone(loader)
583578

579+
deftest_get_loader_is_deprecated(self):
580+
withcheck_warnings(
581+
(r".*\bpkgutil.get_loader\b.*",DeprecationWarning),
582+
):
583+
res=pkgutil.get_loader("sys")
584+
self.assertIsNotNone(res)
585+
586+
deftest_find_loader_is_deprecated(self):
587+
withcheck_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
deftest_find_loader_missing_module(self):
585595
name='totally bogus'
586596
loader=pkgutil.find_loader(name)
587597
self.assertIsNone(loader)
588598

589-
deftest_find_loader_avoids_emulation(self):
590-
withcheck_warnings()asw:
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
deftest_get_importer_avoids_emulation(self):
597600
# We use an illegal path so *none* of the path hooks should fire
598601
withcheck_warnings()asw:
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)

[8]ページ先頭

©2009-2025 Movatter.jp