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 1/5] 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( From d3bd3630ee9599f1bdbc71e0efb3d3b21ee0100e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?latyas=28=E6=87=92=29?= Date: Fri, 15 May 2015 10:16:35 +0800 Subject: [PATCH 2/5] max-age may be not digits --- requests/cookies.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/requests/cookies.py b/requests/cookies.py index 8ca64260..c0865106 100644 --- a/requests/cookies.py +++ b/requests/cookies.py @@ -415,7 +415,10 @@ def morsel_to_cookie(morsel): expires = None if morsel['max-age']: - expires = time.time() + float(morsel['max-age']) + try: + expires = time.time() + float(morsel['max-age']) + except ValueError: + pass elif morsel['expires']: time_template = '%a, %d-%b-%Y %H:%M:%S GMT' expires = time.mktime( From 395a3e888506d3713dba030b73adc21ca99a305c Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Wed, 13 May 2015 07:04:57 +0100 Subject: [PATCH 3/5] Partially revert ab84f9be5740d4649d734e73b84f17f85e52ffc9 --- requests/models.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/requests/models.py b/requests/models.py index f0006334..a7bef46b 100644 --- a/requests/models.py +++ b/requests/models.py @@ -30,7 +30,8 @@ from .utils import ( iter_slices, guess_json_utf, super_len, to_native_string) from .compat import ( cookielib, urlunparse, urlsplit, urlencode, str, bytes, StringIO, - is_py2, chardet, json, builtin_str, basestring) + is_py2, chardet, builtin_str, basestring) +from .compat import json as complexjson from .status_codes import codes #: The set of HTTP status codes that indicate an automatically @@ -415,7 +416,7 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin): if json is not None: content_type = 'application/json' - body = json.dumps(json) + body = complexjson.dumps(json) is_stream = all([ hasattr(data, '__iter__'), From 059e105f8f5897ddf2da7c2317bef995ee099ab5 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Thu, 14 May 2015 22:30:24 +0100 Subject: [PATCH 4/5] Use complexjson everywhere --- requests/models.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/requests/models.py b/requests/models.py index a7bef46b..ccaf5968 100644 --- a/requests/models.py +++ b/requests/models.py @@ -793,14 +793,16 @@ class Response(object): encoding = guess_json_utf(self.content) if encoding is not None: try: - return json.loads(self.content.decode(encoding), **kwargs) + return complexjson.loads( + self.content.decode(encoding), **kwargs + ) except UnicodeDecodeError: # Wrong UTF codec detected; usually because it's not UTF-8 # but some other 8-bit codec. This is an RFC violation, # and the server didn't bother to tell us what codec *was* # used. pass - return json.loads(self.text, **kwargs) + return complexjson.loads(self.text, **kwargs) @property def links(self): From dd0f164f8ed8e6f936c8af4442f6ddfa4d302fd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?latyas=28=E6=87=92=29?= Date: Fri, 15 May 2015 10:28:22 +0800 Subject: [PATCH 5/5] Update cookies.py --- requests/cookies.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/requests/cookies.py b/requests/cookies.py index c0865106..88b478c7 100644 --- a/requests/cookies.py +++ b/requests/cookies.py @@ -416,13 +416,13 @@ def morsel_to_cookie(morsel): expires = None if morsel['max-age']: try: - expires = time.time() + float(morsel['max-age']) + expires = int(time.time() + int(morsel['max-age'])) except ValueError: - pass + raise TypeError('max-age: %s must be integer' % morsel['max-age']) elif morsel['expires']: time_template = '%a, %d-%b-%Y %H:%M:%S GMT' - expires = time.mktime( - time.strptime(morsel['expires'], time_template)) - time.timezone + expires = int(time.mktime( + time.strptime(morsel['expires'], time_template)) - time.timezone) return create_cookie( comment=morsel['comment'], comment_url=bool(morsel['comment']),