mirror of
https://github.com/python/cpython.git
synced 2025-07-24 19:54:21 +00:00
Fix Cookie.py: Fix example in the docstring (encoded SerialCookies contain
unicode now). Fix _quote() and Morsel.set() which were using str8.translate(). As cPickle.dumps() returns bytes now value_encode() and value_decode() methods must encode/decode (however output() might better return a bytes object).
This commit is contained in:
parent
9b775535f8
commit
3f1e65cfde
1 changed files with 10 additions and 12 deletions
|
@ -163,7 +163,7 @@ values, however.)
|
||||||
>>> C["string"].value
|
>>> C["string"].value
|
||||||
'seven'
|
'seven'
|
||||||
>>> C.output().replace('p0', 'p1') # Hack for cPickle/pickle differences
|
>>> C.output().replace('p0', 'p1') # Hack for cPickle/pickle differences
|
||||||
'Set-Cookie: number="I7\\012."\r\nSet-Cookie: string="S\'seven\'\\012p1\\012."'
|
'Set-Cookie: number="I7\\012."\r\nSet-Cookie: string="Vseven\\012p1\\012."'
|
||||||
|
|
||||||
Be warned, however, if SerialCookie cannot de-serialize a value (because
|
Be warned, however, if SerialCookie cannot de-serialize a value (because
|
||||||
it isn't a valid pickle'd object), IT WILL RAISE AN EXCEPTION.
|
it isn't a valid pickle'd object), IT WILL RAISE AN EXCEPTION.
|
||||||
|
@ -305,16 +305,14 @@ _Translator = {
|
||||||
'\375' : '\\375', '\376' : '\\376', '\377' : '\\377'
|
'\375' : '\\375', '\376' : '\\376', '\377' : '\\377'
|
||||||
}
|
}
|
||||||
|
|
||||||
_idmap = ''.join(chr(x) for x in range(256))
|
def _quote(str, LegalChars=_LegalChars):
|
||||||
|
|
||||||
def _quote(str, LegalChars=_LegalChars, idmap=_idmap):
|
|
||||||
#
|
#
|
||||||
# If the string does not need to be double-quoted,
|
# If the string does not need to be double-quoted,
|
||||||
# then just return the string. Otherwise, surround
|
# then just return the string. Otherwise, surround
|
||||||
# the string in doublequotes and precede quote (with a \)
|
# the string in doublequotes and precede quote (with a \)
|
||||||
# special characters.
|
# special characters.
|
||||||
#
|
#
|
||||||
if "" == str.translate(idmap, LegalChars):
|
if len(filter(LegalChars.__contains__, str)) == len(str):
|
||||||
return str
|
return str
|
||||||
else:
|
else:
|
||||||
return '"' + _nulljoin( map(_Translator.get, str, str) ) + '"'
|
return '"' + _nulljoin( map(_Translator.get, str, str) ) + '"'
|
||||||
|
@ -439,12 +437,12 @@ class Morsel(dict):
|
||||||
return K.lower() in self._reserved
|
return K.lower() in self._reserved
|
||||||
# end isReservedKey
|
# end isReservedKey
|
||||||
|
|
||||||
def set(self, key, val, coded_val, LegalChars=_LegalChars, idmap=_idmap):
|
def set(self, key, val, coded_val, LegalChars=_LegalChars):
|
||||||
# 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:
|
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 "" != key.translate(idmap, LegalChars):
|
if len(filter(LegalChars.__contains__, key)) != len(key):
|
||||||
raise CookieError("Illegal key value: %s" % key)
|
raise CookieError("Illegal key value: %s" % key)
|
||||||
|
|
||||||
# It's a good key, so save it.
|
# It's a good key, so save it.
|
||||||
|
@ -680,9 +678,9 @@ class SerialCookie(BaseCookie):
|
||||||
# end __init__
|
# end __init__
|
||||||
def value_decode(self, val):
|
def value_decode(self, val):
|
||||||
# This could raise an exception!
|
# This could raise an exception!
|
||||||
return loads( _unquote(val) ), val
|
return loads( _unquote(val).encode('latin-1') ), val
|
||||||
def value_encode(self, val):
|
def value_encode(self, val):
|
||||||
return val, _quote( dumps(val) )
|
return val, _quote( dumps(val).decode('latin-1') )
|
||||||
# end SerialCookie
|
# end SerialCookie
|
||||||
|
|
||||||
class SmartCookie(BaseCookie):
|
class SmartCookie(BaseCookie):
|
||||||
|
@ -706,14 +704,14 @@ class SmartCookie(BaseCookie):
|
||||||
def value_decode(self, val):
|
def value_decode(self, val):
|
||||||
strval = _unquote(val)
|
strval = _unquote(val)
|
||||||
try:
|
try:
|
||||||
return loads(strval), val
|
return loads(strval.encode('latin-1')), val
|
||||||
except:
|
except:
|
||||||
return strval, val
|
return strval, val
|
||||||
def value_encode(self, val):
|
def value_encode(self, val):
|
||||||
if type(val) == type(""):
|
if isinstance(val, str):
|
||||||
return val, _quote(val)
|
return val, _quote(val)
|
||||||
else:
|
else:
|
||||||
return val, _quote( dumps(val) )
|
return val, _quote( dumps(val).decode('latin-1') )
|
||||||
# end SmartCookie
|
# end SmartCookie
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue