# =======
from io import StringIO, BytesIO, TextIOWrapper
+from collections import Mapping
import sys
import os
import urllib.parse
from email.parser import FeedParser
+from email.message import Message
from warnings import warn
import html
import locale
self.qs_on_post = environ['QUERY_STRING']
if 'CONTENT_LENGTH' in environ:
headers['content-length'] = environ['CONTENT_LENGTH']
+ else:
+ if not (isinstance(headers, (Mapping, Message))):
+ raise TypeError("headers must be mapping or an instance of "
+ "email.message.Message")
+ self.headers = headers
if fp is None:
self.fp = sys.stdin.buffer
# self.fp.read() must return bytes
elif isinstance(fp, TextIOWrapper):
self.fp = fp.buffer
else:
+ if not (hasattr(fp, 'read') and hasattr(fp, 'readline')):
+ raise TypeError("fp must be file pointer")
self.fp = fp
self.encoding = encoding
self.errors = errors
- self.headers = headers
if not isinstance(outerboundary, bytes):
raise TypeError('outerboundary must be bytes, not %s'
% type(outerboundary).__name__)
"""Dictionary style len(x) support."""
return len(self.keys())
- def __nonzero__(self):
+ def __bool__(self):
+ if self.list is None:
+ raise TypeError("Cannot be converted to bool.")
return bool(self.list)
def read_urlencoded(self):
fs.list.append(namedtuple('MockFieldStorage', 'name')('fieldvalue'))
self.assertTrue(fs)
+ def test_fieldstorage_invalid(self):
+ self.assertRaises(TypeError, cgi.FieldStorage, "not-a-file-obj",
+ environ={"REQUEST_METHOD":"PUT"})
+ self.assertRaises(TypeError, cgi.FieldStorage, "foo", "bar")
+ fs = cgi.FieldStorage(headers={'content-type':'text/plain'})
+ self.assertRaises(TypeError, bool, fs)
+
def test_escape(self):
# cgi.escape() is deprecated.
with warnings.catch_warnings():