Skip to content

Commit b0b8ebc

Browse files
miketeachmandpgeorge
authored andcommitted
extmod/uasyncio: Add readinto() method to Stream class.
With docs and a multi-test using TCP server/client. This method is a MicroPython extension, although there is discussion of adding it to CPython: https://bugs.python.org/issue41305 Signed-off-by: Mike Teachman <[email protected]>
1 parent 9504812 commit b0b8ebc

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

docs/library/uasyncio.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,14 @@ TCP stream connections
240240

241241
This is a coroutine.
242242

243+
.. method:: Stream.readinto(buf)
244+
245+
Read up to n bytes into *buf* with n being equal to the length of *buf*.
246+
247+
Return the number of bytes read into *buf*.
248+
249+
This is a coroutine, and a MicroPython extension.
250+
243251
.. method:: Stream.readline()
244252

245253
Read a line and return it.

extmod/uasyncio/stream.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ async def read(self, n):
3030
yield core._io_queue.queue_read(self.s)
3131
return self.s.read(n)
3232

33+
async def readinto(self, buf):
34+
yield core._io_queue.queue_read(self.s)
35+
return self.s.readinto(buf)
36+
3337
async def readexactly(self, n):
3438
r = b""
3539
while n:
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Test uasyncio stream readinto() method using TCP server/client
2+
3+
try:
4+
import uasyncio as asyncio
5+
except ImportError:
6+
try:
7+
import asyncio
8+
except ImportError:
9+
print("SKIP")
10+
raise SystemExit
11+
12+
try:
13+
import uarray as array
14+
except ImportError:
15+
try:
16+
import array
17+
except ImportError:
18+
print("SKIP")
19+
raise SystemExit
20+
21+
PORT = 8000
22+
23+
24+
async def handle_connection(reader, writer):
25+
writer.write(b"ab")
26+
await writer.drain()
27+
28+
writer.write(b"c")
29+
await writer.drain()
30+
31+
print("close")
32+
writer.close()
33+
await writer.wait_closed()
34+
35+
print("done")
36+
ev.set()
37+
38+
39+
async def tcp_server():
40+
global ev
41+
ev = asyncio.Event()
42+
server = await asyncio.start_server(handle_connection, "0.0.0.0", PORT)
43+
print("server running")
44+
multitest.next()
45+
async with server:
46+
await asyncio.wait_for(ev.wait(), 10)
47+
48+
49+
async def tcp_client():
50+
reader, writer = await asyncio.open_connection(IP, PORT)
51+
52+
ba = bytearray(2)
53+
n = await reader.readinto(ba)
54+
print(n)
55+
print(ba[:n])
56+
57+
a = array.array("b", [0, 0])
58+
n = await reader.readinto(a)
59+
print(n)
60+
print(a[:n])
61+
62+
try:
63+
n = await reader.readinto(5)
64+
except TypeError as er:
65+
print("TypeError")
66+
67+
try:
68+
n = await reader.readinto()
69+
except TypeError as er:
70+
print("TypeError")
71+
72+
73+
def instance0():
74+
multitest.globals(IP=multitest.get_network_ip())
75+
asyncio.run(tcp_server())
76+
77+
78+
def instance1():
79+
multitest.next()
80+
asyncio.run(tcp_client())
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--- instance0 ---
2+
server running
3+
close
4+
done
5+
--- instance1 ---
6+
2
7+
bytearray(b'ab')
8+
1
9+
array('b', [99])
10+
TypeError
11+
TypeError

0 commit comments

Comments
 (0)