Repository: pypa/twine

Issue:
twine should fail more gracefully when encountering a metadata version it doesn't support
(It's hard to provide a full reproduce here, so if any of the below isn't clear enough, apologies, but it's a first shot:)

I'm uploading a package (a wheel specifically) generated in the "standard way" with new setuptools and using twine upload, but twine dies by simply saying:

```
⊙  /Users/Julian/Development/Magnetic/Floodgate/.tox/pypy/bin/pypy -m twine upload --repository-url http://127.0.0.1:53385 --username floodgate-unittest-user --password 'some password' /var/folders/kh/mscfrt910qb_qb8wqrz7t_7w0000gn/T/tmp9TRY2p/floodgate_unittest-0.1.0-py2-none-any.whl                                                               Julian@Macnetic
Uploading distributions to http://127.0.0.1:53385
TypeError: must be convertible to a buffer, not None
```

Stepping through with a debugger, it looks like what's failing is that `metadata.name` is `None` in `PackageFile.from_filename`, which `pkg_resources.safe_str` can't cope with (because it regexes it for some reason).

(So that sounds like possibly one improvement, that `None` shouldn't make it all the way down there)

The way it gets to be `None` seems to be that `Distribution.parse` calls `_getHeaderAttrs` to look up a callback to use for extracting metadata for a particular setuptools version, but if the version isn't present, that function returns an empty list, and no metadata is parsed (so `name` is `None`).

Seems like another nice improvement would be failing loudly if the setuptools metadata version isn't supported.

Discussion:

Patch:
diff --git a/docs/changelog.rst b/docs/changelog.rst
index 8b23bbd..f7854e0 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -3,6 +3,7 @@
 =========
 Changelog
 =========
+* :bug:`341` Fail more gracefully when encountering bad metadata
 * :feature:`310` Now provide a more meaningful error on redirect during upload.
 * :release:`1.13.0 <2019-02-13>`
 * :bug:`452 major` Restore prompts while retaining support for suppressing
diff --git a/twine/package.py b/twine/package.py
index c838c88..9fe89d2 100644
--- a/twine/package.py
+++ b/twine/package.py
@@ -84,6 +84,15 @@ class PackageFile(object):
                 os.path.basename(filename)
             )
 
+        # If pkginfo encounters a metadata version it doesn't support, it may
+        # give us back empty metadata. At the very least, we should have a name
+        # and version
+        if not (meta.name and meta.version):
+            raise exceptions.InvalidDistribution(
+                "Invalid distribution metadata. Try upgrading twine if "
+                "possible."
+            )
+
         if dtype == "bdist_egg":
             pkgd = pkg_resources.Distribution.from_filename(filename)
             py_version = pkgd.py_version


Test patch:
diff --git a/tests/test_package.py b/tests/test_package.py
index 344800c..ecb301a 100644
--- a/tests/test_package.py
+++ b/tests/test_package.py
@@ -13,7 +13,7 @@
 # limitations under the License.
 from __future__ import unicode_literals
 import platform
-from twine import package
+from twine import package, exceptions
 
 import pretend
 import pytest
@@ -208,3 +208,22 @@ def test_no_blake2_hash_manager(monkeypatch):
     hasher.hash()
     hashes = TWINE_1_5_0_WHEEL_HEXDIGEST._replace(blake2=None)
     assert hasher.hexdigest() == hashes
+
+
+def test_pkginfo_returns_no_metadata(monkeypatch):
+    """
+    Fail gracefully if pkginfo can't interpret the metadata (possibly due to
+    seeing a version number it doesn't support yet) and gives us back an
+    'empty' object with no metadata
+    """
+
+    def EmptyDist(filename):
+        return pretend.stub(name=None, version=None)
+
+    monkeypatch.setattr(package, "DIST_TYPES", {"bdist_wheel": EmptyDist})
+    filename = 'tests/fixtures/twine-1.5.0-py2.py3-none-any.whl'
+
+    with pytest.raises(exceptions.InvalidDistribution) as err:
+        package.PackageFile.from_filename(filename, comment=None)
+
+    assert 'Invalid distribution metadata' in err.value.args[0]