code stringlengths 1 1.05M | repo_name stringlengths 6 83 | path stringlengths 3 242 | language stringclasses 222
values | license stringclasses 20
values | size int64 1 1.05M |
|---|---|---|---|---|---|
gen = (i for i in range(10))
for i in gen:
print(i)
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/generator2.py | Python | apache-2.0 | 56 |
# Handling of "complicated" arg forms to generators
# https://github.com/micropython/micropython/issues/397
def gen(v=5):
for i in range(v):
yield i
print(list(gen()))
print(list(gen(v=10)))
def g(*args, **kwargs):
for i in args:
yield i
for k, v in kwargs.items():
yield (k, v)
p... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/generator_args.py | Python | apache-2.0 | 354 |
def gen1():
yield 1
yield 2
# Test that it's possible to close just created gen
g = gen1()
print(g.close())
try:
next(g)
except StopIteration:
print("StopIteration")
# Test that it's possible to close gen in progress
g = gen1()
print(next(g))
print(g.close())
try:
next(g)
print("No StopIterati... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/generator_close.py | Python | apache-2.0 | 1,026 |
# a generator that closes over outer variables
def f():
x = 1 # closed over by g
def g():
yield x
yield x + 1
return g()
for i in f():
print(i)
# a generator that has its variables closed over
def f():
x = 1 # closed over by g
def g():
return x + 1
yield g()
x = ... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/generator_closure.py | Python | apache-2.0 | 697 |
# Test proper handling of exceptions within generator across yield
def gen():
try:
yield 1
raise ValueError
except ValueError:
print("Caught")
yield 2
for i in gen():
print(i)
# Test throwing exceptions out of generator
def gen2():
yield 1
raise ValueError
yield 2
... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/generator_exc.py | Python | apache-2.0 | 907 |
# test __name__ on generator functions
def Fun():
yield
class A:
def Fun(self):
yield
try:
print(Fun.__name__)
print(A.Fun.__name__)
print(A().Fun.__name__)
except AttributeError:
print('SKIP')
raise SystemExit
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/generator_name.py | Python | apache-2.0 | 250 |
def gen():
i = 0
while 1:
yield i
i += 1
g = gen()
try:
g.pend_throw
except AttributeError:
print("SKIP")
raise SystemExit
# Verify that an injected exception will be raised from next().
print(next(g))
print(next(g))
g.pend_throw(ValueError())
v = None
try:
v = next(g)
excep... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/generator_pend_throw.py | Python | apache-2.0 | 1,822 |
# tests for correct PEP479 behaviour (introduced in Python 3.5)
# basic case: StopIteration is converted into a RuntimeError
def gen():
yield 1
raise StopIteration
g = gen()
print(next(g))
try:
next(g)
except RuntimeError:
print('RuntimeError')
# trying to continue a failed generator now raises StopIt... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/generator_pep479.py | Python | apache-2.0 | 873 |
def gen():
yield 1
return 42
g = gen()
print(next(g))
try:
print(next(g))
except StopIteration as e:
print(type(e), e.args)
# trying next again should raise StopIteration with no arguments
try:
print(next(g))
except StopIteration as e:
print(type(e), e.args)
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/generator_return.py | Python | apache-2.0 | 285 |
def f():
n = 0
while True:
n = yield n + 1
print(n)
g = f()
try:
g.send(1)
except TypeError:
print("caught")
print(g.send(None))
print(g.send(100))
print(g.send(200))
def f2():
print("entering")
for i in range(3):
print(i)
yield
print("returning 1")
pr... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/generator_send.py | Python | apache-2.0 | 504 |
# case where generator doesn't intercept the thrown/injected exception
def gen():
yield 123
yield 456
g = gen()
print(next(g))
try:
g.throw(KeyError)
except KeyError:
print('got KeyError from downstream!')
# case where a thrown exception is caught and stops the generator
def gen():
try:
... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/generator_throw.py | Python | apache-2.0 | 1,047 |
# Tests that the correct nested exception handler is used when
# throwing into a generator (previously failed on native emitter).
def gen():
try:
yield 1
try:
yield 2
try:
yield 3
except Exception:
yield 4
print(0)
... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/generator_throw_nested.py | Python | apache-2.0 | 734 |
# test __getattr__
class A:
def __init__(self, d):
self.d = d
def __getattr__(self, attr):
return self.d[attr]
a = A({'a':1, 'b':2})
print(a.a, a.b)
# test that any exception raised in __getattr__ propagates out
class A:
def __getattr__(self, attr):
if attr == "value":
... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/getattr.py | Python | apache-2.0 | 519 |
# create a class that has a __getitem__ method
class A:
def __getitem__(self, index):
print('getitem', index)
if index > 10:
raise StopIteration
# test __getitem__
A()[0]
A()[1]
# iterate using a for loop
for i in A():
pass
# iterate manually
it = iter(A())
try:
while True:
... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/getitem.py | Python | apache-2.0 | 708 |
"""
1
"""
def _f(): pass
FunctionType = type(_f)
LambdaType = type(lambda: None)
CodeType = None
MappingProxyType = None
SimpleNamespace = None
def _g():
yield 1
GeneratorType = type(_g())
class _C:
def _m(self): pass
MethodType = type(_C()._m)
BuiltinFunctionType = type(len)
BuiltinMethodType = type([].app... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/globals_del.py | Python | apache-2.0 | 474 |
# test if conditions which are optimised by the compiler
if 0:
print(5)
else:
print(6)
if 1:
print(7)
if 2:
print(8)
if -1:
print(9)
elif 1:
print(10)
if 0:
print(11)
else:
print(12)
if 0:
print(13)
elif 1:
print(14)
if 0:
print(15)
elif 0:
print(16)
else:
prin... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/ifcond.py | Python | apache-2.0 | 1,166 |
# test if-expressions
print(1 if 0 else 2)
print(3 if 1 else 4)
def f(x):
print('a' if x else 'b')
f([])
f([1])
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/ifexpr.py | Python | apache-2.0 | 118 |
print(int(False))
print(int(True))
print(int(0))
print(int(1))
print(int(+1))
print(int(-1))
print(int('0'))
print(int('+0'))
print(int('-0'))
print(int('1'))
print(int('+1'))
print(int('-1'))
print(int('01'))
print(int('9'))
print(int('10'))
print(int('+10'))
print(int('-10'))
print(int('12'))
print(int('-12'))
prin... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int1.py | Python | apache-2.0 | 1,581 |
# test basic int operations
# test conversion of bool on RHS of binary op
a = False
print(1 + a)
a = True
print(1 + a)
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/int2.py | Python | apache-2.0 | 120 |
# to test arbitrariy precision integers
x = 1000000000000000000000000000000
xn = -1000000000000000000000000000000
y = 2000000000000000000000000000000
# printing
print(x)
print(y)
print('%#X' % (x - x)) # print prefix
print('{:#,}'.format(x)) # print with commas
# addition
print(x + 1)
print(x + y)
print(x + xn == 0)... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big1.py | Python | apache-2.0 | 2,935 |
# tests transition from small to large int representation by addition
# 31-bit overflow
i = 0x3fffffff
print(i + i)
print(-i + -i)
# 47-bit overflow
i = 0x3fffffffffff
print(i + i)
print(-i + -i)
# 63-bit overflow
i = 0x3fffffffffffffff
print(i + i)
print(-i + -i)
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big_add.py | Python | apache-2.0 | 268 |
print(0 & (1 << 80))
print(0 & (1 << 80) == 0)
print(bool(0 & (1 << 80)))
a = 0xfffffffffffffffffffffffffffff
print(a & (1 << 80))
print((a & (1 << 80)) >> 80)
print((a & (1 << 80)) >> 80 == 1)
# test negative on rhs
a = 123456789012345678901234567890
print(a & -1)
print(a & -2)
print(a & -234567890123456789012345678... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big_and.py | Python | apache-2.0 | 987 |
# test + +
print( 97989513389222316022151446562729620153292831887555425160965597396
& 23716683549865351578586448630079789776107310103486834795830390982)
print( 53817081128841898634258263553430908085326601592682411889506742059
& 37042558948907407488299113387826240429667200950043601129661240876)
print( 2616... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big_and2.py | Python | apache-2.0 | 2,185 |
# test - +
print( -97989513389222316022151446562729620153292831887555425160965597396
& 23716683549865351578586448630079789776107310103486834795830390982)
print( -53817081128841898634258263553430908085326601592682411889506742059
& 37042558948907407488299113387826240429667200950043601129661240876)
print( -2... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big_and3.py | Python | apache-2.0 | 2,185 |
# test bignum comparisons
i = 1 << 65
print(i == 0)
print(i != 0)
print(i < 0)
print(i > 0)
print(i <= 0)
print(i >= 0)
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big_cmp.py | Python | apache-2.0 | 122 |
for lhs in (1000000000000000000000000, 10000000000100000000000000, 10012003400000000000000007, 12349083434598210349871029923874109871234789):
for rhs in range(1, 555):
print(lhs // rhs)
# these check an edge case on 64-bit machines where two mpz limbs
# are used and the most significant one has the MSB set... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big_div.py | Python | apache-2.0 | 556 |
# test errors operating on bignum
i = 1 << 65
try:
i << -1
except ValueError:
print("ValueError")
try:
len(i)
except TypeError:
print("TypeError")
try:
1 in i
except TypeError:
print("TypeError")
# overflow because arg of bytearray is being converted to machine int
try:
bytearray(i)
exc... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big_error.py | Python | apache-2.0 | 717 |
# tests transition from small to large int representation by left-shift operation
for i in range(1, 17):
for shift in range(70):
print(i, '<<', shift, '=', i << shift)
# test bit-shifting negative integers
for i in range(8):
print(-100000000000000000000000000000 << i)
print(-10000000000000000000000... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big_lshift.py | Python | apache-2.0 | 753 |
# test % operation on big integers
delta = 100000000000000000000000000000012345
for i in range(11):
for j in range(11):
x = delta * (i - 5)
y = delta * (j - 5)
if y != 0:
print(x % y)
# these check an edge case on 64-bit machines where two mpz limbs
# are used and the most sig... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big_mod.py | Python | apache-2.0 | 433 |
# tests transition from small to large int representation by multiplication
for rhs in range(2, 11):
lhs = 1
for k in range(100):
res = lhs * rhs
print(lhs, '*', rhs, '=', res)
lhs = res
# below tests pos/neg combinations that overflow small int
# 31-bit overflow
i = 1 << 20
print(i * ... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big_mul.py | Python | apache-2.0 | 453 |
print(0 | (1 << 80))
a = 0xfffffffffffffffffffffffffffff
print(a | (1 << 200))
# test + +
print(0 | (1 << 80))
print((1 << 80) | (1 << 80))
print((1 << 80) | 0)
a = 0xfffffffffffffffffffffffffffff
print(a | (1 << 100))
print(a | (1 << 200))
print(a | a == 0)
print(bool(a | a))
# test - +
print((-1 << 80) | (1 <... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big_or.py | Python | apache-2.0 | 746 |
# test + +
print( 97989513389222316022151446562729620153292831887555425160965597396
| 23716683549865351578586448630079789776107310103486834795830390982)
print( 53817081128841898634258263553430908085326601592682411889506742059
| 37042558948907407488299113387826240429667200950043601129661240876)
print( 26167... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big_or2.py | Python | apache-2.0 | 2,184 |
# test - +
print( -97989513389222316022151446562729620153292831887555425160965597396
| 23716683549865351578586448630079789776107310103486834795830390982)
print( -53817081128841898634258263553430908085326601592682411889506742059
| 37042558948907407488299113387826240429667200950043601129661240876)
print( -2... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big_or3.py | Python | apache-2.0 | 2,185 |
# test bignum power
i = 1 << 65
print(0 ** i)
print(i ** 0)
print(i ** 1)
print(i ** 2)
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big_pow.py | Python | apache-2.0 | 90 |
i = 123456789012345678901234567890
print(i >> 1)
print(i >> 1000)
# result needs rounding up
i = -(1 << 70)
print(i >> 80)
i = -0xffffffffffffffff
print(i >> 32)
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big_rshift.py | Python | apache-2.0 | 163 |
# test bignum unary operations
i = 1 << 65
print(bool(i))
print(+i)
print(-i)
print(~i)
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big_unary.py | Python | apache-2.0 | 90 |
# test + +
print(0 ^ (1 << 80))
print((1 << 80) ^ (1 << 80))
print((1 << 80) ^ 0)
a = 0xfffffffffffffffffffffffffffff
print(a ^ (1 << 100))
print(a ^ (1 << 200))
print(a ^ a == 0)
print(bool(a ^ a))
# test - +
print((-1 << 80) ^ (1 << 80))
print((-1 << 80) ^ 0)
print((-a) ^ (1 << 100))
print((-a) ^ (1 << 200))
p... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big_xor.py | Python | apache-2.0 | 736 |
# test + +
print( 97989513389222316022151446562729620153292831887555425160965597396
^ 23716683549865351578586448630079789776107310103486834795830390982)
print( 53817081128841898634258263553430908085326601592682411889506742059
^ 37042558948907407488299113387826240429667200950043601129661240876)
print( 2616... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big_xor2.py | Python | apache-2.0 | 2,185 |
# test - +
print( -97989513389222316022151446562729620153292831887555425160965597396
^ 23716683549865351578586448630079789776107310103486834795830390982)
print( -53817081128841898634258263553430908085326601592682411889506742059
^ 37042558948907407488299113387826240429667200950043601129661240876)
print( -2... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big_xor3.py | Python | apache-2.0 | 2,185 |
# test [0,-0,1,-1] edge cases of bignum
long_zero = (2**64) >> 65
long_neg_zero = -long_zero
long_one = long_zero + 1
long_neg_one = -long_one
cases = [long_zero, long_neg_zero, long_one, long_neg_one]
print(cases)
print([-c for c in cases])
print([~c for c in cases])
print([c >> 1 for c in cases])
print([c << 1 for... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_big_zeroone.py | Python | apache-2.0 | 649 |
print((10).to_bytes(1, "little"))
print((111111).to_bytes(4, "little"))
print((100).to_bytes(10, "little"))
print(int.from_bytes(b"\x00\x01\0\0\0\0\0\0", "little"))
print(int.from_bytes(b"\x01\0\0\0\0\0\0\0", "little"))
print(int.from_bytes(b"\x00\x01\0\0\0\0\0\0", "little"))
# check that extra zero bytes don't change... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_bytes.py | Python | apache-2.0 | 764 |
print((2**64).to_bytes(9, "little"))
print((2**64).to_bytes(9, "big"))
b = bytes(range(20))
il = int.from_bytes(b, "little")
ib = int.from_bytes(b, "big")
print(il)
print(ib)
print(il.to_bytes(20, "little"))
print(ib.to_bytes(20, "big"))
# check that extra zero bytes don't change the internal int value
print(int.fro... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_bytes_intbig.py | Python | apache-2.0 | 385 |
# tests int constant folding in compiler
# positive
print(+1)
print(+100)
# negation
print(-1)
print(-(-1))
# 1's complement
print(~0)
print(~1)
print(~-1)
# addition
print(1 + 2)
# subtraction
print(1 - 2)
print(2 - 1)
# multiplication
print(1 * 2)
print(123 * 456)
# floor div and modulo
print(123 // 7, 123 % 7... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_constfolding.py | Python | apache-2.0 | 557 |
# tests int constant folding in compiler
# negation
print(-0x3fffffff) # 32-bit edge case
print(-0x3fffffffffffffff) # 64-bit edge case
print(-(-0x3fffffff - 1)) # 32-bit edge case
print(-(-0x3fffffffffffffff - 1)) # 64-bit edge case
# 1's complement
print(~0x3fffffff) # 32-bit edge case
print(~0x3fffffffffffffff) # ... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_constfolding_intbig.py | Python | apache-2.0 | 544 |
# test integer floor division and modulo
# test all combination of +/-/0 cases
for i in range(-2, 3):
for j in range(-4, 5):
if j != 0:
print(i, j, i // j, i % j, divmod(i, j))
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_divmod.py | Python | apache-2.0 | 202 |
# test integer floor division and modulo
# this tests bignum modulo
a = 987654321987987987987987987987
b = 19
print(a % b)
print(a % -b)
print(-a % b)
print(-a % -b)
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_divmod_intbig.py | Python | apache-2.0 | 167 |
try:
1 // 0
except ZeroDivisionError:
print("ZeroDivisionError")
try:
1 % 0
except ZeroDivisionError:
print("ZeroDivisionError")
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_divzero.py | Python | apache-2.0 | 146 |
# This tests long ints for 32-bit machine
a = 0x1ffffffff
b = 0x100000000
print(a)
print(b)
print(a + b)
print(a - b)
print(b - a)
# overflows long long implementation
#print(a * b)
print(a // b)
print(a % b)
print("&", a & b)
print(a | b)
print(a ^ b)
print(a << 3)
print(a >> 1)
a += b
print(a)
a -= 123456
print(a)
... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_intbig.py | Python | apache-2.0 | 669 |
# This tests small int range for 32-bit machine
# Small ints are variable-length encoded in MicroPython, so first
# test that encoding works as expected.
print(0)
print(1)
print(-1)
# Value is split in 7-bit "subwords", and taking into account that all
# ints in Python are signed, there're 6 bits of magnitude. So, ar... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/int_small.py | Python | apache-2.0 | 1,520 |
import uio as io
try:
io.BytesIO
io.BufferedWriter
except AttributeError:
print('SKIP')
raise SystemExit
bts = io.BytesIO()
buf = io.BufferedWriter(bts, 8)
buf.write(b"foobar")
print(bts.getvalue())
buf.write(b"foobar")
# CPython has different flushing policy, so value below is different
print(bts.ge... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/io_buffered_writer.py | Python | apache-2.0 | 551 |
# Make sure that write operations on io.BytesIO don't
# change original object it was constructed from.
try:
import uio as io
except ImportError:
import io
b = b"foobar"
a = io.BytesIO(b)
a.write(b"1")
print(b)
print(a.getvalue())
b = bytearray(b"foobar")
a = io.BytesIO(b)
a.write(b"1")
print(b)
print(a.get... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/io_bytesio_cow.py | Python | apache-2.0 | 329 |
# Extended stream operations on io.BytesIO
try:
import uio as io
except ImportError:
import io
a = io.BytesIO(b"foobar")
a.seek(10)
print(a.read(10))
a = io.BytesIO()
print(a.seek(8))
a.write(b"123")
print(a.getvalue())
print(a.seek(0, 1))
print(a.seek(-1, 2))
a.write(b"0")
print(a.getvalue())
a.flush()
pr... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/io_bytesio_ext.py | Python | apache-2.0 | 403 |
try:
import uio as io
except ImportError:
import io
a = io.BytesIO(b"foobar")
try:
a.seek(-10)
except Exception as e:
# CPython throws ValueError, but MicroPython has consistent stream
# interface, so BytesIO raises the same error as a real file, which
# is OSError(EINVAL).
print(type(e), e... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/io_bytesio_ext2.py | Python | apache-2.0 | 334 |
try:
import uio as io
except:
import io
try:
io.IOBase
except AttributeError:
print('SKIP')
raise SystemExit
class MyIO(io.IOBase):
def write(self, buf):
# CPython and uPy pass in different types for buf (str vs bytearray)
print('write', len(buf))
return len(buf)
prin... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/io_iobase.py | Python | apache-2.0 | 343 |
try:
import uio as io
except ImportError:
import io
a = io.StringIO()
print('io.StringIO' in repr(a))
print(a.getvalue())
print(a.read())
a = io.StringIO("foobar")
print(a.getvalue())
print(a.read())
print(a.read())
a = io.StringIO()
a.write("foo")
print(a.getvalue())
a = io.StringIO("foo")
a.write("12")
pr... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/io_stringio1.py | Python | apache-2.0 | 894 |
try:
import uio as io
except ImportError:
import io
# test __enter__/__exit__
with io.StringIO() as b:
b.write("foo")
print(b.getvalue())
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/io_stringio_with.py | Python | apache-2.0 | 155 |
# This tests extended (MicroPython-specific) form of write:
# write(buf, len) and write(buf, offset, len)
import uio
try:
uio.BytesIO
except AttributeError:
print('SKIP')
raise SystemExit
buf = uio.BytesIO()
buf.write(b"foo", 2)
print(buf.getvalue())
buf.write(b"foo", 100)
print(buf.getvalue())
buf.wri... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/io_write_ext.py | Python | apache-2.0 | 468 |
print([1, 2] is [1, 2])
a = [1, 2]
b = a
print(b is a)
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/is_isnot.py | Python | apache-2.0 | 55 |
# test "is" and "is not" with literal arguments
# these raise a SyntaxWarning in CPython because the results are
# implementation dependent; see https://bugs.python.org/issue34850
print(1 is 1)
print(1 is 2)
print(1 is not 1)
print(1 is not 2)
print("a" is "a")
print("a" is "b")
print("a" is not "a")
print("a" is not... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/is_isnot_literal.py | Python | apache-2.0 | 326 |
# builtin type that is not iterable
try:
for i in 1:
pass
except TypeError:
print('TypeError')
# builtin type that is iterable, calling __next__ explicitly
print(iter(range(4)).__next__())
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/iter0.py | Python | apache-2.0 | 206 |
# test user defined iterators
# this class is not iterable
class NotIterable:
pass
try:
for i in NotIterable():
pass
except TypeError:
print('TypeError')
# this class has no __next__ implementation
class NotIterable:
def __iter__(self):
return self
try:
print(all(NotIterable()))
ex... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/iter1.py | Python | apache-2.0 | 1,652 |
# user defined iterator used in something other than a for loop
class MyStopIteration(StopIteration):
pass
class myiter:
def __init__(self, i):
self.i = i
def __iter__(self):
return self
def __next__(self):
if self.i == 0:
raise StopIteration
elif self.i =... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/iter2.py | Python | apache-2.0 | 490 |
i = iter(iter((1, 2, 3)))
print(list(i))
i = iter(iter([1, 2, 3]))
print(list(i))
i = iter(iter({1:2, 3:4, 5:6}))
print(sorted(i))
# set, see set_iter_of_iter.py
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/iter_of_iter.py | Python | apache-2.0 | 162 |
# lambda
f = lambda x, y: x + 3 * y
print(f(3, 5))
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/lambda1.py | Python | apache-2.0 | 52 |
# test default args with lambda
f = lambda x=1: x
print(f(), f(2), f(x=3))
y = 'y'
f = lambda x=y: x
print(f())
f = lambda x, y=[]: (x, y)
f(0)[1].append(1)
print(f(1), f(x=2), f(3, 4), f(4, y=5))
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/lambda_defargs.py | Python | apache-2.0 | 200 |
# test the lexer
try:
eval
exec
except NameError:
print("SKIP")
raise SystemExit
# __debug__ is a special symbol
print(type(__debug__))
# short input
exec("")
exec("\n")
exec("\n\n")
exec("\r")
exec("\r\r")
exec("\t")
exec("\r\n")
exec("\nprint(1)")
exec("\rprint(2)")
exec("\r\nprint(3)")
exec("\n5")... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/lexer.py | Python | apache-2.0 | 1,394 |
# basic list functionality
x = [1, 2, 3 * 4]
print(x)
x[0] = 4
print(x)
x[1] += -4
print(x)
x.append(5)
print(x)
f = x.append
f(4)
print(x)
x.extend([100, 200])
print(x)
x.extend(range(3))
print(x)
x += [2, 1]
print(x)
# unsupported type on RHS of add
try:
[] + None
except TypeError:
print('TypeError')
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/list1.py | Python | apache-2.0 | 315 |
# tests list.clear
x = [1, 2, 3, 4]
x.clear()
print(x)
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/list_clear.py | Python | apache-2.0 | 55 |
print([] == [])
print([] > [])
print([] < [])
print([] == [1])
print([1] == [])
print([] > [1])
print([1] > [])
print([] < [1])
print([1] < [])
print([] >= [1])
print([1] >= [])
print([] <= [1])
print([1] <= [])
print([1] == [1])
print([1] != [1])
print([1] == [2])
print([1] == [1, 0])
print([1] > [1])
print([1] > [2... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/list_compare.py | Python | apache-2.0 | 957 |
# list copy tests
a = [1, 2, []]
b = a.copy()
a[-1].append(1)
a.append(4)
print(a)
print(b)
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/list_copy.py | Python | apache-2.0 | 92 |
# list count tests
a = [1, 2, 3]
a = a + a + a
b = [0, 0, a, 0, a, 0]
print(a.count(2))
print(b.count(a))
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/list_count.py | Python | apache-2.0 | 106 |
# test list.__iadd__ and list.extend (they are equivalent)
l = [1, 2]
l.extend([])
print(l)
l.extend([3])
print(l)
l.extend([4, 5])
print(l)
l.extend(range(6, 10))
print(l)
l.extend("abc")
print(l)
l = [1, 2]
l += []
print(l)
l += [3]
print(l)
l += [4, 5]
print(l)
l += range(6, 10)
print(l)
l += "abc"
print(l... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/list_extend.py | Python | apache-2.0 | 322 |
a = [1, 2, 3]
print(a.index(1))
print(a.index(2))
print(a.index(3))
print(a.index(3, 2))
print(a.index(1, -100))
print(a.index(1, False))
try:
print(a.index(1, True))
except ValueError:
print("Raised ValueError")
else:
print("Did not raise ValueError")
try:
print(a.index(3, 2, 2))
except ValueError:
... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/list_index.py | Python | apache-2.0 | 679 |
a = [1, 2, 3]
a.insert(1, 42)
print(a)
a.insert(-1, -1)
print(a)
a.insert(99, 99)
print(a)
a.insert(-99, -99)
print(a)
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/list_insert.py | Python | apache-2.0 | 119 |
# basic multiplication
print([0] * 5)
# check negative, 0, positive; lhs and rhs multiplication
for i in (-4, -2, 0, 2, 4):
print(i * [1, 2])
print([1, 2] * i)
# check that we don't modify existing list
a = [1, 2, 3]
c = a * 3
print(a, c)
# unsupported type on RHS
try:
[] * None
except TypeError:
pri... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/list_mult.py | Python | apache-2.0 | 336 |
# list poppin'
a = [1, 2, 3]
print(a.pop())
print(a.pop())
print(a.pop())
try:
print(a.pop())
except IndexError:
print("IndexError raised")
else:
raise AssertionError("No IndexError raised")
# popping such that list storage shrinks (tests implementation detail of uPy)
l = list(range(20))
for i in range(len... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/list_pop.py | Python | apache-2.0 | 347 |
a = [1, 2, 3]
print(a.remove(2))
print(a)
try:
a.remove(2)
except ValueError:
print("Raised ValueError")
else:
raise AssertionError("Did not raise ValueError")
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/list_remove.py | Python | apache-2.0 | 172 |
a = []
for i in range(100):
a.append(i)
a.reverse()
print(a)
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/list_reverse.py | Python | apache-2.0 | 69 |
# test list slices, getting values
x = list(range(10))
print(x[1:])
print(x[:-1])
print(x[2:3])
a = 2
b = 4
c = 3
print(x[:])
print(x[::])
print(x[::c])
print(x[:b])
print(x[:b:])
print(x[:b:c])
print(x[a])
print(x[a:])
print(x[a::])
print(x[a::c])
print(x[a:b])
print(x[a:b:])
print(x[a:b:c])
# these should not rai... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/list_slice.py | Python | apache-2.0 | 424 |
x = list(range(10))
print(x[::-1])
print(x[::2])
print(x[::-2])
x = list(range(9))
print(x[::-1])
print(x[::2])
print(x[::-2])
x = list(range(5))
print(x[:0:-1])
print(x[:1:-1])
print(x[:2:-1])
print(x[0::-1])
print(x[1::-1])
print(x[2::-1])
x = list(range(5))
print(x[0:0:-1])
print(x[4:4:-1])
print(x[5:5:-1])
x = ... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/list_slice_3arg.py | Python | apache-2.0 | 633 |
# test slices; only 2 argument version supported by MicroPython at the moment
x = list(range(10))
# Assignment
l = list(x)
l[1:3] = [10, 20]
print(l)
l = list(x)
l[1:3] = [10]
print(l)
l = list(x)
l[1:3] = []
print(l)
l = list(x)
del l[1:3]
print(l)
l = list(x)
l[:3] = [10, 20]
print(l)
l = list(x)
l[:3] = []
print(l... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/list_slice_assign.py | Python | apache-2.0 | 621 |
x = list(range(2))
l = list(x)
l[0:0] = [10]
print(l)
l = list(x)
l[:0] = [10, 20]
print(l)
l = list(x)
l[0:0] = [10, 20, 30, 40]
print(l)
l = list(x)
l[1:1] = [10, 20, 30, 40]
print(l)
l = list(x)
l[2:] = [10, 20, 30, 40]
print(l)
# Weird cases
l = list(x)
l[1:0] = [10, 20, 30, 40]
print(l)
l = list(x)
l[100:100]... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/list_slice_assign_grow.py | Python | apache-2.0 | 422 |
l = [1, 3, 2, 5]
print(l)
print(sorted(l))
l.sort()
print(l)
print(l == sorted(l))
print(sorted(l, key=lambda x: -x))
l.sort(key=lambda x: -x)
print(l)
print(l == sorted(l, key=lambda x: -x))
print(sorted(l, key=lambda x: -x, reverse=True))
l.sort(key=lambda x: -x, reverse=True)
print(l)
print(l == sorted(l, key=lam... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/list_sort.py | Python | apache-2.0 | 971 |
# list addition
a = [1,2,3]
b = [4,5,6]
c = a + b
print(c)
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/list_sum.py | Python | apache-2.0 | 59 |
# tests logical constant folding in parser
def f_true():
print('f_true')
return True
def f_false():
print('f_false')
return False
print(0 or False)
print(1 or foo)
print(f_false() or 1 or foo)
print(f_false() or 1 or f_true())
print(0 and foo)
print(1 and True)
print(f_true() and 0 and foo)
print(f_... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/logic_constfolding.py | Python | apache-2.0 | 442 |
# test out-of-memory with malloc
l = list(range(1000))
try:
1000000000 * l
except MemoryError:
print('MemoryError')
print(len(l), l[0], l[-1])
# test out-of-memory with realloc
try:
[].extend(range(1000000000))
except MemoryError:
print('MemoryError')
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/memoryerror.py | Python | apache-2.0 | 269 |
# test memoryview
try:
memoryview
except:
print("SKIP")
raise SystemExit
try:
import uarray as array
except ImportError:
try:
import array
except ImportError:
print("SKIP")
raise SystemExit
# test reading from bytes
b = b'1234'
m = memoryview(b)
print(len(m))
print(m[0],... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/memoryview1.py | Python | apache-2.0 | 1,687 |
# test memoryview accessing maximum values for signed/unsigned elements
try:
memoryview
except:
print("SKIP")
raise SystemExit
try:
from uarray import array
except ImportError:
try:
from array import array
except ImportError:
print("SKIP")
raise SystemExit
print(list(mem... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/memoryview2.py | Python | apache-2.0 | 589 |
# test memoryview retains pointer to original object/buffer
try:
memoryview
except:
print("SKIP")
raise SystemExit
b = bytearray(10)
m = memoryview(b)[1:]
for i in range(len(m)):
m[i] = i
# reclaim b, but hopefully not the buffer
b = None
import gc
gc.collect()
# allocate lots of memory
for i in rang... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/memoryview_gc.py | Python | apache-2.0 | 776 |
# test memoryview accessing maximum values for signed/unsigned elements
try:
memoryview
except:
print("SKIP")
raise SystemExit
try:
from uarray import array
except ImportError:
try:
from array import array
except ImportError:
print("SKIP")
raise SystemExit
print(list(mem... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/memoryview_intbig.py | Python | apache-2.0 | 455 |
try:
memoryview(b'a').itemsize
except:
print("SKIP")
raise SystemExit
try:
from uarray import array
except ImportError:
try:
from array import array
except ImportError:
print("SKIP")
raise SystemExit
for code in ['b', 'h', 'i', 'q', 'f', 'd']:
print(memoryview(array(... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/memoryview_itemsize.py | Python | apache-2.0 | 584 |
# test slice assignment to memoryview
try:
memoryview(bytearray(1))[:] = memoryview(bytearray(1))
except (NameError, TypeError):
print("SKIP")
raise SystemExit
try:
import uarray as array
except ImportError:
try:
import array
except ImportError:
print("SKIP")
raise Syst... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/memoryview_slice_assign.py | Python | apache-2.0 | 1,625 |
# test behaviour of module objects
# this module should always exist
import __main__
# print module
print(repr(__main__).startswith("<module '__main__'"))
# store new attribute
__main__.x = 1
# delete attribute
del __main__.x
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/module1.py | Python | apache-2.0 | 230 |
# uPy behaviour only: builtin modules are read-only
import usys
try:
usys.x = 1
except AttributeError:
print("AttributeError")
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/module2.py | Python | apache-2.0 | 135 |
try:
try:
from ucollections import namedtuple
except ImportError:
from collections import namedtuple
except ImportError:
print("SKIP")
raise SystemExit
T = namedtuple("Tup", ["foo", "bar"])
# CPython prints fully qualified name, what we don't bother to do so far
#print(T)
for t in T(1, ... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/namedtuple1.py | Python | apache-2.0 | 1,613 |
try:
try:
from ucollections import namedtuple
except ImportError:
from collections import namedtuple
except ImportError:
print("SKIP")
raise SystemExit
t = namedtuple("Tup", ["baz", "foo", "bar"])(3, 2, 5)
try:
t._asdict
except AttributeError:
print("SKIP")
raise SystemExit... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/namedtuple_asdict.py | Python | apache-2.0 | 384 |
# test builtin object()
# creation
object()
# printing
print(repr(object())[:7])
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/object1.py | Python | apache-2.0 | 83 |
class Foo:
def __init__(self):
self.a = 1
self.b = "bar"
o = Foo()
if not hasattr(o, "__dict__"):
print("SKIP")
raise SystemExit
print(o.__dict__ == {'a': 1, 'b': 'bar'})
| YifuLiu/AliOS-Things | components/py_engine/tests/basics/object_dict.py | Python | apache-2.0 | 203 |
# object.__new__(cls) is the only way in Python to allocate empty
# (non-initialized) instance of class.
# See e.g. http://infohost.nmt.edu/tcc/help/pubs/python/web/new-new-method.html
# TODO: Find reference in CPython docs
try:
# If we don't expose object.__new__ (small ports), there's
# nothing to test.
o... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/object_new.py | Python | apache-2.0 | 1,074 |
# test errors from bad operations (unary, binary, etc)
# unsupported unary operators
try:
~None
except TypeError:
print('TypeError')
try:
~''
except TypeError:
print('TypeError')
try:
~[]
except TypeError:
print('TypeError')
# unsupported binary operators
try:
False in True
except TypeErro... | YifuLiu/AliOS-Things | components/py_engine/tests/basics/op_error.py | Python | apache-2.0 | 962 |