Skip to content

Commit

Permalink
Fix: cgi.FieldStorage had a misleading implementation of __nonzero__ …
Browse files Browse the repository at this point in the history
…and returned False for bool(request.files.name). This now returns True for existing files.

Thanks to Pascal Rosin via Twitter
  • Loading branch information
defnull committed Jul 20, 2012
1 parent a9b2f4c commit b652c4f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 6 additions & 1 deletion bottle.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ def touni(s, enc='utf8', err='strict'):
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)
Expand Down Expand Up @@ -1092,7 +1097,7 @@ def POST(self):
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
Expand Down
2 changes: 2 additions & 0 deletions test/test_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ def test_multipart(self):
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):
Expand Down

0 comments on commit b652c4f

Please sign in to comment.