tests: Format all Python code with black, except tests in basics subdir.

This adds the Python files in the tests/ directory to be formatted with
./tools/codeformat.py.  The basics/ subdirectory is excluded for now so we
aren't changing too much at once.

In a few places `# fmt: off`/`# fmt: on` was used where the code had
special formatting for readability or where the test was actually testing
the specific formatting.
This commit is contained in:
David Lechner 2020-03-22 21:26:08 -05:00 committed by Damien George
parent 488613bca6
commit 3dc324d3f1
472 changed files with 4396 additions and 2891 deletions

View File

@ -4,9 +4,9 @@
for i in (): for i in ():
pass pass
a = None a = None
b = 'str' b = "str"
c = 'a very long str that will not be interned' c = "a very long str that will not be interned"
d = b'bytes' d = b"bytes"
e = b'a very long bytes that will not be interned' e = b"a very long bytes that will not be interned"
f = 123456789012345678901234567890 f = 123456789012345678901234567890
g = 123 g = 123

View File

@ -1,5 +1,6 @@
# cmdline: -v -v # cmdline: -v -v
# test printing of all bytecodes # test printing of all bytecodes
# fmt: off
def f(): def f():
# constants # constants

View File

@ -7,7 +7,7 @@ arg names:
(N_EXC_STACK 0) (N_EXC_STACK 0)
bc=0 line=1 bc=0 line=1
######## ########
bc=\\d\+ line=159 bc=\\d\+ line=160
00 MAKE_FUNCTION \.\+ 00 MAKE_FUNCTION \.\+
\\d\+ STORE_NAME f \\d\+ STORE_NAME f
\\d\+ MAKE_FUNCTION \.\+ \\d\+ MAKE_FUNCTION \.\+
@ -45,7 +45,7 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
(INIT_CELL 16) (INIT_CELL 16)
bc=0 line=1 bc=0 line=1
######## ########
bc=\\d\+ line=126 bc=\\d\+ line=127
00 LOAD_CONST_NONE 00 LOAD_CONST_NONE
01 LOAD_CONST_FALSE 01 LOAD_CONST_FALSE
02 BINARY_OP 27 __add__ 02 BINARY_OP 27 __add__
@ -320,7 +320,7 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
(N_EXC_STACK 0) (N_EXC_STACK 0)
bc=0 line=1 bc=0 line=1
######## ########
bc=\\d\+ line=132 bc=\\d\+ line=133
00 LOAD_CONST_SMALL_INT 1 00 LOAD_CONST_SMALL_INT 1
01 DUP_TOP 01 DUP_TOP
02 STORE_FAST 0 02 STORE_FAST 0
@ -376,7 +376,7 @@ arg names: a
(N_EXC_STACK 0) (N_EXC_STACK 0)
(INIT_CELL 0) (INIT_CELL 0)
######## ########
bc=\\d\+ line=138 bc=\\d\+ line=139
00 LOAD_CONST_SMALL_INT 2 00 LOAD_CONST_SMALL_INT 2
01 BUILD_TUPLE 1 01 BUILD_TUPLE 1
03 LOAD_NULL 03 LOAD_NULL
@ -393,9 +393,9 @@ arg names:
(N_STATE 2) (N_STATE 2)
(N_EXC_STACK 0) (N_EXC_STACK 0)
bc=0 line=1 bc=0 line=1
bc=0 line=143 bc=0 line=144
bc=3 line=144 bc=3 line=145
bc=6 line=145 bc=6 line=146
00 LOAD_CONST_NONE 00 LOAD_CONST_NONE
01 YIELD_VALUE 01 YIELD_VALUE
02 POP_TOP 02 POP_TOP
@ -418,7 +418,7 @@ arg names:
(N_EXC_STACK 0) (N_EXC_STACK 0)
bc=0 line=1 bc=0 line=1
######## ########
bc=13 line=149 bc=13 line=150
00 LOAD_NAME __name__ (cache=0) 00 LOAD_NAME __name__ (cache=0)
04 STORE_NAME __module__ 04 STORE_NAME __module__
07 LOAD_CONST_STRING 'Class' 07 LOAD_CONST_STRING 'Class'
@ -433,7 +433,7 @@ arg names: self
(N_STATE 4) (N_STATE 4)
(N_EXC_STACK 0) (N_EXC_STACK 0)
bc=0 line=1 bc=0 line=1
bc=0 line=156 bc=0 line=157
00 LOAD_GLOBAL super (cache=0) 00 LOAD_GLOBAL super (cache=0)
\\d\+ LOAD_GLOBAL __class__ (cache=0) \\d\+ LOAD_GLOBAL __class__ (cache=0)
\\d\+ LOAD_FAST 0 \\d\+ LOAD_FAST 0
@ -450,7 +450,7 @@ arg names: * * *
(N_STATE 9) (N_STATE 9)
(N_EXC_STACK 0) (N_EXC_STACK 0)
bc=0 line=1 bc=0 line=1
bc=0 line=59 bc=0 line=60
######## ########
00 LOAD_NULL 00 LOAD_NULL
01 LOAD_FAST 2 01 LOAD_FAST 2
@ -474,7 +474,7 @@ arg names: * * *
(N_STATE 10) (N_STATE 10)
(N_EXC_STACK 0) (N_EXC_STACK 0)
bc=0 line=1 bc=0 line=1
bc=0 line=60 bc=0 line=61
######## ########
00 BUILD_LIST 0 00 BUILD_LIST 0
02 LOAD_FAST 2 02 LOAD_FAST 2
@ -517,7 +517,7 @@ arg names: *
(N_EXC_STACK 0) (N_EXC_STACK 0)
bc=0 line=1 bc=0 line=1
######## ########
bc=\\d\+ line=113 bc=\\d\+ line=114
00 LOAD_DEREF 0 00 LOAD_DEREF 0
02 LOAD_CONST_SMALL_INT 1 02 LOAD_CONST_SMALL_INT 1
03 BINARY_OP 27 __add__ 03 BINARY_OP 27 __add__
@ -536,7 +536,7 @@ arg names: * b
(N_EXC_STACK 0) (N_EXC_STACK 0)
bc=0 line=1 bc=0 line=1
######## ########
bc=\\d\+ line=139 bc=\\d\+ line=140
00 LOAD_FAST 1 00 LOAD_FAST 1
01 LOAD_DEREF 0 01 LOAD_DEREF 0
03 BINARY_OP 27 __add__ 03 BINARY_OP 27 __add__

View File

@ -6,9 +6,11 @@ workaround: Unknown
""" """
import gc import gc
class Foo():
class Foo:
def __del__(self): def __del__(self):
print('__del__') print("__del__")
f = Foo() f = Foo()
del f del f

View File

@ -4,12 +4,16 @@ description: Method Resolution Order (MRO) is not compliant with CPython
cause: Depth first non-exhaustive method resolution order cause: Depth first non-exhaustive method resolution order
workaround: Avoid complex class hierarchies with multiple inheritance and complex method overrides. Keep in mind that many languages don't support multiple inheritance at all. workaround: Avoid complex class hierarchies with multiple inheritance and complex method overrides. Keep in mind that many languages don't support multiple inheritance at all.
""" """
class Foo: class Foo:
def __str__(self): def __str__(self):
return "Foo" return "Foo"
class C(tuple, Foo): class C(tuple, Foo):
pass pass
t = C((1, 2, 3)) t = C((1, 2, 3))
print(t) print(t)

View File

@ -4,24 +4,29 @@ description: When inheriting from multiple classes super() only calls one class
cause: See :ref:`cpydiff_core_class_mro` cause: See :ref:`cpydiff_core_class_mro`
workaround: See :ref:`cpydiff_core_class_mro` workaround: See :ref:`cpydiff_core_class_mro`
""" """
class A: class A:
def __init__(self): def __init__(self):
print("A.__init__") print("A.__init__")
class B(A): class B(A):
def __init__(self): def __init__(self):
print("B.__init__") print("B.__init__")
super().__init__() super().__init__()
class C(A): class C(A):
def __init__(self): def __init__(self):
print("C.__init__") print("C.__init__")
super().__init__() super().__init__()
class D(B,C): class D(B, C):
def __init__(self): def __init__(self):
print("D.__init__") print("D.__init__")
super().__init__() super().__init__()
D() D()

View File

@ -4,15 +4,19 @@ description: Calling super() getter property in subclass will return a property
cause: Unknown cause: Unknown
workaround: Unknown workaround: Unknown
""" """
class A: class A:
@property @property
def p(self): def p(self):
return {"a":10} return {"a": 10}
class AA(A): class AA(A):
@property @property
def p(self): def p(self):
return super().p return super().p
a = AA() a = AA()
print(a.p) print(a.p)

View File

@ -4,8 +4,11 @@ description: User-defined attributes for functions are not supported
cause: MicroPython is highly optimized for memory usage. cause: MicroPython is highly optimized for memory usage.
workaround: Use external dictionary, e.g. ``FUNC_X[f] = 0``. workaround: Use external dictionary, e.g. ``FUNC_X[f] = 0``.
""" """
def f(): def f():
pass pass
f.x = 0 f.x = 0
print(f.x) print(f.x)

View File

@ -4,11 +4,15 @@ description: Context manager __exit__() not called in a generator which does not
cause: Unknown cause: Unknown
workaround: Unknown workaround: Unknown
""" """
class foo(object): class foo(object):
def __enter__(self): def __enter__(self):
print('Enter') print("Enter")
def __exit__(self, *args): def __exit__(self, *args):
print('Exit') print("Exit")
def bar(x): def bar(x):
with foo(): with foo():
@ -16,9 +20,11 @@ def bar(x):
x += 1 x += 1
yield x yield x
def func(): def func():
g = bar(0) g = bar(0)
for _ in range(3): for _ in range(3):
print(next(g)) print(next(g))
func() func()

View File

@ -12,6 +12,7 @@ except NameError as e:
print(e) print(e)
try: try:
from modules import foo from modules import foo
print('Should not get here')
print("Should not get here")
except NameError as e: except NameError as e:
print(e) print(e)

View File

@ -5,6 +5,7 @@ cause: MicroPython's import system is highly optimized for simplicity, minimal m
workaround: Don't install modules belonging to the same namespace package in different directories. For MicroPython, it's recommended to have at most 3-component module search paths: for your current application, per-user (writable), system-wide (non-writable). workaround: Don't install modules belonging to the same namespace package in different directories. For MicroPython, it's recommended to have at most 3-component module search paths: for your current application, per-user (writable), system-wide (non-writable).
""" """
import sys import sys
sys.path.append(sys.path[1] + "/modules") sys.path.append(sys.path[1] + "/modules")
sys.path.append(sys.path[1] + "/modules2") sys.path.append(sys.path[1] + "/modules2")

View File

@ -4,8 +4,11 @@ description: Local variables aren't included in locals() result
cause: MicroPython doesn't maintain symbolic local environment, it is optimized to an array of slots. Thus, local variables can't be accessed by a name. cause: MicroPython doesn't maintain symbolic local environment, it is optimized to an array of slots. Thus, local variables can't be accessed by a name.
workaround: Unknown workaround: Unknown
""" """
def test(): def test():
val = 2 val = 2
print(locals()) print(locals())
test() test()

View File

@ -6,9 +6,11 @@ workaround: Unknown
""" """
val = 1 val = 1
def test(): def test():
val = 2 val = 2
print(val) print(val)
eval("print(val)") eval("print(val)")
test() test()

View File

@ -1,2 +1,2 @@
print('foo') print("foo")
xxx xxx

View File

@ -5,4 +5,5 @@ cause: Unknown
workaround: Unknown workaround: Unknown
""" """
import array import array
print(1 in array.array('B', b'12'))
print(1 in array.array("B", b"12"))

View File

@ -5,6 +5,7 @@ cause: Unknown
workaround: Unknown workaround: Unknown
""" """
import array import array
a = array.array('b', (1, 2, 3))
a = array.array("b", (1, 2, 3))
del a[1] del a[1]
print(a) print(a)

View File

@ -5,5 +5,6 @@ cause: Unknown
workaround: Unknown workaround: Unknown
""" """
import array import array
a = array.array('b', (1, 2, 3))
a = array.array("b", (1, 2, 3))
print(a[3:2:2]) print(a[3:2:2])

View File

@ -5,5 +5,6 @@ cause: Unknown
workaround: Use regular lists. micropython-lib has implementation of collections.deque. workaround: Use regular lists. micropython-lib has implementation of collections.deque.
""" """
import collections import collections
D = collections.deque() D = collections.deque()
print(D) print(D)

View File

@ -5,10 +5,11 @@ cause: Unknown
workaround: Unknown workaround: Unknown
""" """
import json import json
a = bytes(x for x in range(256)) a = bytes(x for x in range(256))
try: try:
z = json.dumps(a) z = json.dumps(a)
x = json.loads(z) x = json.loads(z)
print('Should not get here') print("Should not get here")
except TypeError: except TypeError:
print('TypeError') print("TypeError")

View File

@ -5,12 +5,13 @@ cause: Unknown
workaround: Use ``getenv``, ``putenv`` and ``unsetenv`` workaround: Use ``getenv``, ``putenv`` and ``unsetenv``
""" """
import os import os
try: try:
print(os.environ.get('NEW_VARIABLE')) print(os.environ.get("NEW_VARIABLE"))
os.environ['NEW_VARIABLE'] = 'VALUE' os.environ["NEW_VARIABLE"] = "VALUE"
print(os.environ['NEW_VARIABLE']) print(os.environ["NEW_VARIABLE"])
except AttributeError: except AttributeError:
print('should not get here') print("should not get here")
print(os.getenv('NEW_VARIABLE')) print(os.getenv("NEW_VARIABLE"))
os.putenv('NEW_VARIABLE', 'VALUE') os.putenv("NEW_VARIABLE", "VALUE")
print(os.getenv('NEW_VARIABLE')) print(os.getenv("NEW_VARIABLE"))

View File

@ -5,6 +5,7 @@ cause: The ``environ`` attribute is not implemented
workaround: Unknown workaround: Unknown
""" """
import os import os
print(os.getenv('NEW_VARIABLE'))
os.putenv('NEW_VARIABLE', 'VALUE') print(os.getenv("NEW_VARIABLE"))
print(os.getenv('NEW_VARIABLE')) os.putenv("NEW_VARIABLE", "VALUE")
print(os.getenv("NEW_VARIABLE"))

View File

@ -5,9 +5,10 @@ cause: Unknown
workaround: Test that the return value is ``None`` workaround: Test that the return value is ``None``
""" """
import os import os
try: try:
print(os.getenv('NEW_VARIABLE', 'DEFAULT')) print(os.getenv("NEW_VARIABLE", "DEFAULT"))
except TypeError: except TypeError:
print('should not get here') print("should not get here")
# this assumes NEW_VARIABLE is never an empty variable # this assumes NEW_VARIABLE is never an empty variable
print(os.getenv('NEW_VARIABLE') or 'DEFAULT') print(os.getenv("NEW_VARIABLE") or "DEFAULT")

View File

@ -5,8 +5,9 @@ cause: Unknown
workaround: Unknown workaround: Unknown
""" """
import struct import struct
try: try:
print(struct.pack('bb', 1)) print(struct.pack("bb", 1))
print('Should not get here') print("Should not get here")
except: except:
print('struct.error') print("struct.error")

View File

@ -5,8 +5,9 @@ cause: Unknown
workaround: Unknown workaround: Unknown
""" """
import struct import struct
try: try:
print(struct.pack('bb', 1, 2, 3)) print(struct.pack("bb", 1, 2, 3))
print('Should not get here') print("Should not get here")
except: except:
print('struct.error') print("struct.error")

View File

@ -5,5 +5,6 @@ cause: They are stored in read-only memory.
workaround: Unknown workaround: Unknown
""" """
import sys import sys
sys.stdin = None sys.stdin = None
print(sys.stdin) print(sys.stdin)

View File

@ -5,14 +5,14 @@ cause: Unknown
workaround: Unknown workaround: Unknown
""" """
try: try:
print(eval('1and 0')) print(eval("1and 0"))
except SyntaxError: except SyntaxError:
print('Should have worked') print("Should have worked")
try: try:
print(eval('1or 0')) print(eval("1or 0"))
except SyntaxError: except SyntaxError:
print('Should have worked') print("Should have worked")
try: try:
print(eval('1if 1else 0')) print(eval("1if 1else 0"))
except SyntaxError: except SyntaxError:
print('Should have worked') print("Should have worked")

View File

@ -4,4 +4,4 @@ description: bytes objects support .format() method
cause: MicroPython strives to be a more regular implementation, so if both `str` and `bytes` support ``__mod__()`` (the % operator), it makes sense to support ``format()`` for both too. Support for ``__mod__`` can also be compiled out, which leaves only ``format()`` for bytes formatting. cause: MicroPython strives to be a more regular implementation, so if both `str` and `bytes` support ``__mod__()`` (the % operator), it makes sense to support ``format()`` for both too. Support for ``__mod__`` can also be compiled out, which leaves only ``format()`` for bytes formatting.
workaround: If you are interested in CPython compatibility, don't use ``.format()`` on bytes objects. workaround: If you are interested in CPython compatibility, don't use ``.format()`` on bytes objects.
""" """
print(b'{}'.format(1)) print(b"{}".format(1))

View File

@ -4,4 +4,4 @@ description: bytes() with keywords not implemented
cause: Unknown cause: Unknown
workaround: Pass the encoding as a positional parameter, e.g. ``print(bytes('abc', 'utf-8'))`` workaround: Pass the encoding as a positional parameter, e.g. ``print(bytes('abc', 'utf-8'))``
""" """
print(bytes('abc', encoding='utf8')) print(bytes("abc", encoding="utf8"))

View File

@ -4,4 +4,4 @@ description: Bytes subscription with step != 1 not implemented
cause: MicroPython is highly optimized for memory usage. cause: MicroPython is highly optimized for memory usage.
workaround: Use explicit loop for this very rare operation. workaround: Use explicit loop for this very rare operation.
""" """
print(b'123'[0:3:2]) print(b"123"[0:3:2])

View File

@ -4,4 +4,4 @@ description: Dictionary keys view does not behave as a set.
cause: Not implemented. cause: Not implemented.
workaround: Explicitly convert keys to a set before using set operations. workaround: Explicitly convert keys to a set before using set operations.
""" """
print({1:2, 3:4}.keys() & {1}) print({1: 2, 3: 4}.keys() & {1})

View File

@ -8,8 +8,11 @@ workaround: Call using ``super()`` instead::
def __init__(self): def __init__(self):
super().__init__() super().__init__()
""" """
class A(Exception): class A(Exception):
def __init__(self): def __init__(self):
Exception.__init__(self) Exception.__init__(self)
a = A() a = A()

View File

@ -4,4 +4,4 @@ description: uPy and CPython outputs formats may differ
cause: Unknown cause: Unknown
workaround: Unknown workaround: Unknown
""" """
print('%.1g' % -9.9) print("%.1g" % -9.9)

View File

@ -4,8 +4,11 @@ description: No int conversion for int-derived types available
cause: Unknown cause: Unknown
workaround: Avoid subclassing builtin types unless really needed. Prefer https://en.wikipedia.org/wiki/Composition_over_inheritance . workaround: Avoid subclassing builtin types unless really needed. Prefer https://en.wikipedia.org/wiki/Composition_over_inheritance .
""" """
class A(int): class A(int):
__add__ = lambda self, other: A(int(self) + other) __add__ = lambda self, other: A(int(self) + other)
a = A(42) a = A(42)
print(a+a) print(a + a)

View File

@ -4,4 +4,4 @@ description: Start/end indices such as str.endswith(s, start) not implemented
cause: Unknown cause: Unknown
workaround: Unknown workaround: Unknown
""" """
print('abc'.endswith('c', 1)) print("abc".endswith("c", 1))

View File

@ -4,4 +4,4 @@ description: Attributes/subscr not implemented
cause: Unknown cause: Unknown
workaround: Unknown workaround: Unknown
""" """
print('{a[0]}'.format(a=[1, 2])) print("{a[0]}".format(a=[1, 2]))

View File

@ -4,4 +4,4 @@ description: str(...) with keywords not implemented
cause: Unknown cause: Unknown
workaround: Input the encoding format directly. eg ``print(bytes('abc', 'utf-8'))`` workaround: Input the encoding format directly. eg ``print(bytes('abc', 'utf-8'))``
""" """
print(str(b'abc', encoding='utf8')) print(str(b"abc", encoding="utf8"))

View File

@ -4,4 +4,4 @@ description: str.ljust() and str.rjust() not implemented
cause: MicroPython is highly optimized for memory usage. Easy workarounds available. cause: MicroPython is highly optimized for memory usage. Easy workarounds available.
workaround: Instead of ``s.ljust(10)`` use ``"%-10s" % s``, instead of ``s.rjust(10)`` use ``"% 10s" % s``. Alternatively, ``"{:<10}".format(s)`` or ``"{:>10}".format(s)``. workaround: Instead of ``s.ljust(10)`` use ``"%-10s" % s``, instead of ``s.rjust(10)`` use ``"% 10s" % s``. Alternatively, ``"{:<10}".format(s)`` or ``"{:>10}".format(s)``.
""" """
print('abc'.ljust(10)) print("abc".ljust(10))

View File

@ -4,4 +4,4 @@ description: None as first argument for rsplit such as str.rsplit(None, n) not i
cause: Unknown cause: Unknown
workaround: Unknown workaround: Unknown
""" """
print('a a a'.rsplit(None, 1)) print("a a a".rsplit(None, 1))

View File

@ -4,4 +4,4 @@ description: Subscript with step != 1 is not yet implemented
cause: Unknown cause: Unknown
workaround: Unknown workaround: Unknown
""" """
print('abcdefghi'[0:9:2]) print("abcdefghi"[0:9:2])

View File

@ -6,7 +6,7 @@ except ImportError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
#f = open("_test.db", "w+b") # f = open("_test.db", "w+b")
f = uio.BytesIO() f = uio.BytesIO()
db = btree.open(f, pagesize=512) db = btree.open(f, pagesize=512)

View File

@ -8,9 +8,11 @@ w = 5
h = 16 h = 16
size = w * h // 8 size = w * h // 8
buf = bytearray(size) buf = bytearray(size)
maps = {framebuf.MONO_VLSB : 'MONO_VLSB', maps = {
framebuf.MONO_HLSB : 'MONO_HLSB', framebuf.MONO_VLSB: "MONO_VLSB",
framebuf.MONO_HMSB : 'MONO_HMSB'} framebuf.MONO_HLSB: "MONO_HLSB",
framebuf.MONO_HMSB: "MONO_HMSB",
}
for mapping in maps.keys(): for mapping in maps.keys():
for x in range(size): for x in range(size):
@ -43,33 +45,33 @@ for mapping in maps.keys():
# hline # hline
fbuf.fill(0) fbuf.fill(0)
fbuf.hline(0, 1, w, 1) fbuf.hline(0, 1, w, 1)
print('hline', buf) print("hline", buf)
# vline # vline
fbuf.fill(0) fbuf.fill(0)
fbuf.vline(1, 0, h, 1) fbuf.vline(1, 0, h, 1)
print('vline', buf) print("vline", buf)
# rect # rect
fbuf.fill(0) fbuf.fill(0)
fbuf.rect(1, 1, 3, 3, 1) fbuf.rect(1, 1, 3, 3, 1)
print('rect', buf) print("rect", buf)
#fill rect # fill rect
fbuf.fill(0) fbuf.fill(0)
fbuf.fill_rect(0, 0, 0, 3, 1) # zero width, no-operation fbuf.fill_rect(0, 0, 0, 3, 1) # zero width, no-operation
fbuf.fill_rect(1, 1, 3, 3, 1) fbuf.fill_rect(1, 1, 3, 3, 1)
print('fill_rect', buf) print("fill_rect", buf)
# line # line
fbuf.fill(0) fbuf.fill(0)
fbuf.line(1, 1, 3, 3, 1) fbuf.line(1, 1, 3, 3, 1)
print('line', buf) print("line", buf)
# line steep negative gradient # line steep negative gradient
fbuf.fill(0) fbuf.fill(0)
fbuf.line(3, 3, 2, 1, 1) fbuf.line(3, 3, 2, 1, 1)
print('line', buf) print("line", buf)
# scroll # scroll
fbuf.fill(0) fbuf.fill(0)
@ -89,7 +91,7 @@ for mapping in maps.keys():
fbuf.fill(0) fbuf.fill(0)
fbuf.text("hello", 0, 0, 1) fbuf.text("hello", 0, 0, 1)
print(buf) print(buf)
fbuf.text("hello", 0, 0, 0) # clear fbuf.text("hello", 0, 0, 0) # clear
print(buf) print(buf)
# char out of font range set to chr(127) # char out of font range set to chr(127)

View File

@ -4,28 +4,30 @@ except ImportError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
def printbuf(): def printbuf():
print("--8<--") print("--8<--")
for y in range(h): for y in range(h):
print(buf[y * w * 2:(y + 1) * w * 2]) print(buf[y * w * 2 : (y + 1) * w * 2])
print("-->8--") print("-->8--")
w = 4 w = 4
h = 5 h = 5
buf = bytearray(w * h * 2) buf = bytearray(w * h * 2)
fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.RGB565) fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.RGB565)
# fill # fill
fbuf.fill(0xffff) fbuf.fill(0xFFFF)
printbuf() printbuf()
fbuf.fill(0x0000) fbuf.fill(0x0000)
printbuf() printbuf()
# put pixel # put pixel
fbuf.pixel(0, 0, 0xeeee) fbuf.pixel(0, 0, 0xEEEE)
fbuf.pixel(3, 0, 0xee00) fbuf.pixel(3, 0, 0xEE00)
fbuf.pixel(0, 4, 0x00ee) fbuf.pixel(0, 4, 0x00EE)
fbuf.pixel(3, 4, 0x0ee0) fbuf.pixel(3, 4, 0x0EE0)
printbuf() printbuf()
# get pixel # get pixel
@ -33,7 +35,7 @@ print(fbuf.pixel(0, 4), fbuf.pixel(1, 1))
# scroll # scroll
fbuf.fill(0x0000) fbuf.fill(0x0000)
fbuf.pixel(2, 2, 0xffff) fbuf.pixel(2, 2, 0xFFFF)
printbuf() printbuf()
fbuf.scroll(0, 1) fbuf.scroll(0, 1)
printbuf() printbuf()
@ -48,11 +50,11 @@ buf2 = bytearray(w2 * h2 * 2)
fbuf2 = framebuf.FrameBuffer(buf2, w2, h2, framebuf.RGB565) fbuf2 = framebuf.FrameBuffer(buf2, w2, h2, framebuf.RGB565)
fbuf2.fill(0x0000) fbuf2.fill(0x0000)
fbuf2.pixel(0, 0, 0x0ee0) fbuf2.pixel(0, 0, 0x0EE0)
fbuf2.pixel(0, 2, 0xee00) fbuf2.pixel(0, 2, 0xEE00)
fbuf2.pixel(1, 0, 0x00ee) fbuf2.pixel(1, 0, 0x00EE)
fbuf2.pixel(1, 2, 0xe00e) fbuf2.pixel(1, 2, 0xE00E)
fbuf.fill(0xffff) fbuf.fill(0xFFFF)
fbuf.blit(fbuf2, 3, 3, 0x0000) fbuf.blit(fbuf2, 3, 3, 0x0000)
fbuf.blit(fbuf2, -1, -1, 0x0000) fbuf.blit(fbuf2, -1, -1, 0x0000)
fbuf.blit(fbuf2, 16, 16, 0x0000) fbuf.blit(fbuf2, 16, 16, 0x0000)

View File

@ -4,14 +4,16 @@ except ImportError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
def printbuf(): def printbuf():
print("--8<--") print("--8<--")
for y in range(h): for y in range(h):
for x in range(w): for x in range(w):
print('%u' % ((buf[(x + y * w) // 4] >> ((x & 3) << 1)) & 3), end='') print("%u" % ((buf[(x + y * w) // 4] >> ((x & 3) << 1)) & 3), end="")
print() print()
print("-->8--") print("-->8--")
w = 8 w = 8
h = 5 h = 5
buf = bytearray(w * h // 4) buf = bytearray(w * h // 4)

View File

@ -4,50 +4,52 @@ except ImportError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
def printbuf(): def printbuf():
print("--8<--") print("--8<--")
for y in range(h): for y in range(h):
print(buf[y * w // 2:(y + 1) * w // 2]) print(buf[y * w // 2 : (y + 1) * w // 2])
print("-->8--") print("-->8--")
w = 16 w = 16
h = 8 h = 8
buf = bytearray(w * h // 2) buf = bytearray(w * h // 2)
fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.GS4_HMSB) fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.GS4_HMSB)
# fill # fill
fbuf.fill(0x0f) fbuf.fill(0x0F)
printbuf() printbuf()
fbuf.fill(0xa0) fbuf.fill(0xA0)
printbuf() printbuf()
# put pixel # put pixel
fbuf.pixel(0, 0, 0x01) fbuf.pixel(0, 0, 0x01)
printbuf() printbuf()
fbuf.pixel(w-1, 0, 0x02) fbuf.pixel(w - 1, 0, 0x02)
printbuf() printbuf()
fbuf.pixel(w-1, h-1, 0x03) fbuf.pixel(w - 1, h - 1, 0x03)
printbuf() printbuf()
fbuf.pixel(0, h-1, 0x04) fbuf.pixel(0, h - 1, 0x04)
printbuf() printbuf()
# get pixel # 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(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)) print(fbuf.pixel(1, 0), fbuf.pixel(w - 2, 0), fbuf.pixel(w - 2, h - 1), fbuf.pixel(1, h - 1))
# fill rect # fill rect
fbuf.fill_rect(0, 0, w, h, 0x0f) fbuf.fill_rect(0, 0, w, h, 0x0F)
printbuf() printbuf()
fbuf.fill_rect(0, 0, w, h, 0xf0) fbuf.fill_rect(0, 0, w, h, 0xF0)
fbuf.fill_rect(1, 0, w//2+1, 1, 0xf1) fbuf.fill_rect(1, 0, w // 2 + 1, 1, 0xF1)
printbuf() printbuf()
fbuf.fill_rect(1, 0, w//2+1, 1, 0x10) fbuf.fill_rect(1, 0, w // 2 + 1, 1, 0x10)
fbuf.fill_rect(1, 0, w//2, 1, 0xf1) fbuf.fill_rect(1, 0, w // 2, 1, 0xF1)
printbuf() printbuf()
fbuf.fill_rect(1, 0, w//2, 1, 0x10) fbuf.fill_rect(1, 0, w // 2, 1, 0x10)
fbuf.fill_rect(0, h-4, w//2+1, 4, 0xaf) fbuf.fill_rect(0, h - 4, w // 2 + 1, 4, 0xAF)
printbuf() printbuf()
fbuf.fill_rect(0, h-4, w//2+1, 4, 0xb0) fbuf.fill_rect(0, h - 4, w // 2 + 1, 4, 0xB0)
fbuf.fill_rect(0, h-4, w//2, 4, 0xaf) fbuf.fill_rect(0, h - 4, w // 2, 4, 0xAF)
printbuf() printbuf()
fbuf.fill_rect(0, h-4, w//2, 4, 0xb0) fbuf.fill_rect(0, h - 4, w // 2, 4, 0xB0)

View File

@ -4,14 +4,16 @@ except ImportError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
def printbuf(): def printbuf():
print("--8<--") print("--8<--")
for y in range(h): for y in range(h):
for x in range(w): for x in range(w):
print('%02x' % buf[(x + y * w)], end='') print("%02x" % buf[(x + y * w)], end="")
print() print()
print("-->8--") print("-->8--")
w = 8 w = 8
h = 5 h = 5
buf = bytearray(w * h) buf = bytearray(w * h)
@ -25,7 +27,7 @@ printbuf()
fbuf.pixel(0, 0, 0x11) fbuf.pixel(0, 0, 0x11)
fbuf.pixel(w - 1, 0, 0x22) fbuf.pixel(w - 1, 0, 0x22)
fbuf.pixel(0, h - 1, 0x33) fbuf.pixel(0, h - 1, 0x33)
fbuf.pixel(w - 1, h - 1, 0xff) fbuf.pixel(w - 1, h - 1, 0xFF)
printbuf() printbuf()
# get pixel # get pixel

View File

@ -3,9 +3,10 @@
try: try:
import framebuf import framebuf
except ImportError: except ImportError:
print('SKIP') print("SKIP")
raise SystemExit raise SystemExit
class FB(framebuf.FrameBuffer): class FB(framebuf.FrameBuffer):
def __init__(self, n): def __init__(self, n):
self.n = n self.n = n
@ -14,6 +15,7 @@ class FB(framebuf.FrameBuffer):
def foo(self): def foo(self):
self.hline(0, 2, self.n, 0x0304) self.hline(0, 2, self.n, 0x0304)
fb = FB(n=3) fb = FB(n=3)
fb.pixel(0, 0, 0x0102) fb.pixel(0, 0, 0x0102)
fb.foo() fb.foo()
@ -31,12 +33,13 @@ print(bytes(fb2))
class NotAFrameBuf: class NotAFrameBuf:
pass pass
try: try:
fb.blit(NotAFrameBuf(), 0, 0) fb.blit(NotAFrameBuf(), 0, 0)
except TypeError: except TypeError:
print('TypeError') print("TypeError")
try: try:
fb.blit(None, 0, 0) fb.blit(None, 0, 0)
except TypeError: except TypeError:
print('TypeError') print("TypeError")

View File

@ -10,7 +10,6 @@ except:
class MyPin(machine.PinBase): class MyPin(machine.PinBase):
def __init__(self): def __init__(self):
print("__init__") print("__init__")
self.v = False self.v = False
@ -21,6 +20,7 @@ class MyPin(machine.PinBase):
self.v = not self.v self.v = not self.v
return int(self.v) return int(self.v)
p = MyPin() p = MyPin()
print(p.value()) print(p.value())

View File

@ -11,7 +11,6 @@ except:
class ConstPin(machine.PinBase): class ConstPin(machine.PinBase):
def __init__(self, value): def __init__(self, value):
self.v = value self.v = value
@ -23,7 +22,6 @@ class ConstPin(machine.PinBase):
class TogglePin(machine.PinBase): class TogglePin(machine.PinBase):
def __init__(self): def __init__(self):
self.v = 0 self.v = 0

View File

@ -11,6 +11,7 @@ except:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
class Pin(machine.PinBase): class Pin(machine.PinBase):
def __init__(self): def __init__(self):
self.v = 0 self.v = 0

View File

@ -2,6 +2,7 @@
try: try:
import utime, umachine as machine import utime, umachine as machine
machine.Timer machine.Timer
except: except:
print("SKIP") print("SKIP")
@ -27,11 +28,11 @@ t2.deinit()
t.deinit() t.deinit()
# create one-shot timer with callback and wait for it to print (should be just once) # 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')) t = machine.Timer(period=1, mode=machine.Timer.ONE_SHOT, callback=lambda t: print("one-shot"))
utime.sleep_ms(5) utime.sleep_ms(5)
t.deinit() t.deinit()
# create periodic timer with callback and wait for it to print # 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')) t = machine.Timer(period=4, mode=machine.Timer.PERIODIC, callback=lambda t: print("periodic"))
utime.sleep_ms(14) utime.sleep_ms(14)
t.deinit() t.deinit()

View File

@ -1,5 +1,6 @@
try: try:
import utime import utime
utime.sleep_ms, utime.sleep_us, utime.ticks_diff, utime.ticks_ms, utime.ticks_us, utime.ticks_cpu utime.sleep_ms, utime.sleep_us, utime.ticks_diff, utime.ticks_ms, utime.ticks_us, utime.ticks_cpu
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")

View File

@ -7,40 +7,40 @@ except ImportError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
print(binascii.a2b_base64(b'')) print(binascii.a2b_base64(b""))
print(binascii.a2b_base64(b'Zg==')) print(binascii.a2b_base64(b"Zg=="))
print(binascii.a2b_base64(b'Zm8=')) print(binascii.a2b_base64(b"Zm8="))
print(binascii.a2b_base64(b'Zm9v')) print(binascii.a2b_base64(b"Zm9v"))
print(binascii.a2b_base64(b'Zm9vYg==')) print(binascii.a2b_base64(b"Zm9vYg=="))
print(binascii.a2b_base64(b'Zm9vYmE=')) print(binascii.a2b_base64(b"Zm9vYmE="))
print(binascii.a2b_base64(b'Zm9vYmFy')) print(binascii.a2b_base64(b"Zm9vYmFy"))
print(binascii.a2b_base64(b'AAECAwQFBgc=')) print(binascii.a2b_base64(b"AAECAwQFBgc="))
print(binascii.a2b_base64(b'CAkKCwwNDg8=')) print(binascii.a2b_base64(b"CAkKCwwNDg8="))
print(binascii.a2b_base64(b'f4D/')) print(binascii.a2b_base64(b"f4D/"))
print(binascii.a2b_base64(b'f4D+')) # convert '+' print(binascii.a2b_base64(b"f4D+")) # convert '+'
print(binascii.a2b_base64(b'MTIzNEFCQ0RhYmNk')) print(binascii.a2b_base64(b"MTIzNEFCQ0RhYmNk"))
# Ignore invalid characters and pad sequences # Ignore invalid characters and pad sequences
print(binascii.a2b_base64(b'Zm9v\n')) print(binascii.a2b_base64(b"Zm9v\n"))
print(binascii.a2b_base64(b'Zm\x009v\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===')) print(binascii.a2b_base64(b"Zm9v==="))
print(binascii.a2b_base64(b'Zm9v===YmFy')) print(binascii.a2b_base64(b"Zm9v===YmFy"))
try: try:
print(binascii.a2b_base64(b'abc')) print(binascii.a2b_base64(b"abc"))
except ValueError: except ValueError:
print("ValueError") print("ValueError")
try: try:
print(binascii.a2b_base64(b'abcde=')) print(binascii.a2b_base64(b"abcde="))
except ValueError: except ValueError:
print("ValueError") print("ValueError")
try: try:
print(binascii.a2b_base64(b'ab*d')) print(binascii.a2b_base64(b"ab*d"))
except ValueError: except ValueError:
print("ValueError") print("ValueError")
try: try:
print(binascii.a2b_base64(b'ab=cdef=')) print(binascii.a2b_base64(b"ab=cdef="))
except ValueError: except ValueError:
print("ValueError") print("ValueError")

View File

@ -7,16 +7,16 @@ except ImportError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
print(binascii.b2a_base64(b'')) print(binascii.b2a_base64(b""))
print(binascii.b2a_base64(b'f')) print(binascii.b2a_base64(b"f"))
print(binascii.b2a_base64(b'fo')) print(binascii.b2a_base64(b"fo"))
print(binascii.b2a_base64(b'foo')) print(binascii.b2a_base64(b"foo"))
print(binascii.b2a_base64(b'foob')) print(binascii.b2a_base64(b"foob"))
print(binascii.b2a_base64(b'fooba')) print(binascii.b2a_base64(b"fooba"))
print(binascii.b2a_base64(b'foobar')) print(binascii.b2a_base64(b"foobar"))
print(binascii.b2a_base64(b'\x00\x01\x02\x03\x04\x05\x06\x07')) 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"\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"))
print(binascii.b2a_base64(b'\x7f\x80\xff')) print(binascii.b2a_base64(b"\x7f\x80\xff"))
print(binascii.b2a_base64(b'1234ABCDabcd')) print(binascii.b2a_base64(b"1234ABCDabcd"))
print(binascii.b2a_base64(b'\x00\x00>')) # convert into '+' print(binascii.b2a_base64(b"\x00\x00>")) # convert into '+'

View File

@ -13,12 +13,12 @@ except AttributeError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
print(hex(binascii.crc32(b'The quick brown fox jumps over the lazy dog'))) 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"\x00" * 32)))
print(hex(binascii.crc32(b'\xff' * 32))) print(hex(binascii.crc32(b"\xff" * 32)))
print(hex(binascii.crc32(bytes(range(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" 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"\x00" * 16, binascii.crc32(b"\x00" * 16))))
print(hex(binascii.crc32(b'\xff' * 16, binascii.crc32(b'\xff' * 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)))))) print(hex(binascii.crc32(bytes(range(16, 32)), binascii.crc32(bytes(range(16))))))

View File

@ -7,7 +7,7 @@ except ImportError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
print(binascii.hexlify(b'\x00\x01\x02\x03\x04\x05\x06\x07')) 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"\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"))
print(binascii.hexlify(b'\x7f\x80\xff')) print(binascii.hexlify(b"\x7f\x80\xff"))
print(binascii.hexlify(b'1234ABCDabcd')) print(binascii.hexlify(b"1234ABCDabcd"))

View File

@ -8,8 +8,8 @@ except ImportError:
raise SystemExit raise SystemExit
# two arguments supported in uPy but not CPython # two arguments supported in uPy but not CPython
a = binascii.hexlify(b'123', ':') a = binascii.hexlify(b"123", ":")
print(a) print(a)
# zero length buffer # zero length buffer
print(binascii.hexlify(b'', b':')) print(binascii.hexlify(b"", b":"))

View File

@ -7,17 +7,17 @@ except ImportError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
print(binascii.unhexlify(b'0001020304050607')) print(binascii.unhexlify(b"0001020304050607"))
print(binascii.unhexlify(b'08090a0b0c0d0e0f')) print(binascii.unhexlify(b"08090a0b0c0d0e0f"))
print(binascii.unhexlify(b'7f80ff')) print(binascii.unhexlify(b"7f80ff"))
print(binascii.unhexlify(b'313233344142434461626364')) print(binascii.unhexlify(b"313233344142434461626364"))
try: try:
a = binascii.unhexlify(b'0') # odd buffer length a = binascii.unhexlify(b"0") # odd buffer length
except ValueError: except ValueError:
print('ValueError') print("ValueError")
try: try:
a = binascii.unhexlify(b'gg') # digit not hex a = binascii.unhexlify(b"gg") # digit not hex
except ValueError: except ValueError:
print('ValueError') print("ValueError")

View File

@ -1,5 +1,6 @@
try: try:
from Crypto.Cipher import AES from Crypto.Cipher import AES
aes = AES.new aes = AES.new
except ImportError: except ImportError:
try: try:

View File

@ -10,7 +10,7 @@ def _new(k, ctr_initial):
try: try:
_new(b'x' * 16, b'x' * 16) _new(b"x" * 16, b"x" * 16)
except ValueError as e: except ValueError as e:
# is CTR support disabled? # is CTR support disabled?
if e.args[0] == "mode": if e.args[0] == "mode":
@ -19,9 +19,9 @@ except ValueError as e:
raise e raise e
crypto = _new(b"1234" * 4, b"5678" * 4) crypto = _new(b"1234" * 4, b"5678" * 4)
enc = crypto.encrypt(b'a') enc = crypto.encrypt(b"a")
print(enc) print(enc)
enc += crypto.encrypt(b'b' * 1000) enc += crypto.encrypt(b"b" * 1000)
print(enc) print(enc)
crypto = _new(b"1234" * 4, b"5678" * 4) crypto = _new(b"1234" * 4, b"5678" * 4)

View File

@ -1,5 +1,6 @@
try: try:
from Crypto.Cipher import AES from Crypto.Cipher import AES
aes = AES.new aes = AES.new
except ImportError: except ImportError:
try: try:

View File

@ -3,6 +3,7 @@
# is optional). # is optional).
try: try:
from Crypto.Cipher import AES from Crypto.Cipher import AES
aes = AES.new aes = AES.new
except ImportError: except ImportError:
try: try:

View File

@ -1,5 +1,6 @@
try: try:
from Crypto.Cipher import AES from Crypto.Cipher import AES
aes = AES.new aes = AES.new
except ImportError: except ImportError:
try: try:

View File

@ -1,5 +1,6 @@
try: try:
from Crypto.Cipher import AES from Crypto.Cipher import AES
aes = AES.new aes = AES.new
except ImportError: except ImportError:
try: try:

View File

@ -10,16 +10,16 @@ buf = b"12345678abcd"
struct = uctypes.struct( struct = uctypes.struct(
uctypes.addressof(buf), uctypes.addressof(buf),
{"f32": uctypes.UINT32 | 0, "f64": uctypes.UINT64 | 4}, {"f32": uctypes.UINT32 | 0, "f64": uctypes.UINT64 | 4},
uctypes.LITTLE_ENDIAN uctypes.LITTLE_ENDIAN,
) )
struct.f32 = 0x7fffffff struct.f32 = 0x7FFFFFFF
print(buf) print(buf)
struct.f32 = 0x80000000 struct.f32 = 0x80000000
print(buf) print(buf)
struct.f32 = 0xff010203 struct.f32 = 0xFF010203
print(buf) print(buf)
struct.f64 = 0x80000000 struct.f64 = 0x80000000
@ -34,16 +34,16 @@ buf = b"12345678abcd"
struct = uctypes.struct( struct = uctypes.struct(
uctypes.addressof(buf), uctypes.addressof(buf),
{"f32": uctypes.UINT32 | 0, "f64": uctypes.UINT64 | 4}, {"f32": uctypes.UINT32 | 0, "f64": uctypes.UINT64 | 4},
uctypes.BIG_ENDIAN uctypes.BIG_ENDIAN,
) )
struct.f32 = 0x7fffffff struct.f32 = 0x7FFFFFFF
print(buf) print(buf)
struct.f32 = 0x80000000 struct.f32 = 0x80000000
print(buf) print(buf)
struct.f32 = 0xff010203 struct.f32 = 0xFF010203
print(buf) print(buf)
struct.f64 = 0x80000000 struct.f64 = 0x80000000

View File

@ -10,19 +10,17 @@ desc = {
# arr2 is array at offset 0, size 2, of structures defined recursively # arr2 is array at offset 0, size 2, of structures defined recursively
"arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}), "arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}),
"arr3": (uctypes.ARRAY | 2, uctypes.UINT16 | 2), "arr3": (uctypes.ARRAY | 2, uctypes.UINT16 | 2),
# aligned # aligned
"arr5": (uctypes.ARRAY | 0, uctypes.UINT32 | 1), "arr5": (uctypes.ARRAY | 0, uctypes.UINT32 | 1),
# unaligned # unaligned
"arr6": (uctypes.ARRAY | 1, uctypes.UINT32 | 1), "arr6": (uctypes.ARRAY | 1, uctypes.UINT32 | 1),
"arr7": (uctypes.ARRAY | 0, 1, {"l": uctypes.UINT32 | 0}), "arr7": (uctypes.ARRAY | 0, 1, {"l": uctypes.UINT32 | 0}),
"arr8": (uctypes.ARRAY | 1, 1, {"l": uctypes.UINT32 | 0}) "arr8": (uctypes.ARRAY | 1, 1, {"l": uctypes.UINT32 | 0}),
} }
data = bytearray(5) data = bytearray(5)
S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN) S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN)
# assign byte # assign byte
S.arr[0] = 0x11 S.arr[0] = 0x11
@ -55,4 +53,3 @@ assert hex(S.arr6[0]) == "0xaabbccdd"
print(S.arr6[0] == S.arr8[0].l) print(S.arr6[0] == S.arr8[0].l)
assert S.arr6[0] == S.arr8[0].l assert S.arr6[0] == S.arr8[0].l

View File

@ -1,4 +1,5 @@
import sys import sys
try: try:
import uctypes import uctypes
except ImportError: except ImportError:
@ -15,16 +16,14 @@ desc = {
# arr2 is array at offset 0, size 2, of structures defined recursively # arr2 is array at offset 0, size 2, of structures defined recursively
"arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}), "arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}),
"arr3": (uctypes.ARRAY | 2, uctypes.UINT16 | 2), "arr3": (uctypes.ARRAY | 2, uctypes.UINT16 | 2),
# aligned # aligned
"arr5": (uctypes.ARRAY | 0, uctypes.UINT32 | 1), "arr5": (uctypes.ARRAY | 0, uctypes.UINT32 | 1),
"arr7": (uctypes.ARRAY | 0, 1, {"l": uctypes.UINT32 | 0}), "arr7": (uctypes.ARRAY | 0, 1, {"l": uctypes.UINT32 | 0}),
"arr8": (uctypes.ARRAY | 0, uctypes.INT8 | 1), "arr8": (uctypes.ARRAY | 0, uctypes.INT8 | 1),
"arr9": (uctypes.ARRAY | 0, uctypes.INT16 | 1), "arr9": (uctypes.ARRAY | 0, uctypes.INT16 | 1),
"arr10": (uctypes.ARRAY | 0, uctypes.INT32 | 1), "arr10": (uctypes.ARRAY | 0, uctypes.INT32 | 1),
"arr11": (uctypes.ARRAY | 0, uctypes.INT64 | 1), "arr11": (uctypes.ARRAY | 0, uctypes.INT64 | 1),
"arr12": (uctypes.ARRAY | 0, uctypes.UINT64| 1), "arr12": (uctypes.ARRAY | 0, uctypes.UINT64 | 1),
"arr13": (uctypes.ARRAY | 1, 1, {"l": {}}), "arr13": (uctypes.ARRAY | 1, 1, {"l": {}}),
} }

View File

@ -1,4 +1,5 @@
import sys import sys
try: try:
import uctypes import uctypes
except ImportError: except ImportError:
@ -15,16 +16,14 @@ desc = {
# arr2 is array at offset 0, size 2, of structures defined recursively # arr2 is array at offset 0, size 2, of structures defined recursively
"arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}), "arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}),
"arr3": (uctypes.ARRAY | 2, uctypes.UINT16 | 2), "arr3": (uctypes.ARRAY | 2, uctypes.UINT16 | 2),
# aligned # aligned
"arr5": (uctypes.ARRAY | 0, uctypes.UINT32 | 1), "arr5": (uctypes.ARRAY | 0, uctypes.UINT32 | 1),
"arr7": (uctypes.ARRAY | 0, 1, {"l": uctypes.UINT32 | 0}), "arr7": (uctypes.ARRAY | 0, 1, {"l": uctypes.UINT32 | 0}),
"arr8": (uctypes.ARRAY | 0, uctypes.INT8 | 1), "arr8": (uctypes.ARRAY | 0, uctypes.INT8 | 1),
"arr9": (uctypes.ARRAY | 0, uctypes.INT16 | 1), "arr9": (uctypes.ARRAY | 0, uctypes.INT16 | 1),
"arr10": (uctypes.ARRAY | 0, uctypes.INT32 | 1), "arr10": (uctypes.ARRAY | 0, uctypes.INT32 | 1),
"arr11": (uctypes.ARRAY | 0, uctypes.INT64 | 1), "arr11": (uctypes.ARRAY | 0, uctypes.INT64 | 1),
"arr12": (uctypes.ARRAY | 0, uctypes.UINT64| 1), "arr12": (uctypes.ARRAY | 0, uctypes.UINT64 | 1),
"arr13": (uctypes.ARRAY | 1, 1, {"l": {}}), "arr13": (uctypes.ARRAY | 1, 1, {"l": {}}),
} }

View File

@ -4,7 +4,7 @@ except ImportError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
data = bytearray(b'01234567') data = bytearray(b"01234567")
print(uctypes.bytes_at(uctypes.addressof(data), 4)) print(uctypes.bytes_at(uctypes.addressof(data), 4))
print(uctypes.bytearray_at(uctypes.addressof(data), 4)) print(uctypes.bytearray_at(uctypes.addressof(data), 4))

View File

@ -9,35 +9,35 @@ except ImportError:
data = bytearray(b"01234567") data = bytearray(b"01234567")
# del subscr not supported # del subscr not supported
S = uctypes.struct(uctypes.addressof(data), {}) S = uctypes.struct(uctypes.addressof(data), {})
try: try:
del S[0] del S[0]
except TypeError: except TypeError:
print('TypeError') print("TypeError")
# list is an invalid descriptor # list is an invalid descriptor
S = uctypes.struct(uctypes.addressof(data), []) S = uctypes.struct(uctypes.addressof(data), [])
try: try:
S.x S.x
except TypeError: except TypeError:
print('TypeError') print("TypeError")
# can't access attribute with invalid descriptor # can't access attribute with invalid descriptor
S = uctypes.struct(uctypes.addressof(data), {'x':[]}) S = uctypes.struct(uctypes.addressof(data), {"x": []})
try: try:
S.x S.x
except TypeError: except TypeError:
print('TypeError') print("TypeError")
# can't assign to aggregate # can't assign to aggregate
S = uctypes.struct(uctypes.addressof(data), {'x':(uctypes.ARRAY | 0, uctypes.INT8 | 2)}) S = uctypes.struct(uctypes.addressof(data), {"x": (uctypes.ARRAY | 0, uctypes.INT8 | 2)})
try: try:
S.x = 1 S.x = 1
except TypeError: except TypeError:
print('TypeError') print("TypeError")
# unsupported unary op # unsupported unary op
try: try:
hash(S) hash(S)
except TypeError: except TypeError:
print('TypeError') print("TypeError")

View File

@ -6,20 +6,15 @@ except ImportError:
desc = { desc = {
"s0": uctypes.UINT16 | 0, "s0": uctypes.UINT16 | 0,
"sub": (0, { "sub": (0, {"b0": uctypes.UINT8 | 0, "b1": uctypes.UINT8 | 1,}),
"b0": uctypes.UINT8 | 0,
"b1": uctypes.UINT8 | 1,
}),
"arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 2), "arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 2),
"arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}), "arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}),
"bitf0": uctypes.BFUINT16 | 0 | 0 << uctypes.BF_POS | 8 << uctypes.BF_LEN, "bitf0": uctypes.BFUINT16 | 0 | 0 << uctypes.BF_POS | 8 << uctypes.BF_LEN,
"bitf1": uctypes.BFUINT16 | 0 | 8 << uctypes.BF_POS | 8 << uctypes.BF_LEN, "bitf1": uctypes.BFUINT16 | 0 | 8 << uctypes.BF_POS | 8 << uctypes.BF_LEN,
"bf0": uctypes.BFUINT16 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
"bf0": uctypes.BFUINT16 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN, "bf1": uctypes.BFUINT16 | 0 | 4 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
"bf1": uctypes.BFUINT16 | 0 | 4 << uctypes.BF_POS | 4 << uctypes.BF_LEN, "bf2": uctypes.BFUINT16 | 0 | 8 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
"bf2": uctypes.BFUINT16 | 0 | 8 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
"bf3": uctypes.BFUINT16 | 0 | 12 << uctypes.BF_POS | 4 << uctypes.BF_LEN, "bf3": uctypes.BFUINT16 | 0 | 12 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
"ptr": (uctypes.PTR | 0, uctypes.UINT8), "ptr": (uctypes.PTR | 0, uctypes.UINT8),
"ptr2": (uctypes.PTR | 0, {"b": uctypes.UINT8 | 0}), "ptr2": (uctypes.PTR | 0, {"b": uctypes.UINT8 | 0}),
} }
@ -28,10 +23,10 @@ data = bytearray(b"01")
S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN) S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN)
#print(S) # print(S)
print(hex(S.s0)) print(hex(S.s0))
assert hex(S.s0) == "0x3130" assert hex(S.s0) == "0x3130"
#print(S.sub.b0) # print(S.sub.b0)
print(S.sub.b0, S.sub.b1) print(S.sub.b0, S.sub.b1)
assert S.sub.b0, S.sub.b1 == (0x30, 0x31) assert S.sub.b0, S.sub.b1 == (0x30, 0x31)
@ -73,7 +68,7 @@ assert bytes(data) == b"2Q"
desc2 = { desc2 = {
"bf8": uctypes.BFUINT8 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN, "bf8": uctypes.BFUINT8 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
"bf32": uctypes.BFUINT32 | 0 | 20 << uctypes.BF_POS | 4 << uctypes.BF_LEN "bf32": uctypes.BFUINT32 | 0 | 20 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
} }
data2 = bytearray(b"0123") data2 = bytearray(b"0123")

View File

@ -7,7 +7,7 @@ except ImportError:
desc = { desc = {
"f32": uctypes.FLOAT32 | 0, "f32": uctypes.FLOAT32 | 0,
"f64": uctypes.FLOAT64 | 0, "f64": uctypes.FLOAT64 | 0,
"uf64": uctypes.FLOAT64 | 2, # unaligned "uf64": uctypes.FLOAT64 | 2, # unaligned
} }
data = bytearray(10) data = bytearray(10)
@ -15,10 +15,10 @@ data = bytearray(10)
S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN) S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN)
S.f32 = 12.34 S.f32 = 12.34
print('%.4f' % S.f32) print("%.4f" % S.f32)
S.f64 = 12.34 S.f64 = 12.34
print('%.4f' % S.f64) print("%.4f" % S.f64)
S.uf64 = 12.34 S.uf64 = 12.34
print('%.4f' % S.uf64) print("%.4f" % S.uf64)

View File

@ -14,7 +14,7 @@ data = bytearray(8)
S = uctypes.struct(uctypes.addressof(data), desc, uctypes.NATIVE) S = uctypes.struct(uctypes.addressof(data), desc, uctypes.NATIVE)
S.f32 = 12.34 S.f32 = 12.34
print('%.4f' % S.f32) print("%.4f" % S.f32)
S.f64 = 12.34 S.f64 = 12.34
print('%.4f' % S.f64) print("%.4f" % S.f64)

View File

@ -2,6 +2,7 @@
# Codepaths for packed vs native structures are different. This test only works # Codepaths for packed vs native structures are different. This test only works
# on little-endian machine (no matter if 32 or 64 bit). # on little-endian machine (no matter if 32 or 64 bit).
import sys import sys
try: try:
import uctypes import uctypes
except ImportError: except ImportError:
@ -15,20 +16,15 @@ if sys.byteorder != "little":
desc = { desc = {
"s0": uctypes.UINT16 | 0, "s0": uctypes.UINT16 | 0,
"sub": (0, { "sub": (0, {"b0": uctypes.UINT8 | 0, "b1": uctypes.UINT8 | 1,}),
"b0": uctypes.UINT8 | 0,
"b1": uctypes.UINT8 | 1,
}),
"arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 2), "arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 2),
"arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}), "arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}),
"bitf0": uctypes.BFUINT16 | 0 | 0 << uctypes.BF_POS | 8 << uctypes.BF_LEN, "bitf0": uctypes.BFUINT16 | 0 | 0 << uctypes.BF_POS | 8 << uctypes.BF_LEN,
"bitf1": uctypes.BFUINT16 | 0 | 8 << uctypes.BF_POS | 8 << uctypes.BF_LEN, "bitf1": uctypes.BFUINT16 | 0 | 8 << uctypes.BF_POS | 8 << uctypes.BF_LEN,
"bf0": uctypes.BFUINT16 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
"bf0": uctypes.BFUINT16 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN, "bf1": uctypes.BFUINT16 | 0 | 4 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
"bf1": uctypes.BFUINT16 | 0 | 4 << uctypes.BF_POS | 4 << uctypes.BF_LEN, "bf2": uctypes.BFUINT16 | 0 | 8 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
"bf2": uctypes.BFUINT16 | 0 | 8 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
"bf3": uctypes.BFUINT16 | 0 | 12 << uctypes.BF_POS | 4 << uctypes.BF_LEN, "bf3": uctypes.BFUINT16 | 0 | 12 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
"ptr": (uctypes.PTR | 0, uctypes.UINT8), "ptr": (uctypes.PTR | 0, uctypes.UINT8),
"ptr2": (uctypes.PTR | 0, {"b": uctypes.UINT8 | 0}), "ptr2": (uctypes.PTR | 0, {"b": uctypes.UINT8 | 0}),
} }
@ -37,10 +33,10 @@ data = bytearray(b"01")
S = uctypes.struct(uctypes.addressof(data), desc, uctypes.NATIVE) S = uctypes.struct(uctypes.addressof(data), desc, uctypes.NATIVE)
#print(S) # print(S)
print(hex(S.s0)) print(hex(S.s0))
assert hex(S.s0) == "0x3130" assert hex(S.s0) == "0x3130"
#print(S.sub.b0) # print(S.sub.b0)
print(S.sub.b0, S.sub.b1) print(S.sub.b0, S.sub.b1)
assert S.sub.b0, S.sub.b1 == (0x30, 0x31) assert S.sub.b0, S.sub.b1 == (0x30, 0x31)
@ -81,7 +77,7 @@ assert bytes(data) == b"2Q"
desc2 = { desc2 = {
"bf8": uctypes.BFUINT8 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN, "bf8": uctypes.BFUINT8 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
"bf32": uctypes.BFUINT32 | 0 | 20 << uctypes.BF_POS | 4 << uctypes.BF_LEN "bf32": uctypes.BFUINT32 | 0 | 20 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
} }
data2 = bytearray(b"0123") data2 = bytearray(b"0123")

View File

@ -16,10 +16,10 @@ desc2 = [(uctypes.ARRAY | 0, uctypes.UINT8 | 1)]
S2 = uctypes.struct(0, desc2) S2 = uctypes.struct(0, desc2)
print(S2) print(S2)
desc3 = ((uctypes.ARRAY | 0, uctypes.UINT8 | 1)) desc3 = (uctypes.ARRAY | 0, uctypes.UINT8 | 1)
S3 = uctypes.struct(0, desc3) S3 = uctypes.struct(0, desc3)
print(S3) print(S3)
desc4 = ((uctypes.PTR | 0, uctypes.UINT8 | 1)) desc4 = (uctypes.PTR | 0, uctypes.UINT8 | 1)
S4 = uctypes.struct(0, desc4) S4 = uctypes.struct(0, desc4)
print(S4) print(S4)

View File

@ -1,4 +1,5 @@
import sys import sys
try: try:
import uctypes import uctypes
except ImportError: except ImportError:
@ -32,6 +33,6 @@ assert S.ptr[1] == ord("1")
print(hex(S.ptr16[0])) print(hex(S.ptr16[0]))
assert hex(S.ptr16[0]) == "0x3130" assert hex(S.ptr16[0]) == "0x3130"
print(S.ptr2[0].b, S.ptr2[1].b) print(S.ptr2[0].b, S.ptr2[1].b)
print (S.ptr2[0].b, S.ptr2[1].b) print(S.ptr2[0].b, S.ptr2[1].b)
print(hex(S.ptr16[0])) print(hex(S.ptr16[0]))
assert (S.ptr2[0].b, S.ptr2[1].b) == (48, 49) assert (S.ptr2[0].b, S.ptr2[1].b) == (48, 49)

View File

@ -1,4 +1,5 @@
import sys import sys
try: try:
import uctypes import uctypes
except ImportError: except ImportError:
@ -30,6 +31,6 @@ assert S.ptr[1] == ord("1")
print(hex(S.ptr16[0])) print(hex(S.ptr16[0]))
assert hex(S.ptr16[0]) == "0x3130" assert hex(S.ptr16[0]) == "0x3130"
print(S.ptr2[0].b, S.ptr2[1].b) print(S.ptr2[0].b, S.ptr2[1].b)
print (S.ptr2[0].b, S.ptr2[1].b) print(S.ptr2[0].b, S.ptr2[1].b)
print(hex(S.ptr16[0])) print(hex(S.ptr16[0]))
assert (S.ptr2[0].b, S.ptr2[1].b) == (48, 49) assert (S.ptr2[0].b, S.ptr2[1].b) == (48, 49)

View File

@ -11,10 +11,13 @@ desc = {
"arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}), "arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}),
"arr3": (uctypes.ARRAY | 2, uctypes.UINT16 | 2), "arr3": (uctypes.ARRAY | 2, uctypes.UINT16 | 2),
"arr4": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0, "w": uctypes.UINT16 | 1}), "arr4": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0, "w": uctypes.UINT16 | 1}),
"sub": (0, { "sub": (
'b1': uctypes.BFUINT8 | 0 | 4 << uctypes.BF_POS | 4 << uctypes.BF_LEN, 0,
'b2': uctypes.BFUINT8 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN, {
}), "b1": uctypes.BFUINT8 | 0 | 4 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
"b2": uctypes.BFUINT8 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
},
),
} }
data = bytearray(b"01234567") data = bytearray(b"01234567")

View File

@ -4,5 +4,5 @@ except ImportError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
print(uctypes.sizeof({'f':uctypes.FLOAT32})) print(uctypes.sizeof({"f": uctypes.FLOAT32}))
print(uctypes.sizeof({'f':uctypes.FLOAT64})) print(uctypes.sizeof({"f": uctypes.FLOAT64}))

View File

@ -28,10 +28,7 @@ S5 = {
"b": uctypes.UINT32 | 4, "b": uctypes.UINT32 | 4,
"c": uctypes.UINT8 | 8, "c": uctypes.UINT8 | 8,
"d": uctypes.UINT32 | 0, "d": uctypes.UINT32 | 0,
"sub": (4, { "sub": (4, {"b0": uctypes.UINT8 | 0, "b1": uctypes.UINT8 | 1,}),
"b0": uctypes.UINT8 | 0,
"b1": uctypes.UINT8 | 1,
}),
} }
assert uctypes.sizeof(S5) == 12 assert uctypes.sizeof(S5) == 12

View File

@ -5,18 +5,23 @@ except ImportError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
desc = OrderedDict({ desc = OrderedDict(
# arr is array at offset 0, of UINT8 elements, array size is 2 {
"arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 2), # arr is array at offset 0, of UINT8 elements, array size is 2
# arr2 is array at offset 0, size 2, of structures defined recursively "arr": (uctypes.ARRAY | 0, uctypes.UINT8 | 2),
"arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}), # arr2 is array at offset 0, size 2, of structures defined recursively
"arr3": (uctypes.ARRAY | 2, uctypes.UINT16 | 2), "arr2": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0}),
"arr4": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0, "w": uctypes.UINT16 | 1}), "arr3": (uctypes.ARRAY | 2, uctypes.UINT16 | 2),
"sub": (0, { "arr4": (uctypes.ARRAY | 0, 2, {"b": uctypes.UINT8 | 0, "w": uctypes.UINT16 | 1}),
'b1': uctypes.BFUINT8 | 0 | 4 << uctypes.BF_POS | 4 << uctypes.BF_LEN, "sub": (
'b2': uctypes.BFUINT8 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN, 0,
}), {
}) "b1": uctypes.BFUINT8 | 0 | 4 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
"b2": uctypes.BFUINT8 | 0 | 0 << uctypes.BF_POS | 4 << uctypes.BF_LEN,
},
),
}
)
data = bytearray(b"01234567") data = bytearray(b"01234567")

View File

@ -16,6 +16,6 @@ except AttributeError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
md5 = hashlib.md5(b'hello') md5 = hashlib.md5(b"hello")
md5.update(b'world') md5.update(b"world")
print(md5.digest()) print(md5.digest())

View File

@ -16,6 +16,6 @@ except AttributeError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
sha1 = hashlib.sha1(b'hello') sha1 = hashlib.sha1(b"hello")
sha1.update(b'world') sha1.update(b"world")
print(sha1.digest()) print(sha1.digest())

View File

@ -27,12 +27,12 @@ print(hashlib.sha256(b"\xff" * 64).digest())
print(hashlib.sha256(b"\xff" * 56).digest()) print(hashlib.sha256(b"\xff" * 56).digest())
# TODO: running .digest() several times in row is not supported() # TODO: running .digest() several times in row is not supported()
#h = hashlib.sha256(b'123') # h = hashlib.sha256(b'123')
#print(h.digest()) # print(h.digest())
#print(h.digest()) # print(h.digest())
# TODO: partial digests are not supported # TODO: partial digests are not supported
#h = hashlib.sha256(b'123') # h = hashlib.sha256(b'123')
#print(h.digest()) # print(h.digest())
#h.update(b'456') # h.update(b'456')
#print(h.digest()) # print(h.digest())

View File

@ -17,11 +17,13 @@ try:
except TypeError: except TypeError:
print("TypeError") print("TypeError")
def pop_and_print(h): def pop_and_print(h):
l = [] l = []
while h: while h:
l.append(str(heapq.heappop(h))) l.append(str(heapq.heappop(h)))
print(' '.join(l)) print(" ".join(l))
h = [] h = []
heapq.heappush(h, 3) heapq.heappush(h, 3)

View File

@ -20,11 +20,11 @@ print(s.getvalue())
# dump to a small-int not allowed # dump to a small-int not allowed
try: try:
json.dump(123, 1) json.dump(123, 1)
except (AttributeError, OSError): # CPython and uPy have different errors except (AttributeError, OSError): # CPython and uPy have different errors
print('Exception') print("Exception")
# dump to an object not allowed # dump to an object not allowed
try: try:
json.dump(123, {}) json.dump(123, {})
except (AttributeError, OSError): # CPython and uPy have different errors except (AttributeError, OSError): # CPython and uPy have different errors
print('Exception') print("Exception")

View File

@ -7,27 +7,28 @@ except ImportError:
try: try:
import io, json import io, json
except ImportError: except ImportError:
print('SKIP') print("SKIP")
raise SystemExit raise SystemExit
if not hasattr(io, 'IOBase'): if not hasattr(io, "IOBase"):
print('SKIP') print("SKIP")
raise SystemExit raise SystemExit
# a user stream that only has the write method # a user stream that only has the write method
class S(io.IOBase): class S(io.IOBase):
def __init__(self): def __init__(self):
self.buf = '' self.buf = ""
def write(self, buf): def write(self, buf):
if type(buf) == bytearray: if type(buf) == bytearray:
# uPy passes a bytearray, CPython passes a str # uPy passes a bytearray, CPython passes a str
buf = str(buf, 'ascii') buf = str(buf, "ascii")
self.buf += buf self.buf += buf
return len(buf) return len(buf)
# dump to the user stream # dump to the user stream
s = S() s = S()
json.dump([123, {}], s) json.dump([123, {}], s)
print(s.buf) print(s.buf)

View File

@ -11,8 +11,8 @@ print(json.dumps(False))
print(json.dumps(True)) print(json.dumps(True))
print(json.dumps(None)) print(json.dumps(None))
print(json.dumps(1)) print(json.dumps(1))
print(json.dumps('abc')) print(json.dumps("abc"))
print(json.dumps('\x00\x01\x7e')) print(json.dumps("\x00\x01\x7e"))
print(json.dumps([])) print(json.dumps([]))
print(json.dumps([1])) print(json.dumps([1]))
print(json.dumps([1, 2])) print(json.dumps([1, 2]))
@ -22,10 +22,10 @@ print(json.dumps((1,)))
print(json.dumps((1, 2))) print(json.dumps((1, 2)))
print(json.dumps((1, (2, 3)))) print(json.dumps((1, (2, 3))))
print(json.dumps({})) print(json.dumps({}))
print(json.dumps({"a":1})) print(json.dumps({"a": 1}))
print(json.dumps({"a":(2,[3,None])})) print(json.dumps({"a": (2, [3, None])}))
print(json.dumps('"quoted"')) print(json.dumps('"quoted"'))
print(json.dumps('space\n\r\tspace')) print(json.dumps("space\n\r\tspace"))
print(json.dumps({None: -1})) print(json.dumps({None: -1}))
print(json.dumps({False: 0})) print(json.dumps({False: 0}))
print(json.dumps({True: 1})) print(json.dumps({True: 1}))

View File

@ -6,4 +6,4 @@ except ImportError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
print(ujson.dumps(b'1234')) print(ujson.dumps(b"1234"))

View File

@ -8,4 +8,4 @@ except ImportError:
raise SystemExit raise SystemExit
print(json.dumps(1.2)) print(json.dumps(1.2))
print(json.dumps({1.5: 'hi'})) print(json.dumps({1.5: "hi"}))

View File

@ -9,7 +9,7 @@ except:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
print(json.load(StringIO('null'))) print(json.load(StringIO("null")))
print(json.load(StringIO('"abc\\u0064e"'))) print(json.load(StringIO('"abc\\u0064e"')))
print(json.load(StringIO('[false, true, 1, -2]'))) print(json.load(StringIO("[false, true, 1, -2]")))
print(json.load(StringIO('{"a":true}'))) print(json.load(StringIO('{"a":true}')))

View File

@ -7,23 +7,25 @@ except ImportError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
def my_print(o): def my_print(o):
if isinstance(o, dict): if isinstance(o, dict):
print('sorted dict', sorted(o.items())) print("sorted dict", sorted(o.items()))
else: else:
print(o) print(o)
my_print(json.loads('null'))
my_print(json.loads('false')) my_print(json.loads("null"))
my_print(json.loads('true')) my_print(json.loads("false"))
my_print(json.loads('1')) my_print(json.loads("true"))
my_print(json.loads('-2')) my_print(json.loads("1"))
my_print(json.loads("-2"))
my_print(json.loads('"abc\\u0064e"')) my_print(json.loads('"abc\\u0064e"'))
my_print(json.loads('[]')) my_print(json.loads("[]"))
my_print(json.loads('[null]')) my_print(json.loads("[null]"))
my_print(json.loads('[null,false,true]')) my_print(json.loads("[null,false,true]"))
my_print(json.loads(' [ null , false , true ] ')) my_print(json.loads(" [ null , false , true ] "))
my_print(json.loads('{}')) my_print(json.loads("{}"))
my_print(json.loads('{"a":true}')) my_print(json.loads('{"a":true}'))
my_print(json.loads('{"a":null, "b":false, "c":true}')) my_print(json.loads('{"a":null, "b":false, "c":true}'))
my_print(json.loads('{"a":[], "b":[1], "c":{"3":4}}')) my_print(json.loads('{"a":[], "b":[1], "c":{"3":4}}'))
@ -39,36 +41,36 @@ my_print(json.loads('{\n\t"a":[]\r\n, "b":[1], "c":{"3":4} \n\r\t\r\r\r\n}')
# loading nothing should raise exception # loading nothing should raise exception
try: try:
json.loads('') json.loads("")
except ValueError: except ValueError:
print('ValueError') print("ValueError")
# string which is not closed # string which is not closed
try: try:
my_print(json.loads('"abc')) my_print(json.loads('"abc'))
except ValueError: except ValueError:
print('ValueError') print("ValueError")
# unaccompanied closing brace # unaccompanied closing brace
try: try:
my_print(json.loads(']')) my_print(json.loads("]"))
except ValueError: except ValueError:
print('ValueError') print("ValueError")
# unspecified object type # unspecified object type
try: try:
my_print(json.loads('a')) my_print(json.loads("a"))
except ValueError: except ValueError:
print('ValueError') print("ValueError")
# bad property name # bad property name
try: try:
my_print(json.loads('{{}:"abc"}')) my_print(json.loads('{{}:"abc"}'))
except ValueError: except ValueError:
print('ValueError') print("ValueError")
# unexpected characters after white space # unexpected characters after white space
try: try:
my_print(json.loads('[null] a')) my_print(json.loads("[null] a"))
except ValueError: except ValueError:
print('ValueError') print("ValueError")

View File

@ -9,5 +9,5 @@ except ImportError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
print(json.loads(b'[1,2]')) print(json.loads(b"[1,2]"))
print(json.loads(bytearray(b'[null]'))) print(json.loads(bytearray(b"[null]")))

View File

@ -7,12 +7,14 @@ except ImportError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
def my_print(o):
print('%.3f' % o)
my_print(json.loads('1.2')) def my_print(o):
my_print(json.loads('1e2')) print("%.3f" % o)
my_print(json.loads('-2.3'))
my_print(json.loads('-2e3'))
my_print(json.loads('-2e+3')) my_print(json.loads("1.2"))
my_print(json.loads('-2e-3')) my_print(json.loads("1e2"))
my_print(json.loads("-2.3"))
my_print(json.loads("-2e3"))
my_print(json.loads("-2e+3"))
my_print(json.loads("-2e-3"))

View File

@ -26,4 +26,4 @@ print(random.getrandbits(16) == r)
try: try:
random.getrandbits(0) random.getrandbits(0)
except ValueError: except ValueError:
print('ValueError') print("ValueError")

View File

@ -10,10 +10,10 @@ except ImportError:
try: try:
random.randint random.randint
except AttributeError: except AttributeError:
print('SKIP') print("SKIP")
raise SystemExit raise SystemExit
print('randrange') print("randrange")
for i in range(50): for i in range(50):
assert 0 <= random.randrange(4) < 4 assert 0 <= random.randrange(4) < 4
assert 2 <= random.randrange(2, 6) < 6 assert 2 <= random.randrange(2, 6) < 6
@ -25,27 +25,27 @@ for i in range(50):
try: try:
random.randrange(0) random.randrange(0)
except ValueError: except ValueError:
print('ValueError') print("ValueError")
# empty range # empty range
try: try:
random.randrange(2, 1) random.randrange(2, 1)
except ValueError: except ValueError:
print('ValueError') print("ValueError")
# zero step # zero step
try: try:
random.randrange(2, 1, 0) random.randrange(2, 1, 0)
except ValueError: except ValueError:
print('ValueError') print("ValueError")
# empty range # empty range
try: try:
random.randrange(2, 1, 1) random.randrange(2, 1, 1)
except ValueError: except ValueError:
print('ValueError') print("ValueError")
print('randint') print("randint")
for i in range(50): for i in range(50):
assert 0 <= random.randint(0, 4) <= 4 assert 0 <= random.randint(0, 4) <= 4
assert 2 <= random.randint(2, 6) <= 6 assert 2 <= random.randint(2, 6) <= 6
@ -55,9 +55,9 @@ for i in range(50):
try: try:
random.randint(2, 1) random.randint(2, 1)
except ValueError: except ValueError:
print('ValueError') print("ValueError")
print('choice') print("choice")
lst = [1, 2, 5, 6] lst = [1, 2, 5, 6]
for i in range(50): for i in range(50):
assert random.choice(lst) in lst assert random.choice(lst) in lst
@ -66,4 +66,4 @@ for i in range(50):
try: try:
random.choice([]) random.choice([])
except IndexError: except IndexError:
print('IndexError') print("IndexError")

View File

@ -10,14 +10,14 @@ except ImportError:
try: try:
random.randint random.randint
except AttributeError: except AttributeError:
print('SKIP') print("SKIP")
raise SystemExit raise SystemExit
print('random') print("random")
for i in range(50): for i in range(50):
assert 0 <= random.random() < 1 assert 0 <= random.random() < 1
print('uniform') print("uniform")
for i in range(50): for i in range(50):
assert 0 <= random.uniform(0, 4) <= 4 assert 0 <= random.uniform(0, 4) <= 4
assert 2 <= random.uniform(2, 6) <= 6 assert 2 <= random.uniform(2, 6) <= 6

View File

@ -70,11 +70,16 @@ print(m.group(0))
m = re.search("w.r", "hello world") m = re.search("w.r", "hello world")
print(m.group(0)) print(m.group(0))
m = re.match('a+?', 'ab'); print(m.group(0)) m = re.match("a+?", "ab")
m = re.match('a*?', 'ab'); print(m.group(0)) print(m.group(0))
m = re.match('^ab$', 'ab'); print(m.group(0)) m = re.match("a*?", "ab")
m = re.match('a|b', 'b'); print(m.group(0)) print(m.group(0))
m = re.match('a|b|c', 'c'); print(m.group(0)) m = re.match("^ab$", "ab")
print(m.group(0))
m = re.match("a|b", "b")
print(m.group(0))
m = re.match("a|b|c", "c")
print(m.group(0))
# Case where anchors fail to match # Case where anchors fail to match
r = re.compile("^b|b$") r = re.compile("^b|b$")
@ -87,24 +92,36 @@ except:
print("Caught invalid regex") print("Caught invalid regex")
# bytes objects # bytes objects
m = re.match(rb'a+?', b'ab'); print(m.group(0)) m = re.match(rb"a+?", b"ab")
print(m.group(0))
print("===") print("===")
# escaping # escaping
m = re.match(r'a\.c', 'a.c'); print(m.group(0) if m else '') m = re.match(r"a\.c", "a.c")
m = re.match(r'a\.b', 'abc'); print(m is None) print(m.group(0) if m else "")
m = re.match(r'a\.b', 'a\\bc'); print(m is None) m = re.match(r"a\.b", "abc")
m = re.match(r'[a\-z]', 'abc'); print(m.group(0)) print(m is None)
m = re.match(r'[.\]]*', '.].]a'); print(m.group(0)) m = re.match(r"a\.b", "a\\bc")
m = re.match(r'[.\]+]*', '.]+.]a'); print(m.group(0)) print(m is None)
m = re.match(r'[a-f0-9x\-yz]*', 'abxcd1-23'); print(m.group(0)) m = re.match(r"[a\-z]", "abc")
m = re.match(r'[a\\b]*', 'a\\aa\\bb\\bbab'); print(m.group(0)) print(m.group(0))
m = re.search(r'[a\-z]', '-'); print(m.group(0)) m = re.match(r"[.\]]*", ".].]a")
m = re.search(r'[a\-z]', 'f'); print(m is None) print(m.group(0))
m = re.search(r'[a\]z]', 'a'); print(m.group(0)) m = re.match(r"[.\]+]*", ".]+.]a")
print(re.compile(r'[-a]').split('foo-bar')) print(m.group(0))
print(re.compile(r'[a-]').split('foo-bar')) m = re.match(r"[a-f0-9x\-yz]*", "abxcd1-23")
print(re.compile(r'[ax\-]').split('foo-bar')) print(m.group(0))
print(re.compile(r'[a\-x]').split('foo-bar')) m = re.match(r"[a\\b]*", "a\\aa\\bb\\bbab")
print(re.compile(r'[\-ax]').split('foo-bar')) print(m.group(0))
m = re.search(r"[a\-z]", "-")
print(m.group(0))
m = re.search(r"[a\-z]", "f")
print(m is None)
m = re.search(r"[a\]z]", "a")
print(m.group(0))
print(re.compile(r"[-a]").split("foo-bar"))
print(re.compile(r"[a-]").split("foo-bar"))
print(re.compile(r"[ax\-]").split("foo-bar"))
print(re.compile(r"[a\-x]").split("foo-bar"))
print(re.compile(r"[\-ax]").split("foo-bar"))
print("===") print("===")

View File

@ -1,9 +1,10 @@
# test printing debugging info when compiling # test printing debugging info when compiling
try: try:
import ure import ure
ure.DEBUG ure.DEBUG
except (ImportError, AttributeError): except (ImportError, AttributeError):
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
ure.compile('^a|b[0-9]\w$', ure.DEBUG) ure.compile("^a|b[0-9]\w$", ure.DEBUG)

View File

@ -9,18 +9,20 @@ except ImportError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
def test_re(r): def test_re(r):
try: try:
re.compile(r) re.compile(r)
print("OK") print("OK")
except: # uPy and CPy use different errors, so just ignore the type except: # uPy and CPy use different errors, so just ignore the type
print("Error") print("Error")
test_re(r'?')
test_re(r'*') test_re(r"?")
test_re(r'+') test_re(r"*")
test_re(r')') test_re(r"+")
test_re(r'[') test_re(r")")
test_re(r'([') test_re(r"[")
test_re(r'([)') test_re(r"([")
test_re(r'[a\]') test_re(r"([)")
test_re(r"[a\]")

View File

@ -9,8 +9,9 @@ except ImportError:
print("SKIP") print("SKIP")
raise SystemExit raise SystemExit
def print_groups(match): def print_groups(match):
print('----') print("----")
try: try:
i = 0 i = 0
while True: while True:
@ -19,14 +20,15 @@ def print_groups(match):
except IndexError: except IndexError:
pass pass
m = re.match(r'(([0-9]*)([a-z]*)[0-9]*)','1234hello567')
m = re.match(r"(([0-9]*)([a-z]*)[0-9]*)", "1234hello567")
print_groups(m) print_groups(m)
m = re.match(r'([0-9]*)(([a-z]*)([0-9]*))','1234hello567') m = re.match(r"([0-9]*)(([a-z]*)([0-9]*))", "1234hello567")
print_groups(m) print_groups(m)
# optional group that matches # optional group that matches
print_groups(re.match(r'(a)?b(c)', 'abc')) print_groups(re.match(r"(a)?b(c)", "abc"))
# optional group that doesn't match # optional group that doesn't match
print_groups(re.match(r'(a)?b(c)', 'bc')) print_groups(re.match(r"(a)?b(c)", "bc"))

Some files were not shown because too many files have changed in this diff Show More