Make body_iter async generator compatible with MicroPython
This commit is contained in:
@@ -88,7 +88,7 @@ class Microdot(BaseMicrodot):
|
|||||||
'status': res.status_code,
|
'status': res.status_code,
|
||||||
'headers': [(name, value)
|
'headers': [(name, value)
|
||||||
for name, value in res.headers.items()]})
|
for name, value in res.headers.items()]})
|
||||||
body_iter = res.body_iter()
|
body_iter = res.body_iter().__aiter__()
|
||||||
body = b''
|
body = b''
|
||||||
try:
|
try:
|
||||||
body += await body_iter.__anext__()
|
body += await body_iter.__anext__()
|
||||||
|
|||||||
@@ -136,24 +136,38 @@ class Response(BaseResponse):
|
|||||||
async for body in self.body_iter():
|
async for body in self.body_iter():
|
||||||
await stream.awrite(body)
|
await stream.awrite(body)
|
||||||
|
|
||||||
async def body_iter(self):
|
def body_iter(self):
|
||||||
if self.body:
|
response = self
|
||||||
if hasattr(self.body, 'read'):
|
|
||||||
while True:
|
class iter:
|
||||||
buf = self.body.read(self.send_file_buffer_size)
|
def __aiter__(self):
|
||||||
if _iscoroutine(buf): # pragma: no cover
|
if response.body:
|
||||||
buf = await buf
|
self.i = 0
|
||||||
if len(buf):
|
else:
|
||||||
print('*', buf, self.send_file_buffer_size)
|
self.i = -1
|
||||||
yield buf
|
return self
|
||||||
if len(buf) < self.send_file_buffer_size:
|
|
||||||
break
|
async def __anext__(self):
|
||||||
if hasattr(self.body, 'close'): # pragma: no cover
|
if self.i == -1:
|
||||||
result = self.body.close()
|
raise StopAsyncIteration
|
||||||
if _iscoroutine(result):
|
if self.i == 0:
|
||||||
await result
|
if not hasattr(response.body, 'read'):
|
||||||
else:
|
self.i = -1
|
||||||
yield self.body
|
return response.body
|
||||||
|
else:
|
||||||
|
self.i = 1
|
||||||
|
buf = response.body.read(response.send_file_buffer_size)
|
||||||
|
if _iscoroutine(buf): # pragma: no cover
|
||||||
|
buf = await buf
|
||||||
|
if len(buf) < response.send_file_buffer_size:
|
||||||
|
self.i = -1
|
||||||
|
if hasattr(response.body, 'close'): # pragma: no cover
|
||||||
|
result = response.body.close()
|
||||||
|
if _iscoroutine(result):
|
||||||
|
await result
|
||||||
|
return buf
|
||||||
|
|
||||||
|
return iter()
|
||||||
|
|
||||||
|
|
||||||
class Microdot(BaseMicrodot):
|
class Microdot(BaseMicrodot):
|
||||||
|
|||||||
@@ -65,7 +65,8 @@ class TestMicrodotASGI(unittest.TestCase):
|
|||||||
packet['headers'],
|
packet['headers'],
|
||||||
[('Content-Length', '8'), ('Content-Type', 'text/plain')])
|
[('Content-Length', '8'), ('Content-Type', 'text/plain')])
|
||||||
elif packet['type'] == 'http.response.body':
|
elif packet['type'] == 'http.response.body':
|
||||||
self.assertIn(packet['body'], [b're', b'sp', b'on', b'se'])
|
self.assertIn(packet['body'],
|
||||||
|
[b're', b'sp', b'on', b'se', b''])
|
||||||
|
|
||||||
original_buffer_size = Response.send_file_buffer_size
|
original_buffer_size = Response.send_file_buffer_size
|
||||||
Response.send_file_buffer_size = 2
|
Response.send_file_buffer_size = 2
|
||||||
|
|||||||
Reference in New Issue
Block a user