Commit 21e3de49 by Damien George

### leon: Update set of CORE_BASICS tests from mainline.

parent b9e3b9a6
 ... ... @@ -2,8 +2,13 @@ array('B', [1, 2, 3]) 3 array('I', [1, 2, 3]) 3 1 3 1 -1 2 True 0 array('i') False True False False ValueError
 import array try: import array except ImportError: import sys print("SKIP") sys.exit() a = array.array('B', [1, 2, 3]) print(a, len(a)) ... ... @@ -6,6 +11,12 @@ i = array.array('I', [1, 2, 3]) print(i, len(i)) print(a[0]) print(i[-1]) a = array.array('l', [-1]) print(len(a), a[0]) a1 = array.array('l', [1, 2, 3]) a2 = array.array('L', [1, 2, 3]) print(a2[1]) print(a1 == a2) # Empty arrays print(len(array.array('h'))) ... ... @@ -15,6 +26,10 @@ print(array.array('i')) print(bool(array.array('i'))) print(bool(array.array('i', [1]))) # containment, with incorrect type print('12' in array.array('B', b'12')) print([] in array.array('B', b'12')) # bad typecode try: array.array('X') ... ...
 # test array + array import array try: import array except ImportError: import sys print("SKIP") sys.exit() a1 = array.array('I', [1]) a2 = array.array('I', [2]) ... ...
 # test construction of array.array from different objects from array import array try: from array import array except ImportError: import sys print("SKIP") sys.exit() # tuple, list print(array('b', (1, 2))) ... ...
 from array import array try: from array import array except ImportError: import sys print("SKIP") sys.exit() # construct from something with unknown length (requires generators) print(array('i', (i for i in range(10))))
 # test construction of array.array from different objects from array import array try: from array import array except ImportError: import sys print("SKIP") sys.exit() # raw copy from bytes, bytearray print(array('h', b'12'))
 # test array('q') and array('Q') from array import array try: from array import array except ImportError: import sys print("SKIP") sys.exit() print(array('q')) print(array('Q')) ... ...
 1 2 1
 # test MicroPython-specific features of array.array try: import array except ImportError: import sys print("SKIP") sys.exit() # arrays of objects a = array.array('O') a.append(1) print(a[0]) # arrays of pointers a = array.array('P') a.append(1) print(a[0])
 4 3 2 1 0 0 1 0 0 2 1 0 0 1 0 0 3 2 1 0 0 1 0 0 2 1 0 0 1 0 0 finished
 # test basic await expression # adapted from PEP0492 async def abinary(n): print(n) if n <= 0: return 1 l = await abinary(n - 1) r = await abinary(n - 1) return l + 1 + r o = abinary(4) try: while True: o.send(None) except StopIteration: print('finished')
 wait value: 1 return from send: message from wait(1) wait got back: message from main x = 100 got StopIteration
 # test await expression import sys if sys.implementation.name == 'micropython': # uPy allows normal generators to be awaitables coroutine = lambda f: f else: import types coroutine = types.coroutine @coroutine def wait(value): print('wait value:', value) msg = yield 'message from wait(%u)' % value print('wait got back:', msg) return 10 async def f(): x = await wait(1)**2 print('x =', x) coro = f() print('return from send:', coro.send(None)) try: coro.send('message from main') except StopIteration: print('got StopIteration')
 decorator foo StopIteration
 # test async def def dec(f): print('decorator') return f # test definition with a decorator @dec async def foo(): print('foo') coro = foo() try: coro.send(None) except StopIteration: print('StopIteration')
 init aiter anext a anext b anext c anext finished
 # test basic async for execution # example taken from PEP0492 class AsyncIteratorWrapper: def __init__(self, obj): print('init') self._it = iter(obj) async def __aiter__(self): print('aiter') return self async def __anext__(self): print('anext') try: value = next(self._it) except StopIteration: raise StopAsyncIteration return value async def coro(): async for letter in AsyncIteratorWrapper('abc'): print(letter) o = coro() try: o.send(None) except StopIteration: print('finished')
 init aiter f start: 10 coro yielded: 11 coro yielded: 12 f returned: 13 anext f start: 20 coro yielded: 21 coro yielded: 22 f returned: 23 x 0 anext f start: 20 coro yielded: 21 coro yielded: 22 f returned: 23 x 1 anext f start: 20 coro yielded: 21 coro yielded: 22 f returned: 23 x 2 anext f start: 20 coro yielded: 21 coro yielded: 22 f returned: 23 x 3 anext f start: 20 coro yielded: 21 coro yielded: 22 f returned: 23 finished
 # test waiting within "async for" aiter/anext functions import sys if sys.implementation.name == 'micropython': # uPy allows normal generators to be awaitables coroutine = lambda f: f else: import types coroutine = types.coroutine @coroutine def f(x): print('f start:', x) yield x + 1 yield x + 2 return x + 3 class ARange: def __init__(self, high): print('init') self.cur = 0 self.high = high async def __aiter__(self): print('aiter') print('f returned:', await f(10)) return self async def __anext__(self): print('anext') print('f returned:', await f(20)) if self.cur < self.high: val = self.cur self.cur += 1 return val else: raise StopAsyncIteration async def coro(): async for x in ARange(4): print('x', x) o = coro() try: while True: print('coro yielded:', o.send(None)) except StopIteration: print('finished')
 enter body exit None None finished enter 1 exit error ValueError
 # test simple async with execution class AContext: async def __aenter__(self): print('enter') return 1 async def __aexit__(self, exc_type, exc, tb): print('exit', exc_type, exc) async def f(): async with AContext(): print('body') o = f() try: o.send(None) except StopIteration: print('finished') async def g(): async with AContext() as ac: print(ac) raise ValueError('error') o = g() try: o.send(None) except ValueError: print('ValueError')
 enter f start: 10 coro yielded: 11 coro yielded: 12 f returned: 13 body start f start: 30 coro yielded: 31 coro yielded: 32 body f returned: 33 body end exit None None f start: 20 coro yielded: 21 coro yielded: 22 f returned: 23 finished
 # test waiting within async with enter/exit functions import sys if sys.implementation.name == 'micropython': # uPy allows normal generators to be awaitables coroutine = lambda f: f else: import types coroutine = types.coroutine @coroutine def f(x): print('f start:', x) yield x + 1 yield x + 2 return x + 3 class AContext: async def __aenter__(self): print('enter') print('f returned:', await f(10)) async def __aexit__(self, exc_type, exc, tb): print('exit', exc_type, exc) print('f returned:', await f(20)) async def coro(): async with AContext(): print('body start') print('body f returned:', await f(30)) print('body end') o = coro() try: while True: print('coro yielded:', o.send(None)) except StopIteration: print('finished')
 ... ... @@ -4,6 +4,15 @@ import sys t = sys.implementation # It can be just a normal tuple on small ports try: t.name except AttributeError: import sys print("SKIP") sys.exit() # test printing of attrtuple print(str(t).find("version=") > 0) ... ...
 ... ... @@ -2,3 +2,4 @@ 0 1 21 AttributeError
 ... ... @@ -22,3 +22,9 @@ print(m(1)) # bound method with lots of extra args m = A().h print(m(1, 2, 3, 4, 5, 6)) # can't assign attributes to a bound method try: A().f.x = 1 except AttributeError: print('AttributeError')
 ... ... @@ -2,7 +2,3 @@ 1 1 1 123456789012345678901234567890 123456789012345678901234567890 1073741824 4611686018427387904
 ... ... @@ -4,11 +4,3 @@ print(abs(False)) print(abs(True)) print(abs(1)) print(abs(-1)) # bignum