mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Closing patch #101120 -- After everyone agreed.
This commit is contained in:
parent
dc3d606bd9
commit
a1a4b5916b
3 changed files with 76 additions and 61 deletions
46
Lib/cgi.py
46
Lib/cgi.py
|
@ -19,7 +19,7 @@ written in Python.
|
|||
# responsible for its maintenance.
|
||||
#
|
||||
|
||||
__version__ = "2.2"
|
||||
__version__ = "2.3"
|
||||
|
||||
|
||||
# Imports
|
||||
|
@ -31,6 +31,7 @@ import os
|
|||
import urllib
|
||||
import mimetools
|
||||
import rfc822
|
||||
import UserDict
|
||||
from StringIO import StringIO
|
||||
|
||||
|
||||
|
@ -166,11 +167,10 @@ def parse_qs(qs, keep_blank_values=0, strict_parsing=0):
|
|||
"""
|
||||
dict = {}
|
||||
for name, value in parse_qsl(qs, keep_blank_values, strict_parsing):
|
||||
if len(value) or keep_blank_values:
|
||||
if dict.has_key(name):
|
||||
dict[name].append(value)
|
||||
else:
|
||||
dict[name] = [value]
|
||||
if dict.has_key(name):
|
||||
dict[name].append(value)
|
||||
else:
|
||||
dict[name] = [value]
|
||||
return dict
|
||||
|
||||
def parse_qsl(qs, keep_blank_values=0, strict_parsing=0):
|
||||
|
@ -201,9 +201,10 @@ def parse_qsl(qs, keep_blank_values=0, strict_parsing=0):
|
|||
if strict_parsing:
|
||||
raise ValueError, "bad query field: %s" % `name_value`
|
||||
continue
|
||||
name = urllib.unquote(string.replace(nv[0], '+', ' '))
|
||||
value = urllib.unquote(string.replace(nv[1], '+', ' '))
|
||||
r.append((name, value))
|
||||
if len(nv[1]) or keep_blank_values:
|
||||
name = urllib.unquote(string.replace(nv[0], '+', ' '))
|
||||
value = urllib.unquote(string.replace(nv[1], '+', ' '))
|
||||
r.append((name, value))
|
||||
|
||||
return r
|
||||
|
||||
|
@ -537,6 +538,17 @@ class FieldStorage:
|
|||
else:
|
||||
return found
|
||||
|
||||
def getvalue(self, key, default=None):
|
||||
"""Dictionary style get() method, including 'value' lookup."""
|
||||
if self.has_key(key):
|
||||
value = self[key]
|
||||
if type(value) is type([]):
|
||||
return map(lambda v: v.value, value)
|
||||
else:
|
||||
return value.value
|
||||
else:
|
||||
return default
|
||||
|
||||
def keys(self):
|
||||
"""Dictionary style keys() method."""
|
||||
if self.list is None:
|
||||
|
@ -706,7 +718,7 @@ class FieldStorage:
|
|||
# Backwards Compatibility Classes
|
||||
# ===============================
|
||||
|
||||
class FormContentDict:
|
||||
class FormContentDict(UserDict.UserDict):
|
||||
"""Basic (multiple values per field) form content as dictionary.
|
||||
|
||||
form = FormContentDict()
|
||||
|
@ -720,20 +732,8 @@ class FormContentDict:
|
|||
|
||||
"""
|
||||
def __init__(self, environ=os.environ):
|
||||
self.dict = parse(environ=environ)
|
||||
self.dict = self.data = parse(environ=environ)
|
||||
self.query_string = environ['QUERY_STRING']
|
||||
def __getitem__(self,key):
|
||||
return self.dict[key]
|
||||
def keys(self):
|
||||
return self.dict.keys()
|
||||
def has_key(self, key):
|
||||
return self.dict.has_key(key)
|
||||
def values(self):
|
||||
return self.dict.values()
|
||||
def items(self):
|
||||
return self.dict.items()
|
||||
def __len__( self ):
|
||||
return len(self.dict)
|
||||
|
||||
|
||||
class SvFormContentDict(FormContentDict):
|
||||
|
|
|
@ -116,19 +116,27 @@ def main():
|
|||
d = do_test(orig, "POST")
|
||||
assert d == expect, "Error parsing %s" % repr(orig)
|
||||
|
||||
d = {'QUERY_STRING': orig}
|
||||
fcd = cgi.FormContentDict(d)
|
||||
sd = cgi.SvFormContentDict(d)
|
||||
env = {'QUERY_STRING': orig}
|
||||
fcd = cgi.FormContentDict(env)
|
||||
sd = cgi.SvFormContentDict(env)
|
||||
fs = cgi.FieldStorage(environ=env)
|
||||
if type(expect) == type({}):
|
||||
# test dict interface
|
||||
assert len(expect) == len(fcd)
|
||||
assert norm(expect.keys()) == norm(fcd.keys())
|
||||
assert norm(expect.values()) == norm(fcd.values())
|
||||
assert norm(expect.items()) == norm(fcd.items())
|
||||
assert fcd.get("nonexistent field", "default") == "default"
|
||||
assert len(sd) == len(fs)
|
||||
assert norm(sd.keys()) == norm(fs.keys())
|
||||
assert fs.getvalue("nonexistent field", "default") == "default"
|
||||
# test individual fields
|
||||
for key in expect.keys():
|
||||
expect_val = expect[key]
|
||||
assert fcd.has_key(key)
|
||||
assert norm(fcd[key]) == norm(expect[key])
|
||||
assert fcd.get(key, "default") == fcd[key]
|
||||
assert fs.has_key(key)
|
||||
if len(expect_val) > 1:
|
||||
single_value = 0
|
||||
else:
|
||||
|
@ -137,9 +145,11 @@ def main():
|
|||
val = sd[key]
|
||||
except IndexError:
|
||||
assert not single_value
|
||||
assert fs.getvalue(key) == expect_val
|
||||
else:
|
||||
assert single_value
|
||||
assert val == expect_val[0]
|
||||
assert fs.getvalue(key) == expect_val[0]
|
||||
assert norm(sd.getlist(key)) == norm(expect_val)
|
||||
if single_value:
|
||||
assert norm(sd.values()) == \
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue