[New-bugs-announce] [issue7446] http.cookies.BaseCookie (and SimpleCookie) can't be load from dict

Thomas Courbon report at bugs.python.org
Sat Dec 5 21:36:54 CET 2009

New submission from Thomas Courbon <harthie at yahoo.fr>:

Hi there !

According the documentation [1], the following code should work :
>>> from http.cookies import SimpleCookie
>>> c = SimpleCookie({'field1': 'value1', 'field2': 'value2'})
>>> print(c)
'Set-Cookie: field1=value1\r\nSet-Cookie: field2=value2'

But an exception is raised : 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\python31\lib\http\cookies.py", line 507, in output
    result.append( V.output(attrs, header) )
AttributeError: 'str' object has no attribute 'output'

in BaseCookie.load(...) the call to BaseCookie.update(rawdata) seem to
use dict.__setitem__ instead of BaseCookie.__setitem__.

Despite it's weird (I believe that the __setitem__ of the child class
should be used) I don't know why.

I don't have a solution to this underlying issue so I propose to define
an update method for BaseCookie:
def update(self, other=None, **kwargs):
    if other is None:
        other = kwargs
    elif not hasattr(other, 'items'):
        other = dict(other)
    for k, v in other.items():
        self[k] = v

This method behave like the dict one.

Hope it help and it's not a duplicate.


[1] :

components: Library (Lib)
messages: 96007
nosy: tcourbon
severity: normal
status: open
title: http.cookies.BaseCookie (and SimpleCookie) can't be load from dict
versions: Python 3.1

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list