# HG changeset patch # Parent 1137f7021b9503ea0cb9e2e9a2b5d8cda200b959 diff -r 1137f7021b95 Lib/cgi.py --- a/Lib/cgi.py Sat Jun 25 12:13:40 2011 +0200 +++ b/Lib/cgi.py Mon Jul 04 12:06:46 2011 +0200 @@ -214,17 +214,17 @@ """ import http.client - boundary = "" + boundary = b"" if 'boundary' in pdict: boundary = pdict['boundary'] if not valid_boundary(boundary): raise ValueError('Invalid boundary in multipart form: %r' % (boundary,)) - nextpart = "--" + boundary - lastpart = "--" + boundary + "--" + nextpart = b"--" + boundary + lastpart = b"--" + boundary + b"--" partdict = {} - terminator = "" + terminator = b"" while terminator != lastpart: bytes = -1 @@ -243,7 +243,7 @@ raise ValueError('Maximum content length exceeded') data = fp.read(bytes) else: - data = "" + data = b"" # Read lines until end of part. lines = [] while 1: @@ -251,7 +251,7 @@ if not line: terminator = lastpart # End outer loop break - if line.startswith("--"): + if line.startswith(b"--"): terminator = line.rstrip() if terminator in (nextpart, lastpart): break @@ -263,12 +263,12 @@ if lines: # Strip final line terminator line = lines[-1] - if line[-2:] == "\r\n": + if line[-2:] == b"\r\n": line = line[:-2] - elif line[-1:] == "\n": + elif line[-1:] == b"\n": line = line[:-1] lines[-1] = line - data = "".join(lines) + data = b"".join(lines) line = headers['content-disposition'] if not line: continue diff -r 1137f7021b95 Lib/test/test_cgi.py --- a/Lib/test/test_cgi.py Sat Jun 25 12:13:40 2011 +0200 +++ b/Lib/test/test_cgi.py Mon Jul 04 12:06:46 2011 +0200 @@ -1,5 +1,6 @@ from test.support import run_unittest, check_warnings import cgi +from collections import namedtuple import os import sys import tempfile @@ -118,6 +119,28 @@ class CgiTests(unittest.TestCase): + def test_parse_multipart(self): + fp = BytesIO(POSTDATA.encode('latin-1')) + env = {'boundary': BOUNDARY.encode('latin-1'), + 'CONTENT_LENGTH':'558'} + result = cgi.parse_multipart(fp, env) + expected = {'submit': [b' Add '], 'id': [b'1234'], 'file': [b'Testing 123.\n'], 'title': [b'']} + self.assertEqual(result, expected) + + def test_field_storage_repr(self): + fs = cgi.FieldStorage() + self.assertIn("FieldStorage", repr(fs)) + + def test_field_storage_iter(self): + fs = cgi.FieldStorage() + self.assertEqual(list(fs), list(fs.keys())) + + def test_field_storage_nonzero(self): + fs = cgi.FieldStorage() + self.assertFalse(bool(fs)) + fs.list.append(namedtuple('fakeitem', 'name')('foo')) + self.assertTrue(bool(fs)) + def test_strict(self): for orig, expect in parse_strict_test_cases: # Test basic parsing @@ -147,6 +170,9 @@ def test_log(self): cgi.log("Testing") + # init log should not throw an exception when logfile can't be opened + cgi.logfile = "fail/" + cgi.initlog("%s", "Testing initlog") cgi.logfp = StringIO() cgi.initlog("%s", "Testing initlog 1") cgi.log("%s", "Testing log 2")