Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Address review
  • Loading branch information
sobolevn committed Nov 9, 2022
commit b1e6d4d8b33e2090c84d9691c76d9efb63d7f817
3 changes: 2 additions & 1 deletion Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,9 @@ Deprecated
(Contributed by Brett Cannon in :gh:`65961`.)

* :func:`pkgutil.find_module` and :func:`pkgutil.get_module`
Comment thread
sobolevn marked this conversation as resolved.
Outdated
now raise :exc:`DeprecationWarning`,
now raise :exc:`DeprecationWarning`;
use :func:`importlib.util.find_spec` instead.
Comment thread
sobolevn marked this conversation as resolved.
Outdated
(Contributed by Nikita Sobolev in :gh:`97850`.)


Pending Removal in Python 3.13
Expand Down
16 changes: 8 additions & 8 deletions Lib/pkgutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,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.warn("`pkgutil.get_loader` is deprecated since Python 3.12; "
"this function is slated for removal in Python 3.14, "
"use `importlib.util.find_spec` instead",
DeprecationWarning)
warnings._deprecated("pkgutil.get_loader",
f"{warnings._DEPRECATED_MSG}; "
"use importlib.util.find_spec instead",
Comment thread
brettcannon marked this conversation as resolved.
Outdated
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 @@ -493,10 +493,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.warn("`pkgutil.find_loader` is deprecated since Python 3.12; "
"this function is slated for removal in Python 3.14, "
"use `importlib.util.find_spec` instead",
DeprecationWarning)
warnings._deprecated("pkgutil.find_loader",
f"{warnings._DEPRECATED_MSG}; "
"use importlib.util.find_spec instead",
Comment thread
brettcannon marked this conversation as resolved.
Outdated
remove=(3, 14))
if fullname.startswith('.'):
msg = "Relative module name {!r} not supported".format(fullname)
raise ImportError(msg)
Expand Down
48 changes: 22 additions & 26 deletions Lib/test/test_pkgutil.py
Original file line number Diff line number Diff 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 All @@ -10,7 +10,6 @@
import os.path
import tempfile
import shutil
import warnings
import zipfile

# Note: pkgutil.walk_packages is currently tested in test_runpy. This is
Expand Down Expand Up @@ -550,29 +549,18 @@ def test_loader_deprecated(self):
with self.check_deprecated():
pkgutil.ImpLoader("", "", "", "")

def test_get_loader_is_deprecated(self):
for module in ["sys", "os", "test.support"]:
with check_warnings((
"`pkgutil.get_loader` is deprecated since Python 3.12; "
"this function is slated for removal in Python 3.14, "
"use `importlib.util.find_spec` instead",
DeprecationWarning,
)):
res = pkgutil.get_loader(module)
self.assertIsNotNone(res)

@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 warnings.catch_warnings():
warnings.simplefilter('ignore', DeprecationWarning)
self.assertIsNotNone(pkgutil.get_loader(__name__))
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 @@ -582,6 +570,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 @@ -591,6 +580,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 @@ -600,22 +590,28 @@ def test_get_loader_None_in_sys_modules(self):
del sys.modules[name]
self.assertIsNone(loader)

def test_find_loader_missing_module(self):
name = 'totally bogus'
loader = pkgutil.find_loader(name)
self.assertIsNone(loader)
def test_get_loader_is_deprecated(self):
for module in ["sys", "os", "test.support"]:
Comment thread
brettcannon marked this conversation as resolved.
Outdated
with check_warnings(
(r".*\bpkgutil.get_loader\b.*", DeprecationWarning),
):
res = pkgutil.get_loader(module)
self.assertIsNotNone(res)

def test_find_loader_is_deprecated(self):
for module in ["sys", "os", "test.support"]:
with check_warnings((
"`pkgutil.find_loader` is deprecated since Python 3.12; "
"this function is slated for removal in Python 3.14, "
"use `importlib.util.find_spec` instead",
DeprecationWarning,
)):
with check_warnings(
(r".*\bpkgutil.find_loader\b.*", DeprecationWarning),
):
res = pkgutil.find_loader(module)
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_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