Skip to content
Myr

Fix TypeError when get json-encoded content of a response

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

``self.content`` could be ``None``, so ``len(self.content)`` may raise ``TypeError: object of type 'NoneType' has no len()``

authored by messense · committed by messense · parent 989e8f1 · Browse files · 2 files+12 added−1 removed
This commit

State

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

Diff stats

2 files+12 added−1 removed

  • tests:+11 added−0 removed
  • other:+1 added−1 removed

2 files+12 added−1 removed

modified:requests/models.py+1 added-1 removed
790 unmodified lines
791792 :param \*\*kwargs: Optional arguments that ``json.loads`` takes.793 """794795 if not self.encoding and len(self.content) > 3:795 if not self.encoding and self.content and len(self.content) > 3:796 # No encoding set. JSON RFC 4627 section 3 states we should expect797 # UTF-8, -16 or -32. Detect which one to use; If the detection or798 # decoding fails, fall back to `self.text` (using chardet to make799 # a best guess).
56 unmodified lines
modified:tests/test_requests.py+11 added
1225 unmodified lines
1226 session.close()1227 proxies['one'].clear.assert_called_once_with()1228 proxies['two'].clear.assert_called_once_with()12291230 def test_response_json_when_content_is_None(self, httpbin):1231 r = requests.get(httpbin('/status/204'))1232 # Make sure r.content is None1233 r.status_code = 01234 r._content = False1235 r._content_consumed = False12361237 assert r.content is None1238 with pytest.raises(ValueError):1239 r.json()124012411242class TestCaseInsensitiveDict:12431244 @pytest.mark.parametrize(1245 'cid', (
382 unmodified lines