[mod_python] strange behavior of form + patch for mod_python

Jim Gallacher jpg at jgassociates.ca
Sat Nov 26 13:45:24 EST 2005


Rafal Zawadzki wrote:
> Hi. I am using modpython+psp. Let's take a short psp example:
> 
> 
> 
> <%
> if form.has_key('testkey'):
>         req.write(str(form['testkey']))
> %>
> <form action="dupa.psp" method="get" enctype="multipart/form-data">
> <input type="checkbox" name="testkey" value="1">
> <input type="checkbox" name="testkey" value="2">
> <input type="submit">
> </form>
> 
> When i set one checkbox, form['testkey'] result is key. When I set two or 
> more, this is a list. IMVHO it is a BIG bug. I patched my modpython to give 
> always a list:
> 
> 
> bluszcz at idoru:$ diff util.py util.py.orig
> 234c234
> <             return found
> ---
> 
>>             return found[0]
> 
> bluszcz at idoru:$
> 
> 
> but i understand why it was like that. Anyone got an idea?

The problem with your patch is that it will likely break every piece of 
form processing code ever written, *except* for the use case you've 
given. :) With this patch, *every* form field will be returned as a 
list. I'm pretty sure that was not your intent. ;)

I don't think this is a BIG bug, more of little implentation quirk. If 
you want a list you can always use the getlist method.
http://www.modpython.org/live/current/doc-html/pyapi-util-fstor.html

I could personally support changes to FieldStorage such that form fields 
are coerced to a particular python type, similar to zope. Zope example:

<input type="checkbox" name="testkey:list" value="1" />
<input type="checkbox" name="testkey:list" value="2" />
<input type="text" name="hits:int" />
<input type="text" name="employee.first_name:records" />
<input type="text" name="employee.last_name:records" />
<input type="text" name="employee.wage:records" />

I do find the current implementation of FieldStorage.__getitem__ a 
little odd from a performance perspective. Each __getitem__ call loops 
through the entire field list resulting in O(n^2) behaviour if you need 
to process all the fields in a form. Ouch. I can't see why we can't 
creat a dict when the form data is initally processed. I think this is 
worthwhile investigating for version 3.3.

On another note, it's helpful when submitting a patch to give some 
additional context. At a minimum please give the mod_python version and 
use diff -u.

Regards,
Jim





More information about the Mod_python mailing list