From eb12145398c29f729fae5d9ced49167dbc8cce25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Gr=C3=B6nholm?= Date: Sun, 27 Nov 2022 01:03:41 +0200 Subject: [PATCH] Added a fix for #489 --- src/wheel/_wheelfile.py | 6 ++++-- tests/test_bdist_wheel.py | 41 +++++---------------------------------- tests/test_wheelfile.py | 10 +++++----- 3 files changed, 14 insertions(+), 43 deletions(-) diff --git a/src/wheel/_wheelfile.py b/src/wheel/_wheelfile.py index 61f4b771..e1c263fb 100644 --- a/src/wheel/_wheelfile.py +++ b/src/wheel/_wheelfile.py @@ -13,6 +13,7 @@ from datetime import datetime, timezone from email.generator import Generator from email.message import Message +from email.policy import EmailPolicy from io import StringIO, UnsupportedOperation from os import PathLike from pathlib import Path, PurePath @@ -32,6 +33,7 @@ _DIST_NAME_RE = re.compile(r"[^A-Za-z0-9.]+") _EXCLUDE_FILENAMES = ("RECORD", "RECORD.jws", "RECORD.p7s") DEFAULT_TIMESTAMP = datetime(1980, 1, 1, tzinfo=timezone.utc) +EMAIL_POLICY = EmailPolicy(max_line_length=0, mangle_from_=False, utf8=True) class WheelMetadata(NamedTuple): @@ -410,7 +412,7 @@ def _write_wheelfile(self) -> None: msg["Tag"] = f"{tag.interpreter}-{tag.abi}-{tag.platform}" buffer = StringIO() - Generator(buffer, maxheaderlen=0).flatten(msg) + Generator(buffer, maxheaderlen=0, policy=EMAIL_POLICY).flatten(msg) self.write_distinfo_file("WHEEL", buffer.getvalue()) def write_metadata(self, items: Iterable[tuple[str, str]]) -> None: @@ -430,7 +432,7 @@ def write_metadata(self, items: Iterable[tuple[str, str]]) -> None: msg["Version"] = str(self.metadata.version) buffer = StringIO() - Generator(buffer, maxheaderlen=0).flatten(msg) + Generator(buffer, maxheaderlen=0, policy=EMAIL_POLICY).flatten(msg) self.write_distinfo_file("METADATA", buffer.getvalue()) def write_file( diff --git a/tests/test_bdist_wheel.py b/tests/test_bdist_wheel.py index a394e11f..371c3c11 100644 --- a/tests/test_bdist_wheel.py +++ b/tests/test_bdist_wheel.py @@ -80,43 +80,12 @@ def test_unicode_record(wheel_paths: list[Path]) -> None: assert "åäö_日本語.py" in record -UTF8_PKG_INFO = """\ -Metadata-Version: 2.1 -Name: helloworld -Version: 42 -Author-email: "John X. Ãørçeč" , Γαμα קּ 東 - - -UTF-8 描述 説明 -""" - - -def test_preserve_unicode_metadata(monkeypatch, tmp_path): - monkeypatch.chdir(tmp_path) - egginfo = tmp_path / "dummy_dist.egg-info" - distinfo = tmp_path / "dummy_dist.dist-info" - - egginfo.mkdir() - (egginfo / "PKG-INFO").write_text(UTF8_PKG_INFO, encoding="utf-8") - (egginfo / "dependency_links.txt").touch() - - class simpler_bdist_wheel(bdist_wheel): - """Avoid messing with setuptools/distutils internals""" - - def __init__(self): - pass - - @property - def license_paths(self): - return [] - - cmd_obj = simpler_bdist_wheel() - cmd_obj.egg2dist(egginfo, distinfo) +def test_unicode_metadata(wheel_paths: list[Path]) -> None: + path = next(path for path in wheel_paths if "unicode.dist" in path.name) + with WheelReader(path) as wf: + metadata = wf.read_dist_info("METADATA") - metadata = (distinfo / "METADATA").read_text(encoding="utf-8") - assert 'Author-email: "John X. Ãørçeč"' in metadata - assert "Γαμα קּ 東 " in metadata - assert "UTF-8 描述 説明" in metadata + assert "Summary: A testing distribution ☃" in metadata def test_licenses_default( diff --git a/tests/test_wheelfile.py b/tests/test_wheelfile.py index 3e6437b6..f05487e2 100644 --- a/tests/test_wheelfile.py +++ b/tests/test_wheelfile.py @@ -95,14 +95,14 @@ def test_weak_hash_algorithm(wheel_path: Path, algorithm: str, digest: str) -> N ], ids=["sha256", "sha384", "sha512"], ) -def test_test(wheel_path: Path, algorithm: str, digest: str) -> None: +def test_validate_record(wheel_path: Path, algorithm: str, digest: str) -> None: hash_string = f"{algorithm}={digest}" with ZipFile(wheel_path, "w") as zf: zf.writestr("hello/héllö.py", 'print("Héllö, world!")\n') zf.writestr("test-1.0.dist-info/RECORD", f"hello/héllö.py,{hash_string},25") with WheelReader(wheel_path) as wf: - wf.test() + wf.validate_record() def test_testzip_missing_hash(wheel_path: Path) -> None: @@ -111,11 +111,11 @@ def test_testzip_missing_hash(wheel_path: Path) -> None: zf.writestr("test-1.0.dist-info/RECORD", "") with WheelReader(wheel_path) as wf: - exc = pytest.raises(WheelError, wf.test) + exc = pytest.raises(WheelError, wf.validate_record) exc.match("^No hash found for file 'hello/héllö.py'$") -def test_testzip_bad_hash(wheel_path: Path) -> None: +def test_validate_record_bad_hash(wheel_path: Path) -> None: with ZipFile(wheel_path, "w") as zf: zf.writestr("hello/héllö.py", 'print("Héllö, w0rld!")\n') zf.writestr( @@ -124,7 +124,7 @@ def test_testzip_bad_hash(wheel_path: Path) -> None: ) with WheelReader(wheel_path) as wf: - exc = pytest.raises(WheelError, wf.test) + exc = pytest.raises(WheelError, wf.validate_record) exc.match("^Hash mismatch for file 'hello/héllö.py'$")