-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bool(cgi.FieldStorage(...)) may be False unexpectedly #63296
Comments
It turns out a cgi.FieldStorage object may consider itself False even when it has data. This happens when the initialization decided to use read_single() instead of one of the other ways of reading the field value (read_urlencoded() or read_multi()). Then self.list remains None, and __nonzero__ returns False no matter what the contents of self.file is. To make things worse -- or better? -- the Python 3 version still defines __nonzero__() instead of __bool__(). |
PS. Simple repro: import cgi; bool(cgi.FieldStorage('logo', u'tux.png')) This reveals it's been fixed in Python 3 -- it raises TypeError there as it should. (But __nonzero__ should be deleted, and if someone adds __bool__ they should make sure it raises the same TypeError if self.list is None.) |
FieldStorage("foo", "bar") is invalid because the first argument is supposed to 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:
>>> # 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 The proper fix in Python3 IMO is the just define __bool__ instead of __nonzero__ and that will be return False instead of TypeError. |
I believe you that the example invocation is wrong. But then shouldn't it raise an exception? I still think that if len() raises KeyError, bool() should raise KeyError too. |
Hi Guido, Agree with both your points. Attaching a patch that fixes this issue.
Finally, in the cases where read_single() is called for FieldStorage(), bool() raises valid TypeError for empty FieldStorage. Please review and if it is OK, I can check this in. |
Addressing storchaka's review comments. Here is the updated patch. |
New changeset 2d6e7a5659f0 by Senthil Kumaran in branch '2.7': |
This is fixed in all active branches (2.7,3,3 and 3.4). I have addressed all review comments. Thanks. |
Fixed in: New changeset a3e49868cfd0 by Senthil Kumaran in branch '3.3': New changeset 1638360eea41 by Senthil Kumaran in branch 'default': (Thanks for correcting my bad merge Serhiy. I did fix :w and then did a hg resolve --mark, but still ended up with merge lines. I got to look more carefully next time). |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: