[Patch #101120] add .get() to cgi.FieldStorage and
cgi.FormContentDict
Ka-Ping Yee
ping@lfw.org
Wed, 16 Aug 2000 02:31:48 -0700 (PDT)
-------- the message body, with some edits --------
Hi, guys.
This patch includes a "fix" to make cgi.parse_qsl honour its
"keep_blank_values" argument. This fix might require a bit more
discussion since it could break compatibility, so i thought i
would air it for your consideration.
BACKGROUND
----------
cgi.FieldStorage(), cgi.parse(), cgi.parse_qs(), and cgi.parse_qsl()
all accept an optional "keep_blank_values" argument which is supposed
to indicate whether form fields with empty strings as values should
be included in the result. In reality, only cgi.parse_qs() honours
this flag (cgi.parse() is okay because it uses cgi.parse_qs()).
cgi.parse_qsl() totally ignores it, and always includes all values
including empty ones. cgi.FieldStorage(), which uses cgi.parse_qsl(),
similarly does not honour the "keep_blank_values" flag.
The patch contains a fix to make cgi.parse_qsl() honour this flag.
FOR
---
It's clear that this was the intent of the "keep_blank_values"
argument. The doc string for parse_qsl says:
"""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 God intended.
"""
Its current disregard for "keep_blank_values" clearly contradicts
this doc string.
The doc string for FieldStorage.__init__ says:
"""Constructor. Read multipart/* until last part.
Arguments, all optional:
fp : file pointer; default: sys.stdin
(not used when the request method is GET)
headers : header dictionary-like object; default:
taken from environ as per CGI spec
outerboundary : terminating multipart boundary
(for internal use only)
environ : environment dictionary; default: os.environ
keep_blank_values: flag indicating whether blank values in
URL encoded forms 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.
"""
...and the current behaviour of FieldStorage.__init__ contradicts
what it says here about "keep_blank_values".
AGAINST
-------
Existing code which uses FieldStorage and relies on its current
behaviour could break. If a form field is left blank by the user
and the form is submitted, that key will not appear in the
FieldStorage -- and an attempt to index it with form[key] will
produce a KeyError where previously it yielded a MiniFieldStorage
containing an empty string.
The existing TeX documentation for the cgi module does not mention
empty values or the keep_blank_values argument at all, so to those
who have only read the library reference manual, this could be a
surprise.
OPTIONS
-------
The reason this never showed up before is that there have never
been *any* tests for FieldStorage in test_cgi.py! Naughty, naughty.
One easy way to maintain compatibility is to change FieldStorage so
that the default value for keep_blank_values is 1 (previously this
was 0 but the FieldStorage *acted* as though it were 1).
-- ?!ng