From 58b6f368c33eaae0f25f48ce04e1178ca981a26b Mon Sep 17 00:00:00 2001 From: hellozzm Date: Wed, 6 May 2026 09:06:57 +0800 Subject: [PATCH] Fix: retain stream read error on subsequent response.content access When accessing response.content raises an exception during stream reading, subsequent accesses would silently return an empty bytestring instead of re-raising the original error. This made debugging difficult, especially in debuggers where properties may be accessed multiple times. The fix stores the exception and re-raises it on subsequent accesses. Fixes #4965 --- src/requests/models.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/requests/models.py b/src/requests/models.py index 4142f2a4bb..6ba4ab3e41 100644 --- a/src/requests/models.py +++ b/src/requests/models.py @@ -1039,7 +1039,14 @@ def content(self) -> bytes: if self.status_code == 0 or self.raw is None: self._content = None else: - self._content = b"".join(self.iter_content(CONTENT_CHUNK_SIZE)) or b"" + try: + self._content = b"".join(self.iter_content(CONTENT_CHUNK_SIZE)) or b"" + except Exception as exc: + self._content_error = exc + raise + + if hasattr(self, "_content_error"): + raise self._content_error self._content_consumed = True # don't need to release the connection; that's been handled by urllib3