From: Alex Waygood Date: Mon, 27 Nov 2023 08:19:29 +0000 (+0000) Subject: gh-112414: Fix `AttributeError` when calling `repr()` on a namespace package imported... X-Git-Tag: v3.13.0a3~635 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0622839cfedacbb48eba27180fd0f0586fe97771;p=thirdparty%2FPython%2Fcpython.git gh-112414: Fix `AttributeError` when calling `repr()` on a namespace package imported with a custom loader (#112425) --- 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 858b37effc64..c6996a425346 100644 --- a/Lib/test/test_importlib/import_/test___loader__.py +++ b/Lib/test/test_importlib/import_/test___loader__.py @@ -23,6 +23,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 9b3bef02c668..072e198795d3 100644 --- a/Lib/test/test_importlib/test_namespace_pkgs.py +++ b/Lib/test/test_importlib/test_namespace_pkgs.py @@ -80,7 +80,7 @@ class SingleNamespacePackage(NamespacePackageTest): def test_simple_repr(self): import foo.one - assert repr(foo).startswith("