mirror of https://github.com/python/cpython
Apply SF 562987 modernizing Cookie to subclass from dict instead of UserDict
This commit is contained in:
parent
3a159a8d25
commit
0a2963c797
|
@ -216,7 +216,6 @@ Finis.
|
||||||
# Import our required modules
|
# Import our required modules
|
||||||
#
|
#
|
||||||
import string
|
import string
|
||||||
from UserDict import UserDict
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from cPickle import dumps, loads
|
from cPickle import dumps, loads
|
||||||
|
@ -406,7 +405,7 @@ def _getdate(future=0, weekdayname=_weekdayname, monthname=_monthname):
|
||||||
# pickled for network transit.
|
# pickled for network transit.
|
||||||
#
|
#
|
||||||
|
|
||||||
class Morsel(UserDict):
|
class Morsel(dict):
|
||||||
# RFC 2109 lists these attributes as reserved:
|
# RFC 2109 lists these attributes as reserved:
|
||||||
# path comment domain
|
# path comment domain
|
||||||
# max-age secure version
|
# max-age secure version
|
||||||
|
@ -425,27 +424,25 @@ class Morsel(UserDict):
|
||||||
"secure" : "secure",
|
"secure" : "secure",
|
||||||
"version" : "Version",
|
"version" : "Version",
|
||||||
}
|
}
|
||||||
_reserved_keys = _reserved.keys()
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# Set defaults
|
# Set defaults
|
||||||
self.key = self.value = self.coded_value = None
|
self.key = self.value = self.coded_value = None
|
||||||
UserDict.__init__(self)
|
|
||||||
|
|
||||||
# Set default attributes
|
# Set default attributes
|
||||||
for K in self._reserved_keys:
|
for K in self._reserved:
|
||||||
UserDict.__setitem__(self, K, "")
|
dict.__setitem__(self, K, "")
|
||||||
# end __init__
|
# end __init__
|
||||||
|
|
||||||
def __setitem__(self, K, V):
|
def __setitem__(self, K, V):
|
||||||
K = K.lower()
|
K = K.lower()
|
||||||
if not K in self._reserved_keys:
|
if not K in self._reserved:
|
||||||
raise CookieError("Invalid Attribute %s" % K)
|
raise CookieError("Invalid Attribute %s" % K)
|
||||||
UserDict.__setitem__(self, K, V)
|
dict.__setitem__(self, K, V)
|
||||||
# end __setitem__
|
# end __setitem__
|
||||||
|
|
||||||
def isReservedKey(self, K):
|
def isReservedKey(self, K):
|
||||||
return K.lower() in self._reserved_keys
|
return K.lower() in self._reserved
|
||||||
# end isReservedKey
|
# end isReservedKey
|
||||||
|
|
||||||
def set(self, key, val, coded_val,
|
def set(self, key, val, coded_val,
|
||||||
|
@ -453,7 +450,7 @@ class Morsel(UserDict):
|
||||||
idmap=string._idmap, translate=string.translate ):
|
idmap=string._idmap, translate=string.translate ):
|
||||||
# First we verify that the key isn't a reserved word
|
# First we verify that the key isn't a reserved word
|
||||||
# Second we make sure it only contains legal characters
|
# Second we make sure it only contains legal characters
|
||||||
if key.lower() in self._reserved_keys:
|
if key.lower() in self._reserved:
|
||||||
raise CookieError("Attempt to set a reserved key: %s" % key)
|
raise CookieError("Attempt to set a reserved key: %s" % key)
|
||||||
if "" != translate(key, idmap, LegalChars):
|
if "" != translate(key, idmap, LegalChars):
|
||||||
raise CookieError("Illegal key value: %s" % key)
|
raise CookieError("Illegal key value: %s" % key)
|
||||||
|
@ -495,7 +492,7 @@ class Morsel(UserDict):
|
||||||
|
|
||||||
# Now add any defined attributes
|
# Now add any defined attributes
|
||||||
if attrs is None:
|
if attrs is None:
|
||||||
attrs = self._reserved_keys
|
attrs = self._reserved
|
||||||
items = self.items()
|
items = self.items()
|
||||||
items.sort()
|
items.sort()
|
||||||
for K,V in items:
|
for K,V in items:
|
||||||
|
@ -546,7 +543,7 @@ _CookiePattern = re.compile(
|
||||||
# Using this class is almost just like using a dictionary.
|
# Using this class is almost just like using a dictionary.
|
||||||
# See this module's docstring for example usage.
|
# See this module's docstring for example usage.
|
||||||
#
|
#
|
||||||
class BaseCookie(UserDict):
|
class BaseCookie(dict):
|
||||||
# A container class for a set of Morsels
|
# A container class for a set of Morsels
|
||||||
#
|
#
|
||||||
|
|
||||||
|
@ -571,7 +568,6 @@ class BaseCookie(UserDict):
|
||||||
# end value_encode
|
# end value_encode
|
||||||
|
|
||||||
def __init__(self, input=None):
|
def __init__(self, input=None):
|
||||||
UserDict.__init__(self)
|
|
||||||
if input: self.load(input)
|
if input: self.load(input)
|
||||||
# end __init__
|
# end __init__
|
||||||
|
|
||||||
|
@ -579,7 +575,7 @@ class BaseCookie(UserDict):
|
||||||
"""Private method for setting a cookie's value"""
|
"""Private method for setting a cookie's value"""
|
||||||
M = self.get(key, Morsel())
|
M = self.get(key, Morsel())
|
||||||
M.set(key, real_value, coded_value)
|
M.set(key, real_value, coded_value)
|
||||||
UserDict.__setitem__(self, key, M)
|
dict.__setitem__(self, key, M)
|
||||||
# end __set
|
# end __set
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
|
@ -651,7 +647,7 @@ class BaseCookie(UserDict):
|
||||||
# (Does anyone care?)
|
# (Does anyone care?)
|
||||||
if M:
|
if M:
|
||||||
M[ K[1:] ] = V
|
M[ K[1:] ] = V
|
||||||
elif K.lower() in Morsel._reserved_keys:
|
elif K.lower() in Morsel._reserved:
|
||||||
if M:
|
if M:
|
||||||
M[ K ] = _unquote(V)
|
M[ K ] = _unquote(V)
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue