From c3fe9802ee8cb649785bf4333d307bafc1051ab9 Mon Sep 17 00:00:00 2001 From: Alexandros Tzannes Date: Fri, 14 Mar 2025 17:08:38 -0400 Subject: [PATCH 1/2] Use io.BytesIO() instead of List[bytes] + b"".join() --- aiohttp/streams.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/aiohttp/streams.py b/aiohttp/streams.py index db22f162396..aaa20c08848 100644 --- a/aiohttp/streams.py +++ b/aiohttp/streams.py @@ -1,5 +1,6 @@ import asyncio import collections +import io import warnings from typing import ( Awaitable, @@ -399,12 +400,13 @@ async def read(self, n: int = -1) -> bytes: # deadlock if the subprocess sends more than self.limit # bytes. So just call self.readany() until EOF. blocks = [] + buffer = io.BytesIO() while True: block = await self.readany() if not block: break - blocks.append(block) - return b"".join(blocks) + buffer.write(block) + return buffer.getvalue() # TODO: should be `if` instead of `while` # because waiter maybe triggered on chunk end, @@ -464,16 +466,16 @@ async def readexactly(self, n: int) -> bytes: if self._exception is not None: raise self._exception - blocks: List[bytes] = [] + buffer = io.BytesIO() while n > 0: block = await self.read(n) if not block: - partial = b"".join(blocks) + partial = buffer.getvalue() raise asyncio.IncompleteReadError(partial, len(partial) + n) - blocks.append(block) + buffer.write(block) n -= len(block) - return b"".join(blocks) + return buffer.getvalue() def read_nowait(self, n: int = -1) -> bytes: # default was changed to be consistent with .read(-1) @@ -523,15 +525,16 @@ def _read_nowait(self, n: int) -> bytes: self._timer.assert_timeout() chunks = [] + buffer = io.BytesIO() while self._buffer: chunk = self._read_nowait_chunk(n) - chunks.append(chunk) + buffer.write(chunk) if n != -1: n -= len(chunk) if n == 0: break - return b"".join(chunks) if chunks else b"" + return buffer.getvalue() class EmptyStreamReader(StreamReader): # lgtm [py/missing-call-to-init] From a6969b40e64d69fc6b038355203984ded0415a8f Mon Sep 17 00:00:00 2001 From: Alexandros Tzannes Date: Tue, 18 Mar 2025 17:21:48 -0400 Subject: [PATCH 2/2] Clean up unused code --- aiohttp/streams.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/aiohttp/streams.py b/aiohttp/streams.py index aaa20c08848..bf090cd8925 100644 --- a/aiohttp/streams.py +++ b/aiohttp/streams.py @@ -399,7 +399,6 @@ async def read(self, n: int = -1) -> bytes: # collect everything in self._buffer, but that would # deadlock if the subprocess sends more than self.limit # bytes. So just call self.readany() until EOF. - blocks = [] buffer = io.BytesIO() while True: block = await self.readany() @@ -524,7 +523,6 @@ def _read_nowait(self, n: int) -> bytes: """Read not more than n bytes, or whole buffer if n == -1""" self._timer.assert_timeout() - chunks = [] buffer = io.BytesIO() while self._buffer: chunk = self._read_nowait_chunk(n)