|
8 | 8 | from collections.abc import AsyncIterable
|
9 | 9 | from pathlib import Path
|
10 | 10 |
|
| 11 | +from ._compat import aclosing |
11 | 12 | from ._types import (
|
12 | 13 | AsyncByteStream,
|
13 | 14 | FileContent,
|
@@ -219,7 +220,7 @@ def render_data(self) -> typing.Iterator[bytes]:
|
219 | 220 | yield to_bytes(chunk)
|
220 | 221 | chunk = self.file.read(self.CHUNK_SIZE)
|
221 | 222 |
|
222 |
| - async def arender_data(self) -> typing.AsyncIterator[bytes]: |
| 223 | + async def arender_data(self) -> typing.AsyncGenerator[bytes]: |
223 | 224 | if not isinstance(self.file, AsyncIterable):
|
224 | 225 | for chunk in self.render_data():
|
225 | 226 | yield chunk
|
@@ -247,10 +248,11 @@ def render(self) -> typing.Iterator[bytes]:
|
247 | 248 | yield self.render_headers()
|
248 | 249 | yield from self.render_data()
|
249 | 250 |
|
250 |
| - async def arender(self) -> typing.AsyncIterator[bytes]: |
| 251 | + async def arender(self) -> typing.AsyncGenerator[bytes]: |
251 | 252 | yield self.render_headers()
|
252 |
| - async for chunk in self.arender_data(): |
253 |
| - yield chunk |
| 253 | + async with aclosing(self.arender_data()) as data: |
| 254 | + async for chunk in data: |
| 255 | + yield chunk |
254 | 256 |
|
255 | 257 |
|
256 | 258 | class MultipartStream(SyncByteStream, AsyncByteStream):
|
@@ -294,12 +296,13 @@ def iter_chunks(self) -> typing.Iterator[bytes]:
|
294 | 296 | yield b"\r\n"
|
295 | 297 | yield b"--%s--\r\n" % self.boundary
|
296 | 298 |
|
297 |
| - async def aiter_chunks(self) -> typing.AsyncIterator[bytes]: |
| 299 | + async def aiter_chunks(self) -> typing.AsyncGenerator[bytes]: |
298 | 300 | for field in self.fields:
|
299 | 301 | yield b"--%s\r\n" % self.boundary
|
300 | 302 | if isinstance(field, FileField):
|
301 |
| - async for chunk in field.arender(): |
302 |
| - yield chunk |
| 303 | + async with aclosing(field.arender()) as data: |
| 304 | + async for chunk in data: |
| 305 | + yield chunk |
303 | 306 | else:
|
304 | 307 | for chunk in field.render():
|
305 | 308 | yield chunk
|
@@ -340,5 +343,6 @@ def __iter__(self) -> typing.Iterator[bytes]:
|
340 | 343 | yield chunk
|
341 | 344 |
|
342 | 345 | async def __aiter__(self) -> typing.AsyncIterator[bytes]:
|
343 |
| - async for chunk in self.aiter_chunks(): |
344 |
| - yield chunk |
| 346 | + async with aclosing(self.aiter_chunks()) as data: |
| 347 | + async for chunk in data: |
| 348 | + yield chunk |
0 commit comments