Skip to content

Commit a46be77

Browse files
authored
PEP 594: provide examples of how to replace the relevant parts of cgi (#2304)
1 parent 40e4694 commit a46be77

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

pep-0594.rst

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,28 @@ inefficient because every incoming request is handled in a new process.
341341

342342
"[...] designed poorly and are now near-impossible to fix (``cgi``) [...]"
343343

344-
Several people proposed to either keep the ``cgi`` module for features like
345-
``cgi.parse_qs`` or move ``cgi.escape`` to a different module. The
346-
functions ``cgi.parse_qs`` and ``cgi.parse_qsl`` have been
347-
deprecated for a while and are actually aliases for
348-
``urllib.parse.parse_qs`` and ``urllib.parse.parse_qsl``. The
349-
function ``cgi.quote`` has been deprecated in favor of ``html.quote``
350-
with secure default values.
344+
Replacements for the various parts of ``cgi`` which are not directly
345+
related to executing code are:
346+
347+
- ``parse`` with ``urllib.parse.parse_qs`` (``parse`` is just a wrapper)
348+
- ``parse_header`` with ``email.message.Message`` (see example below)
349+
- ``parse_multipart`` with ``email.message.Message`` (same MIME RFCs)
350+
- ``FieldStorage``/``MiniFieldStorage`` has no direct replacement
351+
- ``valid_boundary`` (undocumented) with ``re.compile("^[ -~]{0,200}[!-~]$")``
352+
353+
As an explicit example of how close ``parse_header`` and
354+
``email.message.Message`` are::
355+
356+
>>> from cgi import parse_header
357+
>>> from email.message import Message
358+
>>> parse_header(h)
359+
('application/json', {'charset': 'utf8'})
360+
>>> m = Message()
361+
>>> m['content-type'] = h
362+
>>> m.get_params()
363+
[('application/json', ''), ('charset', 'utf8')]
364+
>>> m.get_param('charset')
365+
'utf8'
351366

352367

353368
cgitb
@@ -640,6 +655,8 @@ Update 4
640655
--------
641656
* Add Brett as a co-author.
642657
* Retarget the PEP for Python 3.11.
658+
* Examples of how to replace the relevant parts of ``cgi``
659+
(thanks Martijn Pieters).
643660

644661

645662
References

0 commit comments

Comments
 (0)