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
Provenance
- Commit
b15056d- Tree
f5c3bf3- Author
Nate Prewitt <Nate.Prewitt@gmail.com>- Committer
Seth Michael Larson <sethmichaellarson@gmail.com>- Parent
80011a7
3 files changed+3 added−62 removed
modified:requests/models.py+3 added-20 removed
639 unmodified lines640 #: The :class:`PreparedRequest <PreparedRequest>` object to which this641 #: is a response.642 self.request = None643 644- #: 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 = None647-644 def __enter__(self):645 return self646 647 def __exit__(self, *args):101 unmodified lines749 if hasattr(self.raw, 'stream'):750 try:751 for chunk in self.raw.stream(chunk_size, decode_content=True):752 yield chunk757-753 except ProtocolError as e:759- self._error = ChunkedEncodingError(e)760-754+ raise ChunkedEncodingError(e)755 except DecodeError as e:762- self._error = ContentDecodingError(e)763-756+ raise ContentDecodingError(e)757 except ReadTimeoutError as e:765- self._error = ConnectionError(e)766-767- finally:768- # if we had an error - throw the saved error769- if self._error:770- raise self._error771-758+ raise ConnectionError(e)759 else:760 # Standard file-like object.761 while True:762 chunk = self.raw.read(chunk_size)64 unmodified lines827 self._content = None828 else:829 self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''830 844- # if we had an error - throw the saved error845- if self._error is not None:846- raise self._error847-831 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._content123 unmodified linesmodified:tests/test_lowlevel.py-41 removed
1 unmodified line2 3 import pytest4 import threading5 import requests6-from requests.exceptions import ChunkedEncodingError6 7 from tests.testserver.server import Server, consume_socket_content8 9 from .utils import override_environ296 unmodified lines306 # Verify previous fragment is used and not the original.307 assert r.url == 'http://{}:{}/final-url/#relevant-section'.format(host, port)308 309 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-1 removed
187 unmodified lines188 - 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>`_)