Merge pull request #5087 from LuckyDenis/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
#4965 Fix
authored by Kenneth Reitz · committed by GitHub · parents 42c5a4f, e8ff489 · Browse files · 3 files·+63 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+63 added−4 removed
- tests:+41 added−0 removed
- other:+22 added−4 removed
Provenance
- Commit
67a7b2e- Tree
46d6da4- Author
Kenneth Reitz <me@kennethreitz.org>- Committer
GitHub <noreply@github.com>- Parent 1
42c5a4f- Parent 2
e8ff489
3 files changed+63 added−4 removed
modified:requests/models.py+20 added-3 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.645+ self._error = None646+647 def __enter__(self):648 return self649 650 def __exit__(self, *args):101 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:753- raise ChunkedEncodingError(e)758+ self._error = ChunkedEncodingError(e)759+760 except DecodeError as e:755- raise ContentDecodingError(e)761+ self._error = ContentDecodingError(e)762+763 except ReadTimeoutError as e:757- 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 = True848 # don't need to release the connection; that's been handled by urllib3849 # since we exhausted the data.850 return self._content120 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+modified:AUTHORS.rst+2 added-1 removed
187 unmodified lines188 - Demetrios Bairaktaris (`@DemetriosBairaktaris <https://github.com/demetriosbairaktaris>`_)189 - Darren Dormer (`@ddormer <https://github.com/ddormer>`_)190 - Rajiv Mayani (`@mayani <https://github.com/mayani>`_)191 - Antti Kaihola (`@akaihola <https://github.com/akaihola>`_)192+- Belavin Denis (`@luckydenis <https://github.com/luckydenis>`_)192-- "Dull Bananas" <dull.bananas0@gmail.com> (`@dullbananas <https://github.com/dullbananas>`_)193+- Dull Bananas <dull.bananas0@gmail.com> (`@dullbananas <https://github.com/dullbananas>`_)