Skip to content

Commit 8c140a3

Browse files
authored
HTTPRequest: close file uploads to prevent ResourceWarning (#1243)
* testHTTPRequest: use larger file for test_processInputs_w_large_input_gets_tempfile This test was not using a large enough file to use temporary file, but the test was not really asserting that a temporary file was used. * HTTPRequest: close file uploads to prevent ResourceWarning
1 parent 7802542 commit 8c140a3

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ https://github.com/zopefoundation/Zope/blob/4.x/CHANGES.rst
1010
5.11.2 (unreleased)
1111
-------------------
1212

13+
- Fix a ``ResourceWarning`` emitted when uploading large files.
14+
(`#1242 <https://github.com/zopefoundation/Zope/issues/1242>`_)
15+
1316
- OFS/cachable: fix *Cache this object using* label in ZMI.
1417

1518
- Include versions constraints for production and non-production dependencies

src/ZPublisher/HTTPRequest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ def clear(self):
192192
self.stdin = None
193193
self._file = None
194194
self._fs = None
195+
for f in self.form.values():
196+
if isinstance(f, FileUpload):
197+
f.close()
195198
self.form.clear()
196199
# we want to clear the lazy dict here because BaseRequests don't have
197200
# one. Without this, there's the possibility of memory leaking

src/ZPublisher/tests/testHTTPRequest.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -901,8 +901,10 @@ def test_processInputs_w_large_input_gets_tempfile(self):
901901
req.processInputs()
902902
f = req.form.get('largefile')
903903
self.assertTrue(f.name)
904-
self.assertEqual(4006, len(f.file.read()))
905-
f.file.close()
904+
self.assertEqual(40006, len(f.file.read()))
905+
self.assertTrue(f.file.fileno())
906+
req.clear()
907+
self.assertTrue(f.file.closed)
906908

907909
def test_processInputs_with_file_upload_gets_iterator(self):
908910
# checks fileupload object supports the iterator protocol
@@ -1640,7 +1642,7 @@ def __init__(self, file):
16401642
test %s
16411643
16421644
--12345--
1643-
''' % (b'test' * 1000)
1645+
''' % (b'test' * 10000)
16441646

16451647
TEST_ISSUE_1095_DATA = b'''
16461648
--12345

0 commit comments

Comments
 (0)