Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d87346c

Browse files
committed
merge from 3.3
Issue #19092 - Raise a correct exception when cgi.FieldStorage is given an invalid file-obj. Also use __bool__ to determine the bool of the FieldStorage object.
2 parents 932c345 + b4cbb92 commit d87346c

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

Lib/cgi.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@
3232
# =======
3333

3434
from io import StringIO, BytesIO, TextIOWrapper
35+
from collections import Mapping
3536
import sys
3637
import os
3738
import urllib.parse
3839
from email.parser import FeedParser
40+
from email.message import Message
3941
from warnings import warn
4042
import html
4143
import locale
@@ -472,18 +474,24 @@ def __init__(self, fp=None, headers=None, outerboundary=b'',
472474
self.qs_on_post = environ['QUERY_STRING']
473475
if 'CONTENT_LENGTH' in environ:
474476
headers['content-length'] = environ['CONTENT_LENGTH']
477+
else:
478+
if not (isinstance(headers, (Mapping, Message))):
479+
raise TypeError("headers must be mapping or an instance of "
480+
"email.message.Message")
481+
self.headers = headers
475482
if fp is None:
476483
self.fp = sys.stdin.buffer
477484
# self.fp.read() must return bytes
478485
elif isinstance(fp, TextIOWrapper):
479486
self.fp = fp.buffer
480487
else:
488+
if not (hasattr(fp, 'read') and hasattr(fp, 'readline')):
489+
raise TypeError("fp must be file pointer")
481490
self.fp = fp
482491

483492
self.encoding = encoding
484493
self.errors = errors
485494

486-
self.headers = headers
487495
if not isinstance(outerboundary, bytes):
488496
raise TypeError('outerboundary must be bytes, not %s'
489497
% type(outerboundary).__name__)
@@ -642,7 +650,9 @@ def __len__(self):
642650
"""Dictionary style len(x) support."""
643651
return len(self.keys())
644652

645-
def __nonzero__(self):
653+
def __bool__(self):
654+
if self.list is None:
655+
raise TypeError("Cannot be converted to bool.")
646656
return bool(self.list)
647657

648658
def read_urlencoded(self):

Lib/test/test_cgi.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ def test_fieldstorage_properties(self):
137137
fs.list.append(namedtuple('MockFieldStorage', 'name')('fieldvalue'))
138138
self.assertTrue(fs)
139139

140+
def test_fieldstorage_invalid(self):
141+
self.assertRaises(TypeError, cgi.FieldStorage, "not-a-file-obj",
142+
environ={"REQUEST_METHOD":"PUT"})
143+
self.assertRaises(TypeError, cgi.FieldStorage, "foo", "bar")
144+
fs = cgi.FieldStorage(headers={'content-type':'text/plain'})
145+
self.assertRaises(TypeError, bool, fs)
146+
140147
def test_escape(self):
141148
# cgi.escape() is deprecated.
142149
with warnings.catch_warnings():

Misc/NEWS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ Core and Builtins
2525
Library
2626
-------
2727

28+
<<<<<<< local
2829
- Issue #20152: Ported Python/import.c over to Argument Clinic.
30+
=======
31+
- Issue #19097: Raise the correct Exception when cgi.FieldStorage is given an
32+
Invalid fileobj.
33+
34+
- Issue #20217: Fix build in SCHED_SPORADIC is defined.
35+
>>>>>>> other
2936

3037
- Issue #13107: argparse and optparse no longer raises an exception when output
3138
a help on environment with too small COLUMNS. Based on patch by

0 commit comments

Comments
 (0)