Skip to content

Commit

Permalink
Revert "psf#4965 fix: Accessing response.content twice removes forget…
Browse files Browse the repository at this point in the history
…s read error."

This reverts commit bd10047.

This reverts commit d91fe00.
  • Loading branch information
nateprewitt committed Feb 18, 2020
1 parent 80011a7 commit 22ed079
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 62 deletions.
1 change: 0 additions & 1 deletion AUTHORS.rst
Expand Up @@ -189,4 +189,3 @@ Patches and Suggestions
- Antti Kaihola (`@akaihola <https://github.com/akaihola>`_)
- "Dull Bananas" <dull.bananas0@gmail.com> (`@dullbananas <https://github.com/dullbananas>`_)
- Alessio Izzo (`@aless10 <https://github.com/aless10>`_)
- Belavin Denis (`@luckydenis <https://github.com/luckydenis>`_)
23 changes: 3 additions & 20 deletions requests/models.py
Expand Up @@ -641,10 +641,6 @@ def __init__(self):
#: is a response.
self.request = None

#: If there was an error in the processing of content,
#: then save the error that would return the same error when you re-appeal.
self._error = None

def __enter__(self):
return self

Expand Down Expand Up @@ -754,21 +750,12 @@ def generate():
try:
for chunk in self.raw.stream(chunk_size, decode_content=True):
yield chunk

except ProtocolError as e:
self._error = ChunkedEncodingError(e)

raise ChunkedEncodingError(e)
except DecodeError as e:
self._error = ContentDecodingError(e)

raise ContentDecodingError(e)
except ReadTimeoutError as e:
self._error = ConnectionError(e)

finally:
# if we had an error - throw the saved error
if self._error:
raise self._error

raise ConnectionError(e)
else:
# Standard file-like object.
while True:
Expand Down Expand Up @@ -841,10 +828,6 @@ def content(self):
else:
self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''

# if we had an error - throw the saved error
if self._error is not None:
raise self._error

self._content_consumed = True
# don't need to release the connection; that's been handled by urllib3
# since we exhausted the data.
Expand Down
41 changes: 0 additions & 41 deletions tests/test_lowlevel.py
Expand Up @@ -3,7 +3,6 @@
import pytest
import threading
import requests
from requests.exceptions import ChunkedEncodingError

from tests.testserver.server import Server, consume_socket_content

Expand Down Expand Up @@ -308,43 +307,3 @@ def response_handler(sock):
assert r.url == 'http://{}:{}/final-url/#relevant-section'.format(host, port)

close_server.set()


def test_response_content_retains_error():
"""Verify that accessing response.content retains an error.
See https://github.com/kennethreitz/requests/issues/4965
"""

data = "Some random stuff to read from remove server.\n"

def response_handler(sock):
req = consume_socket_content(sock, timeout=0.5)

# Send invalid chunked data (length mismatch)
sock.send(
b'HTTP/1.1 200 OK\r\n'
b'Transfer-Encoding: chunked\r\n'
b'\r\n2\r\n42\r\n8\r\n123\r\n' # 5 bytes missing
)

close_server = threading.Event()
server = Server(response_handler, wait_to_close_event=close_server)

with server as (host, port):
url = 'http://{}:{}/path'.format(host, port)
r = requests.post(url, stream=True)
with pytest.raises(ChunkedEncodingError):
r.content

# Access the bad response data again, I would expect the same
# error again.

try:
content = r.content
except ChunkedEncodingError:
pass # fine, same exception
else:
assert False, "error response has content: {0!r}".format(content)
close_server.set()

0 comments on commit 22ed079

Please sign in to comment.