]> granicus.if.org Git - python/commitdiff
Issue #20289: cgi.FieldStorage() now supports the context management protocol.
authorBerker Peksag <berker.peksag@gmail.com>
Fri, 6 Feb 2015 08:21:37 +0000 (10:21 +0200)
committerBerker Peksag <berker.peksag@gmail.com>
Fri, 6 Feb 2015 08:21:37 +0000 (10:21 +0200)
Doc/library/cgi.rst
Doc/whatsnew/3.5.rst
Lib/cgi.py
Lib/test/test_cgi.py
Misc/NEWS

index fa131453c790b8f6eeec104f58816824c34e1923..0a9e88428026b7379f3314fe28ac672063001255 100644 (file)
@@ -157,6 +157,9 @@ return bytes)::
            if not line: break
            linecount = linecount + 1
 
+:class:`FieldStorage` objects also support being used in a :keyword:`with`
+statement, which will automatically close them when done.
+
 If an error is encountered when obtaining the contents of an uploaded file
 (for example, when the user interrupts the form submission by clicking on
 a Back or Cancel button) the :attr:`~FieldStorage.done` attribute of the
@@ -182,6 +185,10 @@ A form submitted via POST that also has a query string will contain both
    The :attr:`~FieldStorage.file` attribute is automatically closed upon the
    garbage collection of the creating :class:`FieldStorage` instance.
 
+.. versionchanged:: 3.5
+   Added support for the context management protocol to the
+   :class:`FieldStorage` class.
+
 
 Higher Level Interface
 ----------------------
index 979d24186ab5ec1904f8db3d18df8f261c62dbd3..461a14b16fdea539ad4d72218f4de2b278ce84b1 100644 (file)
@@ -136,6 +136,12 @@ New Modules
 Improved Modules
 ================
 
+cgi
+---
+
+* :class:`FieldStorage` now supports the context management protocol.
+  (Contributed by Berker Peksag in :issue:`20289`.)
+
 code
 ----
 
index 1ef780c11c4a88eb1a3eb325f13ed6c0722f762d..a55232eb62d5eed3f301e69d8b4d484a8c43e1e9 100755 (executable)
@@ -566,6 +566,12 @@ class FieldStorage:
         except AttributeError:
             pass
 
+    def __enter__(self):
+        return self
+
+    def __exit__(self, *args):
+        self.file.close()
+
     def __repr__(self):
         """Return a printable representation."""
         return "FieldStorage(%r, %r, %r)" % (
index 86e1f3a5f01cb0b500466f581f1357a727275532..89a6e84df9d2b9d91865315aa3355f68bb74570f 100644 (file)
@@ -1,4 +1,4 @@
-from test.support import run_unittest, check_warnings
+from test.support import check_warnings
 import cgi
 import os
 import sys
@@ -307,6 +307,17 @@ Content-Type: text/plain
                 got = getattr(files[x], k)
                 self.assertEqual(got, exp)
 
+    def test_fieldstorage_as_context_manager(self):
+        fp = BytesIO(b'x' * 10)
+        env = {'REQUEST_METHOD': 'PUT'}
+        with cgi.FieldStorage(fp=fp, environ=env) as fs:
+            content = fs.file.read()
+            self.assertFalse(fs.file.closed)
+        self.assertTrue(fs.file.closed)
+        self.assertEqual(content, 'x' * 10)
+        with self.assertRaisesRegex(ValueError, 'I/O operation on closed file'):
+            fs.file.read()
+
     _qs_result = {
         'key1': 'value1',
         'key2': ['value2x', 'value2y'],
@@ -481,9 +492,5 @@ Content-Transfer-Encoding: binary
 --AaB03x--
 """
 
-
-def test_main():
-    run_unittest(CgiTests)
-
 if __name__ == '__main__':
-    test_main()
+    unittest.main()
index 25ea656b53929b63ba414bdf4c0bc7e3610fb3de..6e7cf75592cca62f736af26a14d01f9300f58d1b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -235,6 +235,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #20289: cgi.FieldStorage() now supports the context management
+  protocol.
+
 - Issue #13128: Print response headers for CONNECT requests when debuglevel
   > 0. Patch by Demian Brecht.