Repository: pypa/wheel

Issue:
Incorrect summary in produced wheels starting from 0.38.1
See https://pypi.org/search/?q=%3D%3Futf-8%3Fq%3F
All wheels produced after 2022/11/04 (0.38.1 release date) have incorrect summaries.

Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/pypa/wheel/pull/489?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pypa) Report
Base: **68.28**% // Head: **66.20**% // Decreases project coverage by **`-2.08%`** :warning:
> Coverage data is based on head [(`cc0c5a1`)](https://codecov.io/gh/pypa/wheel/pull/489?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pypa) compared to base [(`7a633c9`)](https://codecov.io/gh/pypa/wheel/commit/7a633c92f04d7a135459ad07700e0307900c549e?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pypa).
> Patch coverage: 100.00% of modified lines in pull request are covered.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #489      +/-   ##
==========================================
- Coverage   68.28%   66.20%   -2.09%     
==========================================
  Files          12       12              
  Lines         927      929       +2     
==========================================
- Hits          633      615      -18     
- Misses        294      314      +20     
```


| [Impacted Files](https://codecov.io/gh/pypa/wheel/pull/489?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pypa) | Coverage Δ | |
|---|---|---|
| [src/wheel/\_\_init\_\_.py](https://codecov.io/gh/pypa/wheel/pull/489/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pypa#diff-c3JjL3doZWVsL19faW5pdF9fLnB5) | `100.00% <100.00%> (ø)` | |
| [src/wheel/bdist\_wheel.py](https://codecov.io/gh/pypa/wheel/pull/489/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pypa#diff-c3JjL3doZWVsL2JkaXN0X3doZWVsLnB5) | `47.16% <100.00%> (-5.34%)` | :arrow_down: |
| [src/wheel/metadata.py](https://codecov.io/gh/pypa/wheel/pull/489/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pypa#diff-c3JjL3doZWVsL21ldGFkYXRhLnB5) | `91.93% <0.00%> (-6.46%)` | :arrow_down: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pypa). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pypa)

</details>

[:umbrella: View full report at Codecov](https://codecov.io/gh/pypa/wheel/pull/489?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pypa).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pypa).

abravalheri: Please let me know if you think there is a different root cause for the problem and it should be tackled elsewhere.

Patch:
diff --git a/src/wheel/bdist_wheel.py b/src/wheel/bdist_wheel.py
index 4754fd1..7fcf4a3 100644
--- a/src/wheel/bdist_wheel.py
+++ b/src/wheel/bdist_wheel.py
@@ -15,6 +15,7 @@ import sysconfig
 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 @@ class bdist_wheel(Command):
                 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)


Test patch:
diff --git a/tests/test_bdist_wheel.py b/tests/test_bdist_wheel.py
index 531d9e6..5a6db16 100644
--- a/tests/test_bdist_wheel.py
+++ b/tests/test_bdist_wheel.py
@@ -72,6 +72,45 @@ 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č" <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)
+
+    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
+
+
 def test_licenses_default(dummy_dist, monkeypatch, tmpdir):
     monkeypatch.chdir(dummy_dist)
     subprocess.check_call(