Skip to content

Commit 7a42bce

Browse files
authored
Replace cgi module with email.message (#11098)
1 parent 669fc18 commit 7a42bce

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

news/11099.process.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove reliance on the stdlib cgi module, which is deprecated in Python 3.11.

src/pip/_internal/index/collector.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
The main purpose of this module is to expose LinkCollector.collect_sources().
33
"""
44

5-
import cgi
65
import collections
6+
import email.message
77
import functools
88
import itertools
99
import logging
@@ -155,9 +155,11 @@ def _get_html_response(url: str, session: PipSession) -> Response:
155155
def _get_encoding_from_headers(headers: ResponseHeaders) -> Optional[str]:
156156
"""Determine if we have any encoding information in our headers."""
157157
if headers and "Content-Type" in headers:
158-
content_type, params = cgi.parse_header(headers["Content-Type"])
159-
if "charset" in params:
160-
return params["charset"]
158+
m = email.message.Message()
159+
m["content-type"] = headers["Content-Type"]
160+
charset = m.get_param("charset")
161+
if charset:
162+
return str(charset)
161163
return None
162164

163165

src/pip/_internal/network/download.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Download files with progress indicators.
22
"""
3-
import cgi
3+
import email.message
44
import logging
55
import mimetypes
66
import os
@@ -81,12 +81,13 @@ def parse_content_disposition(content_disposition: str, default_filename: str) -
8181
Parse the "filename" value from a Content-Disposition header, and
8282
return the default filename if the result is empty.
8383
"""
84-
_type, params = cgi.parse_header(content_disposition)
85-
filename = params.get("filename")
84+
m = email.message.Message()
85+
m["content-type"] = content_disposition
86+
filename = m.get_param("filename")
8687
if filename:
8788
# We need to sanitize the filename to prevent directory traversal
8889
# in case the filename contains ".." path parts.
89-
filename = sanitize_content_filename(filename)
90+
filename = sanitize_content_filename(str(filename))
9091
return filename or default_filename
9192

9293

0 commit comments

Comments
 (0)