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

gh-97850: Deprecatefind_loader andget_loader inpkgutil#98520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
14 commits
Select commitHold shift + click to select a range
b7de6df
gh-97850: Deprecate `find_module` and `get_module` in `pkgutil`
sobolevnOct 21, 2022
b1e6d4d
Address review
sobolevnNov 9, 2022
d55a9b7
Update Misc/NEWS.d/next/Library/2022-10-21-16-23-31.gh-issue-97850.N4…
sobolevnNov 9, 2022
e282911
Use just one module
sobolevnNov 26, 2022
4f9a697
Merge branch 'deprecate-pkgutil-get-find-loader' of https://github.co…
sobolevnNov 26, 2022
4222f23
Merge branch 'main' into deprecate-pkgutil-get-find-loader
brettcannonJan 21, 2023
817f2da
Apply suggestions from code review
sobolevnFeb 5, 2023
8b116d9
Update Misc/NEWS.d/next/Library/2022-10-21-16-23-31.gh-issue-97850.N4…
sobolevnFeb 5, 2023
e94a6f3
Merge branch 'main' into deprecate-pkgutil-get-find-loader
sobolevnApr 11, 2023
e073fac
Mention deprecation in `pkgutil` docs
sobolevnApr 19, 2023
1e26981
Address review
sobolevnApr 19, 2023
24badea
Merge branch 'main' into deprecate-pkgutil-get-find-loader
arhadthedevApr 30, 2023
619c4dc
Apply suggestions from code review
brettcannonMay 3, 2023
b65576a
Merge branch 'main' into deprecate-pkgutil-get-find-loader
sobolevnMay 3, 2023
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
7 changes: 7 additions & 0 deletionsDoc/library/pkgutil.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -64,6 +64,10 @@ support.
.. versionchanged:: 3.4
Updated to be based on :pep:`451`

.. deprecated-removed:: 3.12 3.14
Use :func:`importlib.util.find_spec` instead.


.. function:: get_importer(path_item)

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

.. deprecated-removed:: 3.12 3.14
Use :func:`importlib.util.find_spec` instead.


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

Expand Down
5 changes: 5 additions & 0 deletionsDoc/whatsnew/3.12.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -812,6 +812,11 @@ Pending Removal in Python 3.14
* The *onerror* argument of :func:`shutil.rmtree` is deprecated in 3.12,
and will be removed in 3.14.

* :func:`pkgutil.find_loader` and :func:`pkgutil.get_loader`
now raise :exc:`DeprecationWarning`;
use :func:`importlib.util.find_spec` instead.
(Contributed by Nikita Sobolev in :gh:`97850`.)

Pending Removal in Future Versions
----------------------------------

Expand Down
8 changes: 8 additions & 0 deletionsLib/pkgutil.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -270,6 +270,10 @@ def get_loader(module_or_name):
If the named module is not already imported, its containing package
(if any) is imported, in order to establish the package __path__.
"""
warnings._deprecated("pkgutil.get_loader",
f"{warnings._DEPRECATED_MSG}; "
"use importlib.util.find_spec() instead",
remove=(3, 14))
if module_or_name in sys.modules:
module_or_name = sys.modules[module_or_name]
if module_or_name is None:
Expand All@@ -294,6 +298,10 @@ def find_loader(fullname):
importlib.util.find_spec that converts most failures to ImportError
and only returns the loader rather than the full spec
"""
warnings._deprecated("pkgutil.find_loader",
f"{warnings._DEPRECATED_MSG}; "
"use importlib.util.find_spec() instead",
remove=(3, 14))
if fullname.startswith('.'):
msg = "Relative module name {!r} not supported".format(fullname)
raise ImportError(msg)
Expand Down
39 changes: 21 additions & 18 deletionsLib/test/test_pkgutil.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
from pathlib import Path
from test.support.import_helper import unload, CleanImport
from test.support.warnings_helper import check_warnings
from test.support.warnings_helper import check_warnings, ignore_warnings
import unittest
import sys
import importlib
Expand DownExpand Up@@ -535,25 +535,18 @@ class ImportlibMigrationTests(unittest.TestCase):
# PEP 302 emulation in this module is in the process of being
# deprecated in favour of importlib proper

def test_get_loader_avoids_emulation(self):
with check_warnings() as w:
self.assertIsNotNone(pkgutil.get_loader("sys"))
self.assertIsNotNone(pkgutil.get_loader("os"))
self.assertIsNotNone(pkgutil.get_loader("test.support"))
self.assertEqual(len(w.warnings), 0)

@unittest.skipIf(__name__ == '__main__', 'not compatible with __main__')
@ignore_warnings(category=DeprecationWarning)
def test_get_loader_handles_missing_loader_attribute(self):
global __loader__
this_loader = __loader__
del __loader__
try:
with check_warnings() as w:
self.assertIsNotNone(pkgutil.get_loader(__name__))
self.assertEqual(len(w.warnings), 0)
self.assertIsNotNone(pkgutil.get_loader(__name__))
finally:
__loader__ = this_loader

@ignore_warnings(category=DeprecationWarning)
def test_get_loader_handles_missing_spec_attribute(self):
name = 'spam'
mod = type(sys)(name)
Expand All@@ -563,6 +556,7 @@ def test_get_loader_handles_missing_spec_attribute(self):
loader = pkgutil.get_loader(name)
self.assertIsNone(loader)

@ignore_warnings(category=DeprecationWarning)
def test_get_loader_handles_spec_attribute_none(self):
name = 'spam'
mod = type(sys)(name)
Expand All@@ -572,6 +566,7 @@ def test_get_loader_handles_spec_attribute_none(self):
loader = pkgutil.get_loader(name)
self.assertIsNone(loader)

@ignore_warnings(category=DeprecationWarning)
def test_get_loader_None_in_sys_modules(self):
name = 'totally bogus'
sys.modules[name] = None
Expand All@@ -581,18 +576,26 @@ def test_get_loader_None_in_sys_modules(self):
del sys.modules[name]
self.assertIsNone(loader)

def test_get_loader_is_deprecated(self):
with check_warnings(
(r".*\bpkgutil.get_loader\b.*", DeprecationWarning),
):
res = pkgutil.get_loader("sys")
self.assertIsNotNone(res)

def test_find_loader_is_deprecated(self):
with check_warnings(
(r".*\bpkgutil.find_loader\b.*", DeprecationWarning),
):
res = pkgutil.find_loader("sys")
self.assertIsNotNone(res)

@ignore_warnings(category=DeprecationWarning)
def test_find_loader_missing_module(self):
name = 'totally bogus'
loader = pkgutil.find_loader(name)
self.assertIsNone(loader)

def test_find_loader_avoids_emulation(self):
with check_warnings() as w:
self.assertIsNotNone(pkgutil.find_loader("sys"))
self.assertIsNotNone(pkgutil.find_loader("os"))
self.assertIsNotNone(pkgutil.find_loader("test.support"))
self.assertEqual(len(w.warnings), 0)

def test_get_importer_avoids_emulation(self):
# We use an illegal path so *none* of the path hooks should fire
with check_warnings() as w:
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
Deprecate :func:`pkgutil.find_loader` and :func:`pkgutil.get_loader`
in favor of :func:`importlib.util.find_spec`.

[8]ページ先頭

©2009-2025 Movatter.jp