mirror of
https://github.com/python/cpython.git
synced 2025-08-27 04:05:34 +00:00
Issue 600362: Relocated parse_qs() and parse_qsl(), from the cgi module
to the urlparse one. Added a DeprecationWarning in the old module, it will be deprecated in the future. Docs and tests updated.
This commit is contained in:
parent
849f79a5d6
commit
c469d4c3aa
7 changed files with 159 additions and 131 deletions
85
Lib/cgi.py
85
Lib/cgi.py
|
@ -37,6 +37,7 @@ import sys
|
|||
import os
|
||||
import urllib.parse
|
||||
import email.parser
|
||||
from warnings import warn
|
||||
|
||||
__all__ = ["MiniFieldStorage", "FieldStorage",
|
||||
"parse", "parse_qs", "parse_qsl", "parse_multipart",
|
||||
|
@ -153,75 +154,23 @@ def parse(fp=None, environ=os.environ, keep_blank_values=0, strict_parsing=0):
|
|||
else:
|
||||
qs = ""
|
||||
environ['QUERY_STRING'] = qs # XXX Shouldn't, really
|
||||
return parse_qs(qs, keep_blank_values, strict_parsing)
|
||||
return urllib.parse.parse_qs(qs, keep_blank_values, strict_parsing)
|
||||
|
||||
|
||||
# parse query string function called from urlparse,
|
||||
# this is done in order to maintain backward compatiblity.
|
||||
|
||||
def parse_qs(qs, keep_blank_values=0, strict_parsing=0):
|
||||
"""Parse a query given as a string argument.
|
||||
|
||||
Arguments:
|
||||
|
||||
qs: URL-encoded query string to be parsed
|
||||
|
||||
keep_blank_values: flag indicating whether blank values in
|
||||
URL encoded queries should be treated as blank strings.
|
||||
A true value indicates that blanks should be retained as
|
||||
blank strings. The default false value indicates that
|
||||
blank values are to be ignored and treated as if they were
|
||||
not included.
|
||||
|
||||
strict_parsing: flag indicating what to do with parsing errors.
|
||||
If false (the default), errors are silently ignored.
|
||||
If true, errors raise a ValueError exception.
|
||||
"""
|
||||
dict = {}
|
||||
for name, value in parse_qsl(qs, keep_blank_values, strict_parsing):
|
||||
if name in dict:
|
||||
dict[name].append(value)
|
||||
else:
|
||||
dict[name] = [value]
|
||||
return dict
|
||||
"""Parse a query given as a string argument."""
|
||||
warn("cgi.parse_qs is deprecated, use urllib.parse.parse_qs instead",
|
||||
DeprecationWarning)
|
||||
return urllib.parse.parse_qs(qs, keep_blank_values, strict_parsing)
|
||||
|
||||
def parse_qsl(qs, keep_blank_values=0, strict_parsing=0):
|
||||
"""Parse a query given as a string argument.
|
||||
|
||||
Arguments:
|
||||
|
||||
qs: URL-encoded query string to be parsed
|
||||
|
||||
keep_blank_values: flag indicating whether blank values in
|
||||
URL encoded queries should be treated as blank strings. A
|
||||
true value indicates that blanks should be retained as blank
|
||||
strings. The default false value indicates that blank values
|
||||
are to be ignored and treated as if they were not included.
|
||||
|
||||
strict_parsing: flag indicating what to do with parsing errors. If
|
||||
false (the default), errors are silently ignored. If true,
|
||||
errors raise a ValueError exception.
|
||||
|
||||
Returns a list, as G-d intended.
|
||||
"""
|
||||
pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
|
||||
r = []
|
||||
for name_value in pairs:
|
||||
if not name_value and not strict_parsing:
|
||||
continue
|
||||
nv = name_value.split('=', 1)
|
||||
if len(nv) != 2:
|
||||
if strict_parsing:
|
||||
raise ValueError("bad query field: %r" % (name_value,))
|
||||
# Handle case of a control-name with no equal sign
|
||||
if keep_blank_values:
|
||||
nv.append('')
|
||||
else:
|
||||
continue
|
||||
if len(nv[1]) or keep_blank_values:
|
||||
name = urllib.parse.unquote(nv[0].replace('+', ' '))
|
||||
value = urllib.parse.unquote(nv[1].replace('+', ' '))
|
||||
r.append((name, value))
|
||||
|
||||
return r
|
||||
|
||||
"""Parse a query given as a string argument."""
|
||||
warn("cgi.parse_qsl is deprecated, use urllib.parse.parse_qs instead",
|
||||
DeprecationWarning)
|
||||
return urllib.parse.parse_qsl(qs, keep_blank_values, strict_parsing)
|
||||
|
||||
def parse_multipart(fp, pdict):
|
||||
"""Parse multipart input.
|
||||
|
@ -624,8 +573,8 @@ class FieldStorage:
|
|||
if self.qs_on_post:
|
||||
qs += '&' + self.qs_on_post
|
||||
self.list = list = []
|
||||
for key, value in parse_qsl(qs, self.keep_blank_values,
|
||||
self.strict_parsing):
|
||||
for key, value in urllib.parse.parse_qsl(qs, self.keep_blank_values,
|
||||
self.strict_parsing):
|
||||
list.append(MiniFieldStorage(key, value))
|
||||
self.skip_lines()
|
||||
|
||||
|
@ -638,8 +587,8 @@ class FieldStorage:
|
|||
raise ValueError('Invalid boundary in multipart form: %r' % (ib,))
|
||||
self.list = []
|
||||
if self.qs_on_post:
|
||||
for key, value in parse_qsl(self.qs_on_post, self.keep_blank_values,
|
||||
self.strict_parsing):
|
||||
for key, value in urllib.parse.parse_qsl(self.qs_on_post,
|
||||
self.keep_blank_values, self.strict_parsing):
|
||||
self.list.append(MiniFieldStorage(key, value))
|
||||
FieldStorageClass = None
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue