Skip to content

Commit

Permalink
Fix handling of continuation lines in Description metadata field
Browse files Browse the repository at this point in the history
Fixes #1218.
  • Loading branch information
dnicolodi committed Jan 23, 2025
1 parent aa3a910 commit ecf7e91
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/1220.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix handling of multiline ``Description`` metadata fields.
55 changes: 55 additions & 0 deletions tests/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,58 @@ def test_setuptools_license_file(read_data, filtered, monkeypatch):
package = package_file.PackageFile.from_filename(filename, comment=None)
meta = package.metadata_dictionary()
assert filtered != ("license_file" in meta)


@pytest.mark.parametrize(
"description,expected",
[
# fmt: off
pytest.param(
"\n"
"Two\n"
"Lines\n",
"Two\nLines\n",
id="body",
),
pytest.param(
"Description: Two\n"
" |Lines\n"
" |\n",
"Two\nLines\n",
id="multiline-header",
),
pytest.param(
"Description: Two\n"
" Lines\n"
" \n",
"Two\nLines\n",
id="multiline-header-setuptools",
),
pytest.param(
"Description: Two\n"
" Lines\n"
" Maybe Three",
"Two\n Lines\n Maybe Three",
id="multiline-inconsistent",
),
pytest.param(
"Description: Two\n"
" |Lines\n"
" Maybe Three",
"Two\n |Lines\n Maybe Three",
id="multiline-mixed",
),
# fmt: on
],
)
def test_description_field_continuation(description, expected, monkeypatch):
"""License-File metadata entries are kept when Metadata-Version is 2.4."""
read_data = (
"Metadata-Version: 2.4\n"
"Name: test-package\n"
"Version: 1.0.0\n" + description
)
monkeypatch.setattr(package_file.wheel.Wheel, "read", lambda _: read_data)
filename = "tests/fixtures/twine-1.5.0-py2.py3-none-any.whl"
package = package_file.PackageFile.from_filename(filename, comment=None)
assert package.metadata["description"] == expected
27 changes: 27 additions & 0 deletions twine/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,28 @@ def _safe_name(name: str) -> str:
return re.sub("[^A-Za-z0-9.]+", "-", name)


def _dedent(string: str) -> str:
"""Remove line continuation suffix used for the ``Description`` metadata field.
The metadata standard prescribes that continuation lines should be
prefixed with 7 spaces followed by a pipe character, however, setuptools
used to prefix continuation lines with 8 spaces. Handle both here.
If not all lines following the first start with either of the accepted
prefixes, the string is returned without modifications.
"""
lines = string.splitlines()
if (
False # This is here only to force black into something sensible.
or all(line.startswith(" |") for line in lines[1:])
or all(line.startswith(" ") for line in lines[1:])
):
for i in range(1, len(lines)):
lines[i] = lines[i][8:]
return "\n".join(lines)
return string


# Map ``metadata.RawMetadata`` fields to ``PackageMetadata`` fields. Some
# fields are renamed to match the names expected in the upload form.
_RAW_TO_PACKAGE_METADATA = {
Expand Down Expand Up @@ -232,6 +254,11 @@ def from_filename(cls, filename: str, comment: Optional[str]) -> "PackageFile":
# than 2.4.
if version.Version(meta.get("metadata_version", "0")) < version.Version("2.4"):
meta.pop("license_files", None)

description = meta.get("description", None)
if description is not None:
meta["description"] = _dedent(description)

try:
metadata.Metadata.from_raw(meta)
except metadata.ExceptionGroup as group:
Expand Down

0 comments on commit ecf7e91

Please sign in to comment.