]> granicus.if.org Git - python/commitdiff
Don't return spurious empty fields if 'keep_empty_values' is True.
authorNeil Schemenauer <nascheme@enme.ucalgary.ca>
Mon, 19 Jul 2004 15:38:11 +0000 (15:38 +0000)
committerNeil Schemenauer <nascheme@enme.ucalgary.ca>
Mon, 19 Jul 2004 15:38:11 +0000 (15:38 +0000)
Fixes SF bug #990307.

Lib/cgi.py
Lib/test/output/test_cgi
Lib/test/test_cgi.py
Misc/NEWS

index 487b01e3f2ebb92e9ab90c82a5261a84763a1d0e..7c3d6579434ff2cd78db0593d3630395a9456003 100755 (executable)
@@ -209,6 +209,8 @@ def parse_qsl(qs, keep_blank_values=0, strict_parsing=0):
     pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
     r = []
     for name_value in pairs:
+        if not name_value and not strict_parsing:
+            continue
         nv = name_value.split('=', 1)
         if len(nv) != 2:
             if strict_parsing:
index 3741c22d01544f09003001a6adb0b565829984e8..d5d6f75c37679516d04ec54d5b620e24a1b342c2 100644 (file)
@@ -1,4 +1,15 @@
 test_cgi
+'' => []
+'&' => []
+'&&' => []
+'=' => [('', '')]
+'=a' => [('', 'a')]
+'a' => [('a', '')]
+'a=' => [('a', '')]
+'a=' => [('a', '')]
+'&a=b' => [('a', 'b')]
+'a=a+b&b=b+c' => [('a', 'a b'), ('b', 'b c')]
+'a=1&a=2' => [('a', '1'), ('a', '2')]
 ''
 '&'
 '&&'
index 980e3b648640183a3803da07b2d9623dcc1b8012..5ecaf7f120cfe38f9d6e1b28626d38f699a26a3f 100644 (file)
@@ -55,7 +55,21 @@ def do_test(buf, method):
 # A list of test cases.  Each test case is a a two-tuple that contains
 # a string with the query and a dictionary with the expected result.
 
-parse_test_cases = [
+parse_qsl_test_cases = [
+    ("", []),
+    ("&", []),
+    ("&&", []),
+    ("=", [('', '')]),
+    ("=a", [('', 'a')]),
+    ("a", [('a', '')]),
+    ("a=", [('a', '')]),
+    ("a=", [('a', '')]),
+    ("&a=b", [('a', 'b')]),
+    ("a=a+b&b=b+c", [('a', 'a b'), ('b', 'b c')]),
+    ("a=1&a=2", [('a', '1'), ('a', '2')]),
+]
+
+parse_strict_test_cases = [
     ("", ValueError("bad query field: ''")),
     ("&", ValueError("bad query field: ''")),
     ("&&", ValueError("bad query field: ''")),
@@ -114,7 +128,12 @@ def first_second_elts(list):
     return map(lambda p:(p[0], p[1][0]), list)
 
 def main():
-    for orig, expect in parse_test_cases:
+    for orig, expect in parse_qsl_test_cases:
+        result = cgi.parse_qsl(orig, keep_blank_values=True)
+        print repr(orig), '=>', result
+        verify(result == expect, "Error parsing %s" % repr(orig))
+
+    for orig, expect in parse_strict_test_cases:
         # Test basic parsing
         print repr(orig)
         d = do_test(orig, "GET")
index cd34d13a8bc8f5f9e52930f95124ac2adde441fe..29adb457b90be8ea0249bd6a3d2aa65058380094 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -94,6 +94,10 @@ Library
 - The threading module has a new class, local, for creating objects
   that provide thread-local data.
 
+- Bug #990307: when keep_empty_values is True, cgi.parse_qsl()
+  no longer returns spurious empty fields.
+
+
 Tools/Demos
 -----------