This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: http.cookies.BaseCookie (and SimpleCookie) can't be load from dict
Type: Stage:
Components: Library (Lib) Versions: Python 3.1
process
Status: closed Resolution: out of date
Dependencies: Superseder: BaseCookie.load doesn't create Morsel objects for mappings
View: 5275
Assigned To: Nosy List: amaury.forgeotdarc, tcourbon
Priority: normal Keywords:

Created on 2009-12-05 20:36 by tcourbon, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg96007 - (view) Author: Thomas Courbon (tcourbon) Date: 2009-12-05 20:36
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.

Regards,
Thomas


[1] :
http://docs.python.org/3.1/library/http.cookies.html#http.cookies.BaseCookie.load
msg96009 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2009-12-05 21:12
This was already corrected with issue5275. Thanks however for the report.
History
Date User Action Args
2022-04-11 14:56:55adminsetgithub: 51695
2009-12-05 21:12:51amaury.forgeotdarcsetstatus: open -> closed

nosy: + amaury.forgeotdarc
messages: + msg96009

superseder: BaseCookie.load doesn't create Morsel objects for mappings
resolution: out of date
2009-12-05 20:36:52tcourboncreate