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)
print(list(g(1, 2, 3, foo="bar")))
|
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 StopIteration")
except StopIteration:
print("StopIteration")
# Test that it's possible to close finished gen
g = gen1()
print(list(g))
print(g.close())
try:
next(g)
print("No StopIteration")
except StopIteration:
print("StopIteration")
# Throwing GeneratorExit in response to close() is ok
def gen2():
try:
yield 1
yield 2
except:
print('raising GeneratorExit')
raise GeneratorExit
g = gen2()
next(g)
print(g.close())
# Any other exception is propagated to the .close() caller
def gen3():
try:
yield 1
yield 2
except:
raise ValueError
g = gen3()
next(g)
try:
print(g.close())
except ValueError:
print("ValueError")
|
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 = 2
yield g()
for i in f():
print(i)
# using comprehensions, the inner generator closes over y
generator_of_generators = (((x, y) for x in range(2)) for y in range(3))
for i in generator_of_generators:
for j in i:
print(j)
# test that printing of closed-over generators doesn't lead to segfaults
def genc():
foo = 1
repr(lambda: (yield foo))
genc()
|
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
yield 3
g = gen2()
print(next(g))
try:
print(next(g))
except ValueError:
print("ValueError")
try:
print(next(g))
except StopIteration:
print("StopIteration")
# Test throwing exception into generator
def gen3():
yield 1
try:
yield 2
except ValueError:
print("ValueError received")
yield 3
yield 4
yield 5
g = gen3()
print(next(g))
print(next(g))
print("out of throw:", g.throw(ValueError))
print(next(g))
try:
print("out of throw2:", g.throw(ValueError))
except ValueError:
print("Boomerang ValueError caught")
|
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)
except Exception as e:
print("raised", repr(e))
print("ret was:", v)
# Verify that pend_throw works on an unstarted coroutine.
g = gen()
g.pend_throw(OSError())
try:
next(g)
except Exception as e:
print("raised", repr(e))
# Verify that you can't resume the coroutine from within the running coroutine.
def gen_next():
next(g)
yield 1
g = gen_next()
try:
next(g)
except Exception as e:
print("raised", repr(e))
# Verify that you can't pend_throw from within the running coroutine.
def gen_pend_throw():
g.pend_throw(ValueError())
yield 1
g = gen_pend_throw()
try:
next(g)
except Exception as e:
print("raised", repr(e))
# Verify that the pend_throw exception can be ignored.
class CancelledError(Exception):
pass
def gen_cancelled():
for i in range(5):
try:
yield i
except CancelledError:
print('ignore CancelledError')
g = gen_cancelled()
print(next(g))
g.pend_throw(CancelledError())
print(next(g))
# ...but not if the generator hasn't started.
g = gen_cancelled()
g.pend_throw(CancelledError())
try:
next(g)
except Exception as e:
print("raised", repr(e))
# Verify that calling pend_throw returns the previous exception.
g = gen()
next(g)
print(repr(g.pend_throw(CancelledError())))
print(repr(g.pend_throw(OSError)))
# Verify that you can pend_throw(None) to cancel a previous pend_throw.
g = gen()
next(g)
g.pend_throw(CancelledError())
print(repr(g.pend_throw(None)))
print(next(g))
|
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 StopIteration
try:
next(g)
except StopIteration:
print('StopIteration')
# throwing a StopIteration which is uncaught will be converted into a RuntimeError
def gen():
yield 1
yield 2
g = gen()
print(next(g))
try:
g.throw(StopIteration)
except RuntimeError:
print('RuntimeError')
# throwing a StopIteration through yield from, will be converted to a RuntimeError
def gen():
yield from range(2)
print('should not get here')
g = gen()
print(next(g))
try:
g.throw(StopIteration)
except RuntimeError:
print('RuntimeError')
|
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")
print("returning 2")
g = f2()
g.send(None)
g.send(1)
g.send(1)
try:
g.send(1)
except StopIteration:
print("caught")
try:
g.send(1)
except StopIteration:
print("caught")
|
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:
yield 1
yield 2
except:
pass
g = gen()
print(next(g))
try:
g.throw(ValueError)
except StopIteration:
print('got StopIteration')
# generator ignores a thrown GeneratorExit (this is allowed)
def gen():
try:
yield 123
except GeneratorExit as e:
print('GeneratorExit', repr(e.args))
yield 456
# thrown a class
g = gen()
print(next(g))
print(g.throw(GeneratorExit))
# thrown an instance
g = gen()
print(next(g))
print(g.throw(GeneratorExit()))
# thrown an instance with None as second arg
g = gen()
print(next(g))
print(g.throw(GeneratorExit(), None))
# thrown a class and instance
g = gen()
print(next(g))
print(g.throw(GeneratorExit, GeneratorExit(123)))
|
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)
yield 5
except Exception:
yield 6
print(1)
yield 7
except Exception:
yield 8
print(2)
yield 9
for i in range(1, 10):
g = gen()
try:
for _ in range(i):
print(next(g))
print(g.throw(ValueError))
except ValueError:
print('ValueError')
except StopIteration:
print('StopIteration')
|
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":
raise ValueError(123)
else:
raise AttributeError(456)
a = A()
try:
a.value
except ValueError as er:
print(er)
try:
a.attr
except AttributeError as er:
print(er)
|
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:
next(it)
except StopIteration:
pass
# this class raises an IndexError to stop the iteration
class A:
def __getitem__(self, i):
raise IndexError
print(list(A()))
# this class raises a non-StopIteration exception on iteration
class A:
def __getitem__(self, i):
raise TypeError
try:
for i in A():
pass
except TypeError:
print("TypeError")
|
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([].append)
del _f
# print only the first 8 chars, since we have different str rep to CPython
print(str(FunctionType)[:8])
print(str(BuiltinFunctionType)[:8])
|
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:
print(17)
if not False:
print('a')
if not True:
print('a')
else:
print('b')
if False:
print('a')
else:
print('b')
if True:
print('a')
if (1,):
print('a')
if not (1,):
print('a')
else:
print('b')
f2 = 0
def f(t1, t2, f1):
if False:
print(1)
if True:
print(1)
if ():
print(1)
if (1,):
print(1)
if (1, 2):
print(1)
if t1 and t2:
print(1)
if (t1 and t2): # parsed differently to above
print(1)
if not (t1 and f1):
print(1)
if t1 or t2:
print(1)
if (t1 or t2): # parse differently to above
print(1)
if f1 or t1:
print(1)
if not (f1 or f2):
print(1)
if t1 and f1 or t1 and t2:
print(1)
if (f1 or t1) and (f2 or t2):
print(1)
f(True, 1, False)
|
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'))
print(int('99'))
print(int('100'))
print(int('314'))
print(int(' 314'))
print(int('314 '))
print(int(' \t\t 314 \t\t '))
print(int(' 1 '))
print(int(' -3 '))
print(int('0', 10))
print(int('1', 10))
print(int(' \t 1 \t ', 10))
print(int('11', 10))
print(int('11', 16))
print(int('11', 8))
print(int('11', 2))
print(int('11', 36))
print(int('xyz', 36))
print(int('0o123', 0))
print(int('8388607'))
print(int('0x123', 16))
print(int('0X123', 16))
print(int('0A', 16))
print(int('0o123', 8))
print(int('0O123', 8))
print(int('0123', 8))
print(int('0b100', 2))
print(int('0B100', 2))
print(int('0100', 2))
print(int(' \t 0o12', 8))
print(int('0o12 \t ', 8))
print(int(b"12", 10))
print(int(b"12"))
def test(value, base):
try:
print(int(value, base))
except ValueError:
print('ValueError')
test('x', 0)
test('1x', 0)
test(' 1x', 0)
test(' 1' + chr(2) + ' ', 0)
test('', 0)
test(' ', 0)
test(' \t\t ', 0)
test('0x', 16)
test('0x', 0)
test('0o', 8)
test('0o', 0)
test('0b', 2)
test('0b', 0)
test('0b2', 2)
test('0o8', 8)
test('0xg', 16)
test('1 1', 16)
test('123', 37)
# check that we don't parse this as a floating point number
print(0x1e+1)
# can't convert list to int
try:
int([])
except TypeError:
print("TypeError")
|
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)
print(bool(x + xn))
# subtraction
print(x - 1)
print(x - y)
print(y - x)
print(x - x == 0)
print(bool(x - x))
# multiplication
print(x * 2)
print(x * y)
# integer division
print(x // 2)
print(y // x)
# bit inversion
print(~x)
print(~(-x))
# left shift
x = 0x10000000000000000000000
for i in range(32):
x = x << 1
print(x)
# right shift
x = 0x10000000000000000000000
for i in range(32):
x = x >> 1
print(x)
# left shift of a negative number
for i in range(8):
print(-10000000000000000000000000 << i)
print(-10000000000000000000000001 << i)
print(-10000000000000000000000002 << i)
print(-10000000000000000000000003 << i)
print(-10000000000000000000000004 << i)
# right shift of a negative number
for i in range(8):
print(-10000000000000000000000000 >> i)
print(-10000000000000000000000001 >> i)
print(-10000000000000000000000002 >> i)
print(-10000000000000000000000003 >> i)
print(-10000000000000000000000004 >> i)
# conversion from string
print(int("123456789012345678901234567890"))
print(int("-123456789012345678901234567890"))
print(int("123456789012345678901234567890abcdef", 16))
print(int("123456789012345678901234567890ABCDEF", 16))
print(int("1234567890abcdefghijklmnopqrstuvwxyz", 36))
# invalid characters in string
try:
print(int("123456789012345678901234567890abcdef"))
except ValueError:
print('ValueError');
try:
print(int("123456789012345678901234567890\x01"))
except ValueError:
print('ValueError');
# test constant integer with more than 255 chars
x = 0x84ce72aa8699df436059f052ac51b6398d2511e49631bcb7e71f89c499b9ee425dfbc13a5f6d408471b054f2655617cbbaf7937b7c80cd8865cf02c8487d30d2b0fbd8b2c4e102e16d828374bbc47b93852f212d5043c3ea720f086178ff798cc4f63f787b9c2e419efa033e7644ea7936f54462dc21a6c4580725f7f0e7d1aaaaaaa
print(x)
# test parsing ints just on threshold of small to big
# for 32 bit archs
x = 1073741823 # small
x = -1073741823 # small
x = 1073741824 # big
x = -1073741824 # big
# for nan-boxing with 47-bit small ints
print(int('0x3fffffffffff', 16)) # small
print(int('-0x3fffffffffff', 16)) # small
print(int('0x400000000000', 16)) # big
print(int('-0x400000000000', 16)) # big
# for 64 bit archs
x = 4611686018427387903 # small
x = -4611686018427387903 # small
x = 4611686018427387904 # big
x = -4611686018427387904 # big
# sys.maxsize is a constant mpz, so test it's compatible with dynamic ones
try:
import usys as sys
except ImportError:
import sys
print(sys.maxsize + 1 - 1 == sys.maxsize)
# test extraction of big int value via mp_obj_get_int_maybe
x = 1 << 70
print('a' * (x + 4 - x))
|
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 & -2345678901234567890123456789)
print(a & (-a))
print(a & (-a - 1))
print(a & (-a + 1))
# test negative on lhs
a = 123456789012345678901234567890
print(-1 & a)
print(-2 & a)
print(-2345678901234567890123456789 & a)
print((-a) & a)
print((-a) & 0xffffffff)
print((-a) & 0xffffffffffffffffffffffffffffffff)
print((-a) & 2)
print((-(1 << 70)) & 2)
# test negative on lhs and rhs
mpz = 1 << 70
a = 123456789012345678901234567890
print(-1 & (-a))
print(-2 & (-a))
print(-2345678901234567890123456789 & (-a))
print((-a) & (-a))
print((-a) & (-0xffffffff))
print((-a) & (-0xffffffffffffffffffffffffffffffff))
print((-1) & (-0xffffffffffffffffffffffffffffffff))
print((-a) & (-2))
print((-mpz) & (-2))
|
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( 26167512042587370698808974207700979337713004510730289760097826496
& 98456276326770292376138852628141531773120376436197321310863125849)
print( 21085380307304977067262070503651827226504797285572981274069266136
& 15928222825828272388778130358888206480162413547887287646273147570)
print( 40827393422334167255488276244226338235131323044408420081160772273
& 63815443187857978125545555033672525708399848575557475462799643340)
print( 5181013159871685724135944379095645225188360725917119022722046448
& 59734090450462480092384049604830976376887859531148103803093112493)
print( 283894311
& 86526825689187217371383854139783231460931720533100376593106943447)
print( 40019818573920230246248826511203818792007462193311949166285967147
& 9487909752)
# test - -
print( -97989513389222316022151446562729620153292831887555425160965597396
& -23716683549865351578586448630079789776107310103486834795830390982)
print( -53817081128841898634258263553430908085326601592682411889506742059
& -37042558948907407488299113387826240429667200950043601129661240876)
print( -26167512042587370698808974207700979337713004510730289760097826496
& -98456276326770292376138852628141531773120376436197321310863125849)
print( -21085380307304977067262070503651827226504797285572981274069266136
& -15928222825828272388778130358888206480162413547887287646273147570)
print( -40827393422334167255488276244226338235131323044408420081160772273
& -63815443187857978125545555033672525708399848575557475462799643340)
print( -5181013159871685724135944379095645225188360725917119022722046448
& -59734090450462480092384049604830976376887859531148103803093112493)
print( -283894311
& -86526825689187217371383854139783231460931720533100376593106943447)
print( -40019818573920230246248826511203818792007462193311949166285967147
& -9487909752)
|
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( -26167512042587370698808974207700979337713004510730289760097826496
& 98456276326770292376138852628141531773120376436197321310863125849)
print( -21085380307304977067262070503651827226504797285572981274069266136
& 15928222825828272388778130358888206480162413547887287646273147570)
print( -40827393422334167255488276244226338235131323044408420081160772273
& 63815443187857978125545555033672525708399848575557475462799643340)
print( -5181013159871685724135944379095645225188360725917119022722046448
& 59734090450462480092384049604830976376887859531148103803093112493)
print( -283894311
& 86526825689187217371383854139783231460931720533100376593106943447)
print( -40019818573920230246248826511203818792007462193311949166285967147
& 9487909752)
# test + -
print( 97989513389222316022151446562729620153292831887555425160965597396
& -23716683549865351578586448630079789776107310103486834795830390982)
print( 53817081128841898634258263553430908085326601592682411889506742059
& -37042558948907407488299113387826240429667200950043601129661240876)
print( 26167512042587370698808974207700979337713004510730289760097826496
& -98456276326770292376138852628141531773120376436197321310863125849)
print( 21085380307304977067262070503651827226504797285572981274069266136
& -15928222825828272388778130358888206480162413547887287646273147570)
print( 40827393422334167255488276244226338235131323044408420081160772273
& -63815443187857978125545555033672525708399848575557475462799643340)
print( 5181013159871685724135944379095645225188360725917119022722046448
& -59734090450462480092384049604830976376887859531148103803093112493)
print( 283894311
& -86526825689187217371383854139783231460931720533100376593106943447)
print( 40019818573920230246248826511203818792007462193311949166285967147
& -9487909752)
|
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
x = 0x8000000000000000
print((x + 1) // x)
x = 0x86c60128feff5330
print((x + 1) // x)
# these check edge cases where borrow overflows
print((2 ** 48 - 1) ** 2 // (2 ** 48 - 1))
print((2 ** 256 - 2 ** 32) ** 2 // (2 ** 256 - 2 ** 32))
|
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)
except OverflowError:
print('OverflowError')
# to test conversion of negative mpz to machine int
# (we know << will convert to machine int, even though it fails to do the shift)
try:
i << (-(i >> 40))
except ValueError:
print('ValueError')
try:
i // 0
except ZeroDivisionError:
print('ZeroDivisionError')
try:
i % 0
except ZeroDivisionError:
print('ZeroDivisionError')
|
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(-100000000000000000000000000001 << i)
print(-100000000000000000000000000002 << i)
print(-100000000000000000000000000003 << i)
print(-100000000000000000000000000004 << i)
print(-100000000000000000000000000000 >> i)
print(-100000000000000000000000000001 >> i)
print(-100000000000000000000000000002 >> i)
print(-100000000000000000000000000003 >> i)
print(-100000000000000000000000000004 >> i)
# shl by zero
print((1<<70) << 0)
|
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 significant one has the MSB set
x = 0x8000000000000000
print((x + 1) % x)
x = 0x86c60128feff5330
print((x + 1) % x)
|
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 * i)
print(i * -i)
print(-i * i)
print(-i * -i)
# 63-bit overflow
i = 1 << 40
print(i * i)
print(i * -i)
print(-i * i)
print(-i * -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 << 80))
print((-1 << 80) | 0)
print((-a) | (1 << 100))
print((-a) | (1 << 200))
print((-a) | a == 0)
print(bool((-a) | a))
# test + -
print(0 | (-1 << 80))
print((1 << 80) | (-1 << 80))
print(a | (-1 << 100))
print(a | (-1 << 200))
print(a | (-a) == 0)
print(bool(a | (-a)))
# test - -
print((-1 << 80) | (-1 << 80))
print((-a) | (-1 << 100))
print((-a) | (-1 << 200))
print((-a) | (-a) == 0)
print(bool((-a) | (-a)))
|
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( 26167512042587370698808974207700979337713004510730289760097826496
| 98456276326770292376138852628141531773120376436197321310863125849)
print( 21085380307304977067262070503651827226504797285572981274069266136
| 15928222825828272388778130358888206480162413547887287646273147570)
print( 40827393422334167255488276244226338235131323044408420081160772273
| 63815443187857978125545555033672525708399848575557475462799643340)
print( 5181013159871685724135944379095645225188360725917119022722046448
| 59734090450462480092384049604830976376887859531148103803093112493)
print( 283894311
| 86526825689187217371383854139783231460931720533100376593106943447)
print( 40019818573920230246248826511203818792007462193311949166285967147
| 9487909752)
# test - -
print( -97989513389222316022151446562729620153292831887555425160965597396
| -23716683549865351578586448630079789776107310103486834795830390982)
print( -53817081128841898634258263553430908085326601592682411889506742059
| -37042558948907407488299113387826240429667200950043601129661240876)
print( -26167512042587370698808974207700979337713004510730289760097826496
| -98456276326770292376138852628141531773120376436197321310863125849)
print( -21085380307304977067262070503651827226504797285572981274069266136
| -15928222825828272388778130358888206480162413547887287646273147570)
print( -40827393422334167255488276244226338235131323044408420081160772273
| -63815443187857978125545555033672525708399848575557475462799643340)
print( -5181013159871685724135944379095645225188360725917119022722046448
| -59734090450462480092384049604830976376887859531148103803093112493)
print( -283894311
| -86526825689187217371383854139783231460931720533100376593106943447)
print( -40019818573920230246248826511203818792007462193311949166285967147
| -9487909752)
|
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( -26167512042587370698808974207700979337713004510730289760097826496
| 98456276326770292376138852628141531773120376436197321310863125849)
print( -21085380307304977067262070503651827226504797285572981274069266136
| 15928222825828272388778130358888206480162413547887287646273147570)
print( -40827393422334167255488276244226338235131323044408420081160772273
| 63815443187857978125545555033672525708399848575557475462799643340)
print( -5181013159871685724135944379095645225188360725917119022722046448
| 59734090450462480092384049604830976376887859531148103803093112493)
print( -283894311
| 86526825689187217371383854139783231460931720533100376593106943447)
print( -40019818573920230246248826511203818792007462193311949166285967147
| 9487909752)
# test + -
print( 97989513389222316022151446562729620153292831887555425160965597396
| -23716683549865351578586448630079789776107310103486834795830390982)
print( 53817081128841898634258263553430908085326601592682411889506742059
| -37042558948907407488299113387826240429667200950043601129661240876)
print( 26167512042587370698808974207700979337713004510730289760097826496
| -98456276326770292376138852628141531773120376436197321310863125849)
print( 21085380307304977067262070503651827226504797285572981274069266136
| -15928222825828272388778130358888206480162413547887287646273147570)
print( 40827393422334167255488276244226338235131323044408420081160772273
| -63815443187857978125545555033672525708399848575557475462799643340)
print( 5181013159871685724135944379095645225188360725917119022722046448
| -59734090450462480092384049604830976376887859531148103803093112493)
print( 283894311
| -86526825689187217371383854139783231460931720533100376593106943447)
print( 40019818573920230246248826511203818792007462193311949166285967147
| -9487909752)
|
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))
print((-a) ^ a == 0)
print(bool((-a) ^ a))
i = -1
print(i ^ 0xffffffffffffffff) # carry overflows to higher digit
# test + -
print(0 ^ (-1 << 80))
print((1 << 80) ^ (-1 << 80))
print(a ^ (-1 << 100))
print(a ^ (-1 << 200))
print(a ^ (-a) == 0)
print(bool(a ^ (-a)))
# test - -
print((-1 << 80) ^ (-1 << 80))
print((-a) ^ (-1 << 100))
print((-a) ^ (-1 << 200))
print((-a) ^ (-a) == 0)
print(bool((-a) ^ (-a)))
|
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( 26167512042587370698808974207700979337713004510730289760097826496
^ 98456276326770292376138852628141531773120376436197321310863125849)
print( 21085380307304977067262070503651827226504797285572981274069266136
^ 15928222825828272388778130358888206480162413547887287646273147570)
print( 40827393422334167255488276244226338235131323044408420081160772273
^ 63815443187857978125545555033672525708399848575557475462799643340)
print( 5181013159871685724135944379095645225188360725917119022722046448
^ 59734090450462480092384049604830976376887859531148103803093112493)
print( 283894311
^ 86526825689187217371383854139783231460931720533100376593106943447)
print( 40019818573920230246248826511203818792007462193311949166285967147
^ 9487909752)
# test - -
print( -97989513389222316022151446562729620153292831887555425160965597396
^ -23716683549865351578586448630079789776107310103486834795830390982)
print( -53817081128841898634258263553430908085326601592682411889506742059
^ -37042558948907407488299113387826240429667200950043601129661240876)
print( -26167512042587370698808974207700979337713004510730289760097826496
^ -98456276326770292376138852628141531773120376436197321310863125849)
print( -21085380307304977067262070503651827226504797285572981274069266136
^ -15928222825828272388778130358888206480162413547887287646273147570)
print( -40827393422334167255488276244226338235131323044408420081160772273
^ -63815443187857978125545555033672525708399848575557475462799643340)
print( -5181013159871685724135944379095645225188360725917119022722046448
^ -59734090450462480092384049604830976376887859531148103803093112493)
print( -283894311
^ -86526825689187217371383854139783231460931720533100376593106943447)
print( -40019818573920230246248826511203818792007462193311949166285967147
^ -9487909752)
|
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( -26167512042587370698808974207700979337713004510730289760097826496
^ 98456276326770292376138852628141531773120376436197321310863125849)
print( -21085380307304977067262070503651827226504797285572981274069266136
^ 15928222825828272388778130358888206480162413547887287646273147570)
print( -40827393422334167255488276244226338235131323044408420081160772273
^ 63815443187857978125545555033672525708399848575557475462799643340)
print( -5181013159871685724135944379095645225188360725917119022722046448
^ 59734090450462480092384049604830976376887859531148103803093112493)
print( -283894311
^ 86526825689187217371383854139783231460931720533100376593106943447)
print( -40019818573920230246248826511203818792007462193311949166285967147
^ 9487909752)
# test + -
print( 97989513389222316022151446562729620153292831887555425160965597396
^ -23716683549865351578586448630079789776107310103486834795830390982)
print( 53817081128841898634258263553430908085326601592682411889506742059
^ -37042558948907407488299113387826240429667200950043601129661240876)
print( 26167512042587370698808974207700979337713004510730289760097826496
^ -98456276326770292376138852628141531773120376436197321310863125849)
print( 21085380307304977067262070503651827226504797285572981274069266136
^ -15928222825828272388778130358888206480162413547887287646273147570)
print( 40827393422334167255488276244226338235131323044408420081160772273
^ -63815443187857978125545555033672525708399848575557475462799643340)
print( 5181013159871685724135944379095645225188360725917119022722046448
^ -59734090450462480092384049604830976376887859531148103803093112493)
print( 283894311
^ -86526825689187217371383854139783231460931720533100376593106943447)
print( 40019818573920230246248826511203818792007462193311949166285967147
^ -9487909752)
|
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 c in cases])
# comparison of 0/-0/+0
print(long_zero == 0)
print(long_neg_zero == 0)
print(long_one - 1 == 0)
print(long_neg_one + 1 == 0)
print(long_zero < 1)
print(long_zero < -1)
print(long_zero > 1)
print(long_zero > -1)
print(long_neg_zero < 1)
print(long_neg_zero < -1)
print(long_neg_zero > 1)
print(long_neg_zero > -1)
|
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 the internal int value
print(int.from_bytes(bytes(20), "little") == 0)
print(int.from_bytes(b"\x01" + bytes(20), "little") == 1)
# big-endian conversion
print((10).to_bytes(1, "big"))
print((100).to_bytes(10, "big"))
print(int.from_bytes(b"\0\0\0\0\0\0\0\0\0\x01", "big"))
print(int.from_bytes(b"\x01\0", "big"))
# negative number of bytes should raise an error
try:
(1).to_bytes(-1, "little")
except ValueError:
print("ValueError")
|
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.from_bytes(b + bytes(10), "little") == int.from_bytes(b, "little"))
|
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)
print(-123 // 7, -123 % 7)
print(123 // -7, 123 % -7)
print(-123 // -7, -123 % -7)
# power
print(2 ** 3)
print(3 ** 4)
# won't fold so an exception can be raised at runtime
try:
1 << -1
except ValueError:
print('ValueError')
|
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) # 64-bit edge case
print(~(-0x3fffffff - 1)) # 32-bit edge case
print(~(-0x3fffffffffffffff - 1)) # 64-bit edge case
# zero big-num on rhs
print(1 + ((1 << 65) - (1 << 65)))
# negative big-num on rhs
print(1 + (-(1 << 65)))
|
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)
a *= 257
print(a)
a //= 257
print(a)
a %= b
print(a)
a ^= b
print(a)
a |= b
print(a)
a &= b
print("&=", a)
a <<= 5
print(a)
a >>= 1
print(a)
# Test referential integrity of long ints
a = 0x1ffffffff
b = a
a += 1
print(a)
print(b)
# Bitwise ops on 64-bit
a = 0x1ffffffffffffffff
b = 0x10000000000000000
print("&", a & b)
print(a | b)
print(a ^ b)
|
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, around 2^6
# there's "turning point"
print(63)
print(64)
print(65)
print(-63)
print(-64)
print(-65)
# Maximum values of small ints on 32-bit platform
print(1073741823)
# Per python semantics, lexical integer is without a sign (i.e. positive)
# and '-' is unary minus operation applied to it. That's why -1073741824
# (min two-complement's negative value) is not allowed.
print(-1073741823)
# Operations tests
# compile-time constexprs
print(1 + 3)
print(3 - 2)
print(2 * 3)
print(1 & 3)
print(1 | 2)
print(1 ^ 3)
print(+3)
print(-3)
print(~3)
a = 0x3fffff
print(a)
a *= 0x10
print(a)
a *= 0x10
print(a)
a += 0xff
print(a)
# This would overflow
#a += 1
a = -0x3fffff
print(a)
a *= 0x10
print(a)
a *= 0x10
print(a)
a -= 0xff
print(a)
# This still doesn't overflow
a -= 1
print(a)
# This would overflow
#a -= 1
# negative shifts are not allowed
try:
a << -1
except ValueError:
print("ValueError")
try:
a >> -1
except ValueError:
print("ValueError")
# Shifts to big amounts are undefined behavior in C and is CPU-specific
# These are compile-time constexprs
print(1 >> 32)
print(1 >> 64)
print(1 >> 128)
# These are runtime calcs
a = 1
print(a >> 32)
print(a >> 64)
print(a >> 128)
|
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.getvalue())
buf.flush()
print(bts.getvalue())
buf.flush()
print(bts.getvalue())
# special case when alloc is a factor of total buffer length
bts = io.BytesIO()
buf = io.BufferedWriter(bts, 1)
buf.write(b"foo")
print(bts.getvalue())
|
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.getvalue())
|
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()
print(a.getvalue())
a.seek(0)
arr = bytearray(10)
print(a.readinto(arr))
print(arr)
|
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.args[0] > 0)
|
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)
print('test', file=MyIO())
|
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")
print(a.getvalue())
a = io.StringIO("foo")
a.write("123")
print(a.getvalue())
a = io.StringIO("foo")
a.write("1234")
print(a.getvalue())
a = io.StringIO()
a.write("foo")
print(a.read())
a = io.StringIO()
print(a.tell())
a.write("foo")
print(a.tell())
a = io.StringIO()
a.close()
for f in [a.read, a.getvalue, lambda:a.write("")]:
# CPython throws for operations on closed I/O, MicroPython makes
# the underlying string empty unless MICROPY_CPYTHON_COMPAT defined
try:
f()
print("ValueError")
except ValueError:
print("ValueError")
|
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.write(b"foobar", 1, 3)
print(buf.getvalue())
buf.write(b"foobar", 1, 100)
print(buf.getvalue())
buf.write(b"foobar", 100, 100)
print(buf.getvalue())
|
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 "b")
|
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()))
except TypeError:
print('TypeError')
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:
# stop in the usual way
raise StopIteration
elif self.i == 10:
# stop with an argument
raise StopIteration(42)
elif self.i == 20:
# raise a non-stop exception
raise TypeError
elif self.i == 30:
# raise a user-defined stop iteration
print('raising MyStopIteration')
raise MyStopIteration
else:
# return the next value
self.i -= 1
return self.i
for i in myiter(5):
print(i)
for i in myiter(12):
print(i)
try:
for i in myiter(22):
print(i)
except TypeError:
print('raised TypeError')
try:
for i in myiter(5):
print(i)
raise StopIteration
except StopIteration:
print('raised StopIteration')
for i in myiter(32):
print(i)
# repeat some of the above tests but use tuple() to walk the iterator (tests mp_iternext)
print(tuple(myiter(5)))
print(tuple(myiter(12)))
print(tuple(myiter(32)))
try:
tuple(myiter(22))
except TypeError:
print('raised TypeError')
|
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 == 1:
raise StopIteration(1)
elif self.i == 2:
raise MyStopIteration
print(list(myiter(0)))
print(list(myiter(1)))
print(list(myiter(2)))
|
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")
exec("\r6")
exec("\r\n7")
print(eval("1"))
print(eval("12"))
print(eval("123"))
print(eval("1\n"))
print(eval("12\n"))
print(eval("123\n"))
print(eval("1\r"))
print(eval("12\r"))
print(eval("123\r"))
# line continuation
print(eval("'123' \\\r '456'"))
print(eval("'123' \\\n '456'"))
print(eval("'123' \\\r\n '456'"))
print(eval("'123'\\\r'456'"))
print(eval("'123'\\\n'456'"))
print(eval("'123'\\\r\n'456'"))
# backslash used to escape a line-break in a string
print('a\
b')
# lots of indentation
def a(x):
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
if x:
print(x)
a(1)
# badly formed hex escape sequences
try:
exec(r"'\x0'")
except SyntaxError:
print("SyntaxError")
try:
exec(r"b'\x0'")
except SyntaxError:
print("SyntaxError")
try:
exec(r"'\u000'")
except SyntaxError:
print("SyntaxError")
try:
exec(r"'\U0000000'")
except SyntaxError:
print("SyntaxError")
|
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])
print([2] > [1])
print([1, 0] > [1])
print([1, -1] > [1])
print([1] > [1, 0])
print([1] > [1, -1])
print([1] < [1])
print([2] < [1])
print([1] < [2])
print([1] < [1, 0])
print([1] < [1, -1])
print([1, 0] < [1])
print([1, -1] < [1])
print([1] >= [1])
print([1] >= [2])
print([2] >= [1])
print([1, 0] >= [1])
print([1, -1] >= [1])
print([1] >= [1, 0])
print([1] >= [1, -1])
print([1] <= [1])
print([2] <= [1])
print([1] <= [2])
print([1] <= [1, 0])
print([1] <= [1, -1])
print([1, 0] <= [1])
print([1, -1] <= [1])
print([] == {})
print([] != {})
print([1] == (1,))
try:
print([] < {})
except TypeError:
print("TypeError")
|
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:
print("Raised ValueError")
else:
print("Did not raise ValueError")
a = a + a
b = [0, 0, a]
print(a.index(2))
print(b.index(a))
print(a.index(2, 2))
try:
a.index(2, 2, 2)
except ValueError:
print("Raised ValueError")
else:
print("Did not raise ValueError")
# 3rd argument to index greater than length of list
print([1, 2].index(1, 0, 4))
|
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:
print('TypeError')
|
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(l)):
l.pop()
print(l)
|
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 raise IndexError
print([][1:])
print([][-1:])
try:
[][::0]
except ValueError:
print('ValueError')
|
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 = list(range(10))
print(x[-1:-1:-1])
print(x[-1:-2:-1])
print(x[-1:-11:-1])
print(x[-10:-11:-1])
print(x[:-15:-1])
# test negative indices that are out-of-bounds
print([][::-1])
print([1][::-1])
print([][0:-10:-1])
print([1][0:-10:-1])
print([][:-20:-1])
print([1][:-20:-1])
print([][-20::-1])
print([1][-20::-1])
|
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)
l = list(x)
del l[:3]
print(l)
l = list(x)
l[:-3] = [10, 20]
print(l)
l = list(x)
l[:-3] = []
print(l)
l = list(x)
del l[:-3]
print(l)
# assign a tuple
l = [1, 2, 3]
l[0:1] = (10, 11, 12)
print(l)
# RHS of slice must be an iterable
try:
[][0:1] = 123
except TypeError:
print('TypeError')
|
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] = [10, 20, 30, 40]
print(l)
# growing by using itself on RHS
l = list(range(10))
l[4:] = l
print(l)
|
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=lambda x: -x, reverse=True))
print(sorted(l, reverse=True))
l.sort(reverse=True)
print(l)
print(l == sorted(l, reverse=True))
print(sorted(l, reverse=False))
l.sort(reverse=False)
print(l)
print(l == sorted(l, reverse=False))
# test large lists (should not stack overflow)
l = list(range(200))
l.sort()
print(l[0], l[-1])
l.sort(reverse=True)
print(l[0], l[-1])
# test user-defined ordering
class A:
def __init__(self, x):
self.x = x
def __lt__(self, other):
return self.x > other.x
def __repr__(self):
return str(self.x)
l = [A(5), A(2), A(1), A(3), A(4)]
print(l)
l.sort()
print(l)
l.sort(reverse=True)
print(l)
|
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_true() and 1 and f_false())
print(not 0)
print(not False)
print(not 1)
print(not True)
print(not not 0)
print(not not 1)
|
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], m[1], m[-1])
print(list(m))
# test writing to bytes
try:
m[0] = 1
except TypeError:
print("TypeError")
try:
m[0:2] = b'00'
except TypeError:
print("TypeError")
# test writing to bytearray
b = bytearray(b)
m = memoryview(b)
m[0] = 1
print(b)
print(list(m))
# test slice
m = memoryview(b'1234')
print(list(m[1:]))
print(list(m[1:-1]))
# this tests get_buffer of memoryview
m = memoryview(bytearray(2))
print(bytearray(m))
print(list(memoryview(memoryview(b'1234')))) # read-only memoryview
a = array.array('i', [1, 2, 3, 4])
m = memoryview(a)
print(list(m))
print(list(m[1:-1]))
m[2] = 6
print(a)
# invalid attribute
try:
memoryview(b'a').noexist
except AttributeError:
print('AttributeError')
# equality
print(memoryview(b'abc') == b'abc')
print(memoryview(b'abc') != b'abc')
print(memoryview(b'abc') == b'xyz')
print(memoryview(b'abc') != b'xyz')
print(b'abc' == memoryview(b'abc'))
print(b'abc' != memoryview(b'abc'))
print(b'abc' == memoryview(b'xyz'))
print(b'abc' != memoryview(b'xyz'))
print(memoryview(b'abcdef')[2:4] == b'cd')
print(memoryview(b'abcdef')[2:4] != b'cd')
print(memoryview(b'abcdef')[2:4] == b'xy')
print(memoryview(b'abcdef')[2:4] != b'xy')
print(b'cd' == memoryview(b'abcdef')[2:4])
print(b'cd' != memoryview(b'abcdef')[2:4])
print(b'xy' == memoryview(b'abcdef')[2:4])
print(b'xy' != memoryview(b'abcdef')[2:4])
|
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(memoryview(b'\x7f\x80\x81\xff')))
print(list(memoryview(array('b', [0x7f, -0x80]))))
print(list(memoryview(array('B', [0x7f, 0x80, 0x81, 0xff]))))
print(list(memoryview(array('h', [0x7f00, -0x8000]))))
print(list(memoryview(array('H', [0x7f00, 0x8000, 0x8100, 0xffff]))))
|
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 range(100000):
[42, 42, 42, 42]
# check that the memoryview is still what we want
print(list(m))
# check that creating a memoryview of a memoryview retains the underlying data
m = None
gc.collect() # cleanup from previous test
m = memoryview(memoryview(bytearray(i for i in range(50)))[5:-5])
print(sum(m), list(m[:10]))
gc.collect()
for i in range(10):
list(range(10)) # allocate memory to overwrite any reclaimed heap
print(sum(m), list(m[:10]))
|
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(memoryview(array('i', [0x7f000000, -0x80000000]))))
print(list(memoryview(array('I', [0x7f000000, 0x80000000, 0x81000000, 0xffffffff]))))
|
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(code)).itemsize)
# 'l' varies depending on word size of the machine
print(memoryview(array('l')).itemsize in (4, 8))
# shouldn't be able to store to the itemsize attribute
try:
memoryview(b'a').itemsize = 1
except AttributeError:
print('AttributeError')
|
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 SystemExit
# test slice assignment between memoryviews
b1 = bytearray(b'1234')
b2 = bytearray(b'5678')
b3 = bytearray(b'5678')
m1 = memoryview(b1)
m2 = memoryview(b2)
m3 = memoryview(b3)
m2[1:3] = m1[0:2]
print(b2)
b3[1:3] = m1[0:2]
print(b3)
m1[2:4] = b3[1:3]
print(b1)
# invalid slice assignments
try:
m2[1:3] = b1[0:4]
except ValueError:
print("ValueError")
try:
m2[1:3] = m1[0:4]
except ValueError:
print("ValueError")
try:
m2[0:4] = m1[1:3]
except ValueError:
print("ValueError")
# test memoryview of arrays with items sized larger than 1
a1 = array.array('i', [0]*5)
m4 = memoryview(a1)
a2 = array.array('i', [3]*5)
m5 = memoryview(a2)
m4[1:3] = m5[1:3]
print(a1)
try:
m4[1:3] = m2[1:3]
except ValueError:
print("ValueError")
# invalid assignment on RHS
try:
memoryview(array.array('i'))[0:2] = b'1234'
except ValueError:
print('ValueError')
# test shift left of bytearray
b = bytearray(range(10))
mv = memoryview(b)
mv[1:] = mv[:-1]
print(b)
# test shift right of bytearray
b = bytearray(range(10))
mv = memoryview(b)
mv[:-1] = mv[1:]
print(b)
# test shift left of array
a = array.array('I', range(10))
mv = memoryview(a)
mv[1:] = mv[:-1]
print(a)
# test shift right of array
a = array.array('I', range(10))
mv = memoryview(a)
mv[:-1] = mv[1:]
print(a)
|
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, 2), T(bar=1, foo=2):
print(t)
print(t[0], t[1])
print(t.foo, t.bar)
print(len(t))
print(bool(t))
print(t + t)
print(t * 3)
print([f for f in t])
print(isinstance(t, tuple))
# Check tuple can compare equal to namedtuple with same elements
print(t == (t[0], t[1]), (t[0], t[1]) == t)
# Create using positional and keyword args
print(T(3, bar=4))
try:
t[0] = 200
except TypeError:
print("TypeError")
try:
t.bar = 200
except AttributeError:
print("AttributeError")
try:
t = T(1)
except TypeError:
print("TypeError")
try:
t = T(1, 2, 3)
except TypeError:
print("TypeError")
try:
t = T(foo=1)
except TypeError:
print("TypeError")
try:
t = T(1, foo=1)
except TypeError:
print("TypeError")
# enough args, but kw is wrong
try:
t = T(1, baz=3)
except TypeError:
print("TypeError")
# bad argument for member spec
try:
namedtuple('T', 1)
except TypeError:
print("TypeError")
# Try single string
T3 = namedtuple("TupComma", "foo bar")
t = T3(1, 2)
print(t.foo, t.bar)
# Try tuple
T4 = namedtuple("TupTuple", ("foo", "bar"))
t = T4(1, 2)
print(t.foo, t.bar)
# Try single string with comma field separator
# Not implemented so far
#T2 = namedtuple("TupComma", "foo,bar")
#t = T2(1, 2)
|
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
d = t._asdict()
print(list(d.keys()))
print(list(d.values()))
|
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.
object.__new__
except AttributeError:
print("SKIP")
raise SystemExit
class Foo:
def __new__(cls):
# Should not be called in this test
print("in __new__")
raise RuntimeError
def __init__(self):
print("in __init__")
self.attr = "something"
o = object.__new__(Foo)
#print(o)
print("Result of __new__ has .attr:", hasattr(o, "attr"))
print("Result of __new__ is already a Foo:", isinstance(o, Foo))
o.__init__()
#print(dir(o))
print("After __init__ has .attr:", hasattr(o, "attr"))
print(".attr:", o.attr)
# should only be able to call __new__ on user types
try:
object.__new__(1)
except TypeError:
print("TypeError")
try:
object.__new__(int)
except TypeError:
print("TypeError")
|
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 TypeError:
print('TypeError')
try:
1 * {}
except TypeError:
print('TypeError')
try:
1 in 1
except TypeError:
print('TypeError')
# unsupported subscription
try:
1[0] = 1
except TypeError:
print('TypeError')
try:
'a'[0] = 1
except TypeError:
print('TypeError')
try:
del 1[0]
except TypeError:
print('TypeError')
# not an iterator
try:
next(1)
except TypeError:
print('TypeError')
# must be an exception type
try:
raise 1
except TypeError:
print('TypeError')
# no such name in import
try:
from sys import youcannotimportmebecauseidontexist
except ImportError:
print('ImportError')
|
YifuLiu/AliOS-Things
|
components/py_engine/tests/basics/op_error.py
|
Python
|
apache-2.0
| 962
|