micropython: add micropython component
This commit is contained in:
89
components/language/micropython/tests/extmod/btree1.py
Normal file
89
components/language/micropython/tests/extmod/btree1.py
Normal file
@@ -0,0 +1,89 @@
|
||||
try:
|
||||
import btree
|
||||
import uio
|
||||
import uerrno
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
# f = open("_test.db", "w+b")
|
||||
f = uio.BytesIO()
|
||||
db = btree.open(f, pagesize=512)
|
||||
|
||||
db[b"foo3"] = b"bar3"
|
||||
db[b"foo1"] = b"bar1"
|
||||
db[b"foo2"] = b"bar2"
|
||||
db[b"bar1"] = b"foo1"
|
||||
|
||||
dbstr = str(db)
|
||||
print(dbstr[:7], dbstr[-1:])
|
||||
|
||||
print(db[b"foo2"])
|
||||
try:
|
||||
print(db[b"foo"])
|
||||
except KeyError:
|
||||
print("KeyError")
|
||||
print(db.get(b"foo"))
|
||||
print(db.get(b"foo", b"dflt"))
|
||||
|
||||
del db[b"foo2"]
|
||||
try:
|
||||
del db[b"foo"]
|
||||
except KeyError:
|
||||
print("KeyError")
|
||||
|
||||
for k, v in db.items():
|
||||
print((k, v))
|
||||
|
||||
print("---")
|
||||
for k, v in db.items(None, None):
|
||||
print((k, v))
|
||||
|
||||
print("---")
|
||||
for k, v in db.items(b"f"):
|
||||
print((k, v))
|
||||
|
||||
print("---")
|
||||
for k, v in db.items(b"f", b"foo3"):
|
||||
print((k, v))
|
||||
|
||||
print("---")
|
||||
for k, v in db.items(None, b"foo3"):
|
||||
print((k, v))
|
||||
|
||||
print("---")
|
||||
for k, v in db.items(b"f", b"foo3", btree.INCL):
|
||||
print((k, v))
|
||||
|
||||
print("---")
|
||||
for k, v in db.items(None, None, btree.DESC):
|
||||
print((k, v))
|
||||
|
||||
print(db.seq(1, b"foo1"))
|
||||
print(db.seq(1, b"qux"))
|
||||
|
||||
try:
|
||||
db.seq(b"foo1")
|
||||
except OSError as e:
|
||||
print(e.errno == uerrno.EINVAL)
|
||||
|
||||
print(list(db.keys()))
|
||||
print(list(db.values()))
|
||||
|
||||
for k in db:
|
||||
print(k)
|
||||
|
||||
db.put(b"baz1", b"qux1")
|
||||
|
||||
print("foo1", "foo1" in db)
|
||||
print("foo2", "foo2" in db)
|
||||
print("baz1", "baz1" in db)
|
||||
|
||||
try:
|
||||
print(db + db[b"foo1"])
|
||||
except TypeError:
|
||||
print("TypeError")
|
||||
|
||||
db.flush()
|
||||
db.close()
|
||||
f.close()
|
40
components/language/micropython/tests/extmod/btree1.py.exp
Normal file
40
components/language/micropython/tests/extmod/btree1.py.exp
Normal file
@@ -0,0 +1,40 @@
|
||||
<btree >
|
||||
b'bar2'
|
||||
KeyError
|
||||
None
|
||||
b'dflt'
|
||||
KeyError
|
||||
(b'bar1', b'foo1')
|
||||
(b'foo1', b'bar1')
|
||||
(b'foo3', b'bar3')
|
||||
---
|
||||
(b'bar1', b'foo1')
|
||||
(b'foo1', b'bar1')
|
||||
(b'foo3', b'bar3')
|
||||
---
|
||||
(b'foo1', b'bar1')
|
||||
(b'foo3', b'bar3')
|
||||
---
|
||||
(b'foo1', b'bar1')
|
||||
---
|
||||
(b'bar1', b'foo1')
|
||||
(b'foo1', b'bar1')
|
||||
---
|
||||
(b'foo1', b'bar1')
|
||||
(b'foo3', b'bar3')
|
||||
---
|
||||
(b'foo3', b'bar3')
|
||||
(b'foo1', b'bar1')
|
||||
(b'bar1', b'foo1')
|
||||
(b'foo1', b'bar1')
|
||||
None
|
||||
True
|
||||
[b'bar1', b'foo1', b'foo3']
|
||||
[b'foo1', b'bar1', b'bar3']
|
||||
b'bar1'
|
||||
b'foo1'
|
||||
b'foo3'
|
||||
foo1 True
|
||||
foo2 False
|
||||
baz1 True
|
||||
TypeError
|
42
components/language/micropython/tests/extmod/btree_error.py
Normal file
42
components/language/micropython/tests/extmod/btree_error.py
Normal file
@@ -0,0 +1,42 @@
|
||||
# Test that errno's propagate correctly through btree module.
|
||||
|
||||
try:
|
||||
import btree, uio, uerrno
|
||||
|
||||
uio.IOBase
|
||||
except (ImportError, AttributeError):
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
class Device(uio.IOBase):
|
||||
def __init__(self, read_ret=0, ioctl_ret=0):
|
||||
self.read_ret = read_ret
|
||||
self.ioctl_ret = ioctl_ret
|
||||
|
||||
def readinto(self, buf):
|
||||
print("read", len(buf))
|
||||
return self.read_ret
|
||||
|
||||
def ioctl(self, cmd, arg):
|
||||
print("ioctl", cmd)
|
||||
return self.ioctl_ret
|
||||
|
||||
|
||||
# Invalid pagesize; errno comes from btree library
|
||||
try:
|
||||
db = btree.open(Device(), pagesize=511)
|
||||
except OSError as er:
|
||||
print("OSError", er.errno == uerrno.EINVAL)
|
||||
|
||||
# Valid pagesize, device returns error on read; errno comes from Device.readinto
|
||||
try:
|
||||
db = btree.open(Device(-1000), pagesize=512)
|
||||
except OSError as er:
|
||||
print(repr(er))
|
||||
|
||||
# Valid pagesize, device returns error on seek; errno comes from Device.ioctl
|
||||
try:
|
||||
db = btree.open(Device(0, -1001), pagesize=512)
|
||||
except OSError as er:
|
||||
print(repr(er))
|
@@ -0,0 +1,6 @@
|
||||
OSError True
|
||||
read 24
|
||||
OSError(1000,)
|
||||
read 24
|
||||
ioctl 2
|
||||
OSError(1001,)
|
26
components/language/micropython/tests/extmod/btree_gc.py
Normal file
26
components/language/micropython/tests/extmod/btree_gc.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# Test btree interaction with the garbage collector.
|
||||
|
||||
try:
|
||||
import btree, uio, gc
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
N = 80
|
||||
|
||||
# Create a BytesIO but don't keep a reference to it.
|
||||
db = btree.open(uio.BytesIO(), pagesize=512)
|
||||
|
||||
# Overwrite lots of the Python stack to make sure no reference to the BytesIO remains.
|
||||
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
|
||||
# Write lots of key/value pairs, which fill up the DB and also allocate temporary heap
|
||||
# memory due to the string addition, and do a GC collect to verify that the BytesIO
|
||||
# is not collected.
|
||||
for i in range(N):
|
||||
db[b"thekey" + str(i)] = b"thelongvalue" + str(i)
|
||||
print(db[b"thekey" + str(i)])
|
||||
gc.collect()
|
||||
|
||||
# Reclaim memory allocated by the db object.
|
||||
db.close()
|
80
components/language/micropython/tests/extmod/btree_gc.py.exp
Normal file
80
components/language/micropython/tests/extmod/btree_gc.py.exp
Normal file
@@ -0,0 +1,80 @@
|
||||
b'thelongvalue0'
|
||||
b'thelongvalue1'
|
||||
b'thelongvalue2'
|
||||
b'thelongvalue3'
|
||||
b'thelongvalue4'
|
||||
b'thelongvalue5'
|
||||
b'thelongvalue6'
|
||||
b'thelongvalue7'
|
||||
b'thelongvalue8'
|
||||
b'thelongvalue9'
|
||||
b'thelongvalue10'
|
||||
b'thelongvalue11'
|
||||
b'thelongvalue12'
|
||||
b'thelongvalue13'
|
||||
b'thelongvalue14'
|
||||
b'thelongvalue15'
|
||||
b'thelongvalue16'
|
||||
b'thelongvalue17'
|
||||
b'thelongvalue18'
|
||||
b'thelongvalue19'
|
||||
b'thelongvalue20'
|
||||
b'thelongvalue21'
|
||||
b'thelongvalue22'
|
||||
b'thelongvalue23'
|
||||
b'thelongvalue24'
|
||||
b'thelongvalue25'
|
||||
b'thelongvalue26'
|
||||
b'thelongvalue27'
|
||||
b'thelongvalue28'
|
||||
b'thelongvalue29'
|
||||
b'thelongvalue30'
|
||||
b'thelongvalue31'
|
||||
b'thelongvalue32'
|
||||
b'thelongvalue33'
|
||||
b'thelongvalue34'
|
||||
b'thelongvalue35'
|
||||
b'thelongvalue36'
|
||||
b'thelongvalue37'
|
||||
b'thelongvalue38'
|
||||
b'thelongvalue39'
|
||||
b'thelongvalue40'
|
||||
b'thelongvalue41'
|
||||
b'thelongvalue42'
|
||||
b'thelongvalue43'
|
||||
b'thelongvalue44'
|
||||
b'thelongvalue45'
|
||||
b'thelongvalue46'
|
||||
b'thelongvalue47'
|
||||
b'thelongvalue48'
|
||||
b'thelongvalue49'
|
||||
b'thelongvalue50'
|
||||
b'thelongvalue51'
|
||||
b'thelongvalue52'
|
||||
b'thelongvalue53'
|
||||
b'thelongvalue54'
|
||||
b'thelongvalue55'
|
||||
b'thelongvalue56'
|
||||
b'thelongvalue57'
|
||||
b'thelongvalue58'
|
||||
b'thelongvalue59'
|
||||
b'thelongvalue60'
|
||||
b'thelongvalue61'
|
||||
b'thelongvalue62'
|
||||
b'thelongvalue63'
|
||||
b'thelongvalue64'
|
||||
b'thelongvalue65'
|
||||
b'thelongvalue66'
|
||||
b'thelongvalue67'
|
||||
b'thelongvalue68'
|
||||
b'thelongvalue69'
|
||||
b'thelongvalue70'
|
||||
b'thelongvalue71'
|
||||
b'thelongvalue72'
|
||||
b'thelongvalue73'
|
||||
b'thelongvalue74'
|
||||
b'thelongvalue75'
|
||||
b'thelongvalue76'
|
||||
b'thelongvalue77'
|
||||
b'thelongvalue78'
|
||||
b'thelongvalue79'
|
111
components/language/micropython/tests/extmod/framebuf1.py
Normal file
111
components/language/micropython/tests/extmod/framebuf1.py
Normal file
@@ -0,0 +1,111 @@
|
||||
try:
|
||||
import framebuf
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
w = 5
|
||||
h = 16
|
||||
size = w * h // 8
|
||||
buf = bytearray(size)
|
||||
maps = {
|
||||
framebuf.MONO_VLSB: "MONO_VLSB",
|
||||
framebuf.MONO_HLSB: "MONO_HLSB",
|
||||
framebuf.MONO_HMSB: "MONO_HMSB",
|
||||
}
|
||||
|
||||
for mapping in maps.keys():
|
||||
for x in range(size):
|
||||
buf[x] = 0
|
||||
fbuf = framebuf.FrameBuffer(buf, w, h, mapping)
|
||||
print(maps[mapping])
|
||||
# access as buffer
|
||||
print(memoryview(fbuf)[0])
|
||||
|
||||
# fill
|
||||
fbuf.fill(1)
|
||||
print(buf)
|
||||
fbuf.fill(0)
|
||||
print(buf)
|
||||
|
||||
# put pixel
|
||||
fbuf.pixel(0, 0, 1)
|
||||
fbuf.pixel(4, 0, 1)
|
||||
fbuf.pixel(0, 15, 1)
|
||||
fbuf.pixel(4, 15, 1)
|
||||
print(buf)
|
||||
|
||||
# clear pixel
|
||||
fbuf.pixel(4, 15, 0)
|
||||
print(buf)
|
||||
|
||||
# get pixel
|
||||
print(fbuf.pixel(0, 0), fbuf.pixel(1, 1))
|
||||
|
||||
# hline
|
||||
fbuf.fill(0)
|
||||
fbuf.hline(0, 1, w, 1)
|
||||
print("hline", buf)
|
||||
|
||||
# vline
|
||||
fbuf.fill(0)
|
||||
fbuf.vline(1, 0, h, 1)
|
||||
print("vline", buf)
|
||||
|
||||
# rect
|
||||
fbuf.fill(0)
|
||||
fbuf.rect(1, 1, 3, 3, 1)
|
||||
print("rect", buf)
|
||||
|
||||
# fill rect
|
||||
fbuf.fill(0)
|
||||
fbuf.fill_rect(0, 0, 0, 3, 1) # zero width, no-operation
|
||||
fbuf.fill_rect(1, 1, 3, 3, 1)
|
||||
print("fill_rect", buf)
|
||||
|
||||
# line
|
||||
fbuf.fill(0)
|
||||
fbuf.line(1, 1, 3, 3, 1)
|
||||
print("line", buf)
|
||||
|
||||
# line steep negative gradient
|
||||
fbuf.fill(0)
|
||||
fbuf.line(3, 3, 2, 1, 1)
|
||||
print("line", buf)
|
||||
|
||||
# scroll
|
||||
fbuf.fill(0)
|
||||
fbuf.pixel(2, 7, 1)
|
||||
fbuf.scroll(0, 1)
|
||||
print(buf)
|
||||
fbuf.scroll(0, -2)
|
||||
print(buf)
|
||||
fbuf.scroll(1, 0)
|
||||
print(buf)
|
||||
fbuf.scroll(-1, 0)
|
||||
print(buf)
|
||||
fbuf.scroll(2, 2)
|
||||
print(buf)
|
||||
|
||||
# print text
|
||||
fbuf.fill(0)
|
||||
fbuf.text("hello", 0, 0, 1)
|
||||
print(buf)
|
||||
fbuf.text("hello", 0, 0, 0) # clear
|
||||
print(buf)
|
||||
|
||||
# char out of font range set to chr(127)
|
||||
fbuf.text(str(chr(31)), 0, 0)
|
||||
print(buf)
|
||||
print()
|
||||
|
||||
# test invalid constructor, and stride argument
|
||||
try:
|
||||
fbuf = framebuf.FrameBuffer(buf, w, h, -1, w)
|
||||
except ValueError:
|
||||
print("ValueError")
|
||||
|
||||
# test legacy constructor
|
||||
fbuf = framebuf.FrameBuffer1(buf, w, h)
|
||||
fbuf = framebuf.FrameBuffer1(buf, w, h, w)
|
||||
print(framebuf.MVLSB == framebuf.MONO_VLSB)
|
@@ -0,0 +1,68 @@
|
||||
MONO_VLSB
|
||||
0
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x01\x00\x00\x00\x01\x80\x00\x00\x00\x80')
|
||||
bytearray(b'\x01\x00\x00\x00\x01\x80\x00\x00\x00\x00')
|
||||
1 0
|
||||
hline bytearray(b'\x02\x02\x02\x02\x02\x00\x00\x00\x00\x00')
|
||||
vline bytearray(b'\x00\xff\x00\x00\x00\x00\xff\x00\x00\x00')
|
||||
rect bytearray(b'\x00\x0e\n\x0e\x00\x00\x00\x00\x00\x00')
|
||||
fill_rect bytearray(b'\x00\x0e\x0e\x0e\x00\x00\x00\x00\x00\x00')
|
||||
line bytearray(b'\x00\x02\x04\x08\x00\x00\x00\x00\x00\x00')
|
||||
line bytearray(b'\x00\x00\x06\x08\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00')
|
||||
bytearray(b'\x00\x00@\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00@\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00@\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01')
|
||||
bytearray(b'\x00\x7f\x7f\x04\x04\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\xaaU\xaaU\xaa\x00\x00\x00\x00\x00')
|
||||
|
||||
MONO_HLSB
|
||||
0
|
||||
bytearray(b'\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x88\x00\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x88\x00\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
1 0
|
||||
hline bytearray(b'\x00\xf8\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
vline bytearray(b'@@@@@@@@@@')
|
||||
rect bytearray(b'\x00pPp\x00\x00\x00\x00\x00\x00')
|
||||
fill_rect bytearray(b'\x00ppp\x00\x00\x00\x00\x00\x00')
|
||||
line bytearray(b'\x00@ \x10\x00\x00\x00\x00\x00\x00')
|
||||
line bytearray(b'\x00 \x10\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00 \x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00 \x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00 \x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00')
|
||||
bytearray(b'``x````\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'P\xa8P\xa8P\xa8P\xa8\x00\x00')
|
||||
|
||||
MONO_HMSB
|
||||
0
|
||||
bytearray(b'\x1f\x1f\x1f\x1f\x1f\x1f\x1f\x1f\x1f\x1f')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x11\x00\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x11\x00\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
1 0
|
||||
hline bytearray(b'\x00\x1f\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
vline bytearray(b'\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02')
|
||||
rect bytearray(b'\x00\x0e\n\x0e\x00\x00\x00\x00\x00\x00')
|
||||
fill_rect bytearray(b'\x00\x0e\x0e\x0e\x00\x00\x00\x00\x00\x00')
|
||||
line bytearray(b'\x00\x02\x04\x08\x00\x00\x00\x00\x00\x00')
|
||||
line bytearray(b'\x00\x04\x04\x08\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00')
|
||||
bytearray(b'\x06\x06\x1e\x06\x06\x06\x06\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\n\x15\n\x15\n\x15\n\x15\x00\x00')
|
||||
|
||||
ValueError
|
||||
True
|
66
components/language/micropython/tests/extmod/framebuf16.py
Normal file
66
components/language/micropython/tests/extmod/framebuf16.py
Normal file
@@ -0,0 +1,66 @@
|
||||
try:
|
||||
import framebuf, usys
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
# This test and its .exp file is based on a little-endian architecture.
|
||||
if usys.byteorder != "little":
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
def printbuf():
|
||||
print("--8<--")
|
||||
for y in range(h):
|
||||
print(buf[y * w * 2 : (y + 1) * w * 2])
|
||||
print("-->8--")
|
||||
|
||||
|
||||
w = 4
|
||||
h = 5
|
||||
buf = bytearray(w * h * 2)
|
||||
fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.RGB565)
|
||||
|
||||
# fill
|
||||
fbuf.fill(0xFFFF)
|
||||
printbuf()
|
||||
fbuf.fill(0x0000)
|
||||
printbuf()
|
||||
|
||||
# put pixel
|
||||
fbuf.pixel(0, 0, 0xEEEE)
|
||||
fbuf.pixel(3, 0, 0xEE00)
|
||||
fbuf.pixel(0, 4, 0x00EE)
|
||||
fbuf.pixel(3, 4, 0x0EE0)
|
||||
printbuf()
|
||||
|
||||
# get pixel
|
||||
print(fbuf.pixel(0, 4), fbuf.pixel(1, 1))
|
||||
|
||||
# scroll
|
||||
fbuf.fill(0x0000)
|
||||
fbuf.pixel(2, 2, 0xFFFF)
|
||||
printbuf()
|
||||
fbuf.scroll(0, 1)
|
||||
printbuf()
|
||||
fbuf.scroll(1, 0)
|
||||
printbuf()
|
||||
fbuf.scroll(-1, -2)
|
||||
printbuf()
|
||||
|
||||
w2 = 2
|
||||
h2 = 3
|
||||
buf2 = bytearray(w2 * h2 * 2)
|
||||
fbuf2 = framebuf.FrameBuffer(buf2, w2, h2, framebuf.RGB565)
|
||||
|
||||
fbuf2.fill(0x0000)
|
||||
fbuf2.pixel(0, 0, 0x0EE0)
|
||||
fbuf2.pixel(0, 2, 0xEE00)
|
||||
fbuf2.pixel(1, 0, 0x00EE)
|
||||
fbuf2.pixel(1, 2, 0xE00E)
|
||||
fbuf.fill(0xFFFF)
|
||||
fbuf.blit(fbuf2, 3, 3, 0x0000)
|
||||
fbuf.blit(fbuf2, -1, -1, 0x0000)
|
||||
fbuf.blit(fbuf2, 16, 16, 0x0000)
|
||||
printbuf()
|
@@ -0,0 +1,57 @@
|
||||
--8<--
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
-->8--
|
||||
--8<--
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
-->8--
|
||||
--8<--
|
||||
bytearray(b'\xee\xee\x00\x00\x00\x00\x00\xee')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\xee\x00\x00\x00\x00\x00\xe0\x0e')
|
||||
-->8--
|
||||
238 0
|
||||
--8<--
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\xff\xff\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
-->8--
|
||||
--8<--
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\xff\xff\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
-->8--
|
||||
--8<--
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\xff\xff')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
-->8--
|
||||
--8<--
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\xff\xff\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\xff\xff')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
-->8--
|
||||
--8<--
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\x0e\xe0\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xe0\x0e')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
-->8--
|
64
components/language/micropython/tests/extmod/framebuf2.py
Normal file
64
components/language/micropython/tests/extmod/framebuf2.py
Normal file
@@ -0,0 +1,64 @@
|
||||
try:
|
||||
import framebuf
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
def printbuf():
|
||||
print("--8<--")
|
||||
for y in range(h):
|
||||
for x in range(w):
|
||||
print("%u" % ((buf[(x + y * w) // 4] >> ((x & 3) << 1)) & 3), end="")
|
||||
print()
|
||||
print("-->8--")
|
||||
|
||||
|
||||
w = 8
|
||||
h = 5
|
||||
buf = bytearray(w * h // 4)
|
||||
fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.GS2_HMSB)
|
||||
|
||||
# fill
|
||||
fbuf.fill(3)
|
||||
printbuf()
|
||||
fbuf.fill(0)
|
||||
printbuf()
|
||||
|
||||
# put pixel
|
||||
fbuf.pixel(0, 0, 1)
|
||||
fbuf.pixel(3, 0, 2)
|
||||
fbuf.pixel(0, 4, 3)
|
||||
fbuf.pixel(3, 4, 2)
|
||||
printbuf()
|
||||
|
||||
# get pixel
|
||||
print(fbuf.pixel(0, 4), fbuf.pixel(1, 1))
|
||||
|
||||
# scroll
|
||||
fbuf.fill(0)
|
||||
fbuf.pixel(2, 2, 3)
|
||||
printbuf()
|
||||
fbuf.scroll(0, 1)
|
||||
printbuf()
|
||||
fbuf.scroll(1, 0)
|
||||
printbuf()
|
||||
fbuf.scroll(-1, -2)
|
||||
printbuf()
|
||||
|
||||
w2 = 2
|
||||
h2 = 3
|
||||
buf2 = bytearray(w2 * h2 // 4)
|
||||
fbuf2 = framebuf.FrameBuffer(buf2, w2, h2, framebuf.GS2_HMSB)
|
||||
|
||||
# blit
|
||||
fbuf2.fill(0)
|
||||
fbuf2.pixel(0, 0, 1)
|
||||
fbuf2.pixel(0, 2, 2)
|
||||
fbuf2.pixel(1, 0, 1)
|
||||
fbuf2.pixel(1, 2, 2)
|
||||
fbuf.fill(3)
|
||||
fbuf.blit(fbuf2, 3, 3, 0)
|
||||
fbuf.blit(fbuf2, -1, -1, 0)
|
||||
fbuf.blit(fbuf2, 16, 16, 0)
|
||||
printbuf()
|
@@ -0,0 +1,57 @@
|
||||
--8<--
|
||||
33333333
|
||||
33333333
|
||||
33333333
|
||||
33333333
|
||||
33333333
|
||||
-->8--
|
||||
--8<--
|
||||
00000000
|
||||
00000000
|
||||
00000000
|
||||
00000000
|
||||
00000000
|
||||
-->8--
|
||||
--8<--
|
||||
10020000
|
||||
00000000
|
||||
00000000
|
||||
00000000
|
||||
30020000
|
||||
-->8--
|
||||
3 0
|
||||
--8<--
|
||||
00000000
|
||||
00000000
|
||||
00300000
|
||||
00000000
|
||||
00000000
|
||||
-->8--
|
||||
--8<--
|
||||
00000000
|
||||
00000000
|
||||
00000000
|
||||
00300000
|
||||
00000000
|
||||
-->8--
|
||||
--8<--
|
||||
00000000
|
||||
00000000
|
||||
00000000
|
||||
00030000
|
||||
00000000
|
||||
-->8--
|
||||
--8<--
|
||||
00000000
|
||||
00300000
|
||||
00000000
|
||||
00030000
|
||||
00000000
|
||||
-->8--
|
||||
--8<--
|
||||
33333333
|
||||
23333333
|
||||
33333333
|
||||
33311333
|
||||
33333333
|
||||
-->8--
|
55
components/language/micropython/tests/extmod/framebuf4.py
Normal file
55
components/language/micropython/tests/extmod/framebuf4.py
Normal file
@@ -0,0 +1,55 @@
|
||||
try:
|
||||
import framebuf
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
def printbuf():
|
||||
print("--8<--")
|
||||
for y in range(h):
|
||||
print(buf[y * w // 2 : (y + 1) * w // 2])
|
||||
print("-->8--")
|
||||
|
||||
|
||||
w = 16
|
||||
h = 8
|
||||
buf = bytearray(w * h // 2)
|
||||
fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.GS4_HMSB)
|
||||
|
||||
# fill
|
||||
fbuf.fill(0x0F)
|
||||
printbuf()
|
||||
fbuf.fill(0xA0)
|
||||
printbuf()
|
||||
|
||||
# put pixel
|
||||
fbuf.pixel(0, 0, 0x01)
|
||||
printbuf()
|
||||
fbuf.pixel(w - 1, 0, 0x02)
|
||||
printbuf()
|
||||
fbuf.pixel(w - 1, h - 1, 0x03)
|
||||
printbuf()
|
||||
fbuf.pixel(0, h - 1, 0x04)
|
||||
printbuf()
|
||||
|
||||
# get pixel
|
||||
print(fbuf.pixel(0, 0), fbuf.pixel(w - 1, 0), fbuf.pixel(w - 1, h - 1), fbuf.pixel(0, h - 1))
|
||||
print(fbuf.pixel(1, 0), fbuf.pixel(w - 2, 0), fbuf.pixel(w - 2, h - 1), fbuf.pixel(1, h - 1))
|
||||
|
||||
# fill rect
|
||||
fbuf.fill_rect(0, 0, w, h, 0x0F)
|
||||
printbuf()
|
||||
fbuf.fill_rect(0, 0, w, h, 0xF0)
|
||||
fbuf.fill_rect(1, 0, w // 2 + 1, 1, 0xF1)
|
||||
printbuf()
|
||||
fbuf.fill_rect(1, 0, w // 2 + 1, 1, 0x10)
|
||||
fbuf.fill_rect(1, 0, w // 2, 1, 0xF1)
|
||||
printbuf()
|
||||
fbuf.fill_rect(1, 0, w // 2, 1, 0x10)
|
||||
fbuf.fill_rect(0, h - 4, w // 2 + 1, 4, 0xAF)
|
||||
printbuf()
|
||||
fbuf.fill_rect(0, h - 4, w // 2 + 1, 4, 0xB0)
|
||||
fbuf.fill_rect(0, h - 4, w // 2, 4, 0xAF)
|
||||
printbuf()
|
||||
fbuf.fill_rect(0, h - 4, w // 2, 4, 0xB0)
|
112
components/language/micropython/tests/extmod/framebuf4.py.exp
Normal file
112
components/language/micropython/tests/extmod/framebuf4.py.exp
Normal file
@@ -0,0 +1,112 @@
|
||||
--8<--
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
-->8--
|
||||
--8<--
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
-->8--
|
||||
--8<--
|
||||
bytearray(b'\x10\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
-->8--
|
||||
--8<--
|
||||
bytearray(b'\x10\x00\x00\x00\x00\x00\x00\x02')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
-->8--
|
||||
--8<--
|
||||
bytearray(b'\x10\x00\x00\x00\x00\x00\x00\x02')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x03')
|
||||
-->8--
|
||||
--8<--
|
||||
bytearray(b'\x10\x00\x00\x00\x00\x00\x00\x02')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'@\x00\x00\x00\x00\x00\x00\x03')
|
||||
-->8--
|
||||
1 2 3 4
|
||||
0 0 0 0
|
||||
--8<--
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff')
|
||||
-->8--
|
||||
--8<--
|
||||
bytearray(b'\x01\x11\x11\x11\x11\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
-->8--
|
||||
--8<--
|
||||
bytearray(b'\x01\x11\x11\x11\x10\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
-->8--
|
||||
--8<--
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\xff\xff\xff\xff\xf0\x00\x00\x00')
|
||||
bytearray(b'\xff\xff\xff\xff\xf0\x00\x00\x00')
|
||||
bytearray(b'\xff\xff\xff\xff\xf0\x00\x00\x00')
|
||||
bytearray(b'\xff\xff\xff\xff\xf0\x00\x00\x00')
|
||||
-->8--
|
||||
--8<--
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
|
||||
bytearray(b'\xff\xff\xff\xff\x00\x00\x00\x00')
|
||||
bytearray(b'\xff\xff\xff\xff\x00\x00\x00\x00')
|
||||
bytearray(b'\xff\xff\xff\xff\x00\x00\x00\x00')
|
||||
bytearray(b'\xff\xff\xff\xff\x00\x00\x00\x00')
|
||||
-->8--
|
34
components/language/micropython/tests/extmod/framebuf8.py
Normal file
34
components/language/micropython/tests/extmod/framebuf8.py
Normal file
@@ -0,0 +1,34 @@
|
||||
try:
|
||||
import framebuf
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
def printbuf():
|
||||
print("--8<--")
|
||||
for y in range(h):
|
||||
for x in range(w):
|
||||
print("%02x" % buf[(x + y * w)], end="")
|
||||
print()
|
||||
print("-->8--")
|
||||
|
||||
|
||||
w = 8
|
||||
h = 5
|
||||
buf = bytearray(w * h)
|
||||
fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.GS8)
|
||||
|
||||
# fill
|
||||
fbuf.fill(0x55)
|
||||
printbuf()
|
||||
|
||||
# put pixel
|
||||
fbuf.pixel(0, 0, 0x11)
|
||||
fbuf.pixel(w - 1, 0, 0x22)
|
||||
fbuf.pixel(0, h - 1, 0x33)
|
||||
fbuf.pixel(w - 1, h - 1, 0xFF)
|
||||
printbuf()
|
||||
|
||||
# get pixel
|
||||
print(hex(fbuf.pixel(0, h - 1)), hex(fbuf.pixel(1, 1)))
|
@@ -0,0 +1,15 @@
|
||||
--8<--
|
||||
5555555555555555
|
||||
5555555555555555
|
||||
5555555555555555
|
||||
5555555555555555
|
||||
5555555555555555
|
||||
-->8--
|
||||
--8<--
|
||||
1155555555555522
|
||||
5555555555555555
|
||||
5555555555555555
|
||||
5555555555555555
|
||||
33555555555555ff
|
||||
-->8--
|
||||
0x33 0x55
|
@@ -0,0 +1,35 @@
|
||||
# Test blit between different color spaces
|
||||
try:
|
||||
import framebuf, usys
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
# Monochrome glyph/icon
|
||||
w = 8
|
||||
h = 8
|
||||
cbuf = bytearray(w * h // 8)
|
||||
fbc = framebuf.FrameBuffer(cbuf, w, h, framebuf.MONO_HLSB)
|
||||
fbc.line(0, 0, 7, 7, 1)
|
||||
|
||||
# RGB565 destination
|
||||
wd = 16
|
||||
hd = 16
|
||||
dest = bytearray(wd * hd * 2)
|
||||
fbd = framebuf.FrameBuffer(dest, wd, hd, framebuf.RGB565)
|
||||
|
||||
wp = 2
|
||||
bg = 0x1234
|
||||
fg = 0xF800
|
||||
pal = bytearray(wp * 2)
|
||||
palette = framebuf.FrameBuffer(pal, wp, 1, framebuf.RGB565)
|
||||
palette.pixel(0, 0, bg)
|
||||
palette.pixel(1, 0, fg)
|
||||
|
||||
fbd.blit(fbc, 0, 0, -1, palette)
|
||||
|
||||
print(fbd.pixel(0, 0) == fg)
|
||||
print(fbd.pixel(7, 7) == fg)
|
||||
print(fbd.pixel(8, 8) == 0) # Ouside blit
|
||||
print(fbd.pixel(0, 1) == bg)
|
||||
print(fbd.pixel(1, 0) == bg)
|
@@ -0,0 +1,5 @@
|
||||
True
|
||||
True
|
||||
True
|
||||
True
|
||||
True
|
@@ -0,0 +1,50 @@
|
||||
# test subclassing framebuf.FrameBuffer
|
||||
|
||||
try:
|
||||
import framebuf, usys
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
# This test and its .exp file is based on a little-endian architecture.
|
||||
if usys.byteorder != "little":
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
class FB(framebuf.FrameBuffer):
|
||||
def __init__(self, n):
|
||||
self.n = n
|
||||
super().__init__(bytearray(2 * n * n), n, n, framebuf.RGB565)
|
||||
|
||||
def foo(self):
|
||||
self.hline(0, 2, self.n, 0x0304)
|
||||
|
||||
|
||||
fb = FB(n=3)
|
||||
fb.pixel(0, 0, 0x0102)
|
||||
fb.foo()
|
||||
print(bytes(fb))
|
||||
|
||||
# Test that blitting a subclass works.
|
||||
fb2 = framebuf.FrameBuffer(bytearray(2 * 3 * 3), 3, 3, framebuf.RGB565)
|
||||
fb.fill(0)
|
||||
fb.pixel(0, 0, 0x0506)
|
||||
fb.pixel(2, 2, 0x0708)
|
||||
fb2.blit(fb, 0, 0)
|
||||
print(bytes(fb2))
|
||||
|
||||
# Test that blitting something that isn't a subclass fails with TypeError.
|
||||
class NotAFrameBuf:
|
||||
pass
|
||||
|
||||
|
||||
try:
|
||||
fb.blit(NotAFrameBuf(), 0, 0)
|
||||
except TypeError:
|
||||
print("TypeError")
|
||||
|
||||
try:
|
||||
fb.blit(None, 0, 0)
|
||||
except TypeError:
|
||||
print("TypeError")
|
@@ -0,0 +1,4 @@
|
||||
b'\x02\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x03\x04\x03\x04\x03'
|
||||
b'\x06\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x07'
|
||||
TypeError
|
||||
TypeError
|
48
components/language/micropython/tests/extmod/machine1.py
Normal file
48
components/language/micropython/tests/extmod/machine1.py
Normal file
@@ -0,0 +1,48 @@
|
||||
# test machine module
|
||||
|
||||
try:
|
||||
try:
|
||||
import umachine as machine
|
||||
except ImportError:
|
||||
import machine
|
||||
machine.mem8
|
||||
except:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
print(machine.mem8)
|
||||
|
||||
try:
|
||||
machine.mem16[1]
|
||||
except ValueError:
|
||||
print("ValueError")
|
||||
|
||||
try:
|
||||
machine.mem16[1] = 1
|
||||
except ValueError:
|
||||
print("ValueError")
|
||||
|
||||
try:
|
||||
del machine.mem8[0]
|
||||
except TypeError:
|
||||
print("TypeError")
|
||||
|
||||
try:
|
||||
machine.mem8[0:1]
|
||||
except TypeError:
|
||||
print("TypeError")
|
||||
|
||||
try:
|
||||
machine.mem8[0:1] = 10
|
||||
except TypeError:
|
||||
print("TypeError")
|
||||
|
||||
try:
|
||||
machine.mem8["hello"]
|
||||
except TypeError:
|
||||
print("TypeError")
|
||||
|
||||
try:
|
||||
machine.mem8["hello"] = 10
|
||||
except TypeError:
|
||||
print("TypeError")
|
@@ -0,0 +1,8 @@
|
||||
<8-bit memory>
|
||||
ValueError
|
||||
ValueError
|
||||
TypeError
|
||||
TypeError
|
||||
TypeError
|
||||
TypeError
|
||||
TypeError
|
@@ -0,0 +1,86 @@
|
||||
# Test machine.I2S data transfer rate, for both TX and RX.
|
||||
|
||||
try:
|
||||
from machine import Pin, I2S
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
import time, sys
|
||||
|
||||
# Configure pins based on the board.
|
||||
if "pyboard" in sys.platform:
|
||||
i2s_id = 2
|
||||
sck_pin = Pin("Y6")
|
||||
ws_pin = Pin("Y5")
|
||||
sd_tx_pin = sd_rx_pin = Pin("Y8")
|
||||
elif "rp2" in sys.platform:
|
||||
i2s_id = 1
|
||||
sck_pin = Pin(0)
|
||||
ws_pin = Pin(1)
|
||||
sd_tx_pin = sd_rx_pin = Pin(2)
|
||||
elif "mimxrt" in sys.platform:
|
||||
i2s_id = 1
|
||||
sck_pin = Pin(26)
|
||||
ws_pin = Pin(27)
|
||||
sd_tx_pin = Pin(7)
|
||||
sd_rx_pin = Pin(8)
|
||||
|
||||
TEST_BYTES = b"01234567"
|
||||
RATE = 11025 # frames/sec
|
||||
|
||||
|
||||
def test(mode, sd_pin, bits_per_sample, frame_format):
|
||||
i2s = I2S(
|
||||
i2s_id,
|
||||
sck=sck_pin,
|
||||
ws=ws_pin,
|
||||
sd=sd_pin,
|
||||
mode=mode,
|
||||
bits=bits_per_sample,
|
||||
format=frame_format,
|
||||
rate=RATE,
|
||||
ibuf=200,
|
||||
)
|
||||
|
||||
if frame_format == I2S.MONO:
|
||||
channels = 1
|
||||
else:
|
||||
channels = 2
|
||||
bits_per_frame = bits_per_sample * channels
|
||||
buf_len_250ms = bits_per_frame // 8 * RATE // 4
|
||||
|
||||
# Create test data and preload I2S buffers.
|
||||
if mode == I2S.TX:
|
||||
mode_str = "TX"
|
||||
data = TEST_BYTES * (buf_len_250ms // len(TEST_BYTES))
|
||||
i2s.write(data)
|
||||
else:
|
||||
mode_str = "RX"
|
||||
data = bytearray(len(TEST_BYTES) * (buf_len_250ms // len(TEST_BYTES)))
|
||||
i2s.readinto(data)
|
||||
|
||||
# Time how long it takes to read/write 2 lots of data.
|
||||
t0 = time.ticks_ms()
|
||||
for i in range(2):
|
||||
if mode == I2S.TX:
|
||||
i2s.write(data)
|
||||
else:
|
||||
i2s.readinto(data)
|
||||
t1 = time.ticks_ms()
|
||||
dt = time.ticks_diff(t1, t0)
|
||||
|
||||
i2s.deinit()
|
||||
|
||||
# Print out test result, time should be in range of 500ms.
|
||||
print(mode_str, bits_per_sample, channels, abs(dt - 500) <= 4)
|
||||
|
||||
|
||||
test(I2S.TX, sd_tx_pin, 16, I2S.MONO)
|
||||
test(I2S.TX, sd_tx_pin, 16, I2S.STEREO)
|
||||
test(I2S.TX, sd_tx_pin, 32, I2S.MONO)
|
||||
test(I2S.TX, sd_tx_pin, 32, I2S.STEREO)
|
||||
test(I2S.RX, sd_rx_pin, 16, I2S.MONO)
|
||||
test(I2S.RX, sd_rx_pin, 16, I2S.STEREO)
|
||||
test(I2S.RX, sd_rx_pin, 32, I2S.MONO)
|
||||
test(I2S.RX, sd_rx_pin, 32, I2S.STEREO)
|
@@ -0,0 +1,8 @@
|
||||
TX 16 1 True
|
||||
TX 16 2 True
|
||||
TX 32 1 True
|
||||
TX 32 2 True
|
||||
RX 16 1 True
|
||||
RX 16 2 True
|
||||
RX 32 1 True
|
||||
RX 32 2 True
|
@@ -0,0 +1,30 @@
|
||||
try:
|
||||
try:
|
||||
import umachine as machine
|
||||
except ImportError:
|
||||
import machine
|
||||
machine.PinBase
|
||||
except:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
class MyPin(machine.PinBase):
|
||||
def __init__(self):
|
||||
print("__init__")
|
||||
self.v = False
|
||||
|
||||
def value(self, v=None):
|
||||
print("value:", v)
|
||||
if v is None:
|
||||
self.v = not self.v
|
||||
return int(self.v)
|
||||
|
||||
|
||||
p = MyPin()
|
||||
|
||||
print(p.value())
|
||||
print(p.value())
|
||||
print(p.value())
|
||||
p.value(1)
|
||||
p.value(0)
|
@@ -0,0 +1,9 @@
|
||||
__init__
|
||||
value: None
|
||||
1
|
||||
value: None
|
||||
0
|
||||
value: None
|
||||
1
|
||||
value: 1
|
||||
value: 0
|
@@ -0,0 +1,44 @@
|
||||
try:
|
||||
try:
|
||||
import umachine as machine
|
||||
except ImportError:
|
||||
import machine
|
||||
machine.PinBase
|
||||
machine.time_pulse_us
|
||||
except:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
class ConstPin(machine.PinBase):
|
||||
def __init__(self, value):
|
||||
self.v = value
|
||||
|
||||
def value(self, v=None):
|
||||
if v is None:
|
||||
return self.v
|
||||
else:
|
||||
self.v = v
|
||||
|
||||
|
||||
class TogglePin(machine.PinBase):
|
||||
def __init__(self):
|
||||
self.v = 0
|
||||
|
||||
def value(self, v=None):
|
||||
if v is None:
|
||||
self.v = 1 - self.v
|
||||
print("value:", self.v)
|
||||
return self.v
|
||||
|
||||
|
||||
p = TogglePin()
|
||||
|
||||
t = machine.time_pulse_us(p, 1)
|
||||
print(type(t))
|
||||
t = machine.time_pulse_us(p, 0)
|
||||
print(type(t))
|
||||
|
||||
p = ConstPin(0)
|
||||
print(machine.time_pulse_us(p, 1, 10))
|
||||
print(machine.time_pulse_us(p, 0, 10))
|
@@ -0,0 +1,9 @@
|
||||
value: 1
|
||||
value: 0
|
||||
<class 'int'>
|
||||
value: 1
|
||||
value: 0
|
||||
value: 1
|
||||
<class 'int'>
|
||||
-2
|
||||
-1
|
@@ -0,0 +1,40 @@
|
||||
# test machine.Signal class
|
||||
|
||||
try:
|
||||
try:
|
||||
import umachine as machine
|
||||
except ImportError:
|
||||
import machine
|
||||
machine.PinBase
|
||||
machine.Signal
|
||||
except:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
class Pin(machine.PinBase):
|
||||
def __init__(self):
|
||||
self.v = 0
|
||||
|
||||
def value(self, v=None):
|
||||
if v is None:
|
||||
return self.v
|
||||
else:
|
||||
self.v = int(v)
|
||||
|
||||
|
||||
# test non-inverted
|
||||
p = Pin()
|
||||
s = machine.Signal(p)
|
||||
s.value(0)
|
||||
print(p.value(), s.value())
|
||||
s.value(1)
|
||||
print(p.value(), s.value())
|
||||
|
||||
# test inverted, and using on/off methods
|
||||
p = Pin()
|
||||
s = machine.Signal(p, invert=True)
|
||||
s.off()
|
||||
print(p.value(), s.value())
|
||||
s.on()
|
||||
print(p.value(), s.value())
|
@@ -0,0 +1,4 @@
|
||||
0 0
|
||||
1 1
|
||||
1 0
|
||||
0 1
|
@@ -0,0 +1,38 @@
|
||||
# test machine.Timer
|
||||
|
||||
try:
|
||||
import utime, umachine as machine
|
||||
|
||||
machine.Timer
|
||||
except:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
# create and deinit
|
||||
t = machine.Timer(freq=1)
|
||||
t.deinit()
|
||||
|
||||
# deinit again
|
||||
t.deinit()
|
||||
|
||||
# create 2 and deinit
|
||||
t = machine.Timer(freq=1)
|
||||
t2 = machine.Timer(freq=1)
|
||||
t.deinit()
|
||||
t2.deinit()
|
||||
|
||||
# create 2 and deinit in different order
|
||||
t = machine.Timer(freq=1)
|
||||
t2 = machine.Timer(freq=1)
|
||||
t2.deinit()
|
||||
t.deinit()
|
||||
|
||||
# create one-shot timer with callback and wait for it to print (should be just once)
|
||||
t = machine.Timer(period=1, mode=machine.Timer.ONE_SHOT, callback=lambda t: print("one-shot"))
|
||||
utime.sleep_ms(5)
|
||||
t.deinit()
|
||||
|
||||
# create periodic timer with callback and wait for it to print
|
||||
t = machine.Timer(period=4, mode=machine.Timer.PERIODIC, callback=lambda t: print("periodic"))
|
||||
utime.sleep_ms(14)
|
||||
t.deinit()
|
@@ -0,0 +1,4 @@
|
||||
one-shot
|
||||
periodic
|
||||
periodic
|
||||
periodic
|
37
components/language/micropython/tests/extmod/ticks_diff.py
Normal file
37
components/language/micropython/tests/extmod/ticks_diff.py
Normal file
@@ -0,0 +1,37 @@
|
||||
try:
|
||||
from utime import ticks_diff, ticks_add
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
MAX = ticks_add(0, -1)
|
||||
# Should be done like this to avoid small int overflow
|
||||
MODULO_HALF = MAX // 2 + 1
|
||||
|
||||
# Invariants:
|
||||
# if ticks_diff(a, b) = c,
|
||||
# then ticks_diff(b, a) = -c
|
||||
|
||||
assert ticks_diff(1, 0) == 1, ticks_diff(1, 0)
|
||||
assert ticks_diff(0, 1) == -1
|
||||
|
||||
assert ticks_diff(0, MAX) == 1
|
||||
assert ticks_diff(MAX, 0) == -1
|
||||
|
||||
assert ticks_diff(0, MAX - 1) == 2
|
||||
|
||||
# Maximum "positive" distance
|
||||
assert ticks_diff(MODULO_HALF, 1) == MODULO_HALF - 1, ticks_diff(MODULO_HALF, 1)
|
||||
# Step further, and it becomes a negative distance
|
||||
assert ticks_diff(MODULO_HALF, 0) == -MODULO_HALF
|
||||
|
||||
# Offsetting that in either direction doesn't affect the result
|
||||
off = 100
|
||||
# Cheating and skipping to use ticks_add() when we know there's no wraparound
|
||||
# Real apps should use always it.
|
||||
assert ticks_diff(MODULO_HALF + off, 1 + off) == MODULO_HALF - 1
|
||||
assert ticks_diff(MODULO_HALF + off, 0 + off) == -MODULO_HALF
|
||||
assert ticks_diff(MODULO_HALF - off, ticks_add(1, -off)) == MODULO_HALF - 1
|
||||
assert ticks_diff(MODULO_HALF - off, ticks_add(0, -off)) == -MODULO_HALF
|
||||
|
||||
print("OK")
|
@@ -0,0 +1 @@
|
||||
OK
|
23
components/language/micropython/tests/extmod/time_ms_us.py
Normal file
23
components/language/micropython/tests/extmod/time_ms_us.py
Normal file
@@ -0,0 +1,23 @@
|
||||
try:
|
||||
import utime
|
||||
|
||||
utime.sleep_ms, utime.sleep_us, utime.ticks_diff, utime.ticks_ms, utime.ticks_us, utime.ticks_cpu
|
||||
except (ImportError, AttributeError):
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
utime.sleep_ms(1)
|
||||
utime.sleep_us(1)
|
||||
|
||||
t0 = utime.ticks_ms()
|
||||
t1 = utime.ticks_ms()
|
||||
print(0 <= utime.ticks_diff(t1, t0) <= 1)
|
||||
|
||||
t0 = utime.ticks_us()
|
||||
t1 = utime.ticks_us()
|
||||
print(0 <= utime.ticks_diff(t1, t0) <= 500)
|
||||
|
||||
# ticks_cpu may not be implemented, at least make sure it doesn't decrease
|
||||
t0 = utime.ticks_cpu()
|
||||
t1 = utime.ticks_cpu()
|
||||
print(utime.ticks_diff(t1, t0) >= 0)
|
@@ -0,0 +1,3 @@
|
||||
True
|
||||
True
|
||||
True
|
@@ -0,0 +1,26 @@
|
||||
# Test that tasks return their value correctly to the caller
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def foo():
|
||||
return 42
|
||||
|
||||
|
||||
async def main():
|
||||
# Call function directly via an await
|
||||
print(await foo())
|
||||
|
||||
# Create a task and await on it
|
||||
task = asyncio.create_task(foo())
|
||||
print(await task)
|
||||
|
||||
|
||||
asyncio.run(main())
|
@@ -0,0 +1,2 @@
|
||||
42
|
||||
42
|
@@ -0,0 +1,51 @@
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
try:
|
||||
import utime
|
||||
|
||||
ticks = utime.ticks_ms
|
||||
ticks_diff = utime.ticks_diff
|
||||
except:
|
||||
import time
|
||||
|
||||
ticks = lambda: int(time.time() * 1000)
|
||||
ticks_diff = lambda t1, t0: t1 - t0
|
||||
|
||||
|
||||
async def delay_print(t, s):
|
||||
await asyncio.sleep(t)
|
||||
print(s)
|
||||
|
||||
|
||||
async def main():
|
||||
print("start")
|
||||
|
||||
await asyncio.sleep(0.001)
|
||||
print("after sleep")
|
||||
|
||||
t0 = ticks()
|
||||
await delay_print(0.2, "short")
|
||||
t1 = ticks()
|
||||
await delay_print(0.4, "long")
|
||||
t2 = ticks()
|
||||
await delay_print(-1, "negative")
|
||||
t3 = ticks()
|
||||
|
||||
print(
|
||||
"took {} {} {}".format(
|
||||
round(ticks_diff(t1, t0), -2),
|
||||
round(ticks_diff(t2, t1), -2),
|
||||
round(ticks_diff(t3, t2), -2),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
asyncio.run(main())
|
@@ -0,0 +1,6 @@
|
||||
start
|
||||
after sleep
|
||||
short
|
||||
long
|
||||
negative
|
||||
took 200 400 0
|
@@ -0,0 +1,24 @@
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def forever():
|
||||
print("forever start")
|
||||
await asyncio.sleep(10)
|
||||
|
||||
|
||||
async def main():
|
||||
print("main start")
|
||||
asyncio.create_task(forever())
|
||||
await asyncio.sleep(0.001)
|
||||
print("main done")
|
||||
return 42
|
||||
|
||||
|
||||
print(asyncio.run(main()))
|
@@ -0,0 +1,4 @@
|
||||
main start
|
||||
forever start
|
||||
main done
|
||||
42
|
@@ -0,0 +1,37 @@
|
||||
# Test fairness of cancelling a task
|
||||
# That tasks which continuously cancel each other don't take over the scheduler
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def task(id, other):
|
||||
for i in range(3):
|
||||
try:
|
||||
print("start", id)
|
||||
await asyncio.sleep(0)
|
||||
print("done", id)
|
||||
except asyncio.CancelledError as er:
|
||||
print("cancelled", id)
|
||||
if other is not None:
|
||||
print(id, "cancels", other)
|
||||
tasks[other].cancel()
|
||||
|
||||
|
||||
async def main():
|
||||
global tasks
|
||||
tasks = [
|
||||
asyncio.create_task(task(0, 1)),
|
||||
asyncio.create_task(task(1, 0)),
|
||||
asyncio.create_task(task(2, None)),
|
||||
]
|
||||
await tasks[2]
|
||||
|
||||
|
||||
asyncio.run(main())
|
@@ -0,0 +1,24 @@
|
||||
start 0
|
||||
start 1
|
||||
start 2
|
||||
done 0
|
||||
0 cancels 1
|
||||
start 0
|
||||
cancelled 1
|
||||
1 cancels 0
|
||||
start 1
|
||||
done 2
|
||||
start 2
|
||||
cancelled 0
|
||||
0 cancels 1
|
||||
start 0
|
||||
cancelled 1
|
||||
1 cancels 0
|
||||
start 1
|
||||
done 2
|
||||
start 2
|
||||
cancelled 0
|
||||
0 cancels 1
|
||||
cancelled 1
|
||||
1 cancels 0
|
||||
done 2
|
@@ -0,0 +1,37 @@
|
||||
# Test fairness of cancelling a task
|
||||
# That tasks which keeps being cancelled by multiple other tasks gets a chance to run
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def task_a():
|
||||
try:
|
||||
while True:
|
||||
print("sleep a")
|
||||
await asyncio.sleep(0)
|
||||
except asyncio.CancelledError:
|
||||
print("cancelled a")
|
||||
|
||||
|
||||
async def task_b(id, other):
|
||||
while other.cancel():
|
||||
print("sleep b", id)
|
||||
await asyncio.sleep(0)
|
||||
print("done b", id)
|
||||
|
||||
|
||||
async def main():
|
||||
t = asyncio.create_task(task_a())
|
||||
for i in range(3):
|
||||
asyncio.create_task(task_b(i, t))
|
||||
await t
|
||||
|
||||
|
||||
asyncio.run(main())
|
@@ -0,0 +1,8 @@
|
||||
sleep a
|
||||
sleep b 0
|
||||
sleep b 1
|
||||
sleep b 2
|
||||
cancelled a
|
||||
done b 0
|
||||
done b 1
|
||||
done b 2
|
@@ -0,0 +1,31 @@
|
||||
# Test a task cancelling itself (currently unsupported)
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def task():
|
||||
print("task start")
|
||||
global_task.cancel()
|
||||
|
||||
|
||||
async def main():
|
||||
global global_task
|
||||
global_task = asyncio.create_task(task())
|
||||
try:
|
||||
await global_task
|
||||
except asyncio.CancelledError:
|
||||
print("main cancel")
|
||||
print("main done")
|
||||
|
||||
|
||||
try:
|
||||
asyncio.run(main())
|
||||
except RuntimeError as er:
|
||||
print(er)
|
@@ -0,0 +1,2 @@
|
||||
task start
|
||||
can't cancel self
|
@@ -0,0 +1,85 @@
|
||||
# Test cancelling a task
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def task(s, allow_cancel):
|
||||
try:
|
||||
print("task start")
|
||||
await asyncio.sleep(s)
|
||||
print("task done")
|
||||
except asyncio.CancelledError as er:
|
||||
print("task cancel")
|
||||
if allow_cancel:
|
||||
raise er
|
||||
|
||||
|
||||
async def task2(allow_cancel):
|
||||
print("task 2")
|
||||
try:
|
||||
await asyncio.create_task(task(0.05, allow_cancel))
|
||||
except asyncio.CancelledError as er:
|
||||
print("task 2 cancel")
|
||||
raise er
|
||||
print("task 2 done")
|
||||
|
||||
|
||||
async def main():
|
||||
# Cancel task immediately
|
||||
t = asyncio.create_task(task(2, True))
|
||||
print(t.cancel())
|
||||
|
||||
# Cancel task after it has started
|
||||
t = asyncio.create_task(task(2, True))
|
||||
await asyncio.sleep(0.01)
|
||||
print(t.cancel())
|
||||
print("main sleep")
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
# Cancel task multiple times after it has started
|
||||
t = asyncio.create_task(task(2, True))
|
||||
await asyncio.sleep(0.01)
|
||||
for _ in range(4):
|
||||
print(t.cancel())
|
||||
print("main sleep")
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
# Await on a cancelled task
|
||||
print("main wait")
|
||||
try:
|
||||
await t
|
||||
except asyncio.CancelledError:
|
||||
print("main got CancelledError")
|
||||
|
||||
# Cancel task after it has finished
|
||||
t = asyncio.create_task(task(0.01, False))
|
||||
await asyncio.sleep(0.05)
|
||||
print(t.cancel())
|
||||
|
||||
# Nested: task2 waits on task, task2 is cancelled (should cancel task then task2)
|
||||
print("----")
|
||||
t = asyncio.create_task(task2(True))
|
||||
await asyncio.sleep(0.01)
|
||||
print("main cancel")
|
||||
t.cancel()
|
||||
print("main sleep")
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
# Nested: task2 waits on task, task2 is cancelled but task doesn't allow it (task2 should continue)
|
||||
print("----")
|
||||
t = asyncio.create_task(task2(False))
|
||||
await asyncio.sleep(0.01)
|
||||
print("main cancel")
|
||||
t.cancel()
|
||||
print("main sleep")
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
|
||||
asyncio.run(main())
|
@@ -0,0 +1,31 @@
|
||||
True
|
||||
task start
|
||||
True
|
||||
main sleep
|
||||
task cancel
|
||||
task start
|
||||
True
|
||||
True
|
||||
True
|
||||
True
|
||||
main sleep
|
||||
task cancel
|
||||
main wait
|
||||
main got CancelledError
|
||||
task start
|
||||
task done
|
||||
False
|
||||
----
|
||||
task 2
|
||||
task start
|
||||
main cancel
|
||||
main sleep
|
||||
task cancel
|
||||
task 2 cancel
|
||||
----
|
||||
task 2
|
||||
task start
|
||||
main cancel
|
||||
main sleep
|
||||
task cancel
|
||||
task 2 done
|
@@ -0,0 +1,41 @@
|
||||
# Test cancelling a task that is waiting on a task that just finishes.
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def sleep_task():
|
||||
print("sleep_task sleep")
|
||||
await asyncio.sleep(0)
|
||||
print("sleep_task wake")
|
||||
|
||||
|
||||
async def wait_task(t):
|
||||
print("wait_task wait")
|
||||
await t
|
||||
print("wait_task wake")
|
||||
|
||||
|
||||
async def main():
|
||||
waiting_task = asyncio.create_task(wait_task(asyncio.create_task(sleep_task())))
|
||||
|
||||
print("main sleep")
|
||||
await asyncio.sleep(0)
|
||||
print("main sleep")
|
||||
await asyncio.sleep(0)
|
||||
|
||||
waiting_task.cancel()
|
||||
print("main wait")
|
||||
try:
|
||||
await waiting_task
|
||||
except asyncio.CancelledError as er:
|
||||
print(repr(er))
|
||||
|
||||
|
||||
asyncio.run(main())
|
@@ -0,0 +1,7 @@
|
||||
main sleep
|
||||
sleep_task sleep
|
||||
wait_task wait
|
||||
main sleep
|
||||
sleep_task wake
|
||||
main wait
|
||||
CancelledError()
|
@@ -0,0 +1,25 @@
|
||||
# Test current_task() function
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def task(result):
|
||||
result[0] = asyncio.current_task()
|
||||
|
||||
|
||||
async def main():
|
||||
result = [None]
|
||||
t = asyncio.create_task(task(result))
|
||||
await asyncio.sleep(0)
|
||||
await asyncio.sleep(0)
|
||||
print(t is result[0])
|
||||
|
||||
|
||||
asyncio.run(main())
|
@@ -0,0 +1 @@
|
||||
True
|
@@ -0,0 +1,98 @@
|
||||
# Test Event class
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def task(id, ev):
|
||||
print("start", id)
|
||||
print(await ev.wait())
|
||||
print("end", id)
|
||||
|
||||
|
||||
async def task_delay_set(t, ev):
|
||||
await asyncio.sleep(t)
|
||||
print("set event")
|
||||
ev.set()
|
||||
|
||||
|
||||
async def main():
|
||||
ev = asyncio.Event()
|
||||
|
||||
# Set and clear without anything waiting, and test is_set()
|
||||
print(ev.is_set())
|
||||
ev.set()
|
||||
print(ev.is_set())
|
||||
ev.clear()
|
||||
print(ev.is_set())
|
||||
|
||||
# Create 2 tasks waiting on the event
|
||||
print("----")
|
||||
asyncio.create_task(task(1, ev))
|
||||
asyncio.create_task(task(2, ev))
|
||||
print("yield")
|
||||
await asyncio.sleep(0)
|
||||
print("set event")
|
||||
ev.set()
|
||||
print("yield")
|
||||
await asyncio.sleep(0)
|
||||
|
||||
# Create a task waiting on the already-set event
|
||||
print("----")
|
||||
asyncio.create_task(task(3, ev))
|
||||
print("yield")
|
||||
await asyncio.sleep(0)
|
||||
|
||||
# Clear event, start a task, then set event again
|
||||
print("----")
|
||||
print("clear event")
|
||||
ev.clear()
|
||||
asyncio.create_task(task(4, ev))
|
||||
await asyncio.sleep(0)
|
||||
print("set event")
|
||||
ev.set()
|
||||
await asyncio.sleep(0)
|
||||
|
||||
# Cancel a task waiting on an event (set event then cancel task)
|
||||
print("----")
|
||||
ev = asyncio.Event()
|
||||
t = asyncio.create_task(task(5, ev))
|
||||
await asyncio.sleep(0)
|
||||
ev.set()
|
||||
t.cancel()
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
# Cancel a task waiting on an event (cancel task then set event)
|
||||
print("----")
|
||||
ev = asyncio.Event()
|
||||
t = asyncio.create_task(task(6, ev))
|
||||
await asyncio.sleep(0)
|
||||
t.cancel()
|
||||
ev.set()
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
# Wait for an event that does get set in time
|
||||
print("----")
|
||||
ev.clear()
|
||||
asyncio.create_task(task_delay_set(0.01, ev))
|
||||
await asyncio.wait_for(ev.wait(), 0.1)
|
||||
await asyncio.sleep(0)
|
||||
|
||||
# Wait for an event that doesn't get set in time
|
||||
print("----")
|
||||
ev.clear()
|
||||
asyncio.create_task(task_delay_set(0.1, ev))
|
||||
try:
|
||||
await asyncio.wait_for(ev.wait(), 0.01)
|
||||
except asyncio.TimeoutError:
|
||||
print("TimeoutError")
|
||||
await ev.wait()
|
||||
|
||||
|
||||
asyncio.run(main())
|
@@ -0,0 +1,33 @@
|
||||
False
|
||||
True
|
||||
False
|
||||
----
|
||||
yield
|
||||
start 1
|
||||
start 2
|
||||
set event
|
||||
yield
|
||||
True
|
||||
end 1
|
||||
True
|
||||
end 2
|
||||
----
|
||||
yield
|
||||
start 3
|
||||
True
|
||||
end 3
|
||||
----
|
||||
clear event
|
||||
start 4
|
||||
set event
|
||||
True
|
||||
end 4
|
||||
----
|
||||
start 5
|
||||
----
|
||||
start 6
|
||||
----
|
||||
set event
|
||||
----
|
||||
TimeoutError
|
||||
set event
|
@@ -0,0 +1,40 @@
|
||||
# Test fairness of Event.set()
|
||||
# That tasks which continuously wait on events don't take over the scheduler
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def task1(id):
|
||||
for i in range(4):
|
||||
print("sleep", id)
|
||||
await asyncio.sleep(0)
|
||||
|
||||
|
||||
async def task2(id, ev):
|
||||
for i in range(4):
|
||||
ev.set()
|
||||
ev.clear()
|
||||
print("wait", id)
|
||||
await ev.wait()
|
||||
|
||||
|
||||
async def main():
|
||||
ev = asyncio.Event()
|
||||
tasks = [
|
||||
asyncio.create_task(task1(0)),
|
||||
asyncio.create_task(task2(2, ev)),
|
||||
asyncio.create_task(task1(1)),
|
||||
asyncio.create_task(task2(3, ev)),
|
||||
]
|
||||
await tasks[1]
|
||||
ev.set()
|
||||
|
||||
|
||||
asyncio.run(main())
|
@@ -0,0 +1,16 @@
|
||||
sleep 0
|
||||
wait 2
|
||||
sleep 1
|
||||
wait 3
|
||||
sleep 0
|
||||
sleep 1
|
||||
wait 2
|
||||
sleep 0
|
||||
sleep 1
|
||||
wait 3
|
||||
sleep 0
|
||||
sleep 1
|
||||
wait 2
|
||||
wait 3
|
||||
wait 2
|
||||
wait 3
|
@@ -0,0 +1,60 @@
|
||||
# Test general exception handling
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
# main task raising an exception
|
||||
async def main():
|
||||
print("main start")
|
||||
raise ValueError(1)
|
||||
print("main done")
|
||||
|
||||
|
||||
try:
|
||||
asyncio.run(main())
|
||||
except ValueError as er:
|
||||
print("ValueError", er.args[0])
|
||||
|
||||
# sub-task raising an exception
|
||||
async def task():
|
||||
print("task start")
|
||||
raise ValueError(2)
|
||||
print("task done")
|
||||
|
||||
|
||||
async def main():
|
||||
print("main start")
|
||||
t = asyncio.create_task(task())
|
||||
await t
|
||||
print("main done")
|
||||
|
||||
|
||||
try:
|
||||
asyncio.run(main())
|
||||
except ValueError as er:
|
||||
print("ValueError", er.args[0])
|
||||
|
||||
# main task raising an exception with sub-task not yet scheduled
|
||||
# TODO not currently working, task is never scheduled
|
||||
async def task():
|
||||
# print('task run') uncomment this line when it works
|
||||
pass
|
||||
|
||||
|
||||
async def main():
|
||||
print("main start")
|
||||
asyncio.create_task(task())
|
||||
raise ValueError(3)
|
||||
print("main done")
|
||||
|
||||
|
||||
try:
|
||||
asyncio.run(main())
|
||||
except ValueError as er:
|
||||
print("ValueError", er.args[0])
|
@@ -0,0 +1,7 @@
|
||||
main start
|
||||
ValueError 1
|
||||
main start
|
||||
task start
|
||||
ValueError 2
|
||||
main start
|
||||
ValueError 3
|
@@ -0,0 +1,34 @@
|
||||
# Test fairness of scheduler
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def task(id, t):
|
||||
print("task start", id)
|
||||
while True:
|
||||
if t > 0:
|
||||
print("task work", id)
|
||||
await asyncio.sleep(t)
|
||||
|
||||
|
||||
async def main():
|
||||
t1 = asyncio.create_task(task(1, -0.01))
|
||||
t2 = asyncio.create_task(task(2, 0.1))
|
||||
t3 = asyncio.create_task(task(3, 0.18))
|
||||
t4 = asyncio.create_task(task(4, -100))
|
||||
await asyncio.sleep(0.5)
|
||||
t1.cancel()
|
||||
t2.cancel()
|
||||
t3.cancel()
|
||||
t4.cancel()
|
||||
print("finish")
|
||||
|
||||
|
||||
asyncio.run(main())
|
@@ -0,0 +1,13 @@
|
||||
task start 1
|
||||
task start 2
|
||||
task work 2
|
||||
task start 3
|
||||
task work 3
|
||||
task start 4
|
||||
task work 2
|
||||
task work 3
|
||||
task work 2
|
||||
task work 2
|
||||
task work 3
|
||||
task work 2
|
||||
finish
|
109
components/language/micropython/tests/extmod/uasyncio_gather.py
Normal file
109
components/language/micropython/tests/extmod/uasyncio_gather.py
Normal file
@@ -0,0 +1,109 @@
|
||||
# test uasyncio.gather() function
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def factorial(name, number):
|
||||
f = 1
|
||||
for i in range(2, number + 1):
|
||||
print("Task {}: Compute factorial({})...".format(name, i))
|
||||
await asyncio.sleep(0.01)
|
||||
f *= i
|
||||
print("Task {}: factorial({}) = {}".format(name, number, f))
|
||||
return f
|
||||
|
||||
|
||||
async def task(id, t=0.1):
|
||||
print("start", id)
|
||||
await asyncio.sleep(t)
|
||||
print("end", id)
|
||||
return id
|
||||
|
||||
|
||||
async def task_loop(id):
|
||||
print("task_loop start", id)
|
||||
while True:
|
||||
await asyncio.sleep(0.1)
|
||||
print("task_loop loop", id)
|
||||
|
||||
|
||||
async def task_raise(id, t=0.1):
|
||||
print("task_raise start", id)
|
||||
await asyncio.sleep(t)
|
||||
print("task_raise raise", id)
|
||||
raise ValueError(id)
|
||||
|
||||
|
||||
async def gather_task(t0, t1):
|
||||
print("gather_task")
|
||||
await asyncio.gather(t0, t1)
|
||||
print("gather_task2")
|
||||
|
||||
|
||||
async def main():
|
||||
# Simple gather with return values
|
||||
print(await asyncio.gather(factorial("A", 2), factorial("B", 3), factorial("C", 4)))
|
||||
|
||||
print("====")
|
||||
|
||||
# Test return_exceptions, where one task is cancelled and the other finishes normally
|
||||
tasks = [asyncio.create_task(task(1)), asyncio.create_task(task(2))]
|
||||
tasks[0].cancel()
|
||||
print(await asyncio.gather(*tasks, return_exceptions=True))
|
||||
|
||||
print("====")
|
||||
|
||||
# Test return_exceptions, where one task raises an exception and the other finishes normally.
|
||||
tasks = [asyncio.create_task(task(1)), asyncio.create_task(task_raise(2))]
|
||||
print(await asyncio.gather(*tasks, return_exceptions=True))
|
||||
|
||||
print("====")
|
||||
|
||||
# Test case where one task raises an exception and other task keeps running.
|
||||
tasks = [asyncio.create_task(task_loop(1)), asyncio.create_task(task_raise(2))]
|
||||
try:
|
||||
await asyncio.gather(*tasks)
|
||||
except ValueError as er:
|
||||
print(repr(er))
|
||||
print(tasks[0].done(), tasks[1].done())
|
||||
for t in tasks:
|
||||
t.cancel()
|
||||
await asyncio.sleep(0.2)
|
||||
|
||||
print("====")
|
||||
|
||||
# Test case where both tasks raise an exception.
|
||||
# Use t=0 so they raise one after the other, between the gather starting and finishing.
|
||||
tasks = [asyncio.create_task(task_raise(1, t=0)), asyncio.create_task(task_raise(2, t=0))]
|
||||
try:
|
||||
await asyncio.gather(*tasks)
|
||||
except ValueError as er:
|
||||
print(repr(er))
|
||||
print(tasks[0].done(), tasks[1].done())
|
||||
|
||||
print("====")
|
||||
|
||||
# Cancel a multi gather.
|
||||
t = asyncio.create_task(gather_task(task(1), task(2)))
|
||||
await asyncio.sleep(0.05)
|
||||
t.cancel()
|
||||
await asyncio.sleep(0.2)
|
||||
|
||||
# Test edge cases where the gather is cancelled just as tasks are created and ending.
|
||||
for i in range(1, 4):
|
||||
print("====")
|
||||
t = asyncio.create_task(gather_task(task(1, t=0), task(2, t=0)))
|
||||
for _ in range(i):
|
||||
await asyncio.sleep(0)
|
||||
t.cancel()
|
||||
await asyncio.sleep(0.2)
|
||||
|
||||
|
||||
asyncio.run(main())
|
@@ -0,0 +1,54 @@
|
||||
Task A: Compute factorial(2)...
|
||||
Task B: Compute factorial(2)...
|
||||
Task C: Compute factorial(2)...
|
||||
Task A: factorial(2) = 2
|
||||
Task B: Compute factorial(3)...
|
||||
Task C: Compute factorial(3)...
|
||||
Task B: factorial(3) = 6
|
||||
Task C: Compute factorial(4)...
|
||||
Task C: factorial(4) = 24
|
||||
[2, 6, 24]
|
||||
====
|
||||
start 2
|
||||
end 2
|
||||
[CancelledError(), 2]
|
||||
====
|
||||
start 1
|
||||
task_raise start 2
|
||||
end 1
|
||||
task_raise raise 2
|
||||
[1, ValueError(2,)]
|
||||
====
|
||||
task_loop start 1
|
||||
task_raise start 2
|
||||
task_loop loop 1
|
||||
task_raise raise 2
|
||||
ValueError(2,)
|
||||
False True
|
||||
====
|
||||
task_raise start 1
|
||||
task_raise start 2
|
||||
task_raise raise 1
|
||||
task_raise raise 2
|
||||
ValueError(1,)
|
||||
True True
|
||||
====
|
||||
gather_task
|
||||
start 1
|
||||
start 2
|
||||
====
|
||||
gather_task
|
||||
start 1
|
||||
start 2
|
||||
====
|
||||
gather_task
|
||||
start 1
|
||||
start 2
|
||||
end 1
|
||||
end 2
|
||||
====
|
||||
gather_task
|
||||
start 1
|
||||
start 2
|
||||
end 1
|
||||
end 2
|
@@ -0,0 +1,53 @@
|
||||
# Test uasyncio.gather() function, features that are not implemented.
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
def custom_handler(loop, context):
|
||||
print(repr(context["exception"]))
|
||||
|
||||
|
||||
async def task(id):
|
||||
print("task start", id)
|
||||
await asyncio.sleep(0.01)
|
||||
print("task end", id)
|
||||
return id
|
||||
|
||||
|
||||
async def gather_task(t0, t1):
|
||||
print("gather_task start")
|
||||
await asyncio.gather(t0, t1)
|
||||
print("gather_task end")
|
||||
|
||||
|
||||
async def main():
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.set_exception_handler(custom_handler)
|
||||
|
||||
# Test case where can't wait on a task being gathered.
|
||||
tasks = [asyncio.create_task(task(1)), asyncio.create_task(task(2))]
|
||||
gt = asyncio.create_task(gather_task(tasks[0], tasks[1]))
|
||||
await asyncio.sleep(0) # let the gather start
|
||||
try:
|
||||
await tasks[0] # can't await because this task is part of the gather
|
||||
except RuntimeError as er:
|
||||
print(repr(er))
|
||||
await gt
|
||||
|
||||
print("====")
|
||||
|
||||
# Test case where can't gather on a task being waited.
|
||||
tasks = [asyncio.create_task(task(1)), asyncio.create_task(task(2))]
|
||||
asyncio.create_task(gather_task(tasks[0], tasks[1]))
|
||||
await tasks[0] # wait on this task before the gather starts
|
||||
await tasks[1]
|
||||
|
||||
|
||||
asyncio.run(main())
|
@@ -0,0 +1,14 @@
|
||||
task start 1
|
||||
task start 2
|
||||
gather_task start
|
||||
RuntimeError("can't wait",)
|
||||
task end 1
|
||||
task end 2
|
||||
gather_task end
|
||||
====
|
||||
task start 1
|
||||
task start 2
|
||||
gather_task start
|
||||
RuntimeError("can't gather",)
|
||||
task end 1
|
||||
task end 2
|
@@ -0,0 +1,20 @@
|
||||
# Test get_event_loop()
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def main():
|
||||
print("start")
|
||||
await asyncio.sleep(0.01)
|
||||
print("end")
|
||||
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(main())
|
@@ -0,0 +1,46 @@
|
||||
# test that basic scheduling of tasks, and uasyncio.sleep_ms, does not use the heap
|
||||
|
||||
import micropython
|
||||
|
||||
# strict stackless builds can't call functions without allocating a frame on the heap
|
||||
try:
|
||||
f = lambda: 0
|
||||
micropython.heap_lock()
|
||||
f()
|
||||
micropython.heap_unlock()
|
||||
except RuntimeError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def task(id, n, t):
|
||||
for i in range(n):
|
||||
print(id, i)
|
||||
await asyncio.sleep_ms(t)
|
||||
|
||||
|
||||
async def main():
|
||||
t1 = asyncio.create_task(task(1, 4, 100))
|
||||
t2 = asyncio.create_task(task(2, 2, 250))
|
||||
|
||||
micropython.heap_lock()
|
||||
|
||||
print("start")
|
||||
await asyncio.sleep_ms(5)
|
||||
print("sleep")
|
||||
await asyncio.sleep_ms(350)
|
||||
print("finish")
|
||||
|
||||
micropython.heap_unlock()
|
||||
|
||||
|
||||
asyncio.run(main())
|
@@ -0,0 +1,9 @@
|
||||
start
|
||||
1 0
|
||||
2 0
|
||||
sleep
|
||||
1 1
|
||||
1 2
|
||||
2 1
|
||||
1 3
|
||||
finish
|
@@ -0,0 +1,97 @@
|
||||
# Test Lock class
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def task_loop(id, lock):
|
||||
print("task start", id)
|
||||
for i in range(3):
|
||||
async with lock:
|
||||
print("task have", id, i)
|
||||
print("task end", id)
|
||||
|
||||
|
||||
async def task_sleep(lock):
|
||||
async with lock:
|
||||
print("task have", lock.locked())
|
||||
await asyncio.sleep(0.2)
|
||||
print("task release", lock.locked())
|
||||
await lock.acquire()
|
||||
print("task have again")
|
||||
lock.release()
|
||||
|
||||
|
||||
async def task_cancel(id, lock, to_cancel=None):
|
||||
try:
|
||||
async with lock:
|
||||
print("task got", id)
|
||||
await asyncio.sleep(0.1)
|
||||
print("task release", id)
|
||||
if to_cancel:
|
||||
to_cancel[0].cancel()
|
||||
except asyncio.CancelledError:
|
||||
print("task cancel", id)
|
||||
|
||||
|
||||
async def main():
|
||||
lock = asyncio.Lock()
|
||||
|
||||
# Basic acquire/release
|
||||
print(lock.locked())
|
||||
await lock.acquire()
|
||||
print(lock.locked())
|
||||
await asyncio.sleep(0)
|
||||
lock.release()
|
||||
print(lock.locked())
|
||||
await asyncio.sleep(0)
|
||||
|
||||
# Use with "async with"
|
||||
async with lock:
|
||||
print("have lock")
|
||||
|
||||
# 3 tasks wanting the lock
|
||||
print("----")
|
||||
asyncio.create_task(task_loop(1, lock))
|
||||
asyncio.create_task(task_loop(2, lock))
|
||||
t3 = asyncio.create_task(task_loop(3, lock))
|
||||
await lock.acquire()
|
||||
await asyncio.sleep(0)
|
||||
lock.release()
|
||||
await t3
|
||||
|
||||
# 2 sleeping tasks both wanting the lock
|
||||
print("----")
|
||||
asyncio.create_task(task_sleep(lock))
|
||||
await asyncio.sleep(0.1)
|
||||
await task_sleep(lock)
|
||||
|
||||
# 3 tasks, the first cancelling the second, the third should still run
|
||||
print("----")
|
||||
ts = [None]
|
||||
asyncio.create_task(task_cancel(0, lock, ts))
|
||||
ts[0] = asyncio.create_task(task_cancel(1, lock))
|
||||
asyncio.create_task(task_cancel(2, lock))
|
||||
await asyncio.sleep(0.3)
|
||||
print(lock.locked())
|
||||
|
||||
# 3 tasks, the second and third being cancelled while waiting on the lock
|
||||
print("----")
|
||||
t0 = asyncio.create_task(task_cancel(0, lock))
|
||||
t1 = asyncio.create_task(task_cancel(1, lock))
|
||||
t2 = asyncio.create_task(task_cancel(2, lock))
|
||||
await asyncio.sleep(0.05)
|
||||
t1.cancel()
|
||||
await asyncio.sleep(0.1)
|
||||
t2.cancel()
|
||||
await asyncio.sleep(0.1)
|
||||
print(lock.locked())
|
||||
|
||||
|
||||
asyncio.run(main())
|
@@ -0,0 +1,41 @@
|
||||
False
|
||||
True
|
||||
False
|
||||
have lock
|
||||
----
|
||||
task start 1
|
||||
task start 2
|
||||
task start 3
|
||||
task have 1 0
|
||||
task have 2 0
|
||||
task have 3 0
|
||||
task have 1 1
|
||||
task have 2 1
|
||||
task have 3 1
|
||||
task have 1 2
|
||||
task end 1
|
||||
task have 2 2
|
||||
task end 2
|
||||
task have 3 2
|
||||
task end 3
|
||||
----
|
||||
task have True
|
||||
task release False
|
||||
task have True
|
||||
task release False
|
||||
task have again
|
||||
task have again
|
||||
----
|
||||
task got 0
|
||||
task release 0
|
||||
task cancel 1
|
||||
task got 2
|
||||
task release 2
|
||||
False
|
||||
----
|
||||
task got 0
|
||||
task cancel 1
|
||||
task release 0
|
||||
task got 2
|
||||
task cancel 2
|
||||
False
|
@@ -0,0 +1,55 @@
|
||||
# Test that locks work when cancelling multiple waiters on the lock
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def task(i, lock, lock_flag):
|
||||
print("task", i, "start")
|
||||
try:
|
||||
await lock.acquire()
|
||||
except asyncio.CancelledError:
|
||||
print("task", i, "cancel")
|
||||
return
|
||||
print("task", i, "lock_flag", lock_flag[0])
|
||||
lock_flag[0] = True
|
||||
await asyncio.sleep(0)
|
||||
lock.release()
|
||||
lock_flag[0] = False
|
||||
print("task", i, "done")
|
||||
|
||||
|
||||
async def main():
|
||||
# Create a lock and acquire it so the tasks below must wait
|
||||
lock = asyncio.Lock()
|
||||
await lock.acquire()
|
||||
lock_flag = [True]
|
||||
|
||||
# Create 4 tasks and let them all run
|
||||
t0 = asyncio.create_task(task(0, lock, lock_flag))
|
||||
t1 = asyncio.create_task(task(1, lock, lock_flag))
|
||||
t2 = asyncio.create_task(task(2, lock, lock_flag))
|
||||
t3 = asyncio.create_task(task(3, lock, lock_flag))
|
||||
await asyncio.sleep(0)
|
||||
|
||||
# Cancel 2 of the tasks (which are waiting on the lock) and release the lock
|
||||
t1.cancel()
|
||||
t2.cancel()
|
||||
lock.release()
|
||||
lock_flag[0] = False
|
||||
|
||||
# Let the tasks run to completion
|
||||
for _ in range(4):
|
||||
await asyncio.sleep(0)
|
||||
|
||||
# The locke should be unlocked
|
||||
print(lock.locked())
|
||||
|
||||
|
||||
asyncio.run(main())
|
@@ -0,0 +1,11 @@
|
||||
task 0 start
|
||||
task 1 start
|
||||
task 2 start
|
||||
task 3 start
|
||||
task 1 cancel
|
||||
task 2 cancel
|
||||
task 0 lock_flag False
|
||||
task 0 done
|
||||
task 3 lock_flag False
|
||||
task 3 done
|
||||
False
|
@@ -0,0 +1,45 @@
|
||||
# Test Loop.stop() to stop the event loop
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def task():
|
||||
print("task")
|
||||
|
||||
|
||||
async def main():
|
||||
print("start")
|
||||
|
||||
# Stop the loop after next yield
|
||||
loop.stop()
|
||||
|
||||
# Check that calling stop() again doesn't do/break anything
|
||||
loop.stop()
|
||||
|
||||
# This await should stop
|
||||
print("sleep")
|
||||
await asyncio.sleep(0)
|
||||
|
||||
# Schedule stop, then create a new task, then yield
|
||||
loop.stop()
|
||||
asyncio.create_task(task())
|
||||
await asyncio.sleep(0)
|
||||
|
||||
# Final stop
|
||||
print("end")
|
||||
loop.stop()
|
||||
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.create_task(main())
|
||||
|
||||
for i in range(3):
|
||||
print("run", i)
|
||||
loop.run_forever()
|
@@ -0,0 +1,7 @@
|
||||
run 0
|
||||
start
|
||||
sleep
|
||||
run 1
|
||||
run 2
|
||||
task
|
||||
end
|
@@ -0,0 +1,37 @@
|
||||
# Test MicroPython extensions on CPython asyncio:
|
||||
# - sleep_ms
|
||||
# - wait_for_ms
|
||||
|
||||
try:
|
||||
import utime, uasyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def task(id, t):
|
||||
print("task start", id)
|
||||
await uasyncio.sleep_ms(t)
|
||||
print("task end", id)
|
||||
return id * 2
|
||||
|
||||
|
||||
async def main():
|
||||
# Simple sleep_ms
|
||||
t0 = utime.ticks_ms()
|
||||
await uasyncio.sleep_ms(1)
|
||||
print(utime.ticks_diff(utime.ticks_ms(), t0) < 100)
|
||||
|
||||
# When task finished before the timeout
|
||||
print(await uasyncio.wait_for_ms(task(1, 5), 50))
|
||||
|
||||
# When timeout passes and task is cancelled
|
||||
try:
|
||||
print(await uasyncio.wait_for_ms(task(2, 50), 5))
|
||||
except uasyncio.TimeoutError:
|
||||
print("timeout")
|
||||
|
||||
print("finish")
|
||||
|
||||
|
||||
uasyncio.run(main())
|
@@ -0,0 +1,7 @@
|
||||
True
|
||||
task start 1
|
||||
task end 1
|
||||
2
|
||||
task start 2
|
||||
timeout
|
||||
finish
|
@@ -0,0 +1,36 @@
|
||||
# Test Loop.new_event_loop()
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def task():
|
||||
for i in range(4):
|
||||
print("task", i)
|
||||
await asyncio.sleep(0)
|
||||
await asyncio.sleep(0)
|
||||
|
||||
|
||||
async def main():
|
||||
print("start")
|
||||
loop.create_task(task())
|
||||
await asyncio.sleep(0)
|
||||
print("stop")
|
||||
loop.stop()
|
||||
|
||||
|
||||
# Use default event loop to run some tasks
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.create_task(main())
|
||||
loop.run_forever()
|
||||
|
||||
# Create new event loop, old one should not keep running
|
||||
loop = asyncio.new_event_loop()
|
||||
loop.create_task(main())
|
||||
loop.run_forever()
|
@@ -0,0 +1,6 @@
|
||||
start
|
||||
task 0
|
||||
stop
|
||||
start
|
||||
task 0
|
||||
stop
|
@@ -0,0 +1,56 @@
|
||||
# Test that tasks return their value correctly to the caller
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
def custom_handler(loop, context):
|
||||
print("custom_handler", repr(context["exception"]))
|
||||
|
||||
|
||||
async def task(i):
|
||||
# Raise with 2 args so exception prints the same in uPy and CPython
|
||||
raise ValueError(i, i + 1)
|
||||
|
||||
|
||||
async def main():
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
# Check default exception handler, should be None
|
||||
print(loop.get_exception_handler())
|
||||
|
||||
# Set exception handler and test it was set
|
||||
loop.set_exception_handler(custom_handler)
|
||||
print(loop.get_exception_handler() == custom_handler)
|
||||
|
||||
# Create a task that raises and uses the custom exception handler
|
||||
asyncio.create_task(task(0))
|
||||
print("sleep")
|
||||
for _ in range(2):
|
||||
await asyncio.sleep(0)
|
||||
|
||||
# Create 2 tasks to test order of printing exception
|
||||
asyncio.create_task(task(1))
|
||||
asyncio.create_task(task(2))
|
||||
print("sleep")
|
||||
for _ in range(2):
|
||||
await asyncio.sleep(0)
|
||||
|
||||
# Create a task, let it run, then await it (no exception should be printed)
|
||||
t = asyncio.create_task(task(3))
|
||||
await asyncio.sleep(0)
|
||||
try:
|
||||
await t
|
||||
except ValueError as er:
|
||||
print(repr(er))
|
||||
|
||||
print("done")
|
||||
|
||||
|
||||
asyncio.run(main())
|
@@ -0,0 +1,9 @@
|
||||
None
|
||||
True
|
||||
sleep
|
||||
custom_handler ValueError(0, 1)
|
||||
sleep
|
||||
custom_handler ValueError(1, 2)
|
||||
custom_handler ValueError(2, 3)
|
||||
ValueError(3, 4)
|
||||
done
|
@@ -0,0 +1,66 @@
|
||||
# Test the Task.done() method
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def task(t, exc=None):
|
||||
print("task start")
|
||||
if t >= 0:
|
||||
await asyncio.sleep(t)
|
||||
if exc:
|
||||
raise exc
|
||||
print("task done")
|
||||
|
||||
|
||||
async def main():
|
||||
# Task that finishes immediately.
|
||||
print("=" * 10)
|
||||
t = asyncio.create_task(task(-1))
|
||||
print(t.done())
|
||||
await asyncio.sleep(0)
|
||||
print(t.done())
|
||||
await t
|
||||
print(t.done())
|
||||
|
||||
# Task that starts, runs and finishes.
|
||||
print("=" * 10)
|
||||
t = asyncio.create_task(task(0.01))
|
||||
print(t.done())
|
||||
await asyncio.sleep(0)
|
||||
print(t.done())
|
||||
await t
|
||||
print(t.done())
|
||||
|
||||
# Task that raises immediately.
|
||||
print("=" * 10)
|
||||
t = asyncio.create_task(task(-1, ValueError))
|
||||
print(t.done())
|
||||
await asyncio.sleep(0)
|
||||
print(t.done())
|
||||
try:
|
||||
await t
|
||||
except ValueError as er:
|
||||
print(repr(er))
|
||||
print(t.done())
|
||||
|
||||
# Task that raises after a delay.
|
||||
print("=" * 10)
|
||||
t = asyncio.create_task(task(0.01, ValueError))
|
||||
print(t.done())
|
||||
await asyncio.sleep(0)
|
||||
print(t.done())
|
||||
try:
|
||||
await t
|
||||
except ValueError as er:
|
||||
print(repr(er))
|
||||
print(t.done())
|
||||
|
||||
|
||||
asyncio.run(main())
|
@@ -0,0 +1,24 @@
|
||||
==========
|
||||
False
|
||||
task start
|
||||
task done
|
||||
True
|
||||
True
|
||||
==========
|
||||
False
|
||||
task start
|
||||
False
|
||||
task done
|
||||
True
|
||||
==========
|
||||
False
|
||||
task start
|
||||
True
|
||||
ValueError()
|
||||
True
|
||||
==========
|
||||
False
|
||||
task start
|
||||
False
|
||||
ValueError()
|
||||
True
|
@@ -0,0 +1,79 @@
|
||||
# Test Event class
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
import micropython
|
||||
|
||||
try:
|
||||
micropython.schedule
|
||||
except AttributeError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
try:
|
||||
# Unix port can't select/poll on user-defined types.
|
||||
import uselect as select
|
||||
|
||||
poller = select.poll()
|
||||
poller.register(asyncio.ThreadSafeFlag())
|
||||
except TypeError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def task(id, flag):
|
||||
print("task", id)
|
||||
await flag.wait()
|
||||
print("task", id, "done")
|
||||
|
||||
|
||||
def set_from_schedule(flag):
|
||||
print("schedule")
|
||||
flag.set()
|
||||
print("schedule done")
|
||||
|
||||
|
||||
async def main():
|
||||
flag = asyncio.ThreadSafeFlag()
|
||||
|
||||
# Set the flag from within the loop.
|
||||
t = asyncio.create_task(task(1, flag))
|
||||
print("yield")
|
||||
await asyncio.sleep(0)
|
||||
print("set event")
|
||||
flag.set()
|
||||
print("yield")
|
||||
await asyncio.sleep(0)
|
||||
print("wait task")
|
||||
await t
|
||||
|
||||
# Set the flag from scheduler context.
|
||||
print("----")
|
||||
t = asyncio.create_task(task(2, flag))
|
||||
print("yield")
|
||||
await asyncio.sleep(0)
|
||||
print("set event")
|
||||
micropython.schedule(set_from_schedule, flag)
|
||||
print("yield")
|
||||
await asyncio.sleep(0)
|
||||
print("wait task")
|
||||
await t
|
||||
|
||||
# Flag already set.
|
||||
print("----")
|
||||
print("set event")
|
||||
flag.set()
|
||||
t = asyncio.create_task(task(3, flag))
|
||||
print("yield")
|
||||
await asyncio.sleep(0)
|
||||
print("wait task")
|
||||
await t
|
||||
|
||||
|
||||
asyncio.run(main())
|
@@ -0,0 +1,21 @@
|
||||
yield
|
||||
task 1
|
||||
set event
|
||||
yield
|
||||
wait task
|
||||
task 1 done
|
||||
----
|
||||
yield
|
||||
task 2
|
||||
set event
|
||||
yield
|
||||
schedule
|
||||
schedule done
|
||||
wait task
|
||||
task 2 done
|
||||
----
|
||||
set event
|
||||
yield
|
||||
task 3
|
||||
task 3 done
|
||||
wait task
|
@@ -0,0 +1,132 @@
|
||||
# Test asyncio.wait_for
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def task(id, t):
|
||||
print("task start", id)
|
||||
await asyncio.sleep(t)
|
||||
print("task end", id)
|
||||
return id * 2
|
||||
|
||||
|
||||
async def task_catch():
|
||||
print("task_catch start")
|
||||
try:
|
||||
await asyncio.sleep(0.2)
|
||||
except asyncio.CancelledError:
|
||||
print("ignore cancel")
|
||||
print("task_catch done")
|
||||
|
||||
|
||||
async def task_raise():
|
||||
print("task start")
|
||||
raise ValueError
|
||||
|
||||
|
||||
async def task_cancel_other(t, other):
|
||||
print("task_cancel_other start")
|
||||
await asyncio.sleep(t)
|
||||
print("task_cancel_other cancel")
|
||||
other.cancel()
|
||||
|
||||
|
||||
async def task_wait_for_cancel(id, t, t_wait):
|
||||
print("task_wait_for_cancel start")
|
||||
try:
|
||||
await asyncio.wait_for(task(id, t), t_wait)
|
||||
except asyncio.CancelledError as er:
|
||||
print("task_wait_for_cancel cancelled")
|
||||
raise er
|
||||
|
||||
|
||||
async def task_wait_for_cancel_ignore(t_wait):
|
||||
print("task_wait_for_cancel_ignore start")
|
||||
try:
|
||||
await asyncio.wait_for(task_catch(), t_wait)
|
||||
except asyncio.CancelledError as er:
|
||||
print("task_wait_for_cancel_ignore cancelled")
|
||||
raise er
|
||||
|
||||
|
||||
async def main():
|
||||
sep = "-" * 10
|
||||
|
||||
# When task finished before the timeout
|
||||
print(await asyncio.wait_for(task(1, 0.01), 10))
|
||||
print(sep)
|
||||
|
||||
# When timeout passes and task is cancelled
|
||||
try:
|
||||
print(await asyncio.wait_for(task(2, 10), 0.01))
|
||||
except asyncio.TimeoutError:
|
||||
print("timeout")
|
||||
print(sep)
|
||||
|
||||
# When timeout passes and task is cancelled, but task ignores the cancellation request
|
||||
try:
|
||||
print(await asyncio.wait_for(task_catch(), 0.1))
|
||||
except asyncio.TimeoutError:
|
||||
print("TimeoutError")
|
||||
print(sep)
|
||||
|
||||
# When task raises an exception
|
||||
try:
|
||||
print(await asyncio.wait_for(task_raise(), 1))
|
||||
except ValueError:
|
||||
print("ValueError")
|
||||
print(sep)
|
||||
|
||||
# Timeout of None means wait forever
|
||||
print(await asyncio.wait_for(task(3, 0.1), None))
|
||||
print(sep)
|
||||
|
||||
# When task is cancelled by another task
|
||||
t = asyncio.create_task(task(4, 10))
|
||||
asyncio.create_task(task_cancel_other(0.01, t))
|
||||
try:
|
||||
print(await asyncio.wait_for(t, 1))
|
||||
except asyncio.CancelledError as er:
|
||||
print(repr(er))
|
||||
print(sep)
|
||||
|
||||
# When wait_for gets cancelled
|
||||
t = asyncio.create_task(task_wait_for_cancel(4, 1, 2))
|
||||
await asyncio.sleep(0.01)
|
||||
t.cancel()
|
||||
await asyncio.sleep(0.01)
|
||||
print(sep)
|
||||
|
||||
# When wait_for gets cancelled and awaited task ignores the cancellation request
|
||||
t = asyncio.create_task(task_wait_for_cancel_ignore(2))
|
||||
await asyncio.sleep(0.01)
|
||||
t.cancel()
|
||||
await asyncio.sleep(0.01)
|
||||
print(sep)
|
||||
|
||||
# When wait_for gets cancelled and the task it's waiting on finishes around the
|
||||
# same time as the cancellation of the wait_for
|
||||
for num_sleep in range(1, 5):
|
||||
t = asyncio.create_task(task_wait_for_cancel(4 + num_sleep, 0, 2))
|
||||
for _ in range(num_sleep):
|
||||
await asyncio.sleep(0)
|
||||
assert not t.done()
|
||||
print("cancel wait_for")
|
||||
t.cancel()
|
||||
try:
|
||||
await t
|
||||
except asyncio.CancelledError as er:
|
||||
print(repr(er))
|
||||
print(sep)
|
||||
|
||||
print("finish")
|
||||
|
||||
|
||||
asyncio.run(main())
|
@@ -0,0 +1,62 @@
|
||||
task start 1
|
||||
task end 1
|
||||
2
|
||||
----------
|
||||
task start 2
|
||||
timeout
|
||||
----------
|
||||
task_catch start
|
||||
ignore cancel
|
||||
task_catch done
|
||||
TimeoutError
|
||||
----------
|
||||
task start
|
||||
ValueError
|
||||
----------
|
||||
task start 3
|
||||
task end 3
|
||||
6
|
||||
----------
|
||||
task start 4
|
||||
task_cancel_other start
|
||||
task_cancel_other cancel
|
||||
CancelledError()
|
||||
----------
|
||||
task_wait_for_cancel start
|
||||
task start 4
|
||||
task_wait_for_cancel cancelled
|
||||
----------
|
||||
task_wait_for_cancel_ignore start
|
||||
task_catch start
|
||||
task_wait_for_cancel_ignore cancelled
|
||||
ignore cancel
|
||||
task_catch done
|
||||
----------
|
||||
task_wait_for_cancel start
|
||||
cancel wait_for
|
||||
task start 5
|
||||
task_wait_for_cancel cancelled
|
||||
CancelledError()
|
||||
----------
|
||||
task_wait_for_cancel start
|
||||
task start 6
|
||||
cancel wait_for
|
||||
task end 6
|
||||
task_wait_for_cancel cancelled
|
||||
CancelledError()
|
||||
----------
|
||||
task_wait_for_cancel start
|
||||
task start 7
|
||||
task end 7
|
||||
cancel wait_for
|
||||
task_wait_for_cancel cancelled
|
||||
CancelledError()
|
||||
----------
|
||||
task_wait_for_cancel start
|
||||
task start 8
|
||||
task end 8
|
||||
cancel wait_for
|
||||
task_wait_for_cancel cancelled
|
||||
CancelledError()
|
||||
----------
|
||||
finish
|
@@ -0,0 +1,60 @@
|
||||
# Test asyncio.wait_for, with forwarding cancellation
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
async def awaiting(t, return_if_fail):
|
||||
try:
|
||||
print("awaiting started")
|
||||
await asyncio.sleep(t)
|
||||
except asyncio.CancelledError as er:
|
||||
# CPython wait_for raises CancelledError inside task but TimeoutError in wait_for
|
||||
print("awaiting canceled")
|
||||
if return_if_fail:
|
||||
return False # return has no effect if Cancelled
|
||||
else:
|
||||
raise er
|
||||
except Exception as er:
|
||||
print("caught exception", er)
|
||||
raise er
|
||||
|
||||
|
||||
async def test_cancellation_forwarded(catch, catch_inside):
|
||||
print("----------")
|
||||
|
||||
async def wait():
|
||||
try:
|
||||
await asyncio.wait_for(awaiting(2, catch_inside), 1)
|
||||
except asyncio.TimeoutError as er:
|
||||
print("Got timeout error")
|
||||
raise er
|
||||
except asyncio.CancelledError as er:
|
||||
print("Got canceled")
|
||||
if not catch:
|
||||
raise er
|
||||
|
||||
async def cancel(t):
|
||||
print("cancel started")
|
||||
await asyncio.sleep(0.01)
|
||||
print("cancel wait()")
|
||||
t.cancel()
|
||||
|
||||
t = asyncio.create_task(wait())
|
||||
k = asyncio.create_task(cancel(t))
|
||||
try:
|
||||
await t
|
||||
except asyncio.CancelledError:
|
||||
print("waiting got cancelled")
|
||||
|
||||
|
||||
asyncio.run(test_cancellation_forwarded(False, False))
|
||||
asyncio.run(test_cancellation_forwarded(False, True))
|
||||
asyncio.run(test_cancellation_forwarded(True, True))
|
||||
asyncio.run(test_cancellation_forwarded(True, False))
|
@@ -0,0 +1,26 @@
|
||||
----------
|
||||
cancel started
|
||||
awaiting started
|
||||
cancel wait()
|
||||
Got canceled
|
||||
awaiting canceled
|
||||
waiting got cancelled
|
||||
----------
|
||||
cancel started
|
||||
awaiting started
|
||||
cancel wait()
|
||||
Got canceled
|
||||
awaiting canceled
|
||||
waiting got cancelled
|
||||
----------
|
||||
cancel started
|
||||
awaiting started
|
||||
cancel wait()
|
||||
Got canceled
|
||||
awaiting canceled
|
||||
----------
|
||||
cancel started
|
||||
awaiting started
|
||||
cancel wait()
|
||||
Got canceled
|
||||
awaiting canceled
|
@@ -0,0 +1,77 @@
|
||||
# Test waiting on a task
|
||||
|
||||
try:
|
||||
import uasyncio as asyncio
|
||||
except ImportError:
|
||||
try:
|
||||
import asyncio
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
|
||||
try:
|
||||
import utime
|
||||
|
||||
ticks = utime.ticks_ms
|
||||
ticks_diff = utime.ticks_diff
|
||||
except:
|
||||
import time
|
||||
|
||||
ticks = lambda: int(time.time() * 1000)
|
||||
ticks_diff = lambda t1, t0: t1 - t0
|
||||
|
||||
|
||||
async def task(t):
|
||||
print("task", t)
|
||||
|
||||
|
||||
async def delay_print(t, s):
|
||||
await asyncio.sleep(t)
|
||||
print(s)
|
||||
|
||||
|
||||
async def task_raise():
|
||||
print("task_raise")
|
||||
raise ValueError
|
||||
|
||||
|
||||
async def main():
|
||||
print("start")
|
||||
|
||||
# Wait on a task
|
||||
t = asyncio.create_task(task(1))
|
||||
await t
|
||||
|
||||
# Wait on a task that's already done
|
||||
t = asyncio.create_task(task(2))
|
||||
await asyncio.sleep(0.001)
|
||||
await t
|
||||
|
||||
# Wait again on same task
|
||||
await t
|
||||
|
||||
print("----")
|
||||
|
||||
# Create 2 tasks
|
||||
ts1 = asyncio.create_task(delay_print(0.2, "hello"))
|
||||
ts2 = asyncio.create_task(delay_print(0.4, "world"))
|
||||
|
||||
# Time how long the tasks take to finish, they should execute in parallel
|
||||
print("start")
|
||||
t0 = ticks()
|
||||
await ts1
|
||||
t1 = ticks()
|
||||
await ts2
|
||||
t2 = ticks()
|
||||
print("took {} {}".format(round(ticks_diff(t1, t0), -2), round(ticks_diff(t2, t1), -2)))
|
||||
|
||||
# Wait on a task that raises an exception
|
||||
t = asyncio.create_task(task_raise())
|
||||
try:
|
||||
await t
|
||||
except ValueError:
|
||||
print("ValueError")
|
||||
|
||||
|
||||
asyncio.run(main())
|
@@ -0,0 +1,10 @@
|
||||
start
|
||||
task 1
|
||||
task 2
|
||||
----
|
||||
start
|
||||
hello
|
||||
world
|
||||
took 200 200
|
||||
task_raise
|
||||
ValueError
|
@@ -0,0 +1,46 @@
|
||||
try:
|
||||
try:
|
||||
import ubinascii as binascii
|
||||
except ImportError:
|
||||
import binascii
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
print(binascii.a2b_base64(b""))
|
||||
print(binascii.a2b_base64(b"Zg=="))
|
||||
print(binascii.a2b_base64(b"Zm8="))
|
||||
print(binascii.a2b_base64(b"Zm9v"))
|
||||
print(binascii.a2b_base64(b"Zm9vYg=="))
|
||||
print(binascii.a2b_base64(b"Zm9vYmE="))
|
||||
print(binascii.a2b_base64(b"Zm9vYmFy"))
|
||||
|
||||
print(binascii.a2b_base64(b"AAECAwQFBgc="))
|
||||
print(binascii.a2b_base64(b"CAkKCwwNDg8="))
|
||||
print(binascii.a2b_base64(b"f4D/"))
|
||||
print(binascii.a2b_base64(b"f4D+")) # convert '+'
|
||||
print(binascii.a2b_base64(b"MTIzNEFCQ0RhYmNk"))
|
||||
|
||||
# Ignore invalid characters and pad sequences
|
||||
print(binascii.a2b_base64(b"Zm9v\n"))
|
||||
print(binascii.a2b_base64(b"Zm\x009v\n"))
|
||||
print(binascii.a2b_base64(b"Zm9v=="))
|
||||
print(binascii.a2b_base64(b"Zm9v==="))
|
||||
print(binascii.a2b_base64(b"Zm9v===YmFy"))
|
||||
|
||||
try:
|
||||
print(binascii.a2b_base64(b"abc"))
|
||||
except ValueError:
|
||||
print("ValueError")
|
||||
try:
|
||||
print(binascii.a2b_base64(b"abcde="))
|
||||
except ValueError:
|
||||
print("ValueError")
|
||||
try:
|
||||
print(binascii.a2b_base64(b"ab*d"))
|
||||
except ValueError:
|
||||
print("ValueError")
|
||||
try:
|
||||
print(binascii.a2b_base64(b"ab=cdef="))
|
||||
except ValueError:
|
||||
print("ValueError")
|
@@ -0,0 +1,25 @@
|
||||
try:
|
||||
try:
|
||||
import ubinascii as binascii
|
||||
except ImportError:
|
||||
import binascii
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
print(binascii.b2a_base64(b""))
|
||||
print(binascii.b2a_base64(b"f"))
|
||||
print(binascii.b2a_base64(b"fo"))
|
||||
print(binascii.b2a_base64(b"foo"))
|
||||
print(binascii.b2a_base64(b"foob"))
|
||||
print(binascii.b2a_base64(b"fooba"))
|
||||
print(binascii.b2a_base64(b"foobar"))
|
||||
|
||||
print(binascii.b2a_base64(b"\x00\x01\x02\x03\x04\x05\x06\x07"))
|
||||
print(binascii.b2a_base64(b"\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"))
|
||||
print(binascii.b2a_base64(b"\x7f\x80\xff"))
|
||||
print(binascii.b2a_base64(b"1234ABCDabcd"))
|
||||
print(binascii.b2a_base64(b"\x00\x00>")) # convert into '+'
|
||||
|
||||
print(binascii.b2a_base64(b"foobar", newline=True))
|
||||
print(binascii.b2a_base64(b"foobar", newline=False))
|
@@ -0,0 +1,24 @@
|
||||
try:
|
||||
try:
|
||||
import ubinascii as binascii
|
||||
except ImportError:
|
||||
import binascii
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
try:
|
||||
binascii.crc32
|
||||
except AttributeError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
print(hex(binascii.crc32(b"The quick brown fox jumps over the lazy dog")))
|
||||
print(hex(binascii.crc32(b"\x00" * 32)))
|
||||
print(hex(binascii.crc32(b"\xff" * 32)))
|
||||
print(hex(binascii.crc32(bytes(range(32)))))
|
||||
|
||||
print(hex(binascii.crc32(b" over the lazy dog", binascii.crc32(b"The quick brown fox jumps"))))
|
||||
print(hex(binascii.crc32(b"\x00" * 16, binascii.crc32(b"\x00" * 16))))
|
||||
print(hex(binascii.crc32(b"\xff" * 16, binascii.crc32(b"\xff" * 16))))
|
||||
print(hex(binascii.crc32(bytes(range(16, 32)), binascii.crc32(bytes(range(16))))))
|
@@ -0,0 +1,13 @@
|
||||
try:
|
||||
try:
|
||||
import ubinascii as binascii
|
||||
except ImportError:
|
||||
import binascii
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
print(binascii.hexlify(b"\x00\x01\x02\x03\x04\x05\x06\x07"))
|
||||
print(binascii.hexlify(b"\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"))
|
||||
print(binascii.hexlify(b"\x7f\x80\xff"))
|
||||
print(binascii.hexlify(b"1234ABCDabcd"))
|
@@ -0,0 +1,15 @@
|
||||
try:
|
||||
try:
|
||||
import ubinascii as binascii
|
||||
except ImportError:
|
||||
import binascii
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
# two arguments supported in uPy but not CPython
|
||||
a = binascii.hexlify(b"123", ":")
|
||||
print(a)
|
||||
|
||||
# zero length buffer
|
||||
print(binascii.hexlify(b"", b":"))
|
@@ -0,0 +1,2 @@
|
||||
b'31:32:33'
|
||||
b''
|
@@ -0,0 +1,23 @@
|
||||
try:
|
||||
try:
|
||||
import ubinascii as binascii
|
||||
except ImportError:
|
||||
import binascii
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
print(binascii.unhexlify(b"0001020304050607"))
|
||||
print(binascii.unhexlify(b"08090a0b0c0d0e0f"))
|
||||
print(binascii.unhexlify(b"7f80ff"))
|
||||
print(binascii.unhexlify(b"313233344142434461626364"))
|
||||
|
||||
try:
|
||||
a = binascii.unhexlify(b"0") # odd buffer length
|
||||
except ValueError:
|
||||
print("ValueError")
|
||||
|
||||
try:
|
||||
a = binascii.unhexlify(b"gg") # digit not hex
|
||||
except ValueError:
|
||||
print("ValueError")
|
@@ -0,0 +1,16 @@
|
||||
try:
|
||||
from Crypto.Cipher import AES
|
||||
|
||||
aes = AES.new
|
||||
except ImportError:
|
||||
try:
|
||||
from ucryptolib import aes
|
||||
except ImportError:
|
||||
print("SKIP")
|
||||
raise SystemExit
|
||||
|
||||
crypto = aes(b"1234" * 4, 2, b"5678" * 4)
|
||||
enc = crypto.encrypt(bytes(range(32)))
|
||||
print(enc)
|
||||
crypto = aes(b"1234" * 4, 2, b"5678" * 4)
|
||||
print(crypto.decrypt(enc))
|
@@ -0,0 +1,2 @@
|
||||
b'\x1d\x84\xfa\xaa%\x0e9\x143\x8b6\xf8\xdf^yh\xd0\x94g\xf4\xcf\x1d\xa0I)\x8a\xa0\x00u0+C'
|
||||
b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f'
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user