Skip to content

Commit

Permalink
Replaced deprecated cgi module with email module. Ref #232.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Mar 2, 2024
1 parent 6f1f439 commit 309e9a0
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions distutils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import os
import email.message
from configparser import RawConfigParser

from .cmd import Command
Expand Down Expand Up @@ -121,11 +122,8 @@ def _read_pypirc(self): # noqa: C901

def _read_pypi_response(self, response):
"""Read and decode a PyPI HTTP response."""
import cgi

content_type = response.getheader('content-type', 'text/plain')
encoding = cgi.parse_header(content_type)[1].get('charset', 'ascii')
return response.read().decode(encoding)
return response.read().decode(_extract_encoding(content_type))

def initialize_options(self):
"""Initialize options."""
Expand All @@ -139,3 +137,15 @@ def finalize_options(self):
self.repository = self.DEFAULT_REPOSITORY
if self.realm is None:
self.realm = self.DEFAULT_REALM


def _extract_encoding(content_type):
"""
>>> _extract_encoding('text/plain')
'ascii'
>>> _extract_encoding('text/html; charset="utf8"')
'utf8'
"""
msg = email.message.EmailMessage()
msg['content-type'] = content_type
return msg['content-type'].params.get('charset', 'ascii')

0 comments on commit 309e9a0

Please sign in to comment.