[Q] Bug in cgi.FieldStorage?

Ames Andreas (MPA/DF) Andreas.Ames at tenovis.com
Fri Aug 20 09:30:21 EDT 2004


Hi,

I'm currently using Zope 2.7.2-0 which in turn uses cgi.FieldStorage
from Python 2.3.4.

FieldStorage can either build a 'list' or a 'file' to represent a
request.  Unfortunately it overwrites __len__ such that it throws an
exception when the request wasn't of type 'multipart', i.e. when it
doesn't 'return' a list but a file.  I'd consider this a bug because
if you have an instance fs of FieldStorage you can't safely write

if fs:
   ...

because of the unfortunate __len__ implementation (it raises a
TypeError exception in some cases).  I might be willing to accept that
this isn't python's bug but mine if someone argues smartly enough.

What's is the right thing to do in this case (and in the general
case)?

1) The idiom 'if fs:' is borked. I'm not supposed to use it, I should
   have known and I'm dumb.  Instead I'm supposed to use 'if fs is not
   None:' or something.  (This seems to be smart enough an argument
   for me :-)

2) cgi.FieldStorage should have a more appropriate __len__
   implementation.  If so, how?

3) cgi.FieldStorage should overwrite __nonzero__ to always return 1.
   I'm leaning towards this one.  Am I right?


tia,

andreas




More information about the Python-list mailing list