[Q] Bug in cgi.FieldStorage?

  • Thread starter Ames Andreas (MPA/DF)
  • Start date
A

Ames Andreas (MPA/DF)

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
 
T

Tim Roberts

Ames Andreas (MPA/DF) said:
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:
...

What question do you intuitively expect that statement to be asking?

I've been using cgi.FieldStorage() for quite a while, and I can't say that
I have ever wanted to write a statement like that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top