From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Mon, 27 Nov 2023 08:53:57 +0000 (+0100) Subject: [3.12] gh-112414: Fix `AttributeError` when calling `repr()` on a namespace package... X-Git-Tag: v3.12.1~89 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f9861e69c913bfaabf57bfa9a0207d19e192dcde;p=thirdparty%2FPython%2Fcpython.git [3.12] gh-112414: Fix `AttributeError` when calling `repr()` on a namespace package imported with a custom loader (GH-112425) (#112440) gh-112414: Fix `AttributeError` when calling `repr()` on a namespace package imported with a custom loader (GH-112425) (cherry picked from commit 0622839cfedacbb48eba27180fd0f0586fe97771) Co-authored-by: Alex Waygood --- diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index ec2e56f6ea9c..d942045f3de6 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -824,10 +824,16 @@ def _module_repr_from_spec(spec): """Return the repr to use for the module.""" name = '?' if spec.name is None else spec.name if spec.origin is None: - if spec.loader is None: + loader = spec.loader + if loader is None: return f'' + elif ( + _bootstrap_external is not None + and isinstance(loader, _bootstrap_external.NamespaceLoader) + ): + return f'' else: - return f'' + return f'' else: if spec.has_location: return f'' diff --git a/Lib/test/test_importlib/import_/test___loader__.py b/Lib/test/test_importlib/import_/test___loader__.py index a14163919af6..cb9a03229965 100644 --- a/Lib/test/test_importlib/import_/test___loader__.py +++ b/Lib/test/test_importlib/import_/test___loader__.py @@ -26,6 +26,10 @@ class SpecLoaderAttributeTests: with util.uncache('blah'), util.import_state(meta_path=[loader]): module = self.__import__('blah') self.assertEqual(loader, module.__loader__) + expected_repr_pattern = ( + r"\)>" + ) + self.assertRegex(repr(module), expected_repr_pattern) (Frozen_SpecTests, diff --git a/Lib/test/test_importlib/test_namespace_pkgs.py b/Lib/test/test_importlib/test_namespace_pkgs.py index 65428c3d3ead..cdbdecc94fed 100644 --- a/Lib/test/test_importlib/test_namespace_pkgs.py +++ b/Lib/test/test_importlib/test_namespace_pkgs.py @@ -81,7 +81,7 @@ class SingleNamespacePackage(NamespacePackageTest): def test_simple_repr(self): import foo.one - assert repr(foo).startswith("