From 7b5dba0252719055a4b131849d9e20465c3036d1 Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Wed, 9 Nov 2022 18:08:36 +0000 Subject: [PATCH 1/4] Add test capturing issue #488 behaviour --- tests/test_bdist_wheel.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/test_bdist_wheel.py b/tests/test_bdist_wheel.py index 531d9e6f..8567f63c 100644 --- a/tests/test_bdist_wheel.py +++ b/tests/test_bdist_wheel.py @@ -6,6 +6,7 @@ import subprocess import sys import sysconfig +from unittest.mock import Mock, patch from zipfile import ZipFile import pytest @@ -72,6 +73,38 @@ def test_unicode_record(wheel_paths): assert "åäö_日本語.py".encode() in record +UTF8_PKG_INFO = """\ +Metadata-Version: 2.1 +Name: helloworld +Version: 42 +Author-email: John X. Ãørçeč" , Γαμα קּ 東 + + +UTF-8 描述 説明 +""" + + +@patch("distutils.dist.Distribution", new=Mock) +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() + + metadata = Mock(license_files=[], get_option_dict=lambda: {}) + dist = Mock(metadata=metadata) + cmd_obj = bdist_wheel(dist) + cmd_obj.egg2dist(egginfo, distinfo) + + metadata = (distinfo / "METADATA").read_text(encoding="utf-8") + assert "John X. Ãørçeč" in metadata + assert "Γαμα קּ 東 " in metadata + assert "UTF-8 描述 説明" in metadata + + def test_licenses_default(dummy_dist, monkeypatch, tmpdir): monkeypatch.chdir(dummy_dist) subprocess.check_call( From cc0c5a16e602c12f0b6764d52ff3a4bf9400c4f7 Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Wed, 9 Nov 2022 18:24:40 +0000 Subject: [PATCH 2/4] Allow use an UTF-8 email policy when writing METADATA --- src/wheel/bdist_wheel.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/wheel/bdist_wheel.py b/src/wheel/bdist_wheel.py index 4754fd11..7fcf4a37 100644 --- a/src/wheel/bdist_wheel.py +++ b/src/wheel/bdist_wheel.py @@ -15,6 +15,7 @@ import warnings from collections import OrderedDict from email.generator import BytesGenerator, Generator +from email.policy import EmailPolicy from glob import iglob from io import BytesIO from shutil import rmtree @@ -534,8 +535,13 @@ def adios(p): adios(dependency_links_path) pkg_info_path = os.path.join(distinfo_path, "METADATA") + serialization_policy = EmailPolicy( + utf8=True, + mangle_from_=False, + max_line_length=0, + ) with open(pkg_info_path, "w", encoding="utf-8") as out: - Generator(out, mangle_from_=False, maxheaderlen=0).flatten(pkg_info) + Generator(out, policy=serialization_policy).flatten(pkg_info) for license_path in self.license_paths: filename = os.path.basename(license_path) From 3416b9bdd5ec0970131fb64064546478db9dfb6c Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Wed, 9 Nov 2022 18:37:57 +0000 Subject: [PATCH 3/4] Simplify mocking in test --- tests/test_bdist_wheel.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/test_bdist_wheel.py b/tests/test_bdist_wheel.py index 8567f63c..bcb01c0f 100644 --- a/tests/test_bdist_wheel.py +++ b/tests/test_bdist_wheel.py @@ -6,7 +6,6 @@ import subprocess import sys import sysconfig -from unittest.mock import Mock, patch from zipfile import ZipFile import pytest @@ -84,7 +83,6 @@ def test_unicode_record(wheel_paths): """ -@patch("distutils.dist.Distribution", new=Mock) def test_preserve_unicode_metadata(monkeypatch, tmp_path): monkeypatch.chdir(tmp_path) egginfo = tmp_path / "dummy_dist.egg-info" @@ -94,9 +92,17 @@ def test_preserve_unicode_metadata(monkeypatch, tmp_path): (egginfo / "PKG-INFO").write_text(UTF8_PKG_INFO, encoding="utf-8") (egginfo / "dependency_links.txt").touch() - metadata = Mock(license_files=[], get_option_dict=lambda: {}) - dist = Mock(metadata=metadata) - cmd_obj = bdist_wheel(dist) + 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) metadata = (distinfo / "METADATA").read_text(encoding="utf-8") From 882dcb8e8c28039f4961f4f58b299ad6ceb9068c Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Wed, 9 Nov 2022 18:40:08 +0000 Subject: [PATCH 4/4] Fix author email --- tests/test_bdist_wheel.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_bdist_wheel.py b/tests/test_bdist_wheel.py index bcb01c0f..5a6db16e 100644 --- a/tests/test_bdist_wheel.py +++ b/tests/test_bdist_wheel.py @@ -76,7 +76,7 @@ def test_unicode_record(wheel_paths): Metadata-Version: 2.1 Name: helloworld Version: 42 -Author-email: John X. Ãørçeč" , Γαμα קּ 東 +Author-email: "John X. Ãørçeč" , Γαμα קּ 東 UTF-8 描述 説明 @@ -106,7 +106,7 @@ def license_paths(self): cmd_obj.egg2dist(egginfo, distinfo) metadata = (distinfo / "METADATA").read_text(encoding="utf-8") - assert "John X. Ãørçeč" in metadata + assert 'Author-email: "John X. Ãørçeč"' in metadata assert "Γαμα קּ 東 " in metadata assert "UTF-8 描述 説明" in metadata