[Python-3000-checkins] r55827 - python/branches/py3k-struni/Lib/Cookie.py

walter.doerwald python-3000-checkins at python.org
Fri Jun 8 17:33:50 CEST 2007


Author: walter.doerwald
Date: Fri Jun  8 17:33:46 2007
New Revision: 55827

Modified:
   python/branches/py3k-struni/Lib/Cookie.py
Log:
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).


Modified: python/branches/py3k-struni/Lib/Cookie.py
==============================================================================
--- python/branches/py3k-struni/Lib/Cookie.py	(original)
+++ python/branches/py3k-struni/Lib/Cookie.py	Fri Jun  8 17:33:46 2007
@@ -163,7 +163,7 @@
    >>> C["string"].value
    'seven'
    >>> 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
 it isn't a valid pickle'd object), IT WILL RAISE AN EXCEPTION.
@@ -305,16 +305,14 @@
     '\375' : '\\375',  '\376' : '\\376',  '\377' : '\\377'
     }
 
-_idmap = ''.join(chr(x) for x in range(256))
-
-def _quote(str, LegalChars=_LegalChars, idmap=_idmap):
+def _quote(str, LegalChars=_LegalChars):
     #
     # If the string does not need to be double-quoted,
     # then just return the string.  Otherwise, surround
     # the string in doublequotes and precede quote (with a \)
     # special characters.
     #
-    if "" == str.translate(idmap, LegalChars):
+    if len(filter(LegalChars.__contains__, str)) == len(str):
         return str
     else:
         return '"' + _nulljoin( map(_Translator.get, str, str) ) + '"'
@@ -439,12 +437,12 @@
         return K.lower() in self._reserved
     # 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
         # Second we make sure it only contains legal characters
         if key.lower() in self._reserved:
             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)
 
         # It's a good key, so save it.
@@ -680,9 +678,9 @@
     # end __init__
     def value_decode(self, val):
         # This could raise an exception!
-        return loads( _unquote(val) ), val
+        return loads( _unquote(val).encode('latin-1') ), val
     def value_encode(self, val):
-        return val, _quote( dumps(val) )
+        return val, _quote( dumps(val).decode('latin-1') )
 # end SerialCookie
 
 class SmartCookie(BaseCookie):
@@ -706,14 +704,14 @@
     def value_decode(self, val):
         strval = _unquote(val)
         try:
-            return loads(strval), val
+            return loads(strval.encode('latin-1')), val
         except:
             return strval, val
     def value_encode(self, val):
-        if type(val) == type(""):
+        if isinstance(val, str):
             return val, _quote(val)
         else:
-            return val, _quote( dumps(val) )
+            return val, _quote( dumps(val).decode('latin-1') )
 # end SmartCookie
 
 


More information about the Python-3000-checkins mailing list