Skip to content

Commit 05cefc0

Browse files
jaracobhrutledge
andauthored
Rely on email.message to parse a header. (#969)
* Rely on email.message to parse a header. * 👹 Feed the hobgoblins (delint). * 🧎‍♀️ Genuflect to the types. * 🧎‍♀️ Genuflect to the types. * Indicate private utility method * Replace comment with docstring Co-authored-by: Brian Rutledge <brian@bhrutledge.com>
1 parent a33d984 commit 05cefc0

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

twine/commands/check.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
import argparse
16-
import cgi
16+
import email.message
1717
import io
1818
import logging
1919
import re
20-
from typing import List, Optional, Tuple, cast
20+
from typing import Dict, List, Optional, Tuple, cast
2121

2222
import readme_renderer.rst
2323
from rich import print
@@ -63,6 +63,16 @@ def __str__(self) -> str:
6363
return self.getvalue().strip()
6464

6565

66+
def _parse_content_type(value: str) -> Tuple[str, Dict[str, str]]:
67+
"""Implement logic of deprecated cgi.parse_header().
68+
69+
From https://docs.python.org/3.11/library/cgi.html#cgi.parse_header.
70+
"""
71+
msg = email.message.EmailMessage()
72+
msg["content-type"] = value
73+
return msg.get_content_type(), msg["content-type"].params
74+
75+
6676
def _check_file(
6777
filename: str, render_warning_stream: _WarningStream
6878
) -> Tuple[List[str], bool]:
@@ -82,7 +92,7 @@ def _check_file(
8292
)
8393
description_content_type = "text/x-rst"
8494

85-
content_type, params = cgi.parse_header(description_content_type)
95+
content_type, params = _parse_content_type(description_content_type)
8696
renderer = _RENDERERS.get(content_type, _RENDERERS[None])
8797

8898
if description is None or description.rstrip() == "UNKNOWN":

0 commit comments

Comments
 (0)