Skip to content
Myr

Throw value error when serializing JSON object with NaN value (#5810)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* disallow nan values in json serialize * test nan value in json post * added exception for invalid json in request * use invalid json exception * invalid json test

authored by tallalnparis4ev · committed by GitHub · parent c45a4df · Browse files · 3 files+17 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+17 added−3 removed

  • tests:+6 added−1 removed
  • other:+11 added−2 removed

3 files+17 added−3 removed

modified:requests/exceptions.py+4 added
23 unmodified lines
24 self.request = self.response.request25 super(RequestException, self).__init__(*args, **kwargs)262728class InvalidJSONError(RequestException):29 """A JSON error occurred."""303132class HTTPError(RequestException):33 """An HTTP error occurred."""3435
92 unmodified lines
modified:requests/models.py+7 added-2 removed
27 unmodified lines
28from .auth import HTTPBasicAuth29from .cookies import cookiejar_from_dict, get_cookie_header, _copy_cookie_jar30from .exceptions import (31 HTTPError, MissingSchema, InvalidURL, ChunkedEncodingError,32 ContentDecodingError, ConnectionError, StreamConsumedError)32 ContentDecodingError, ConnectionError, StreamConsumedError, InvalidJSONError)33from ._internal_utils import to_native_string, unicode_is_ascii34from .utils import (35 guess_filename, get_auth_from_url, requote_uri,36 stream_decode_response_unicode, to_key_val_list, parse_header_links,
428 unmodified lines
465 if not data and json is not None:466 # urllib3 requires a bytes-like body. Python 2's json.dumps467 # provides this natively, but Python 3 gives a Unicode string.468 content_type = 'application/json'469470 try:469 body = complexjson.dumps(json)471 body = complexjson.dumps(json, allow_nan=False)472 except ValueError as ve:473 raise InvalidJSONError(ve, request=self)474475 if not isinstance(body, bytes):476 body = body.encode('utf-8')477478 is_stream = all([
488 unmodified lines
modified:tests/test_requests.py+6 added-1 removed
22 unmodified lines
23 cookiejar_from_dict, morsel_to_cookie)24from requests.exceptions import (25 ConnectionError, ConnectTimeout, InvalidSchema, InvalidURL,26 MissingSchema, ReadTimeout, Timeout, RetryError, TooManyRedirects,27 ProxyError, InvalidHeader, UnrewindableBodyError, SSLError, InvalidProxyURL)27 ProxyError, InvalidHeader, UnrewindableBodyError, SSLError, InvalidProxyURL, InvalidJSONError)28from requests.models import PreparedRequest29from requests.structures import CaseInsensitiveDict30from requests.sessions import SessionRedirectMixin31from requests.models import urlencode
2533 unmodified lines
2565 """2566 r = requests.Request('GET', url=input, params=params)2567 p = r.prepare()2568 assert p.url == expected25692570 def test_post_json_nan(self, httpbin):2571 data = {"foo": float("nan")}2572 with pytest.raises(requests.exceptions.InvalidJSONError):2573 r = requests.post(httpbin('post'), json=data)