diff options
author | Marcel Hellkamp <marc@gsites.de> | 2012-07-20 16:27:43 +0200 |
---|---|---|
committer | Marcel Hellkamp <marc@gsites.de> | 2012-07-20 16:32:23 +0200 |
commit | b652c4f1b587a6fa6af33d82a9f3482f0f934b3b (patch) | |
tree | 9f5240779d407f12018e4c56cbbcab989dabd195 | |
parent | a9b2f4cedf364c1cad4ba83f70a7f57307485458 (diff) | |
download | bottle-b652c4f1b587a6fa6af33d82a9f3482f0f934b3b.tar.gz |
Fix: cgi.FieldStorage had a misleading implementation of __nonzero__ and returned False for bool(request.files.name). This now returns True for existing files.
Thanks to Pascal Rosin via Twitter
-rw-r--r-- | bottle.py | 7 | ||||
-rwxr-xr-x | test/test_environ.py | 2 |
2 files changed, 8 insertions, 1 deletions
@@ -122,6 +122,11 @@ if py31: class NCTextIOWrapper(TextIOWrapper): def close(self): pass # Keep wrapped buffer open. +# The truth-value of cgi.FieldStorage is misleading. +class FieldStorage(cgi.FieldStorage): + def __nonzero__(self): + return bool(self.list or self.file) + # A bug in functools causes it to break if the wrapper is an instance method def update_wrapper(wrapper, wrapped, *a, **ka): try: functools.update_wrapper(wrapper, wrapped, *a, **ka) @@ -1092,7 +1097,7 @@ class BaseRequest(object): newline='\n') elif py3k: args['encoding'] = 'ISO-8859-1' - data = cgi.FieldStorage(**args) + data = FieldStorage(**args) for item in (data.list or [])[:self.MAX_PARAMS]: post[item.name] = item if item.filename else item.value return post diff --git a/test/test_environ.py b/test/test_environ.py index 6637fa4..aabd929 100755 --- a/test/test_environ.py +++ b/test/test_environ.py @@ -285,6 +285,8 @@ class TestRequest(unittest.TestCase): self.assertTrue('file2' in request.files) self.assertTrue('file2' not in request.forms) self.assertEqual('filename2.py', request.POST['file2'].filename) + self.assertTrue(request.files.file2) + self.assertFalse(request.files.file77) # UTF-8 files x = request.POST['file2'].file.read() if (3,2,0) > sys.version_info >= (3,0,0): |