diff options
author | Brett Cannon <brett@python.org> | 2021-03-24 15:26:56 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-24 15:26:56 (GMT) |
commit | 9cb31d671646a5ff0901f79d2d61022621447190 (patch) | |
tree | 07877b815e17e8def80ed4e70c385b4b3a23b5af /Lib/importlib | |
parent | 3ba3d513b1e3c63d09cb798b982a9e6c369cea4c (diff) | |
download | cpython-9cb31d671646a5ff0901f79d2d61022621447190.zip cpython-9cb31d671646a5ff0901f79d2d61022621447190.tar.gz cpython-9cb31d671646a5ff0901f79d2d61022621447190.tar.bz2 |
bpo-42137: have ModuleType.__repr__ prefer __spec__ over module_repr() (GH-24953)
This is to work towards the removal of the use of module_repr() in Python 3.12 (documented as deprecated since 3.4).
Diffstat (limited to 'Lib/importlib')
-rw-r--r-- | Lib/importlib/_bootstrap.py | 20 |
1 files changed, 6 insertions, 14 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index e4f893c..5038b46 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -275,7 +275,7 @@ def _requires_frozen(fxn): def _load_module_shim(self, fullname): """Load the specified module into sys.modules and return it. - This method is deprecated. Use loader.exec_module instead. + This method is deprecated. Use loader.exec_module() instead. """ msg = ("the load_module() method is deprecated and slated for removal in " @@ -292,24 +292,16 @@ def _load_module_shim(self, fullname): # Module specifications ####################################################### def _module_repr(module): - # The implementation of ModuleType.__repr__(). + """The implementation of ModuleType.__repr__().""" loader = getattr(module, '__loader__', None) - if hasattr(loader, 'module_repr'): - # As soon as BuiltinImporter, FrozenImporter, and NamespaceLoader - # drop their implementations for module_repr. we can add a - # deprecation warning here. + if spec := getattr(module, "__spec__", None): + return _module_repr_from_spec(spec) + elif hasattr(loader, 'module_repr'): try: return loader.module_repr(module) except Exception: pass - try: - spec = module.__spec__ - except AttributeError: - pass - else: - if spec is not None: - return _module_repr_from_spec(spec) - + # Fall through to a catch-all which always succeeds. # We could use module.__class__.__name__ instead of 'module' in the # various repr permutations. try: |