[Python-checkins] cpython: Issue #20289: cgi.FieldStorage() now supports the context management protocol.

berker.peksag python-checkins at python.org
Fri Feb 6 09:22:43 CET 2015


https://hg.python.org/cpython/rev/367f5e98ffbb
changeset:   94539:367f5e98ffbb
user:        Berker Peksag <berker.peksag at gmail.com>
date:        Fri Feb 06 10:21:37 2015 +0200
summary:
  Issue #20289: cgi.FieldStorage() now supports the context management protocol.

files:
  Doc/library/cgi.rst  |   7 +++++++
  Doc/whatsnew/3.5.rst |   6 ++++++
  Lib/cgi.py           |   6 ++++++
  Lib/test/test_cgi.py |  19 +++++++++++++------
  Misc/NEWS            |   3 +++
  5 files changed, 35 insertions(+), 6 deletions(-)


diff --git a/Doc/library/cgi.rst b/Doc/library/cgi.rst
--- a/Doc/library/cgi.rst
+++ b/Doc/library/cgi.rst
@@ -157,6 +157,9 @@
            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 @@
    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
 ----------------------
diff --git a/Doc/whatsnew/3.5.rst b/Doc/whatsnew/3.5.rst
--- a/Doc/whatsnew/3.5.rst
+++ b/Doc/whatsnew/3.5.rst
@@ -136,6 +136,12 @@
 Improved Modules
 ================
 
+cgi
+---
+
+* :class:`FieldStorage` now supports the context management protocol.
+  (Contributed by Berker Peksag in :issue:`20289`.)
+
 code
 ----
 
diff --git a/Lib/cgi.py b/Lib/cgi.py
--- a/Lib/cgi.py
+++ b/Lib/cgi.py
@@ -566,6 +566,12 @@
         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)" % (
diff --git a/Lib/test/test_cgi.py b/Lib/test/test_cgi.py
--- a/Lib/test/test_cgi.py
+++ b/Lib/test/test_cgi.py
@@ -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 @@
                 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 @@
 --AaB03x--
 """
 
-
-def test_main():
-    run_unittest(CgiTests)
-
 if __name__ == '__main__':
-    test_main()
+    unittest.main()
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -235,6 +235,9 @@
 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.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list