Skip to content

Commit 357f6e0

Browse files
Merge pull request #896 from RonnyPfannschmidt/fix-883-robust-mime-parsing
fix mime parsing - ignore potential mime headers after end of headers
2 parents 26eaeab + e739f66 commit 357f6e0

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ features
3939
* support using rich.logging as console log handler if installed
4040
* fix #527: type annotation in default version template
4141

42+
bugfixes
43+
--------
44+
45+
* fix #883: use HeadersParser to ensure only mime metadata in headers is used
46+
4247
v7.1.0
4348
======
4449

src/setuptools_scm/integration.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,22 @@
99
log = logging.getLogger(__name__)
1010

1111

12-
def data_from_mime(path: _t.PathT) -> dict[str, str]:
13-
content = Path(path).read_text(encoding="utf-8")
12+
def data_from_mime(path: _t.PathT, content: None | str = None) -> dict[str, str]:
13+
"""return a mapping from mime/pseudo-mime content
14+
:param path: path to the mime file
15+
:param content: content of the mime file, if None, read from path
16+
:rtype: dict[str, str]
17+
18+
"""
19+
20+
if content is None:
21+
content = Path(path).read_text(encoding="utf-8")
1422
log.debug("mime %s content:\n%s", path, textwrap.indent(content, " "))
15-
# the complex conditions come from reading pseudo-mime-messages
16-
data = dict(x.split(": ", 1) for x in content.splitlines() if ": " in x)
1723

24+
from email.parser import HeaderParser
25+
26+
parser = HeaderParser()
27+
message = parser.parsestr(content)
28+
data = dict(message.items())
1829
log.debug("mime %s data:\n%s", path, data)
1930
return data

testing/test_regressions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
from setuptools_scm import Configuration
1313
from setuptools_scm._run_cmd import run
1414
from setuptools_scm.git import parse
15+
from setuptools_scm.integration import data_from_mime
16+
17+
18+
def test_data_from_mime_ignores_body() -> None:
19+
assert data_from_mime(
20+
"test",
21+
"version: 1.0\r\n\r\nversion: bad",
22+
) == {"version": "1.0"}
1523

1624

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

0 commit comments

Comments
 (0)