You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Responses library is already compatible with both Python 2 and 3. It uses Unicode literals, and the Cookies library instead of http.cookies on Python 2. It does this by using http.cookies if it's available and falling back to the Cookies library if it's not. However, it breaks on Python 2 if python-future is installed because the http.cookies module provided by it (which just imports the Python 2 Cookie module) doesn't handle Unicode strings. The reason for this is in the Cookie.py file provided by the Python 2 standard library:
defload(self, rawdata):
"""Load cookies from a string (presumably HTTP_COOKIE) or from a dictionary. Loading cookies from a dictionary 'd' is equivalent to calling: map(Cookie.__setitem__, d.keys(), d.values()) """iftype(rawdata) ==type(""):
self.__ParseString(rawdata)
else:
# self.update() wouldn't call our custom __setitem__fork, vinrawdata.items():
self[k] =vreturn
The type of "" is str, which is not equal to unicode, so it tries to parse rawdata as a dict which causes the following error:
test_responses.py:1119: in test_cookies_from_headers
cookiejar = responses._cookies_from_headers(headers)
responses.py:122: in _cookies_from_headers
resp_cookie.load(headers["set-cookie"])
/usr/lib/python2.7/Cookie.py:643: in load
for k, v in rawdata.items():
E AttributeError: 'unicode' object has no attribute 'items'
The following code can be used to reproduce this:
from __future__ importprint_function, unicode_literalstry:
importhttp.cookiesascookiesprint('On Python 3, using http.cookies')
resp_cookie=cookies.SimpleCookie()
resp_cookie.load('test=test')
print('Success!')
exceptImportError:
print('On Python 2, using Cookies library')
Output on Python 3:
On Python 3, using http.cookies
Success!
Output on Python 2 without python-future installed:
On Python 2, using Cookies library
Output on Python 2 with python-future installed:
On Python 3, using http.cookies
Traceback (most recent call last):
File "main.py", line 8, in <module>
resp_cookie.load('test=test')
File "/usr/lib/python2.7/Cookie.py", line 643, in load
for k, v in rawdata.items():
AttributeError: 'unicode' object has no attribute 'items'
The text was updated successfully, but these errors were encountered:
(I have also reported this at getsentry/responses#330)
The Responses library is already compatible with both Python 2 and 3. It uses Unicode literals, and the Cookies library instead of
http.cookies
on Python 2. It does this by usinghttp.cookies
if it's available and falling back to the Cookies library if it's not. However, it breaks on Python 2 if python-future is installed because thehttp.cookies
module provided by it (which just imports the Python 2Cookie
module) doesn't handle Unicode strings. The reason for this is in theCookie.py
file provided by the Python 2 standard library:The type of
""
isstr
, which is not equal tounicode
, so it tries to parserawdata
as a dict which causes the following error:The following code can be used to reproduce this:
Output on Python 3:
Output on Python 2 without python-future installed:
Output on Python 2 with python-future installed:
The text was updated successfully, but these errors were encountered: