[mod_python] strange behavior of form + patch for mod_python

Jim Gallacher jpg at jgassociates.ca
Sat Nov 26 16:33:04 EST 2005

Gregory (Grisha) Trubetskoy wrote:
> On Sat, 26 Nov 2005, Jim Gallacher wrote:
>> 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" />
> IMO that's more functionality than necessary, you can always do such 
> conversion in user code and there is no need for mod_python to dictate 
> how it's done and introduce new syntax - this is something that'd be 
> appropriate for a mod_python-based framework of some kind, but not in mp 
> itself.

Fair enough.

>> 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.
> I'm pretty sure there was a reason why it had to be a list, I just can't 
> remember what it was. It may be that order of fields is significant.

That may be so, but I would think that the most common use case would be 
to use FieldStorage as a dict. It could be a simple as creating and 
dictionary to use as an index. eg.

class FieldStorage:

     def __init__():
         self.index = {}
         self.list = []

         ... blah blah blah ...
         ... create the field and get it's name ...

         if name in self.index:
             self.index[name] = [field,]

     def __getitem__(self, key):
         if key not in self.index:
             raise KeyError, key

        found = self.index[key]
        if len(found) == 1:
            return found[0]
            return found

The FieldStorage methods get, getitems, has_key, __len__, getfirst and 
getlist *all* iterate over the complete list each time they are called. 
For any given field, the order will still be preserved since our dict is 
using a list to hold the field references for each field's key.

I can't see any problems in using this implementation as the users 
application code will still get the same results.

If created a JIRA issue for this, and I'll copy this message there so 
Nicolas won't get mad at me. ;)

This discussion should likely move to python-dev at this point anyway.


More information about the Mod_python mailing list