Skip to content

Commit

Permalink
Added a fix for #489
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Nov 26, 2022
1 parent d5c7dc3 commit eb12145
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 43 deletions.
6 changes: 4 additions & 2 deletions src/wheel/_wheelfile.py
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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(
Expand Down
41 changes: 5 additions & 36 deletions tests/test_bdist_wheel.py
Expand Up @@ -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č" <john@utf8.org>, Γαμα קּ 東 <gama@utf8.org>
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(
Expand Down
10 changes: 5 additions & 5 deletions tests/test_wheelfile.py
Expand Up @@ -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:
Expand All @@ -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(
Expand All @@ -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'$")


Expand Down

0 comments on commit eb12145

Please sign in to comment.