Skip to content
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

Fix file uploads on python 3.4 and up. #13

Merged
merged 1 commit into from
Nov 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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