Skip to content

Commit

Permalink
fix #883: use HeadersParser to ensure only mime metadata in headers i…
Browse files Browse the repository at this point in the history
…s used
  • Loading branch information
RonnyPfannschmidt committed Sep 11, 2023
1 parent 62b7e3f commit d60e767
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ features
* support using rich.logging as console log handler if installed
* fix #527: type annotation in default version template

bugfixes
--------

* fix #883: use HeadersParser to ensure only mime metadata in headers is used

v7.1.0
======

Expand Down
19 changes: 15 additions & 4 deletions src/setuptools_scm/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@
log = logging.getLogger(__name__)


def data_from_mime(path: _t.PathT) -> dict[str, str]:
content = Path(path).read_text(encoding="utf-8")
def data_from_mime(path: _t.PathT, content: None | str = None) -> dict[str, str]:
"""return a mapping from mime/pseudo-mime content
:param path: path to the mime file
:param content: content of the mime file, if None, read from path
:rtype: dict[str, str]
"""

if content is None:
content = Path(path).read_text(encoding="utf-8")
log.debug("mime %s content:\n%s", path, textwrap.indent(content, " "))
# the complex conditions come from reading pseudo-mime-messages
data = dict(x.split(": ", 1) for x in content.splitlines() if ": " in x)

from email.parser import HeaderParser

parser = HeaderParser()
message = parser.parsestr(content)
data = dict(message.items())
log.debug("mime %s data:\n%s", path, data)
return data
8 changes: 8 additions & 0 deletions testing/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
from setuptools_scm import Configuration
from setuptools_scm._run_cmd import run
from setuptools_scm.git import parse
from setuptools_scm.integration import data_from_mime


def test_data_from_mime_ignores_body() -> None:
assert data_from_mime(
"test",
"version: 1.0\r\n\r\nversion: bad",
) == {"version": "1.0"}


def test_pkginfo_noscmroot(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
Expand Down

0 comments on commit d60e767

Please sign in to comment.