PythonCharmers / python-future

Easy, clean, reliable Python 2/3 compatibility
http://python-future.org
MIT License
1.17k stars 291 forks source link

SimpleCookie throws AttributeError: 'str' object has no attribute 'items' #203

Open serac opened 8 years ago

serac commented 8 years ago

Interactive session demonstrating problem:

marvin@eiger:~$ python
Python 2.7.10 (default, Jul 13 2015, 12:05:58)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from future.backports.http.cookies import SimpleCookie
>>> kaka='JSESSIONID=189xm7lb936kjxt7bbwxqf1lb'
>>> cookie_object = SimpleCookie(kaka)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/future-0.15.2-py2.7.egg/future/backports/http/cookies.py", line 492, in __init__
    self.load(input)
  File "/usr/local/lib/python2.7/site-packages/future-0.15.2-py2.7.egg/future/backports/http/cookies.py", line 545, in load
    for key, value in rawdata.items():

The error is caused by the following isinstance check:

    if isinstance(rawdata, str):
        self.__parse_string(rawdata)
    else:
        # self.update() wouldn't call our custom __setitem__
        for key, value in rawdata.items():
            self[key] = value
    return

I have verified that replacing str with basestring fixes the problem, but I am totally at a loss as to why.

djdutcher commented 8 years ago

The problem seems to be that str has been replaced:

from future.builtins import chr, dict, int, str

Therefore in Python 2 a literal string won't be of type str:

Python 2.7.11+ (default, Apr 17 2016, 14:00:29)
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 'foo'
>>> isinstance(a, str)
True
>>> from future.builtins import str
>>> isinstance(a, str)
False
>>> b = 'bar'
>>> isinstance(b, str)
False

Maybe the code will need to check if it is PY2 and then test for an instance of basestring.