[Python-checkins] cpython (merge 3.3 -> default): merge from 3.3

senthil.kumaran python-checkins at python.org
Sun Jan 12 07:22:35 CET 2014


http://hg.python.org/cpython/rev/1638360eea41
changeset:   88417:1638360eea41
parent:      88414:90cd87f64632
parent:      88416:a3e49868cfd0
user:        Senthil Kumaran <senthil at uthcode.com>
date:        Sat Jan 11 22:22:21 2014 -0800
summary:
  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.

files:
  Lib/cgi.py           |  14 ++++++++++++--
  Lib/test/test_cgi.py |   7 +++++++
  Misc/NEWS            |   7 +++++++
  3 files changed, 26 insertions(+), 2 deletions(-)


diff --git a/Lib/cgi.py b/Lib/cgi.py
--- a/Lib/cgi.py
+++ b/Lib/cgi.py
@@ -32,10 +32,12 @@
 # =======
 
 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
@@ -472,18 +474,24 @@
                 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__)
@@ -642,7 +650,9 @@
         """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):
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
@@ -137,6 +137,13 @@
         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():
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -25,7 +25,14 @@
 Library
 -------
 
+<<<<<<< local
 - Issue #20152: Ported Python/import.c over to Argument Clinic.
+=======
+- Issue #19097: Raise the correct Exception when cgi.FieldStorage is given an
+  Invalid fileobj.
+
+- Issue #20217: Fix build in SCHED_SPORADIC is defined.
+>>>>>>> other
 
 - Issue #13107: argparse and optparse no longer raises an exception when output
   a help on environment with too small COLUMNS.  Based on patch by

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


More information about the Python-checkins mailing list