Accessing response.content twice removes forgets read error
I had a hard debugging time today because an error in the response stream is only reported when accessing response.content for the first time.
This is especially irritating when running code in a debugger.
Expected Result
If accessing response.content the first time raises an exception I would expect that accessing response.content again would also raise an exception (ideally the same).
Actual Result
Instead after raising on the first get, getting response.content again returns an empty string.
Reproduction Steps
Here is a patch with a new test case for this: error_replay_test.diff.gz.
Basically, it boils down to this:
import requests
response = requests.post("http://connreset.biz/get/incomplete/chunked", stream=True)
try:
response.content
except Exception:
# Error handling code, may try something else or fall through
pass
content = response.content # empty string
Output of my test case:
$ pipenv run py.test tests/test_lowlevel.py -q --tb=short -k retain
F [100%]
============================= FAILURES =============================
_______________ test_response_content_retains_error ________________
tests/test_lowlevel.py:343: in test_response_content_retains_error
assert False, "error response has content: {0!r}".format(content)
E AssertionError: error response has content: ''
E assert False
1 failed, 15 deselected in 0.60 seconds
System Information
$ python -m requests.help
Edit: Oops, I used pipenv run python -m requests.help which actually called into system python 2.7. Here comes the real data:
$ pipenv run python3 -m requests.help
{
"chardet": {
"version": "3.0.4"
},
"cryptography": {
"version": ""
},
"idna": {
"version": "2.7"
},
"implementation": {
"name": "CPython",
"version": "3.6.8+"
},
"platform": {
"release": "4.15.0-43-generic",
"system": "Linux"
},
"pyOpenSSL": {
"openssl_version": "",
"version": null
},
"requests": {
"version": "2.21.0"
},
"system_ssl": {
"version": "1000207f"
},
"urllib3": {
"version": "1.24"
},
"using_pyopenssl": false
}
Thanks for looking into this!
It looks like we never closed the issue after the PR was merged. I'm reverting #5087 because it doesn't cover all of our read APIs (content, iter_content, iter_lines, text, __iter__) uniformly.
I took a quick pass but the edge cases the tests are missing aren't something I can get resolved today. We can reopen a new PR and revisit options later if anyone wants.
For the response.content losing read error on second access:
The issue is that _content is cached after first read, but the error state is not preserved. Fix:
@property
def content(self):
if self._content is False:
try:
self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes()
except AttributeError:
# Catch the error and store it
self._content_error = sys.exc_info()
self._content = bytes()
return self._content
Or alternatively, raise the stored error on subsequent access:
if hasattr(self, '_content_error') and self._content_error:
raise self._content_error[1]
Hi, I'd like to take this one.