From cda95d3cdee339f10d48287395368e31c386f296 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?latyas=28=E6=87=92=29?= Date: Thu, 14 May 2015 18:44:33 +0800 Subject: [PATCH] morsel['max-age'] may be a str. Example: here are Set-Cookie list: ```python sclst = ['dwac_bcIBMiaagZmkYaaadeYtg11eVR=j9jHdmPjUhgDOhyH9f89X4lQgehEmflVyeA%3D|dw-only|||CNY|false|Asia%2FShanghai|true; Path=/', 'sid=j9jHdmPjUhgDOhyH9f89X4lQgehEmflVyeA; Path=/', 'geoLocation=CN; Path=/', 'dwpersonalization_fae107a9dd0fc32ed99532ec1977f31f=bc8sEiaagZqRsaaadk8XoNTL8h20150506; Expires=Sun, 14-Jun-2015 10:37:07 GMT; Path=/', 'dwanonymous_fae107a9dd0fc32ed99532ec1977f31f=abjpA8kng31LjPp8ZEERDT4XVg; Version=1; Comment="Demandware anonymous cookie for site Sites-abercrombie_cn-Site"; Max-Age=15552000; Expires=Tue, 10-Nov-2015 10:37:07 GMT; Path=/', 'myStore=91156; Path=/', 'AF_PREF=en_CN; Path=/', 'dwsid=MiHJy3KYZKDcN0lZg4HS1zSpj1VV4s_tFu39ar0KXNpAx9aX8X2LlvZQ9m5fOOknb6QXtmmukHcOjmivYf31hg==; path=/; HttpOnly'] ``` ```python for sc in sclst: C = Cookie.SimpleCookie(sc) for morsel in C.values(): cookie = requests.cookies.morsel_to_cookie(morsel) cookiejar.set_cookie(cookie) ``` Then, exception occured ```shell File "/Library/Python/2.7/site-packages/requests/cookies.py", line 402, in morsel_to_cookie expires = time.time() + morsel['max-age'] TypeError: unsupported operand type(s) for +: 'float' and 'str' ``` As Cookie.SimpleCookie is in STL, should `morsel_to_cookie` check the type of `max-age`? On the other hand, if **max-age** can not be converted to float, it's illegal obviously. --- requests/cookies.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requests/cookies.py b/requests/cookies.py index 1fbc934c..8ca64260 100644 --- a/requests/cookies.py +++ b/requests/cookies.py @@ -415,7 +415,7 @@ def morsel_to_cookie(morsel): expires = None if morsel['max-age']: - expires = time.time() + morsel['max-age'] + expires = time.time() + float(morsel['max-age']) elif morsel['expires']: time_template = '%a, %d-%b-%Y %H:%M:%S GMT' expires = time.mktime(