Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

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 deletions Doc/reference/import.rst
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,13 @@ Here are the exact rules used:
:meth:`~importlib.abc.Loader.module_repr` method, if defined, before
trying either approach described above. However, the method is deprecated.

.. versionchanged:: 3.10

Calling :meth:`~importlib.abc.Loader.module_repr` now occurs after trying to
use a module's ``__spec__`` attribute but before falling back on
``__file__``. Use of :meth:`~importlib.abc.Loader.module_repr` is slated to
stop in Python 3.12.

.. _pyc-invalidation:

Cached bytecode invalidation
Expand Down
6 changes: 6 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,12 @@ Deprecated
for Python 3.12.
(Contributed by Brett Cannon in :issue:`42137`.)

* :meth:`importlib.abc.Loader.module_repr`,
:meth:`importlib.machinery.FrozenLoader.module_repr`, and
:meth:`importlib.machinery.BuiltinLoader.module_repr` are deprecated and
slated for removal in Python 3.12.
(Contributed by Brett Cannon in :issue:`42136`.)

* ``sqlite3.OptimizedUnicode`` has been undocumented and obsolete since Python
3.3, when it was made an alias to :class:`str`. It is now deprecated,
scheduled for removal in Python 3.12.
Expand Down
3 changes: 3 additions & 0 deletions Lib/importlib/_abc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Subset of importlib.abc used to reduce importlib.util imports."""
from . import _bootstrap
import abc
import warnings


class Loader(metaclass=abc.ABCMeta):
Expand Down Expand Up @@ -47,5 +48,7 @@ def module_repr(self, module):
This method is deprecated.

"""
warnings.warn("importlib.abc.Loader.module_repr() is deprecated and "
"slated for removal in Python 3.12", DeprecationWarning)
# The exception will cause ModuleType.__repr__ to ignore this method.
raise NotImplementedError
6 changes: 4 additions & 2 deletions Lib/importlib/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,6 @@ def _module_repr(module):
except Exception:
pass
# Fall through to a catch-all which always succeeds.
# We could use module.__class__.__name__ instead of 'module' in the
# various repr permutations.
try:
name = module.__name__
except AttributeError:
Expand Down Expand Up @@ -741,6 +739,8 @@ def module_repr(module):
The method is deprecated. The import machinery does the job itself.

"""
_warnings.warn("BuiltinImporter.module_repr() is deprecated and "
"slated for removal in Python 3.12", DeprecationWarning)
return f'<module {module.__name__!r} ({BuiltinImporter._ORIGIN})>'

@classmethod
Expand Down Expand Up @@ -816,6 +816,8 @@ def module_repr(m):
The method is deprecated. The import machinery does the job itself.

"""
_warnings.warn("FrozenImporter.module_repr() is deprecated and "
"slated for removal in Python 3.12", DeprecationWarning)
return '<module {!r} ({})>'.format(m.__name__, FrozenImporter._ORIGIN)

@classmethod
Expand Down
2 changes: 2 additions & 0 deletions Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,8 @@ def module_repr(module):
The method is deprecated. The import machinery does the job itself.

"""
_warnings.warn("_NamespaceLoader.module_repr() is deprecated and "
"slated for removal in Python 3.12", DeprecationWarning)
return '<module {!r} (namespace)>'.format(module.__name__)

def is_package(self, fullname):
Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_importlib/test_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,10 @@ def test_load_module(self):

def test_module_repr(self):
mod = types.ModuleType('blah')
with self.assertRaises(NotImplementedError):
self.ins.module_repr(mod)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
with self.assertRaises(NotImplementedError):
self.ins.module_repr(mod)
original_repr = repr(mod)
mod.__loader__ = self.ins
# Should still return a proper repr.
Expand Down
7 changes: 5 additions & 2 deletions Lib/test/test_importlib/test_namespace_pkgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import sys
import unittest
import warnings

from test.test_importlib import util

Expand Down Expand Up @@ -82,8 +83,10 @@ def test_cant_import_other(self):

def test_module_repr(self):
import foo.one
self.assertEqual(foo.__spec__.loader.module_repr(foo),
"<module 'foo' (namespace)>")
with warnings.catch_warnings():
warnings.simplefilter("ignore")
self.assertEqual(foo.__spec__.loader.module_repr(foo),
"<module 'foo' (namespace)>")


class DynamicPathNamespacePackage(NamespacePackageTest):
Expand Down
22 changes: 20 additions & 2 deletions Lib/test/test_importlib/test_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import unittest
from test import support
from test.support import import_helper
from distutils.util import get_platform
from contextlib import contextmanager
from .util import temp_module

Expand All @@ -18,6 +17,25 @@
EnumKey, CloseKey, DeleteKey, OpenKey
)

def get_platform():
# Port of distutils.util.get_platform().
TARGET_TO_PLAT = {
'x86' : 'win32',
'x64' : 'win-amd64',
'arm' : 'win-arm32',
}
if ('VSCMD_ARG_TGT_ARCH' in os.environ and
os.environ['VSCMD_ARG_TGT_ARCH'] in TARGET_TO_PLAT):
return TARGET_TO_PLAT[os.environ['VSCMD_ARG_TGT_ARCH']]
elif 'amd64' in sys.version.lower():
return 'win-amd64'
elif '(arm)' in sys.version.lower():
return 'win-arm32'
elif '(arm64)' in sys.version.lower():
return 'win-arm64'
else:
return sys.platform

def delete_registry_tree(root, subkey):
try:
hkey = OpenKey(root, subkey, access=KEY_ALL_ACCESS)
Expand Down Expand Up @@ -101,7 +119,7 @@ def test_tagged_suffix(self):

self.assertIn(expected_tag, suffixes)

# Ensure the tags are in the correct order
# Ensure the tags are in the correct order.
tagged_i = suffixes.index(expected_tag)
self.assertLess(tagged_i, untagged_i)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Deprecate all module_repr() methods found in importlib as their use is being
phased out by Python 3.12.
Loading