]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-98139: enhance namespace package repr
authorAnh71me <iyumelive@gmail.com>
Sun, 6 Nov 2022 22:13:40 +0000 (06:13 +0800)
committerGitHub <noreply@github.com>
Sun, 6 Nov 2022 22:13:40 +0000 (22:13 +0000)
Lib/importlib/_bootstrap.py
Lib/test/test_importlib/test_namespace_pkgs.py
Misc/NEWS.d/next/Library/2022-10-30-15-26-33.gh-issue-98139.qtm-9T.rst [new file with mode: 0644]

index e0110e701d6cded79b876124dc8443b29b7115ca..1a1d5cf7c9c33e151a300f8482b81af7c6a85748 100644 (file)
@@ -580,7 +580,7 @@ def _module_repr_from_spec(spec):
         if spec.loader is None:
             return f'<module {name!r}>'
         else:
-            return f'<module {name!r} ({spec.loader!r})>'
+            return f'<module {name!r} (namespace) from {list(spec.loader._path)}>'
     else:
         if spec.has_location:
             return f'<module {name!r} from {spec.origin!r}>'
index f451f7547b35bb68d42f9c8dae98878b96ae7b6b..65428c3d3ead9bfa876a0e50c31dd673b9e0407e 100644 (file)
@@ -79,6 +79,10 @@ class SingleNamespacePackage(NamespacePackageTest):
         with self.assertRaises(ImportError):
             import foo.two
 
+    def test_simple_repr(self):
+        import foo.one
+        assert repr(foo).startswith("<module 'foo' (namespace) from [")
+
 
 class DynamicPathNamespacePackage(NamespacePackageTest):
     paths = ['portion1']
diff --git a/Misc/NEWS.d/next/Library/2022-10-30-15-26-33.gh-issue-98139.qtm-9T.rst b/Misc/NEWS.d/next/Library/2022-10-30-15-26-33.gh-issue-98139.qtm-9T.rst
new file mode 100644 (file)
index 0000000..ed5dd00
--- /dev/null
@@ -0,0 +1,2 @@
+In :mod:`importlib._bootstrap`, enhance namespace package repr to `<module
+'x' (namespace) from ['path']>`.