diff --git a/Lib/cgi.py b/Lib/cgi.py index b96bd1f0fe39ac..6e54c02a88896e 100755 --- a/Lib/cgi.py +++ b/Lib/cgi.py @@ -483,7 +483,10 @@ def __enter__(self): return self def __exit__(self, *args): - self.file.close() + try: + self.file.close() + except AttributeError: + pass def __repr__(self): """Return a printable representation.""" diff --git a/Lib/test/test_cgi.py b/Lib/test/test_cgi.py index 0922555982596c..2dc2966cc68776 100644 --- a/Lib/test/test_cgi.py +++ b/Lib/test/test_cgi.py @@ -363,6 +363,11 @@ def test_fieldstorage_as_context_manager(self): with self.assertRaisesRegex(ValueError, 'I/O operation on closed file'): fs.file.read() + def test_fieldstorage_exit_context(self): + fs = cgi.FieldStorage() + with fs: + fs.bytes_read = 0 + _qs_result = { 'key1': 'value1', 'key2': ['value2x', 'value2y'], diff --git a/Misc/NEWS.d/next/Library/2019-07-17-15-52-19.bpo-37560.S4BYfK.rst b/Misc/NEWS.d/next/Library/2019-07-17-15-52-19.bpo-37560.S4BYfK.rst new file mode 100644 index 00000000000000..f580a7a8d57647 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-07-17-15-52-19.bpo-37560.S4BYfK.rst @@ -0,0 +1 @@ +Add exception handler for :class:`FieldStorage` in :mod:`cgi.FieldStorage` at method `__exit__`