super cookiejar_from_dict powwerrrs
authorKenneth Reitz <me@kennethreitz.com>
Wed, 17 Aug 2011 06:01:13 +0000 (02:01 -0400)
committerKenneth Reitz <me@kennethreitz.com>
Wed, 17 Aug 2011 06:01:13 +0000 (02:01 -0400)
requests/utils.py

index 716986b..e4236b0 100644 (file)
@@ -9,7 +9,6 @@ that are also useful for external consumption.
 
 """
 
-import Cookie
 import cookielib
 
 
@@ -21,28 +20,45 @@ def dict_from_cookiejar(cookiejar):
     for _, cookies in cookiejar._cookies.items():
         for _, cookies in cookies.items():
             for cookie in cookies.values():
+                # print cookie
                 cookie_dict[cookie.name] = cookie.value
 
     return cookie_dict
 
 
-def cookiejar_from_dict(cookie_dict, domain=None):
+def cookiejar_from_dict(cookie_dict):
     """Returns a CookieJar from a key/value dictionary."""
 
+    # return cookiejar if one was passed in
+    if isinstance(cookie_dict, cookielib.CookieJar):
+        return cookie_dict
+
     # create cookiejar
     cj = cookielib.CookieJar()
 
     for k, v in cookie_dict.items():
 
-        # create cookie
-        ck = Cookie.SimpleCookie()
-        ck.name = v
-        ck.expires = 0
-        ck.path = '/'
-        ck.domain = domain
+        cookie = cookielib.Cookie(
+            version=0,
+            name=k,
+            value=v,
+            port=None,
+            port_specified=False,
+            domain='',
+            domain_specified=False,
+            domain_initial_dot=False,
+            path='/',
+            path_specified=True,
+            secure=False,
+            expires=None,
+            discard=True,
+            comment=None,
+            comment_url=None,
+            rest={'HttpOnly': None},
+            rfc2109=False
+        )
 
         # add cookie to cookiejar
-        cj.set_cookie(ck)
+        cj.set_cookie(cookie)
 
     return cj
-