Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3f1e65c

Browse files
committed
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).
1 parent 9b77553 commit 3f1e65c

1 file changed

Lines changed: 10 additions & 12 deletions

File tree

Lib/Cookie.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@
163163
>>> C["string"].value
164164
'seven'
165165
>>> C.output().replace('p0', 'p1') # Hack for cPickle/pickle differences
166-
'Set-Cookie: number="I7\\012."\r\nSet-Cookie: string="S\'seven\'\\012p1\\012."'
166+
'Set-Cookie: number="I7\\012."\r\nSet-Cookie: string="Vseven\\012p1\\012."'
167167
168168
Be warned, however, if SerialCookie cannot de-serialize a value (because
169169
it isn't a valid pickle'd object), IT WILL RAISE AN EXCEPTION.
@@ -305,16 +305,14 @@ class CookieError(Exception):
305305
'\375' : '\\375', '\376' : '\\376', '\377' : '\\377'
306306
}
307307

308-
_idmap = ''.join(chr(x) for x in range(256))
309-
310-
def _quote(str, LegalChars=_LegalChars, idmap=_idmap):
308+
def _quote(str, LegalChars=_LegalChars):
311309
#
312310
# If the string does not need to be double-quoted,
313311
# then just return the string. Otherwise, surround
314312
# the string in doublequotes and precede quote (with a \)
315313
# special characters.
316314
#
317-
if "" == str.translate(idmap, LegalChars):
315+
if len(filter(LegalChars.__contains__, str)) == len(str):
318316
return str
319317
else:
320318
return '"' + _nulljoin( map(_Translator.get, str, str) ) + '"'
@@ -439,12 +437,12 @@ def isReservedKey(self, K):
439437
return K.lower() in self._reserved
440438
# end isReservedKey
441439

442-
def set(self, key, val, coded_val, LegalChars=_LegalChars, idmap=_idmap):
440+
def set(self, key, val, coded_val, LegalChars=_LegalChars):
443441
# First we verify that the key isn't a reserved word
444442
# Second we make sure it only contains legal characters
445443
if key.lower() in self._reserved:
446444
raise CookieError("Attempt to set a reserved key: %s" % key)
447-
if "" != key.translate(idmap, LegalChars):
445+
if len(filter(LegalChars.__contains__, key)) != len(key):
448446
raise CookieError("Illegal key value: %s" % key)
449447

450448
# It's a good key, so save it.
@@ -680,9 +678,9 @@ def __init__(self, input=None):
680678
# end __init__
681679
def value_decode(self, val):
682680
# This could raise an exception!
683-
return loads( _unquote(val) ), val
681+
return loads( _unquote(val).encode('latin-1') ), val
684682
def value_encode(self, val):
685-
return val, _quote( dumps(val) )
683+
return val, _quote( dumps(val).decode('latin-1') )
686684
# end SerialCookie
687685

688686
class SmartCookie(BaseCookie):
@@ -706,14 +704,14 @@ def __init__(self, input=None):
706704
def value_decode(self, val):
707705
strval = _unquote(val)
708706
try:
709-
return loads(strval), val
707+
return loads(strval.encode('latin-1')), val
710708
except:
711709
return strval, val
712710
def value_encode(self, val):
713-
if type(val) == type(""):
711+
if isinstance(val, str):
714712
return val, _quote(val)
715713
else:
716-
return val, _quote( dumps(val) )
714+
return val, _quote( dumps(val).decode('latin-1') )
717715
# end SmartCookie
718716

719717

0 commit comments

Comments
 (0)