Skip to content

Accessing response.content twice removes forgets read error #4965

Open
@tlandschoff-scale

Description

@tlandschoff-scale

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions