Skip to content

Commit

Permalink
Merge pull request #13 from zopefoundation/fix-file-uploads-python-3.4
Browse files Browse the repository at this point in the history
Fix file uploads on python 3.4 and up.
  • Loading branch information
Brian Sutherland committed Nov 4, 2016
2 parents 61af99d + 3680166 commit 39d67bc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ Changes
4.3.1 (unreleased)
------------------

- TBD
- Fix file uploads on python 3.4 and up. cgi.FieldStorage explicitly
closes files when it is garbage collected. For details, see:

* http://bugs.python.org/issue18394
* https://hg.python.org/cpython/rev/c0e9ba7b26d5

We now keep a reference to the FieldStorage till we are finished
processing the request.


4.3.0 (2016-07-04)
Expand Down
8 changes: 8 additions & 0 deletions src/zope/publisher/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ def processInputs(self):
args = {'encoding': 'utf-8'} if not PYTHON2 else {}
fs = ZopeFieldStorage(fp=fp, environ=env,
keep_blank_values=1, **args)
# On python 3.4 and up, FieldStorage explictly closes files
# when it is garbage collected
# see:
# http://bugs.python.org/issue18394
# https://hg.python.org/cpython/rev/c0e9ba7b26d5
# so we keep a reference to the FieldStorage till we are
# finished processing the request.
self.hold(fs)

fslist = getattr(fs, 'list', None)
if fslist is not None:
Expand Down
2 changes: 2 additions & 0 deletions src/zope/publisher/tests/test_browserrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ def testFileUploadPost(self):
request.processInputs()
self.assertEqual(request.form['upload'].filename, 'notepad.exe')

# Test that we can actually read the file data
self.assertEqual(request.form['upload'].read(), b'Some data')

def testDefault2(self):
extra = {'PATH_INFO': '/folder/item2/view'}
Expand Down

0 comments on commit 39d67bc

Please sign in to comment.