Skip to content

Commit d4a0c49

Browse files
committed
Replace cgi.parse_header() with custom function for Python 3.13 compat
The cgi module has been deprecated since Python 3.11 and got removed in Python 3.13. Replace the cgi.parse_header() function with a custom one, as recommended by the Python documentation. Closes #712
1 parent f105ec2 commit d4a0c49

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

spyne/protocol/soap/mime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def collapse_swa(ctx, content_type, ns_soap_env):
115115
http://www.w3.org/Submission/soap11mtom10/
116116
117117
:param content_type: value of the Content-Type header field, parsed by
118-
cgi.parse_header() function
118+
spyne.util.http.parse_content_type_header() function
119119
:param ctx: request context
120120
"""
121121

spyne/protocol/soap/soap11.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
logger = logging.getLogger(__name__)
3939
logger_invalid = logging.getLogger(__name__ + ".invalid")
4040

41-
import cgi
42-
4341
from itertools import chain
4442

4543
import spyne.const.xml as ns
@@ -50,6 +48,7 @@
5048

5149
from spyne import BODY_STYLE_WRAPPED
5250
from spyne.util import six
51+
from spyne.util.http import parse_content_type_header
5352
from spyne.const.xml import DEFAULT_NS
5453
from spyne.const.http import HTTP_405, HTTP_500
5554
from spyne.error import RequestNotAllowed
@@ -197,7 +196,7 @@ def create_in_document(self, ctx, charset=None):
197196
"You must issue a POST request with the Content-Type "
198197
"header properly set.")
199198

200-
content_type = cgi.parse_header(content_type)
199+
content_type = parse_content_type_header(content_type)
201200
ctx.in_string = collapse_swa(ctx, content_type, self.ns_soap_env)
202201

203202
ctx.in_document = _parse_xml_string(ctx.in_string,

spyne/server/wsgi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import logging
2828
logger = logging.getLogger(__name__)
2929

30-
import cgi
3130
import threading
3231

3332
from inspect import isgenerator
@@ -44,6 +43,7 @@
4443
from spyne.server.http import HttpBase, HttpMethodContext, HttpTransportContext
4544
from spyne.util.odict import odict
4645
from spyne.util.address import address_parser
46+
from spyne.util.http import parse_content_type_header
4747

4848
from spyne.const.ansi_color import LIGHT_GREEN
4949
from spyne.const.ansi_color import END_COLOR
@@ -527,9 +527,9 @@ def __reconstruct_wsgi_request(self, http_env):
527527
charset = None
528528
if content_type is not None:
529529
# fyi, here's what the parse_header function returns:
530-
# >>> import cgi; cgi.parse_header("text/xml; charset=utf-8")
530+
# >>> parse_content_type_header("text/xml; charset=utf-8")
531531
# ('text/xml', {'charset': 'utf-8'})
532-
content_type = cgi.parse_header(content_type)
532+
content_type = parse_content_type_header(content_type)
533533
charset = content_type[1].get('charset', None)
534534

535535
return self.__wsgi_input_to_iterable(http_env), charset

spyne/util/http.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import sys
2424
import time
2525

26+
from email.message import EmailMessage
2627
from time import strftime
2728
from time import gmtime
2829
from collections import deque
@@ -72,3 +73,8 @@ def generate_cookie(k, v, max_age=None, domain=None, path=None,
7273
retval.append("Secure")
7374

7475
return '; '.join(retval)
76+
77+
def parse_content_type_header(h):
78+
msg = EmailMessage()
79+
msg['content-type'] = h
80+
return msg['content-type'].content_type, msg['content-type'].params

0 commit comments

Comments
 (0)