From bc9e5d6676906e4bef10fcb499d5f233ed2c394e Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Thu, 10 Oct 2024 13:44:02 +0000 Subject: [PATCH] [PR #9448/93e87c2e backport][3.10] Improve performance of fetching the content-length for web responses (#9449) Co-authored-by: J. Nick Koston --- aiohttp/helpers.py | 6 +----- aiohttp/web_response.py | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/aiohttp/helpers.py b/aiohttp/helpers.py index 1ea6a56db46..6ee70786cfb 100644 --- a/aiohttp/helpers.py +++ b/aiohttp/helpers.py @@ -814,11 +814,7 @@ def charset(self) -> Optional[str]: def content_length(self) -> Optional[int]: """The value of Content-Length HTTP header.""" content_length = self._headers.get(hdrs.CONTENT_LENGTH) - - if content_length is not None: - return int(content_length) - else: - return None + return None if content_length is None else int(content_length) def set_result(fut: "asyncio.Future[_T]", result: _T) -> None: diff --git a/aiohttp/web_response.py b/aiohttp/web_response.py index 4d5095a4fea..2036a8d088b 100644 --- a/aiohttp/web_response.py +++ b/aiohttp/web_response.py @@ -709,7 +709,7 @@ def content_length(self) -> Optional[int]: return None if hdrs.CONTENT_LENGTH in self._headers: - return super().content_length + return int(self._headers[hdrs.CONTENT_LENGTH]) if self._compressed_body is not None: # Return length of the compressed body