Browse Source

[compat] add compat_SimpleCookie

Remita Amine 4 years ago
parent
commit
162bf9e10a
2 changed files with 12 additions and 6 deletions
  1. 9 0
      youtube_dl/compat.py
  2. 3 6
      youtube_dl/extractor/common.py

+ 9 - 0
youtube_dl/compat.py

@@ -73,6 +73,15 @@ try:
 except ImportError:  # Python 2
 except ImportError:  # Python 2
     import Cookie as compat_cookies
     import Cookie as compat_cookies
 
 
+if sys.version_info[0] == 2:
+    class compat_SimpleCookie(compat_cookies.SimpleCookie):
+        def load(self, rawdata):
+            if isinstance(rawdata, unicode):
+                rawdata = str(rawdata)
+            return super(compat_SimpleCookie, self).load(rawdata)
+else:
+    compat_SimpleCookie = compat_cookies.SimpleCookie
+
 try:
 try:
     import html.entities as compat_html_entities
     import html.entities as compat_html_entities
 except ImportError:  # Python 2
 except ImportError:  # Python 2

+ 3 - 6
youtube_dl/extractor/common.py

@@ -17,13 +17,13 @@ import math
 
 
 from ..compat import (
 from ..compat import (
     compat_cookiejar_Cookie,
     compat_cookiejar_Cookie,
-    compat_cookies,
     compat_etree_Element,
     compat_etree_Element,
     compat_etree_fromstring,
     compat_etree_fromstring,
     compat_getpass,
     compat_getpass,
     compat_integer_types,
     compat_integer_types,
     compat_http_client,
     compat_http_client,
     compat_os_name,
     compat_os_name,
+    compat_SimpleCookie,
     compat_str,
     compat_str,
     compat_urllib_error,
     compat_urllib_error,
     compat_urllib_parse_unquote,
     compat_urllib_parse_unquote,
@@ -2901,13 +2901,10 @@ class InfoExtractor(object):
         self._downloader.cookiejar.set_cookie(cookie)
         self._downloader.cookiejar.set_cookie(cookie)
 
 
     def _get_cookies(self, url):
     def _get_cookies(self, url):
-        """ Return a compat_cookies.SimpleCookie with the cookies for the url """
+        """ Return a compat_SimpleCookie with the cookies for the url """
         req = sanitized_Request(url)
         req = sanitized_Request(url)
         self._downloader.cookiejar.add_cookie_header(req)
         self._downloader.cookiejar.add_cookie_header(req)
-        cookie = req.get_header('Cookie')
-        if cookie and sys.version_info[0] == 2:
-            cookie = str(cookie)
-        return compat_cookies.SimpleCookie(cookie)
+        return compat_SimpleCookie(req.get_header('Cookie'))
 
 
     def _apply_first_set_cookie_header(self, url_handle, cookie):
     def _apply_first_set_cookie_header(self, url_handle, cookie):
         """
         """