[Patches] [ python-Patches-452174 ] cgi: new methods: getfirst(), getlist()

noreply@sourceforge.net noreply@sourceforge.net
Fri, 17 Aug 2001 11:28:32 -0700


Patches item #452174, was opened at 2001-08-17 11:20
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=452174&group_id=5470

Category: library
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Nobody/Anonymous (nobody)
Assigned to: Nobody/Anonymous (nobody)
Summary: cgi: new methods: getfirst(), getlist()

Initial Comment:
I find very annoying the need to check whether a user
checked one or more checkboxes in a form like this:

<input type="checkbox" name="item" value"1" />
<input type="checkbox" name="item" value="2" />

Currently, I must do something like this:

value = form.getvalue("item")
if type(value) is type([]):
    # Multiple items checked.
else:
    # One item checked.

I added two methods to cgi.py that simplify and make
CGI value processing much more readable and intuitive.

The first method ALWAYS returns a single value. Even if
more values of the same name were posted, this method
always returns only the first one, as a string.

scalar_value = form.getfirst("item")
scalar_value is now: '1'

The second method ALWAYS returns a list. If only one
value was posted then the method returns a list which
contains this one value.

list_values = form.getlist("item")
list_values is now: ['1', '2']

This allows me to write much more compact code like:

for item in form.getlist("item"):
    do_something()


Using the getfirst() method I don't need to worry about
a malicious user posting two values in a query string
where I expect only one value.

Documentation says:

"If the submitted form data contains more than one
field with the same name, the object retrieved by
"form[key]" is not a FieldStorage or MiniFieldStorage
instance but a list of such instances. Similarly, in
this situation, "form.getvalue(key)" would return a
list of strings. If you expect this possibility (i.e.,
when your HTML form contains multiple fields with the
same name), use the type() function to determine
whether you have a single instance or a list of instances."

That's IMHO wrong - one shouldn't count on a client to
provide valid input. If it doesn't, the script crashes.
That's bad.

So I added these two very simple methods to the
FieldStorage class.
The patch is against version 2.6 of the cgi.py module.





----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2001-08-17 11:28

Message:
Logged In: YES 
user_id=6380

Nice idea, but I wouldn't use "default=[]" in the argument
list.
If the user modifies the list it returns, the default is
permanently changed! I suggest that there's no need to allow
the user to specify an alternate default for getlist(), so
you can just delete this argument and say "return []" in the
default case.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=452174&group_id=5470