summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Sutherland <brian@vanguardistas.net>2016-11-04 11:00:12 +0100
committerBrian Sutherland <brian@vanguardistas.net>2016-11-04 11:00:12 +0100
commit368016697cdd0c150c811658b1a3fc5701af6e96 (patch)
tree780009c32f3032b0f1069b46669255469248f106
parentc1ba72f6a3838269badc433ba9f06a4f1a0eabf2 (diff)
downloadzope-publisher-368016697cdd0c150c811658b1a3fc5701af6e96.tar.gz
Fix file uploads on python 3.4 and up.fix-file-uploads-python-3.4
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.
-rw-r--r--CHANGES.rst9
-rw-r--r--src/zope/publisher/browser.py8
-rw-r--r--src/zope/publisher/tests/test_browserrequest.py2
3 files changed, 18 insertions, 1 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 016fdcf..4ce1647 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -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)
diff --git a/src/zope/publisher/browser.py b/src/zope/publisher/browser.py
index 8395a2e..710793c 100644
--- a/src/zope/publisher/browser.py
+++ b/src/zope/publisher/browser.py
@@ -302,6 +302,14 @@ class BrowserRequest(HTTPRequest):
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:
diff --git a/src/zope/publisher/tests/test_browserrequest.py b/src/zope/publisher/tests/test_browserrequest.py
index 250f93c..767bebb7 100644
--- a/src/zope/publisher/tests/test_browserrequest.py
+++ b/src/zope/publisher/tests/test_browserrequest.py
@@ -217,6 +217,8 @@ class BrowserTests(HTTPTests):
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'}