#4965 fix: Accessing response.content twice removes forgets read error.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Белавин Денис · committed by Белавин Денис · parent d91fe00 · Browse files · 2 files·+57 added−7 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+57 added−7 removed
- tests:+41 added−0 removed
- other:+16 added−7 removed
Provenance
- Commit
bd10047- Tree
30a3c95- Author
Белавин Денис <luckydenis@yandex-team.ru>- Committer
Белавин Денис <luckydenis@yandex-team.ru>- Parent
d91fe00
2 files changed+57 added−7 removed
modified:requests/models.py+16 added-7 removed
638 unmodified lines639 #: The :class:`PreparedRequest <PreparedRequest>` object to which this640 #: is a response.641 self.request = None642 643+ #: If there was an error in the processing of content,644+ #: then save the error that would return the same error when you re-appeal.643- self.error = None645+ self._error = None646 647 def __enter__(self):648 return self649 102 unmodified lines752 if hasattr(self.raw, 'stream'):753 try:754 for chunk in self.raw.stream(chunk_size, decode_content=True):755 yield chunk756+757 except ProtocolError as e:755- self._error = e756- raise ChunkedEncodingError(e)758+ self._error = ChunkedEncodingError(e)759+760 except DecodeError as e:758- self._error = e759- raise ContentDecodingError(e)761+ self._error = ContentDecodingError(e)762+763 except ReadTimeoutError as e:761- self._error = e762- raise ConnectionError(e)764+ self._error = ConnectionError(e)765+766+ finally:767+ # if we had an error - throw the saved error768+ if self._error:769+ raise self._error770+771 else:772 # Standard file-like object.773 while True:774 chunk = self.raw.read(chunk_size)64 unmodified lines839 self._content = None840 else:841 self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''842 843+ # if we had an error - throw the saved error844 if self._error is not None:845 raise self._error846 847 self._content_consumed = True123 unmodified linesmodified:tests/test_lowlevel.py+41 added
1 unmodified line2 3 import pytest4 import threading5 import requests6+from requests.exceptions import ChunkedEncodingError7 8 from tests.testserver.server import Server, consume_socket_content9 10 from .utils import override_environ296 unmodified lines307 # Verify previous fragment is used and not the original.308 assert r.url == 'http://{}:{}/final-url/#relevant-section'.format(host, port)309 310 close_server.set()311+312+313+def test_response_content_retains_error():314+ """Verify that accessing response.content retains an error.315+316+ See https://github.com/kennethreitz/requests/issues/4965317+ """318+319+ data = "Some random stuff to read from remove server.\n"320+321+ def response_handler(sock):322+ req = consume_socket_content(sock, timeout=0.5)323+324+ # Send invalid chunked data (length mismatch)325+ sock.send(326+ b'HTTP/1.1 200 OK\r\n'327+ b'Transfer-Encoding: chunked\r\n'328+ b'\r\n2\r\n42\r\n8\r\n123\r\n' # 5 bytes missing329+ )330+331+ close_server = threading.Event()332+ server = Server(response_handler, wait_to_close_event=close_server)333+334+ with server as (host, port):335+ url = 'http://{}:{}/path'.format(host, port)336+ r = requests.post(url, stream=True)337+ with pytest.raises(ChunkedEncodingError):338+ r.content339+340+ # Access the bad response data again, I would expect the same341+ # error again.342+343+ try:344+ content = r.content345+ except ChunkedEncodingError:346+ pass # fine, same exception347+ else:348+ assert False, "error response has content: {0!r}".format(content)349+ close_server.set()350+