Author: lukeplant
Date: 2011-06-29 08:12:48 -0700 (Wed, 29 Jun 2011)
New Revision: 16485

Modified:
   django/trunk/django/http/__init__.py
Log:
Fixed our SimpleCookie overriding and use to be compatible with a (potential) 
stdlib SimpleCookie that fixes http://bugs.python.org/issue2193

The previous code tested the stdlib in a way that would always fail. It then
used an overridden SimpleCookie.load method that wouldn't work for the
stdlib. And it did some completely unnecessary monkey patching.

Modified: django/trunk/django/http/__init__.py
===================================================================
--- django/trunk/django/http/__init__.py        2011-06-29 13:52:27 UTC (rev 
16484)
+++ django/trunk/django/http/__init__.py        2011-06-29 15:12:48 UTC (rev 
16485)
@@ -28,8 +28,11 @@
 _cookie_encodes_correctly = Cookie.SimpleCookie().value_encode(';') == (';', 
'"\\073"')
 # See ticket #13007, http://bugs.python.org/issue2193 and 
http://trac.edgewall.org/ticket/2256
 _tc = Cookie.SimpleCookie()
-_tc.load('f:oo')
-_cookie_allows_colon_in_names = 'Set-Cookie: f:oo=' in _tc.output()
+try:
+    _tc.load('foo:bar=1')
+    _cookie_allows_colon_in_names = True
+except Cookie.CookieError:
+    _cookie_allows_colon_in_names = False
 
 if _morsel_supports_httponly and _cookie_encodes_correctly and 
_cookie_allows_colon_in_names:
     SimpleCookie = Cookie.SimpleCookie
@@ -89,19 +92,16 @@
                 return val, encoded
 
         if not _cookie_allows_colon_in_names:
-            def load(self, rawdata, ignore_parse_errors=False):
-                if ignore_parse_errors:
-                    self.bad_cookies = set()
-                    self._BaseCookie__set = self._loose_set
+            def load(self, rawdata):
+                self.bad_cookies = set()
                 super(SimpleCookie, self).load(rawdata)
-                if ignore_parse_errors:
-                    self._BaseCookie__set = self._strict_set
-                    for key in self.bad_cookies:
-                        del self[key]
+                for key in self.bad_cookies:
+                    del self[key]
 
             _strict_set = Cookie.BaseCookie._BaseCookie__set
 
-            def _loose_set(self, key, real_value, coded_value):
+            # override private __set() method:
+            def _BaseCookie__set(self, key, real_value, coded_value):
                 try:
                     self._strict_set(key, real_value, coded_value)
                 except Cookie.CookieError:
@@ -519,7 +519,7 @@
     if not isinstance(cookie, Cookie.BaseCookie):
         try:
             c = SimpleCookie()
-            c.load(cookie, ignore_parse_errors=True)
+            c.load(cookie)
         except Cookie.CookieError:
             # Invalid cookie
             return {}

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to