]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-42089: Sync with current cpython branch of importlib_metadata (GH-22775)
authorBarry Warsaw <barry@python.org>
Mon, 19 Oct 2020 21:14:21 +0000 (14:14 -0700)
committerGitHub <noreply@github.com>
Mon, 19 Oct 2020 21:14:21 +0000 (14:14 -0700)
~~The only differences are in the test files.~~

Automerge-Triggered-By: @jaraco
Lib/importlib/metadata.py
Lib/test/test_importlib/fixtures.py
Lib/test/test_importlib/test_main.py
Misc/NEWS.d/next/Library/2020-10-19-16-53-19.bpo-42089.R1dthW.rst [new file with mode: 0644]

index ffa0cba45706d6e77ffea484fc05ce2cfb405e67..302d61d505cb3a3a97fc136375d63b87638b4f1a 100644 (file)
@@ -37,6 +37,15 @@ __all__ = [
 class PackageNotFoundError(ModuleNotFoundError):
     """The package was not found."""
 
+    def __str__(self):
+        tmpl = "No package metadata was found for {self.name}"
+        return tmpl.format(**locals())
+
+    @property
+    def name(self):
+        name, = self.args
+        return name
+
 
 class EntryPoint(
         collections.namedtuple('EntryPointBase', 'name value group')):
index 985277f64615feca224b0878e1e59239052873d1..8fa92909d583ede8c2a5bf5d6bf3593b84d7f23c 100644 (file)
@@ -6,6 +6,8 @@ import tempfile
 import textwrap
 import contextlib
 
+from test.support.os_helper import FS_NONASCII
+
 
 @contextlib.contextmanager
 def tempdir():
@@ -212,12 +214,7 @@ def build_files(file_defs, prefix=pathlib.Path()):
 
 class FileBuilder:
     def unicode_filename(self):
-        try:
-            from test.support import os_helper
-        except ImportError:
-            # outside CPython, hard-code a unicode snowman
-            return '☃'
-        return os_helper.FS_NONASCII or \
+        return FS_NONASCII or \
             self.skip("File system does not support non-ascii.")
 
 
index 91e501a2eb7cdc4889ca9a67a073086e57bbfb75..a26bab63615484fa5104bb5db566c0424b9121dc 100644 (file)
@@ -32,6 +32,18 @@ class BasicTests(fixtures.DistInfoPkg, unittest.TestCase):
         with self.assertRaises(PackageNotFoundError):
             Distribution.from_name('does-not-exist')
 
+    def test_package_not_found_mentions_metadata(self):
+        """
+        When a package is not found, that could indicate that the
+        packgae is not installed or that it is installed without
+        metadata. Ensure the exception mentions metadata to help
+        guide users toward the cause. See #124.
+        """
+        with self.assertRaises(PackageNotFoundError) as ctx:
+            Distribution.from_name('does-not-exist')
+
+        assert "metadata" in str(ctx.exception)
+
     def test_new_style_classes(self):
         self.assertIsInstance(Distribution, type)
 
diff --git a/Misc/NEWS.d/next/Library/2020-10-19-16-53-19.bpo-42089.R1dthW.rst b/Misc/NEWS.d/next/Library/2020-10-19-16-53-19.bpo-42089.R1dthW.rst
new file mode 100644 (file)
index 0000000..3f3affd
--- /dev/null
@@ -0,0 +1,2 @@
+In ``importlib.metadata.PackageNotFoundError``, make reference to the
+package metadata being missing to improve the user experience.