[issue19097] bool(cgi.FieldStorage(...)) may be False unexpectedly

Senthil Kumaran report at bugs.python.org
Tue Oct 1 20:06:26 CEST 2013


Senthil Kumaran added the comment:

FieldStorage("foo", "bar") is invalid because the first argument is supposed to
be file-like object and second one headers. Here we are sending invalid headers. By
default, if the headers is none, the content-type is urlencoded.

The right way of using FieldStorage is either using the keyword arguments in a tightly couple manner. Or use it as default instance (fs = cgi.FieldStorage())

So the expectation could be:

>>>fs = cgi.FieldStorage()
>>>bool(fs)
False


>>> # sending correct fs, env
>>> # http://hg.python.org/cpython/file/32de3923bb94/Lib/test/test_cgi.py#l259
>>> fs = cgi.FieldStorage(fs, environ=env)
>>> bool(fs)
True 

The TypeError failure in python3 for bool(fs) is, in the absence of __bool__, it is trying to do
a len() and which ultimately ends up calling keys():
http://hg.python.org/cpython/file/32de3923bb94/Lib/cgi.py#l579

The proper fix in Python3 IMO is the just define __bool__ instead of __nonzero__ and that will be return False instead of TypeError.

----------
nosy: +orsenthil

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19097>
_______________________________________


More information about the Python-bugs-list mailing list