[Python-checkins] python/dist/src/Lib Cookie.py,1.18,1.19

birkenfeld@users.sourceforge.net birkenfeld at users.sourceforge.net
Thu Aug 25 00:34:31 CEST 2005


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27394/Lib

Modified Files:
	Cookie.py 
Log Message:
patch #848017: make Cookie more RFC-compliant.



Index: Cookie.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/Cookie.py,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -d -r1.18 -r1.19
--- Cookie.py	26 Jun 2005 21:02:49 -0000	1.18
+++ Cookie.py	24 Aug 2005 22:34:21 -0000	1.19
@@ -69,9 +69,8 @@
    >>> C = Cookie.SmartCookie()
    >>> C["fig"] = "newton"
    >>> C["sugar"] = "wafer"
-   >>> print C
-   Set-Cookie: fig=newton;
-   Set-Cookie: sugar=wafer;
+   >>> C.output()
+   'Set-Cookie: fig=newton\r\nSet-Cookie: sugar=wafer'
 
 Notice that the printable representation of a Cookie is the
 appropriate format for a Set-Cookie: header.  This is the
@@ -82,9 +81,9 @@
    >>> C["rocky"] = "road"
    >>> C["rocky"]["path"] = "/cookie"
    >>> print C.output(header="Cookie:")
-   Cookie: rocky=road; Path=/cookie;
+   Cookie: rocky=road; Path=/cookie
    >>> print C.output(attrs=[], header="Cookie:")
-   Cookie: rocky=road;
+   Cookie: rocky=road
 
 The load() method of a Cookie extracts cookies from a string.  In a
 CGI script, you would use this method to extract the cookies from the
@@ -92,9 +91,8 @@
 
    >>> C = Cookie.SmartCookie()
    >>> C.load("chips=ahoy; vienna=finger")
-   >>> print C
-   Set-Cookie: chips=ahoy;
-   Set-Cookie: vienna=finger;
+   >>> C.output()
+   'Set-Cookie: chips=ahoy\r\nSet-Cookie: vienna=finger'
 
 The load() method is darn-tootin smart about identifying cookies
 within a string.  Escaped quotation marks, nested semicolons, and other
@@ -103,7 +101,7 @@
    >>> C = Cookie.SmartCookie()
    >>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";')
    >>> print C
-   Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;";
+   Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;"
 
 Each element of the Cookie also supports all of the RFC 2109
 Cookie attributes.  Here's an example which sets the Path
@@ -113,7 +111,7 @@
    >>> C["oreo"] = "doublestuff"
    >>> C["oreo"]["path"] = "/"
    >>> print C
-   Set-Cookie: oreo=doublestuff; Path=/;
+   Set-Cookie: oreo=doublestuff; Path=/
 
 Each dictionary element has a 'value' attribute, which gives you
 back the value associated with the key.
@@ -144,9 +142,8 @@
    '7'
    >>> C["string"].value
    'seven'
-   >>> print C
-   Set-Cookie: number=7;
-   Set-Cookie: string=seven;
+   >>> C.output()
+   'Set-Cookie: number=7\r\nSet-Cookie: string=seven'
 
 
 SerialCookie
@@ -165,9 +162,8 @@
    7
    >>> C["string"].value
    'seven'
-   >>> print C
-   Set-Cookie: number="I7\012.";
-   Set-Cookie: string="S'seven'\012p1\012.";
+   >>> C.output()
+   'Set-Cookie: number="I7\\012."\r\nSet-Cookie: string="S\'seven\'\\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.
@@ -190,9 +186,8 @@
    7
    >>> C["string"].value
    'seven'
-   >>> print C
-   Set-Cookie: number="I7\012.";
-   Set-Cookie: string=seven;
+   >>> C.output()
+   'Set-Cookie: number="I7\\012."\r\nSet-Cookie: string=seven'
 
 
 Backwards Compatibility
@@ -228,7 +223,7 @@
            "SmartCookie","Cookie"]
 
 _nulljoin = ''.join
-_spacejoin = ' '.join
+_semispacejoin = '; '.join
 
 #
 # Define an exception visible to External modules
@@ -485,7 +480,7 @@
         RA = result.append
 
         # First, the key=value pair
-        RA("%s=%s;" % (self.key, self.coded_value))
+        RA("%s=%s" % (self.key, self.coded_value))
 
         # Now add any defined attributes
         if attrs is None:
@@ -496,16 +491,16 @@
             if V == "": continue
             if K not in attrs: continue
             if K == "expires" and type(V) == type(1):
-                RA("%s=%s;" % (self._reserved[K], _getdate(V)))
+                RA("%s=%s" % (self._reserved[K], _getdate(V)))
             elif K == "max-age" and type(V) == type(1):
-                RA("%s=%d;" % (self._reserved[K], V))
+                RA("%s=%d" % (self._reserved[K], V))
             elif K == "secure":
-                RA("%s;" % self._reserved[K])
+                RA(str(self._reserved[K]))
             else:
-                RA("%s=%s;" % (self._reserved[K], V))
+                RA("%s=%s" % (self._reserved[K], V))
 
         # Return the result
-        return _spacejoin(result)
+        return _semispacejoin(result)
     # end OutputString
 # end Morsel class
 
@@ -581,7 +576,7 @@
         self.__set(key, rval, cval)
     # end __setitem__
 
-    def output(self, attrs=None, header="Set-Cookie:", sep="\n"):
+    def output(self, attrs=None, header="Set-Cookie:", sep="\015\012"):
         """Return a string suitable for HTTP."""
         result = []
         items = self.items()
@@ -599,7 +594,7 @@
         items.sort()
         for K,V in items:
             L.append( '%s=%s' % (K,repr(V.value) ) )
-        return '<%s: %s>' % (self.__class__.__name__, _spacejoin(L))
+        return '<%s: %s>' % (self.__class__.__name__, _semispacejoin(L))
 
     def js_output(self, attrs=None):
         """Return a string suitable for JavaScript."""



More information about the Python-checkins mailing list