Skip to content

Commit

Permalink
FileUpload: use latin-1 as charset when nothing is passed.
Browse files Browse the repository at this point in the history
Without this change, some tests (and possibly live code) in Plone fail because they do not pass the new required `charset` parameter.
See plone/buildout.coredev#844
And see discussion that I just started here:
#1094 (comment)

Also fixes an unclosed file in a test.
  • Loading branch information
mauritsvanrees committed Mar 7, 2023
1 parent 3b69a1e commit 2ba5dc8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/ZPublisher/HTTPRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1483,12 +1483,17 @@ class FileUpload:
# that protected code can use DTML to work with FileUploads.
__allow_access_to_unprotected_subobjects__ = 1

def __init__(self, aFieldStorage, charset):
def __init__(self, aFieldStorage, charset="latin-1"):
self.file = aFieldStorage.file
self.headers = aFieldStorage.headers
self.filename = aFieldStorage.filename\
.encode("latin-1").decode(charset)
self.name = aFieldStorage.name.encode("latin-1").decode(charset)
# Prevent needless encode-decode when both charsets are the same.
if charset != "latin-1":
self.filename = aFieldStorage.filename\
.encode("latin-1").decode(charset)
self.name = aFieldStorage.name.encode("latin-1").decode(charset)
else:
self.filename = aFieldStorage.filename
self.name = aFieldStorage.name

# Add an assertion to the rfc822.Message object that implements
# self.headers so that managed code can access them.
Expand Down
1 change: 1 addition & 0 deletions src/ZPublisher/tests/testHTTPRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,7 @@ def test_processInputs_w_large_input_gets_tempfile(self):
f = req.form.get('largefile')
self.assertTrue(f.name)
self.assertEqual(4006, len(f.file.read()))
f.file.close()

def test_processInputs_with_file_upload_gets_iterator(self):
# checks fileupload object supports the iterator protocol
Expand Down

0 comments on commit 2ba5dc8

Please sign in to comment.