Skip to content

Commit

Permalink
Add __slots__ to stream classes
Browse files Browse the repository at this point in the history
We use `__slots__` almost everywhere else in the codebase, however `__slots__`
were not implemented in stream

related issue #4618
  • Loading branch information
bdraco committed Oct 3, 2024
1 parent 3f43bd1 commit 8c2aae2
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions aiohttp/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class EofStream(Exception):


class AsyncStreamIterator(Generic[_T]):

__slots__ = ("read_func",)

def __init__(self, read_func: Callable[[], Awaitable[_T]]) -> None:
self.read_func = read_func

Expand All @@ -59,6 +62,9 @@ async def __anext__(self) -> _T:


class ChunkTupleAsyncStreamIterator:

__slots__ = ("_stream",)

def __init__(self, stream: "StreamReader") -> None:
self._stream = stream

Expand Down Expand Up @@ -107,6 +113,25 @@ class StreamReader(AsyncStreamReaderMixin):
"""

__slots__ = (
"_protocol",
"_low_water",
"_high_water",
"_loop",
"_size",
"_cursor",
"_http_chunk_splits",
"_buffer",
"_buffer_offset",
"_eof",
"_waiter",
"_eof_waiter",
"_exception",
"_timer",
"_eof_callbacks",
"total_bytes",
)

total_bytes = 0

def __init__(
Expand Down Expand Up @@ -505,6 +530,9 @@ def _read_nowait(self, n: int) -> bytes:


class EmptyStreamReader(StreamReader): # lgtm [py/missing-call-to-init]

Check failure

Code scanning / CodeQL

Missing call to `__init__` during object initialization Error

Class EmptyStreamReader may not be initialized properly as
method StreamReader.__init__
is not called from its
__init__ method
.

__slots__ = ("_read_eof_chunk",)

def __init__(self) -> None:
self._read_eof_chunk = False

Expand Down Expand Up @@ -573,6 +601,15 @@ def read_nowait(self, n: int = -1) -> bytes:
class DataQueue(Generic[_SizedT]):
"""DataQueue is a general-purpose blocking queue with one reader."""

__slots__ = (
"_loop",
"_eof",
"_waiter",
"_exception",
"_size",
"_buffer",
)

def __init__(self, loop: asyncio.AbstractEventLoop) -> None:
self._loop = loop
self._eof = False
Expand Down Expand Up @@ -653,6 +690,8 @@ class FlowControlDataQueue(DataQueue[_SizedT]):
It is a destination for parsed data.
"""

__slots__ = ("_protocol", "_limit")

def __init__(
self, protocol: BaseProtocol, limit: int, *, loop: asyncio.AbstractEventLoop
) -> None:
Expand Down

0 comments on commit 8c2aae2

Please sign in to comment.