Skip to content
Myr

Revert "#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.

This reverts commit bd100472443dcf4189630f6b1ed3ec0a61259dd8. This reverts commit d91fe00983f1c66b9a428fe1ad15bf6ea8238972.

authored by Nate Prewitt · committed by Seth Michael Larson · parent 80011a7 · Browse files · 3 files+3 added−62 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+3 added−62 removed

  • tests:+0 added−41 removed
  • other:+3 added−21 removed

3 files+3 added−62 removed

modified:requests/models.py+3 added-20 removed
639 unmodified lines
640 #: The :class:`PreparedRequest <PreparedRequest>` object to which this641 #: is a response.642 self.request = None643644 #: If there was an error in the processing of content,645 #: then save the error that would return the same error when you re-appeal.646 self._error = None647644 def __enter__(self):645 return self646647 def __exit__(self, *args):
101 unmodified lines
749 if hasattr(self.raw, 'stream'):750 try:751 for chunk in self.raw.stream(chunk_size, decode_content=True):752 yield chunk757753 except ProtocolError as e:759 self._error = ChunkedEncodingError(e)760754 raise ChunkedEncodingError(e)755 except DecodeError as e:762 self._error = ContentDecodingError(e)763756 raise ContentDecodingError(e)757 except ReadTimeoutError as e:765 self._error = ConnectionError(e)766767 finally:768 # if we had an error - throw the saved error769 if self._error:770 raise self._error771758 raise ConnectionError(e)759 else:760 # Standard file-like object.761 while True:762 chunk = self.raw.read(chunk_size)
64 unmodified lines
827 self._content = None828 else:829 self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''830844 # if we had an error - throw the saved error845 if self._error is not None:846 raise self._error847831 self._content_consumed = True832 # don't need to release the connection; that's been handled by urllib3833 # since we exhausted the data.834 return self._content
123 unmodified lines
modified:tests/test_lowlevel.py-41 removed
1 unmodified line
23import pytest4import threading5import requests6from requests.exceptions import ChunkedEncodingError67from tests.testserver.server import Server, consume_socket_content89from .utils import override_environ
296 unmodified lines
306 # Verify previous fragment is used and not the original.307 assert r.url == 'http://{}:{}/final-url/#relevant-section'.format(host, port)308309 close_server.set()311312313def test_response_content_retains_error():314 """Verify that accessing response.content retains an error.315316 See https://github.com/kennethreitz/requests/issues/4965317 """318319 data = "Some random stuff to read from remove server.\n"320321 def response_handler(sock):322 req = consume_socket_content(sock, timeout=0.5)323324 # 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 )330331 close_server = threading.Event()332 server = Server(response_handler, wait_to_close_event=close_server)333334 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.content339340 # Access the bad response data again, I would expect the same341 # error again.342343 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-1 removed
187 unmodified lines
188- Rajiv Mayani (`@mayani <https://github.com/mayani>`_)189- Antti Kaihola (`@akaihola <https://github.com/akaihola>`_)190- "Dull Bananas" <dull.bananas0@gmail.com> (`@dullbananas <https://github.com/dullbananas>`_)191- Alessio Izzo (`@aless10 <https://github.com/aless10>`_)192- Belavin Denis (`@luckydenis <https://github.com/luckydenis>`_)