File tree Expand file tree Collapse file tree 5 files changed +35
-6
lines changed Expand file tree Collapse file tree 5 files changed +35
-6
lines changed Original file line number Diff line number Diff line change @@ -157,6 +157,9 @@ return bytes)::
157
157
if not line: break
158
158
linecount = linecount + 1
159
159
160
+ :class: `FieldStorage ` objects also support being used in a :keyword: `with `
161
+ statement, which will automatically close them when done.
162
+
160
163
If an error is encountered when obtaining the contents of an uploaded file
161
164
(for example, when the user interrupts the form submission by clicking on
162
165
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
182
185
The :attr: `~FieldStorage.file ` attribute is automatically closed upon the
183
186
garbage collection of the creating :class: `FieldStorage ` instance.
184
187
188
+ .. versionchanged :: 3.5
189
+ Added support for the context management protocol to the
190
+ :class: `FieldStorage ` class.
191
+
185
192
186
193
Higher Level Interface
187
194
----------------------
Original file line number Diff line number Diff line change @@ -136,6 +136,12 @@ New Modules
136
136
Improved Modules
137
137
================
138
138
139
+ cgi
140
+ ---
141
+
142
+ * :class: `FieldStorage ` now supports the context management protocol.
143
+ (Contributed by Berker Peksag in :issue: `20289 `.)
144
+
139
145
code
140
146
----
141
147
Original file line number Diff line number Diff line change @@ -566,6 +566,12 @@ def __del__(self):
566
566
except AttributeError :
567
567
pass
568
568
569
+ def __enter__ (self ):
570
+ return self
571
+
572
+ def __exit__ (self , * args ):
573
+ self .file .close ()
574
+
569
575
def __repr__ (self ):
570
576
"""Return a printable representation."""
571
577
return "FieldStorage(%r, %r, %r)" % (
Original file line number Diff line number Diff line change 1
- from test .support import run_unittest , check_warnings
1
+ from test .support import check_warnings
2
2
import cgi
3
3
import os
4
4
import sys
@@ -307,6 +307,17 @@ def test_fieldstorage_multipart_w3c(self):
307
307
got = getattr (files [x ], k )
308
308
self .assertEqual (got , exp )
309
309
310
+ def test_fieldstorage_as_context_manager (self ):
311
+ fp = BytesIO (b'x' * 10 )
312
+ env = {'REQUEST_METHOD' : 'PUT' }
313
+ with cgi .FieldStorage (fp = fp , environ = env ) as fs :
314
+ content = fs .file .read ()
315
+ self .assertFalse (fs .file .closed )
316
+ self .assertTrue (fs .file .closed )
317
+ self .assertEqual (content , 'x' * 10 )
318
+ with self .assertRaisesRegex (ValueError , 'I/O operation on closed file' ):
319
+ fs .file .read ()
320
+
310
321
_qs_result = {
311
322
'key1' : 'value1' ,
312
323
'key2' : ['value2x' , 'value2y' ],
@@ -481,9 +492,5 @@ def test_parse_header(self):
481
492
--AaB03x--
482
493
"""
483
494
484
-
485
- def test_main ():
486
- run_unittest (CgiTests )
487
-
488
495
if __name__ == '__main__' :
489
- test_main ()
496
+ unittest . main ()
Original file line number Diff line number Diff line change @@ -235,6 +235,9 @@ Core and Builtins
235
235
Library
236
236
-------
237
237
238
+ - Issue #20289: cgi.FieldStorage() now supports the context management
239
+ protocol.
240
+
238
241
- Issue #13128: Print response headers for CONNECT requests when debuglevel
239
242
> 0. Patch by Demian Brecht.
240
243
You can’t perform that action at this time.
0 commit comments