bpo-27777: cgi.FieldStorage can't parse simple body with Cont… by ar45 · Pull Request #11764 · python/cpython (original) (raw)
The test added in PR #10771 does not pass with this PR:
def test_content_length_no_content_disposition(self):
body = b'{"test":123}'
env = {
'CONTENT_LENGTH': len(body),
'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': 'application/json',
'wsgi.input': BytesIO(body),
}
form = cgi.FieldStorage(fp=env['wsgi.input'], environ=env)
self.assertEqual(form.file.read(), body.decode(form.encoding))
The tests fails with
Traceback (most recent call last):
File "/.../myfile.py", line 17, in test_content_length_no_content_disposition
form = cgi.FieldStorage(fp=env['wsgi.input'], environ=env)
File "/mypydistrib/cgi.py", line 474, in __init__
self.read_single()
File "/mypydistrib/cgi.py", line 665, in read_single
self.read_content()
File "/mypydistrib/cgi.py", line 687, in read_content
self.__write(data)
File "/mypydistrib/cgi.py", line 702, in __write
if self.__file is not None:
File "/mypydistrib/cgi.py", line 498, in __getattr__
raise AttributeError(name)
AttributeError: _FieldStorage__file
The attr self.__file
must be defined before calling self.__write
. Adding self.__file = self.file
to read_content
(as it is done in read_lines
) fixes the issue:
def read_content(self):
if self.length > 1000:
self.file = self.make_file()
else:
self.file = self.io_object()
self.__file = self.file #line added here