Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid ResourceWarning in DataBody.__aiter__ #302

Merged
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/quart/wrappers/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,8 @@ async def __aenter__(self) -> DataBody:
async def __aexit__(self, exc_type: type, exc_value: BaseException, tb: TracebackType) -> None:
pass

def __aiter__(self) -> AsyncIterator:
async def _aiter() -> AsyncGenerator[bytes, None]:
yield self.data[self.begin : self.end]

return _aiter()
def __aiter__(self) -> AsyncIterator[bytes]:
return _DataBodyGen(self)

async def make_conditional(self, begin: int, end: int | None) -> int:
self.begin = begin
Expand All @@ -91,6 +88,19 @@ async def make_conditional(self, begin: int, end: int | None) -> int:
return len(self.data)


class _DataBodyGen(AsyncIterator[bytes]):
def __init__(self, data_body: DataBody):
self._data_body = data_body
self._iterated = False

async def __anext__(self) -> bytes:
if self._iterated:
raise StopAsyncIteration

self._iterated = True
return self._data_body.body[self._data_body.begin : self._data_body.end]
graingert marked this conversation as resolved.
Show resolved Hide resolved


class IterableBody(ResponseBody):
def __init__(self, iterable: AsyncGenerator[bytes, None] | Iterable) -> None:
self.iter: AsyncGenerator[bytes, None]
Expand Down
Loading