|
6 | 6 | from collections.abc import AsyncIterable
|
7 | 7 | from pathlib import Path
|
8 | 8 |
|
| 9 | +from ._compat import aclosing |
9 | 10 | from ._types import (
|
10 | 11 | AsyncByteStream,
|
11 | 12 | FileContent,
|
@@ -188,7 +189,7 @@ def render_data(self) -> typing.Iterator[bytes]:
|
188 | 189 | yield to_bytes(chunk)
|
189 | 190 | chunk = self.file.read(self.CHUNK_SIZE)
|
190 | 191 |
|
191 |
| - async def arender_data(self) -> typing.AsyncIterator[bytes]: |
| 192 | + async def arender_data(self) -> typing.AsyncGenerator[bytes]: |
192 | 193 | if not isinstance(self.file, AsyncIterable):
|
193 | 194 | for chunk in self.render_data():
|
194 | 195 | yield chunk
|
@@ -216,10 +217,11 @@ def render(self) -> typing.Iterator[bytes]:
|
216 | 217 | yield self.render_headers()
|
217 | 218 | yield from self.render_data()
|
218 | 219 |
|
219 |
| - async def arender(self) -> typing.AsyncIterator[bytes]: |
| 220 | + async def arender(self) -> typing.AsyncGenerator[bytes]: |
220 | 221 | yield self.render_headers()
|
221 |
| - async for chunk in self.arender_data(): |
222 |
| - yield chunk |
| 222 | + async with aclosing(self.arender_data()) as data: |
| 223 | + async for chunk in data: |
| 224 | + yield chunk |
223 | 225 |
|
224 | 226 |
|
225 | 227 | class MultipartStream(SyncByteStream, AsyncByteStream):
|
@@ -263,12 +265,13 @@ def iter_chunks(self) -> typing.Iterator[bytes]:
|
263 | 265 | yield b"\r\n"
|
264 | 266 | yield b"--%s--\r\n" % self.boundary
|
265 | 267 |
|
266 |
| - async def aiter_chunks(self) -> typing.AsyncIterator[bytes]: |
| 268 | + async def aiter_chunks(self) -> typing.AsyncGenerator[bytes]: |
267 | 269 | for field in self.fields:
|
268 | 270 | yield b"--%s\r\n" % self.boundary
|
269 | 271 | if isinstance(field, FileField):
|
270 |
| - async for chunk in field.arender(): |
271 |
| - yield chunk |
| 272 | + async with aclosing(field.arender()) as data: |
| 273 | + async for chunk in data: |
| 274 | + yield chunk |
272 | 275 | else:
|
273 | 276 | for chunk in field.render():
|
274 | 277 | yield chunk
|
@@ -309,5 +312,6 @@ def __iter__(self) -> typing.Iterator[bytes]:
|
309 | 312 | yield chunk
|
310 | 313 |
|
311 | 314 | async def __aiter__(self) -> typing.AsyncIterator[bytes]:
|
312 |
| - async for chunk in self.aiter_chunks(): |
313 |
| - yield chunk |
| 315 | + async with aclosing(self.aiter_chunks()) as data: |
| 316 | + async for chunk in data: |
| 317 | + yield chunk |
0 commit comments