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
# Test calling non-special method inherited from native type class mylist(list): pass l = mylist([1, 2, 3]) print(l) print([e for e in l]) class mylist2(list): def __iter__(self): return iter([10, 20, 30]) l = mylist2([1, 2, 3]) print(l) print([e for e in l])
YifuLiu/AliOS-Things
components/py_engine/tests/basics/subclass_native_specmeth.py
Python
apache-2.0
282
# Test subclassing built-in str class S(str): pass s = S('hello') print(s == 'hello') print('hello' == s) print(s == 'Hello') print('Hello' == s)
YifuLiu/AliOS-Things
components/py_engine/tests/basics/subclass_native_str.py
Python
apache-2.0
152
# test syntax errors try: exec except NameError: print("SKIP") raise SystemExit def test_syntax(code): try: exec(code) print("no SyntaxError") except IndentationError: print("IndentationError") except SyntaxError: print("SyntaxError") # non-newline after line-continuation character (lexer error) test_syntax("a \\a\n") # dedent mismatch (lexer error) test_syntax("def f():\n a\n a\n") # unclosed string (lexer error) test_syntax("'abc") # invalid (lexer error) test_syntax("!") test_syntax("$") test_syntax("`") # bad indentation (lexer error) test_syntax(" a\n") # malformed integer literal (parser error) test_syntax("123z") # input doesn't match the grammar (parser error) test_syntax('1 or 2 or') test_syntax('{1:') # can't assign to literals test_syntax("1 = 2") test_syntax("'' = 1") test_syntax("{} = 1") # can't assign to comprehension test_syntax("(i for i in a) = 1") # can't assign to function test_syntax("f() = 1") # can't assign to power test_syntax("f**2 = 1") # can't assign to power of composite test_syntax("f[0]**2 = 1") # can't have *x on RHS test_syntax("x = *x") # can't have multiple *x on LHS test_syntax("*a, *b = c") # can't do augmented assignment to tuple test_syntax("a, b += c") test_syntax("(a, b) += c") # can't do augmented assignment to list test_syntax("[a, b] += c") # non-default argument can't follow default argument test_syntax("def f(a=1, b): pass") # can't delete these things test_syntax("del f()") test_syntax("del f[0]**2") test_syntax("del (a for a in a)") # must be in a "loop" test_syntax("break") test_syntax("continue") # must be in a function test_syntax("yield") test_syntax("nonlocal a") test_syntax("await 1") # error on uPy, warning on CPy #test_syntax("def f():\n a = 1\n global a") # default except must be last test_syntax("try:\n a\nexcept:\n pass\nexcept:\n pass") # LHS of keywords must be id's test_syntax("f(1=2)") # non-keyword after keyword test_syntax("f(a=1, 2)") # doesn't error on uPy but should #test_syntax("f(1, i for i in i)") # all elements of dict/set must be pairs or singles test_syntax("{1:2, 3}") test_syntax("{1, 2:3}") # can't mix non-bytes with bytes when concatenating test_syntax("'abc' b'def'") # can't reuse same name for argument test_syntax("def f(a, a): pass") # nonlocal must exist in outer function/class scope test_syntax("def f():\n def g():\n nonlocal a") # param can't be redefined as global test_syntax('def f(x):\n global x') # param can't be redefined as nonlocal test_syntax('def f(x):\n nonlocal x') # can define variable to be both nonlocal and global test_syntax('def f():\n nonlocal x\n global x') # can't have multiple *'s test_syntax('def f(x, *a, *):\n pass') test_syntax('lambda x, *a, *: 1') # **kw must be last test_syntax('def f(x, *a, **kw, r):\n pass') test_syntax('lambda x, *a, **kw, r: 1')
YifuLiu/AliOS-Things
components/py_engine/tests/basics/syntaxerror.py
Python
apache-2.0
2,900
# With MICROPY_CPYTHON_COMPAT, the "return" statement can only appear in a # function. # Otherwise (in minimal builds), it ends execution of a module/class. try: exec except NameError: print("SKIP") raise SystemExit try: exec('return; print("this should not be executed.")') # if we get here then MICROPY_CPYTHON_COMPAT is disabled and test # should be skipped. print("SKIP") raise SystemExit except SyntaxError: print('SyntaxError')
YifuLiu/AliOS-Things
components/py_engine/tests/basics/syntaxerror_return.py
Python
apache-2.0
472
# test sys module try: import usys as sys except ImportError: import sys print(sys.__name__) print(type(sys.path)) print(type(sys.argv)) print(sys.byteorder in ('little', 'big')) try: print(sys.maxsize > 100) except AttributeError: # Effectively skip subtests print(True) try: print(sys.implementation.name in ('cpython', 'micropython')) except AttributeError: # Effectively skip subtests print(True) if hasattr(sys.implementation, 'mpy'): print(type(sys.implementation.mpy)) else: # Effectively skip subtests print(int)
YifuLiu/AliOS-Things
components/py_engine/tests/basics/sys1.py
Python
apache-2.0
570
# test sys module's exit function try: import usys as sys except ImportError: import sys try: sys.exit except AttributeError: print("SKIP") raise SystemExit try: raise SystemExit except SystemExit as e: print("SystemExit", e.args) try: sys.exit() except SystemExit as e: print("SystemExit", e.args) try: sys.exit(42) except SystemExit as e: print("SystemExit", e.args)
YifuLiu/AliOS-Things
components/py_engine/tests/basics/sys_exit.py
Python
apache-2.0
418
# test sys.getsizeof() function try: import usys as sys except ImportError: import sys try: sys.getsizeof except AttributeError: print('SKIP') raise SystemExit print(sys.getsizeof([1, 2]) >= 2) print(sys.getsizeof({1: 2}) >= 2) class A: pass print(sys.getsizeof(A()) > 0) # Only test deque if we have it try: from ucollections import deque assert sys.getsizeof(deque((), 1)) > 0 except ImportError: pass
YifuLiu/AliOS-Things
components/py_engine/tests/basics/sys_getsizeof.py
Python
apache-2.0
444
# Test true-ish value handling if not False: print("False") if not None: print("None") if not 0: print("0") if not "": print("Empty string") if "foo": print("Non-empty string") if not (): print("Empty tuple") if ("",): print("Non-empty tuple") if not []: print("Empty list") if [0]: print("Non-empty list") if not {}: print("Empty dict") if {0:0}: print("Non-empty dict")
YifuLiu/AliOS-Things
components/py_engine/tests/basics/true_value.py
Python
apache-2.0
423
# basic exceptions x = 1 try: x.a() except: print(x) try: raise IndexError except IndexError: print("caught")
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try1.py
Python
apache-2.0
127
# nested try's try: print("try 1") try: print("try 2") foo() except: print("except 2") bar() except: print("except 1") try: print("try 1") try: print("try 2") foo() except TypeError: print("except 2") bar() except NameError: print("except 1") # raised exception not contained in except tuple try: try: raise Exception except (RuntimeError, SyntaxError): print('except 2') except Exception: print('except 1') # Check that exceptions across function boundaries work as expected def func1(): try: print("try func1") func2() except NameError: print("except func1") def func2(): try: print("try func2") foo() except TypeError: print("except func2") func1()
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try2.py
Python
apache-2.0
838
# nested exceptions def f(): try: foo() except: print("except 1") try: baz() except: print("except 2") bar() try: f() except: print("f except")
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try3.py
Python
apache-2.0
226
# triple nested exceptions def f(): try: foo() except: print("except 1") try: bar() except: print("except 2") try: baz() except: print("except 3") bak() try: f() except: print("f except")
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try4.py
Python
apache-2.0
330
try: raise ValueError(534) except ValueError as e: print(type(e), e.args) # Var bound in except block is automatically deleted try: e except NameError: print("NameError")
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try_as_var.py
Python
apache-2.0
188
# test continue within exception handler def f(): lst = [1, 2, 3] for x in lst: print('a', x) try: if x == 2: raise Exception except Exception: continue print('b', x) f()
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try_continue.py
Python
apache-2.0
252
# test try-else statement # base case try: print(1) except: print(2) else: print(3) # basic case that should skip else try: print(1) raise Exception except: print(2) else: print(3) # uncaught exception should skip else try: try: print(1) raise ValueError except TypeError: print(2) else: print(3) except: print('caught') # nested within outer try try: print(1) try: print(2) raise Exception except: print(3) else: print(4) except: print(5) else: print(6) # nested within outer except, one else should be skipped try: print(1) raise Exception except: print(2) try: print(3) except: print(4) else: print(5) else: print(6) # nested within outer except, both else should be skipped try: print(1) raise Exception except: print(2) try: print(3) raise Exception except: print(4) else: print(5) else: print(6)
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try_else.py
Python
apache-2.0
1,052
# test try-else-finally statement # base case try: print(1) except: print(2) else: print(3) finally: print(4) # basic case that should skip else try: print(1) raise Exception except: print(2) else: print(3) finally: print(4) # uncaught exception should skip else try: try: print(1) raise ValueError except TypeError: print(2) else: print(3) finally: print(4) except: print('caught') # nested within outer try try: print(1) try: print(2) raise Exception except: print(3) else: print(4) finally: print(5) except: print(6) else: print(7) finally: print(8) # nested within outer except, one else should be skipped try: print(1) raise Exception except: print(2) try: print(3) except: print(4) else: print(5) finally: print(6) else: print(7) finally: print(8) # nested within outer except, both else should be skipped try: print(1) raise Exception except: print(2) try: print(3) raise Exception except: print(4) else: print(5) finally: print(6) else: print(7) finally: print(8)
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try_else_finally.py
Python
apache-2.0
1,290
# test bad exception match try: try: a except 1: pass except TypeError: print("TypeError") try: try: a except (1,): pass except TypeError: print("TypeError")
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try_error.py
Python
apache-2.0
216
# test deep unwind via break from nested try-except (22 of them) while True: print(1) try: try: try: try: try: try: try: try: try: try: try: try: try: try: try: try: try: try: try: try: try: try: print(2) break print(3) except: pass except: pass except: pass except: pass except: pass except: pass except: pass except: pass except: pass except: pass except: pass except: pass except: pass except: pass except: pass except: pass except: pass except: pass except: pass except: pass except: pass except: pass print(4)
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try_except_break.py
Python
apache-2.0
1,366
print("noexc-finally") try: print("try") finally: print("finally") print("noexc-finally-finally") try: print("try1") try: print("try2") finally: print("finally2") finally: print("finally1") print() print("noexc-finally-func-finally") def func2(): try: print("try2") finally: print("finally2") try: print("try1") func2() finally: print("finally1") print() print("exc-finally-except") try: print("try1") try: print("try2") foo() except: print("except2") finally: print("finally1") print() print("exc-finally-except-filter") try: print("try1") try: print("try2") foo() except NameError: print("except2") finally: print("finally1") print() print("exc-except-finally-finally") try: # top-level catch-all except to not fail script try: print("try1") try: print("try2") foo() finally: print("finally2") finally: print("finally1") except: print("catch-all except") print() # case where a try-except within a finally cancels the exception print("exc-finally-subexcept") try: print("try1") finally: try: print("try2") foo except: print("except2") print("finally1") print() # case where exception is raised after a finally has finished (tests that the finally doesn't run again) def func(): try: print("try") finally: print("finally") foo try: func() except: print("except")
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try_finally1.py
Python
apache-2.0
1,587
# check that the Python stack does not overflow when the finally # block itself uses more stack than the rest of the function def f1(a, b): pass def test1(): val = 1 try: raise ValueError() finally: f1(2, 2) # use some stack print(val) # check that the local variable is the same try: test1() except ValueError: pass # same as above but with 3 args instead of 2, to use an extra stack entry def f2(a, b, c): pass def test2(): val = 1 try: raise ValueError() finally: f2(2, 2, 2) # use some stack print(val) # check that the local variable is the same try: test2() except ValueError: pass
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try_finally2.py
Python
apache-2.0
685
# test break within (nested) finally # basic case with break in finally def f(): for _ in range(2): print(1) try: pass finally: print(2) break print(3) print(4) print(5) f() # where the finally swallows an exception def f(): lst = [1, 2, 3] for x in lst: print('a', x) try: raise Exception finally: print(1) break print('b', x) f() # basic nested finally with break in inner finally def f(): for i in range(2): print('iter', i) try: raise TypeError finally: print(1) try: raise ValueError finally: break print(f()) # similar to above but more nesting def f(): for i in range(2): try: raise ValueError finally: print(1) try: raise TypeError finally: print(2) try: pass finally: break print(f()) # lots of nesting def f(): for i in range(2): try: raise ValueError finally: print(1) try: raise TypeError finally: print(2) try: raise Exception finally: break print(f()) # basic case combined with try-else def f(arg): for _ in range(2): print(1) try: if arg == 1: raise ValueError elif arg == 2: raise TypeError except ValueError: print(2) else: print(3) finally: print(4) break print(5) print(6) print(7) f(0) # no exception, else should execute f(1) # exception caught, else should be skipped f(2) # exception not caught, finally swallows exception, else should be skipped
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try_finally_break.py
Python
apache-2.0
2,065
def foo(x): for i in range(x): for j in range(x): try: print(x, i, j, 1) finally: try: try: print(x, i, j, 2) finally: try: 1 / 0 finally: print(x, i, j, 3) break finally: print(x, i, j, 4) break print(foo(4))
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try_finally_break2.py
Python
apache-2.0
530
def foo(x): for i in range(x): try: pass finally: try: try: print(x, i) finally: try: 1 / 0 finally: return 42 finally: print('continue') continue print(foo(4))
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try_finally_continue.py
Python
apache-2.0
389
# Test various loop types, some may be implemented/optimized differently while True: try: break finally: print('finally 1') for i in [1, 5, 10]: try: continue finally: print('finally 2') for i in range(3): try: continue finally: print('finally 3') # Multi-level for i in range(4): print(i) try: while True: try: try: break finally: print('finally 1') finally: print('finally 2') print('here') finally: print('finnaly 3') # break from within try-finally, within for-loop for i in [1]: try: print(i) break finally: print('finally 4') # Test unwind-jump where there is nothing in the body of the try or finally. # This checks that the bytecode emitter allocates enough stack for the unwind. for i in [1]: try: break finally: pass # The following test checks that the globals dict is valid after a call to a # function that has an unwind jump. # There was a bug where an unwind jump would trash the globals dict upon return # from a function, because it used the Python-stack incorrectly. def f(): for i in [1]: try: break finally: pass def g(): global global_var f() print(global_var) global_var = 'global' g()
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try_finally_loops.py
Python
apache-2.0
1,447
def func1(): try: return "it worked" finally: print("finally 1") print(func1()) def func2(): try: return "it worked" finally: print("finally 2") def func3(): try: s = func2() return s + ", did this work?" finally: print("finally 3") print(func3()) # for loop within try-finally def f(): try: for i in [1, 2]: return i finally: print('finally') print(f()) # multiple for loops within try-finally def f(): try: for i in [1, 2]: for j in [3, 4]: return (i, j) finally: print('finally') print(f()) # multiple for loops and nested try-finally's def f(): try: for i in [1, 2]: for j in [3, 4]: try: for k in [5, 6]: for l in [7, 8]: return (i, j, k, l) finally: print('finally 2') finally: print('finally 1') print(f()) # multiple for loops that are optimised, and nested try-finally's def f(): try: for i in range(1, 3): for j in range(3, 5): try: for k in range(5, 7): for l in range(7, 9): return (i, j, k, l) finally: print('finally 2') finally: print('finally 1') print(f())
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try_finally_return.py
Python
apache-2.0
1,471
# test 'return' within the finally block # it should swallow the exception # simple case def f(): try: raise ValueError() finally: print('finally') return 0 print('got here') print(f()) # nested, return in outer def f(): try: try: raise ValueError finally: print('finally 1') print('got here') finally: print('finally 2') return 2 print('got here') print(f()) # nested, return in inner def f(): try: try: raise ValueError finally: print('finally 1') return 1 print('got here') finally: print('finally 2') print('got here') print(f()) # nested, return in inner and outer def f(): try: try: raise ValueError finally: print('finally 1') return 1 print('got here') finally: print('finally 2') return 2 print('got here') print(f()) # nested with reraise def f(): try: try: raise ValueError except: raise print('got here') finally: print('finally') return 0 print('got here') print(f()) # triple nesting with reraise def f(): try: try: try: raise ValueError except: raise except: raise finally: print('finally') return 0 print(f()) # exception when matching exception def f(): try: raise ValueError except NonExistingError: pass finally: print('finally') return 0 print(f()) # raising exception class, not instance def f(): try: raise ValueError finally: print('finally') return 0 print(f())
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try_finally_return2.py
Python
apache-2.0
1,834
# test 'return' within the finally block, with nested finally's # only inactive finally's should be executed, and only once # basic nested finally's, the print should only be executed once def f(): try: raise TypeError finally: print(1) try: raise ValueError finally: return 42 print(f()) # similar to above but more nesting def f(): try: raise ValueError finally: print(1) try: raise TypeError finally: print(2) try: pass finally: return 42 print(f()) # similar to above but even more nesting def f(): try: raise ValueError finally: print(1) try: raise TypeError finally: print(2) try: raise Exception finally: print(3) return 42 print(f()) # upon return some try's are active, some finally's are active, some inactive def f(): try: try: pass finally: print(2) return 42 finally: print(1) print(f()) # same as above but raise instead of pass def f(): try: try: raise ValueError finally: print(2) return 42 finally: print(1) print(f()) # upon return exception stack holds: active finally, inactive finally, active finally def f(): try: raise Exception finally: print(1) try: try: pass finally: print(3) return 42 finally: print(2) print(f()) # same as above but raise instead of pass in innermost try block def f(): try: raise Exception finally: print(1) try: try: raise Exception finally: print(3) return 42 finally: print(2) print(f())
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try_finally_return3.py
Python
apache-2.0
2,049
# test try-finally with return, where unwinding return has to go through # another try-finally which may affect the behaviour of the return # case where a simple try-finally executes during an unwinding return def f(x): try: try: if x: return 42 finally: try: print(1) finally: print(2) print(3) print(4) finally: print(5) print(f(0)) print(f(1)) # case where an unwinding return is replaced by another one def f(x): try: try: if x: return 42 finally: try: print(1) return 43 finally: print(2) print(3) print(4) finally: print(5) print(f(0)) print(f(1)) # case where an unwinding return is cancelled by an exception def f(x): try: try: if x: return 42 finally: try: print(1) raise ValueError # cancels any active return finally: print(2) print(3) print(4) finally: print(5) try: print(f(0)) except: print('caught') try: print(f(1)) except: print('caught') # case where an unwinding return is cancelled then resumed def f(x): try: try: if x: return 42 finally: try: print(1) raise Exception # cancels any active return except: # cancels the exception and resumes any active return print(2) print(3) print(4) finally: print(5) print(f(0)) print(f(1))
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try_finally_return4.py
Python
apache-2.0
1,761
def foo(x): for i in range(x): try: pass finally: try: try: print(x, i) finally: try: 1 / 0 finally: return 42 finally: print('return') return 43 print(foo(4))
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try_finally_return5.py
Python
apache-2.0
388
# Reraising last exception with raise w/o args def f(): try: raise ValueError("val", 3) except: raise try: f() except ValueError as e: print(repr(e)) # Can reraise only in except block try: raise except RuntimeError: print("RuntimeError")
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try_reraise.py
Python
apache-2.0
283
# Reraise not the latest occurred exception def f(): try: raise ValueError("val", 3) except: try: print(1) raise TypeError except: print(2) try: print(3) try: print(4) raise AttributeError except: print(5) pass print(6) raise except TypeError: print(7) pass print(8) print(9) # This should raise original ValueError, not the most recently occurred AttributeError raise try: f() except ValueError as e: print(repr(e))
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try_reraise2.py
Python
apache-2.0
746
# test use of return with try-except def f(): try: print(1) return except: print(2) print(3) f() def f(l, i): try: return l[i] except IndexError: print('IndexError') return -1 print(f([1], 0)) print(f([], 0))
YifuLiu/AliOS-Things
components/py_engine/tests/basics/try_return.py
Python
apache-2.0
280
# basic tuple functionality x = (1, 2, 3 * 4) print(x) try: x[0] = 4 except TypeError: print("TypeError") print(x) try: x.append(5) except AttributeError: print("AttributeError") print(x + (10, 100, 10000)) # inplace add operator x += (10, 11, 12) print(x) # construction of tuple from large iterator (tests implementation detail of uPy) print(tuple(range(20))) # unsupported unary operation try: +() except TypeError: print('TypeError') # unsupported type on RHS of add try: () + None except TypeError: print('TypeError')
YifuLiu/AliOS-Things
components/py_engine/tests/basics/tuple1.py
Python
apache-2.0
560
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((10, 0) > (1, 1)) print((10, 0) < (1, 1)) print((0, 0, 10, 0) > (0, 0, 1, 1)) print((0, 0, 10, 0) < (0, 0, 1, 1)) print(() == {}) print(() != {}) print((1,) == [1]) try: print(() < {}) except TypeError: print("TypeError")
YifuLiu/AliOS-Things
components/py_engine/tests/basics/tuple_compare.py
Python
apache-2.0
1,152
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/tuple_count.py
Python
apache-2.0
87
a = (1, 2, 3) print(a.index(1)) print(a.index(2)) print(a.index(3)) print(a.index(3, 2)) 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")
YifuLiu/AliOS-Things
components/py_engine/tests/basics/tuple_index.py
Python
apache-2.0
419
# 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 tuple a = (1, 2, 3) c = a * 3 print(a, c) # inplace multiplication a = (1, 2) a *= 2 print(a) # unsupported type on RHS try: () * None except TypeError: print('TypeError')
YifuLiu/AliOS-Things
components/py_engine/tests/basics/tuple_mult.py
Python
apache-2.0
391
# tuple slicing x = (1, 2, 3 * 4) print(x[1:]) print(x[:-1]) print(x[2:3])
YifuLiu/AliOS-Things
components/py_engine/tests/basics/tuple_slice.py
Python
apache-2.0
77
# basic types # similar test for set type is done in set_type.py print(bool) print(int) print(tuple) print(list) print(dict) print(type(bool()) == bool) print(type(int()) == int) print(type(tuple()) == tuple) print(type(list()) == list) print(type(dict()) == dict) print(type(False) == bool) print(type(0) == int) print(type(()) == tuple) print(type([]) == list) print(type({}) == dict) try: bool.foo except AttributeError: print("AttributeError")
YifuLiu/AliOS-Things
components/py_engine/tests/basics/types1.py
Python
apache-2.0
460
# Types are hashable print(hash(type) != 0) print(hash(int) != 0) print(hash(list) != 0) class Foo: pass print(hash(Foo) != 0) print(int == int) print(int != list) d = {} d[int] = list d[list] = int print(len(d))
YifuLiu/AliOS-Things
components/py_engine/tests/basics/types2.py
Python
apache-2.0
215
x = 1 print(+x) print(-x) print(~x) print(not None) print(not False) print(not True) print(not 0) print(not 1) print(not -1) print(not ()) print(not (1,)) print(not []) print(not [1,]) print(not {}) print(not {1:1}) # check user instance class A: pass print(not A()) # check user instances derived from builtins class B(int): pass print(not B()) print(True if B() else False) class C(list): pass print(not C()) print(True if C() else False) # type doesn't define unary_op class D(type): pass d = D("foo", (), {}) print(not d) print(True if d else False)
YifuLiu/AliOS-Things
components/py_engine/tests/basics/unary_op.py
Python
apache-2.0
557
# locals referenced before assignment def f1(): print(x) x = 1 def f2(): for i in range(0): print(i) print(i) def check(f): try: f() except NameError: print("NameError") check(f1) check(f2)
YifuLiu/AliOS-Things
components/py_engine/tests/basics/unboundlocal.py
Python
apache-2.0
242
# unpack sequences a, = 1, ; print(a) a, b = 2, 3 ; print(a, b) a, b, c = 1, 2, 3; print(a, b, c) a, = range(1); print(a) a, b = range(2); print(a, b) a, b, c = range(3); print(a, b, c) (a) = range(1); print(a) (a,) = range(1); print(a) (a, b) = range(2); print(a, b) (a, b, c) = range(3); print(a, b, c) (a, (b, c)) = [-1, range(2)]; print(a, b, c) # lists [] = [] [a] = range(1); print(a) [a, b] = range(2); print(a, b) [a, b, c] = range(3); print(a, b, c) # with star *a, = () ; print(a) *a, = 4, ; print(a) *a, = 5, 6 ; print(a) *a, b = 7, ; print(a, b) *a, b = 8, 9 ; print(a, b) *a, b = 10, 11, 12 ; print(a, b) a, *b = 13, ; print(a, b) a, *b = 14, 15 ; print(a, b) a, *b = 16, 17, 18 ; print(a, b) a, *b, c = 19, 20 ; print(a, b) a, *b, c = 21, 22, 23 ; print(a, b) a, *b, c = 24, 25, 26, 27 ; print(a, b) a = [28, 29] *b, = a print(a, b, a == b, a is b) [*a] = [1, 2, 3] print(a) try: a, *b, c = (30,) except ValueError: print("ValueError") # with star and generic iterator *a, = range(5) ; print(a) *a, b = range(5) ; print(a, b) *a, b, c = range(5) ; print(a, b, c) a, *b = range(5) ; print(a, b) a, *b, c = range(5) ; print(a, b, c) a, *b, c, d = range(5) ; print(a, b, c, d) a, b, *c = range(5) ; print(a, b, c) a, b, *c, d = range(5) ; print(a, b, c, d) a, b, *c, d, e = range(5) ; print(a, b, c, d, e) *a, = [x * 2 for x in [1, 2, 3, 4]] ; print(a) *a, b = [x * 2 for x in [1, 2, 3, 4]] ; print(a, b) a, *b = [x * 2 for x in [1, 2, 3, 4]] ; print(a, b) a, *b, c = [x * 2 for x in [1, 2, 3, 4]]; print(a, b, c) try: a, *b, c = range(0) except ValueError: print("ValueError") try: a, *b, c = range(1) except ValueError: print("ValueError")
YifuLiu/AliOS-Things
components/py_engine/tests/basics/unpack1.py
Python
apache-2.0
1,802
# basic while loop x = 0 while x < 2: y = 0 while y < 2: z = 0 while z < 2: z = z + 1 print(x, y, z) y = y + 1 x = x + 1
YifuLiu/AliOS-Things
components/py_engine/tests/basics/while1.py
Python
apache-2.0
182
# test while conditions which are optimised by the compiler while 0: print(0) else: print(1) while 1: print(2) break while 2: print(3) break while -1: print(4) break while False: print('a') else: print('b') while True: print('a') break while not False: print('a') break while not True: print('a') else: print('b')
YifuLiu/AliOS-Things
components/py_engine/tests/basics/while_cond.py
Python
apache-2.0
386
# test nested whiles within a try-except while 1: print(1) try: print(2) while 1: print(3) break except: print(4) print(5) break
YifuLiu/AliOS-Things
components/py_engine/tests/basics/while_nest_exc.py
Python
apache-2.0
198
class CtxMgr: def __enter__(self): print("__enter__") return self def __exit__(self, a, b, c): print("__exit__", repr(a), repr(b)) with CtxMgr() as a: print(isinstance(a, CtxMgr)) try: with CtxMgr() as a: raise ValueError except ValueError: print("ValueError") class CtxMgr2: def __enter__(self): print("__enter__") return self def __exit__(self, a, b, c): print("__exit__", repr(a), repr(b)) return True try: with CtxMgr2() as a: raise ValueError print("No ValueError2") except ValueError: print("ValueError2") # These recursive try-finally tests are attempt to get some interpretation # of last phrase in http://docs.python.org/3.4/library/dis.html#opcode-WITH_CLEANUP # "If the stack represents an exception, and the function call returns a 'true' # value, this information is "zapped" and replaced with a single WHY_SILENCED # to prevent END_FINALLY from re-raising the exception. (But non-local gotos # will still be resumed.)" print("===") with CtxMgr2() as a: try: try: raise ValueError print("No ValueError3") finally: print("finally1") finally: print("finally2") print("===") try: try: with CtxMgr2() as a: try: try: raise ValueError print("No ValueError3") finally: print("finally1") finally: print("finally2") finally: print("finally3") finally: print("finally4")
YifuLiu/AliOS-Things
components/py_engine/tests/basics/with1.py
Python
apache-2.0
1,628
class CtxMgr: def __enter__(self): print("__enter__") return self def __exit__(self, a, b, c): print("__exit__", repr(a), repr(b)) for i in range(5): print(i) with CtxMgr(): if i == 3: break
YifuLiu/AliOS-Things
components/py_engine/tests/basics/with_break.py
Python
apache-2.0
254
class CtxMgr: def __enter__(self): print("__enter__") return self def __exit__(self, a, b, c): print("__exit__", repr(a), repr(b)) for i in range(5): print(i) with CtxMgr(): if i == 3: continue
YifuLiu/AliOS-Things
components/py_engine/tests/basics/with_continue.py
Python
apache-2.0
257
# test with when context manager raises in __enter__/__exit__ class CtxMgr: def __init__(self, id): self.id = id def __enter__(self): print("__enter__", self.id) if 10 <= self.id < 20: raise Exception('enter', self.id) return self def __exit__(self, a, b, c): print("__exit__", self.id, repr(a), repr(b)) if 15 <= self.id < 25: raise Exception('exit', self.id) # no raising try: with CtxMgr(1): pass except Exception as e: print(e) # raise in enter try: with CtxMgr(10): pass except Exception as e: print(e) # raise in enter and exit try: with CtxMgr(15): pass except Exception as e: print(e) # raise in exit try: with CtxMgr(20): pass except Exception as e: print(e)
YifuLiu/AliOS-Things
components/py_engine/tests/basics/with_raise.py
Python
apache-2.0
823
class CtxMgr: def __init__(self, id): self.id = id def __enter__(self): print("__enter__", self.id) return self def __exit__(self, a, b, c): print("__exit__", self.id, repr(a), repr(b)) # simple case def foo(): with CtxMgr(1): return 4 print(foo()) # for loop within with (iterator needs removing upon return) def f(): with CtxMgr(1): for i in [1, 2]: return i print(f()) # multiple for loops within with def f(): with CtxMgr(1): for i in [1, 2]: for j in [3, 4]: return (i, j) print(f()) # multiple for loops within nested withs def f(): with CtxMgr(1): for i in [1, 2]: for j in [3, 4]: with CtxMgr(2): for k in [5, 6]: for l in [7, 8]: return (i, j, k, l) print(f()) # multiple for loops that are optimised, and nested withs def f(): with CtxMgr(1): for i in range(1, 3): for j in range(3, 5): with CtxMgr(2): for k in range(5, 7): for l in range(7, 9): return (i, j, k, l) print(f())
YifuLiu/AliOS-Things
components/py_engine/tests/basics/with_return.py
Python
apache-2.0
1,239
# cmdline: -O # test optimisation output print(__debug__) assert 0
YifuLiu/AliOS-Things
components/py_engine/tests/cmdline/cmd_optimise.py
Python
apache-2.0
67
# cmdline: -v -v -v # test printing of the parse-tree for i in (): pass a = None b = "str" c = "a very long str that will not be interned" d = b"bytes" e = b"a very long bytes that will not be interned" f = 123456789012345678901234567890 g = 123 h = f"fstring: '{b}'"
YifuLiu/AliOS-Things
components/py_engine/tests/cmdline/cmd_parsetree.py
Python
apache-2.0
273
# cmdline: -v -v # test printing of all bytecodes # fmt: off def f(): # constants a = None + False + True a = 0 a = 1000 a = -1000 # constructing data a = 1 b = (1, 2) c = [1, 2] d = {1, 2} e = {} f = {1:2} g = 'a' h = b'a' # unary/binary ops i = 1 j = 2 k = a + b l = -a m = not a m = a == b == c m = not (a == b and b == c) # attributes n = b.c b.c = n # subscript p = b[0] b[0] = p b[0] += p # slice a = b[::] # sequenc unpacking a, b = c a, *a = a # tuple swapping a, b = b, a a, b, c = c, b, a # del fast del a # globals global gl gl = a del gl # comprehensions a = (b for c in d if e) a = [b for c in d if e] a = {b:b for c in d if e} # function calls a() a(1) a(b=1) a(*b) # method calls a.b() a.b(1) a.b(c=1) a.b(*c) # jumps if a: x else: y while a: b while not a: b a = a or a # for loop for a in b: c # exceptions try: while a: break except: b finally: c while a: try: break except: pass # with with a: b # closed over variables x = 1 def closure(): nonlocal x; a = x + 1 x = 1 del x # import import a from a import b #from sys import * # tested at module scope # raise raise raise 1 # return return return 1 # function with lots of locals def f(): l1 = l2 = l3 = l4 = l5 = l6 = l7 = l8 = l9 = l10 = 1 m1 = m2 = m3 = m4 = m5 = m6 = m7 = m8 = m9 = m10 = 2 l10 + m10 # functions with default args def f(a=1): pass def f(b=2): return b + a # function which yields def f(): yield yield 1 yield from 1 # class class Class: pass # delete name del Class # load super method def f(self): super().f() # import * (needs to be in module scope) from sys import *
YifuLiu/AliOS-Things
components/py_engine/tests/cmdline/cmd_showbc.py
Python
apache-2.0
2,126
# cmdline: -v -v # test verbose output print(1)
YifuLiu/AliOS-Things
components/py_engine/tests/cmdline/cmd_verbose.py
Python
apache-2.0
48
# tests for autocompletion impo sys not_exist.  not_exist  x = '123' 1, x.isdi () i = str i.lowe ('ABC') None. 
YifuLiu/AliOS-Things
components/py_engine/tests/cmdline/repl_autocomplete.py
Python
apache-2.0
136
# basic REPL tests print(1)  2
YifuLiu/AliOS-Things
components/py_engine/tests/cmdline/repl_basic.py
Python
apache-2.0
34
# check REPL allows to continue input 1 \ + 2 '"' "'" '\'' "\"" '\'(' "\"(" print("\"(") print('\'(') print("\'(") print('\"(') 'abc' "abc" '''abc def''' """ABC DEF""" print( 1 + 2) l = [1, 2] print(l) d = {1:'one', 2:'two'} print(d[2]) def f(x): print(x)  f(3) if1=1 if1 = 2 print(if1)
YifuLiu/AliOS-Things
components/py_engine/tests/cmdline/repl_cont.py
Python
apache-2.0
288
# REPL tests of GNU-ish readline navigation # history buffer navigation 1 2 3   # input line motion t = 12 'boofar fbar'
YifuLiu/AliOS-Things
components/py_engine/tests/cmdline/repl_emacs_keys.py
Python
apache-2.0
136
# cmdline: -i -c print("test") # -c option combined with -i option results in REPL
YifuLiu/AliOS-Things
components/py_engine/tests/cmdline/repl_inspect.py
Python
apache-2.0
83
# cmdline: cmdline/repl_micropyinspect # setting MICROPYINSPECT environment variable before program exit triggers REPL
YifuLiu/AliOS-Things
components/py_engine/tests/cmdline/repl_micropyinspect.py
Python
apache-2.0
119
# word movement # backward-word, start in word 234b1 # backward-word, don't start in word 234 b1 # backward-word on start of line. if cursor is moved, this will result in a SyntaxError 1 2 + 3b+ # forward-word, start in word 1+2 12+f+3 # forward-word, don't start in word 1+ 12 3f+ # forward-word on eol. if cursor is moved, this will result in a SyntaxError 1 + 2 3f+ # kill word # backward-kill-word, start in word 100 + 45623 # backward-kill-word, don't start in word 100 + 456231 # forward-kill-word, start in word 100 + 256d3 # forward-kill-word, don't start in word 1 + 256d2 # extra move/kill shortcuts # ctrl-left 2341 # ctrl-right 123 # ctrl-w 1231
YifuLiu/AliOS-Things
components/py_engine/tests/cmdline/repl_words_move.py
Python
apache-2.0
709
""" categories: Modules,builtins description: Second argument to next() is not implemented cause: MicroPython is optimised for code space. workaround: Instead of ``val = next(it, deflt)`` use:: try: val = next(it) except StopIteration: val = deflt """ print(next(iter(range(0)), 42))
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/builtin_next_arg2.py
Python
apache-2.0
309
""" categories: Core,Classes description: Special method __del__ not implemented for user-defined classes cause: Unknown workaround: Unknown """ import gc class Foo: def __del__(self): print("__del__") f = Foo() del f gc.collect()
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/core_class_delnotimpl.py
Python
apache-2.0
248
""" categories: Core,Classes description: Method Resolution Order (MRO) is not compliant with CPython cause: Depth first non-exhaustive method resolution order workaround: Avoid complex class hierarchies with multiple inheritance and complex method overrides. Keep in mind that many languages don't support multiple inheritance at all. """ class Foo: def __str__(self): return "Foo" class C(tuple, Foo): pass t = C((1, 2, 3)) print(t)
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/core_class_mro.py
Python
apache-2.0
457
""" categories: Core,Classes description: When inheriting from multiple classes super() only calls one class cause: See :ref:`cpydiff_core_class_mro` workaround: See :ref:`cpydiff_core_class_mro` """ class A: def __init__(self): print("A.__init__") class B(A): def __init__(self): print("B.__init__") super().__init__() class C(A): def __init__(self): print("C.__init__") super().__init__() class D(B, C): def __init__(self): print("D.__init__") super().__init__() D()
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/core_class_supermultiple.py
Python
apache-2.0
551
""" categories: Core,Classes description: Calling super() getter property in subclass will return a property object, not the value cause: Unknown workaround: Unknown """ class A: @property def p(self): return {"a": 10} class AA(A): @property def p(self): return super().p a = AA() print(a.p)
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/core_class_superproperty.py
Python
apache-2.0
330
""" categories: Core description: f-strings don't support concatenation with adjacent literals if the adjacent literals contain braces cause: MicroPython is optimised for code space. workaround: Use the + operator between literal strings when either is an f-string """ x = 1 print("aa" f"{x}") print(f"{x}" "ab") print("a{}a" f"{x}") print(f"{x}" "a{}b")
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/core_fstring_concat.py
Python
apache-2.0
356
""" categories: Core description: f-strings cannot support expressions that require parsing to resolve nested braces cause: MicroPython is optimised for code space. workaround: Only use simple expressions inside f-strings """ f'{"hello {} world"}' f"{repr({})}"
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/core_fstring_parser.py
Python
apache-2.0
263
""" categories: Core description: Raw f-strings are not supported cause: MicroPython is optimised for code space. workaround: Unknown """ rf"hello"
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/core_fstring_raw.py
Python
apache-2.0
149
""" categories: Core description: f-strings don't support the !r, !s, and !a conversions cause: MicroPython is optimised for code space. workaround: Use repr(), str(), and ascii() explictly. """ class X: def __repr__(self): return "repr" def __str__(self): return "str" print(f"{X()!r}") print(f"{X()!s}")
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/core_fstring_repr.py
Python
apache-2.0
335
""" categories: Core,Functions description: Error messages for methods may display unexpected argument counts cause: MicroPython counts "self" as an argument. workaround: Interpret error messages with the information above in mind. """ try: [].append() except Exception as e: print(e)
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/core_function_argcount.py
Python
apache-2.0
293
""" categories: Core,Functions description: Function objects do not have the ``__module__`` attribute cause: MicroPython is optimized for reduced code size and RAM usage. workaround: Use ``sys.modules[function.__globals__['__name__']]`` for non-builtin modules. """ def f(): pass print(f.__module__)
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/core_function_moduleattr.py
Python
apache-2.0
308
""" categories: Core,Functions description: User-defined attributes for functions are not supported cause: MicroPython is highly optimized for memory usage. workaround: Use external dictionary, e.g. ``FUNC_X[f] = 0``. """ def f(): pass f.x = 0 print(f.x)
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/core_function_userattr.py
Python
apache-2.0
263
""" categories: Core,Generator description: Context manager __exit__() not called in a generator which does not run to completion cause: Unknown workaround: Unknown """ class foo(object): def __enter__(self): print("Enter") def __exit__(self, *args): print("Exit") def bar(x): with foo(): while True: x += 1 yield x def func(): g = bar(0) for _ in range(3): print(next(g)) func()
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/core_generator_noexit.py
Python
apache-2.0
465
""" categories: Core,import description: __all__ is unsupported in __init__.py in MicroPython. cause: Not implemented. workaround: Manually import the sub-modules directly in __init__.py using ``from . import foo, bar``. """ from modules3 import * foo.hello()
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/core_import_all.py
Python
apache-2.0
261
""" categories: Core,import description: __path__ attribute of a package has a different type (single string instead of list of strings) in MicroPython cause: MicroPython does't support namespace packages split across filesystem. Beyond that, MicroPython's import system is highly optimized for minimal memory usage. workaround: Details of import handling is inherently implementation dependent. Don't rely on such details in portable applications. """ import modules print(modules.__path__)
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/core_import_path.py
Python
apache-2.0
493
""" categories: Core,import description: Failed to load modules are still registered as loaded cause: To make module handling more efficient, it's not wrapped with exception handling. workaround: Test modules before production use; during development, use ``del sys.modules["name"]``, or just soft or hard reset the board. """ import sys try: from modules import foo except NameError as e: print(e) try: from modules import foo print("Should not get here") except NameError as e: print(e)
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/core_import_prereg.py
Python
apache-2.0
511
""" categories: Core,import description: MicroPython does't support namespace packages split across filesystem. cause: MicroPython's import system is highly optimized for simplicity, minimal memory usage, and minimal filesystem search overhead. workaround: Don't install modules belonging to the same namespace package in different directories. For MicroPython, it's recommended to have at most 3-component module search paths: for your current application, per-user (writable), system-wide (non-writable). """ import sys sys.path.append(sys.path[1] + "/modules") sys.path.append(sys.path[1] + "/modules2") import subpkg.foo import subpkg.bar print("Two modules of a split namespace package imported")
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/core_import_split_ns_pkgs.py
Python
apache-2.0
705
""" categories: Core,Runtime description: Local variables aren't included in locals() result cause: MicroPython doesn't maintain symbolic local environment, it is optimized to an array of slots. Thus, local variables can't be accessed by a name. workaround: Unknown """ def test(): val = 2 print(locals()) test()
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/core_locals.py
Python
apache-2.0
325
""" categories: Core,Runtime description: Code running in eval() function doesn't have access to local variables cause: MicroPython doesn't maintain symbolic local environment, it is optimized to an array of slots. Thus, local variables can't be accessed by a name. Effectively, ``eval(expr)`` in MicroPython is equivalent to ``eval(expr, globals(), globals())``. workaround: Unknown """ val = 1 def test(): val = 2 print(val) eval("print(val)") test()
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/core_locals_eval.py
Python
apache-2.0
469
""" categories: Modules,array description: Comparison between different typecodes not supported cause: Code size workaround: Compare individual elements """ import array array.array("b", [1, 2]) == array.array("i", [1, 2])
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/module_array_comparison.py
Python
apache-2.0
224
""" categories: Modules,array description: Overflow checking is not implemented cause: MicroPython implements implicit truncation in order to reduce code size and execution time workaround: If CPython compatibility is needed then mask the value explicitly """ import array a = array.array("b", [257]) print(a)
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/module_array_constructor.py
Python
apache-2.0
311
print("foo") xxx
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/modules/foo.py
Python
apache-2.0
17
__all__ = ["foo"]
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/modules3/__init__.py
Python
apache-2.0
18
def hello(): print("hello")
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/modules3/foo.py
Python
apache-2.0
32
""" categories: Modules,array description: Looking for integer not implemented cause: Unknown workaround: Unknown """ import array print(1 in array.array("B", b"12"))
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/modules_array_containment.py
Python
apache-2.0
168
""" categories: Modules,array description: Array deletion not implemented cause: Unknown workaround: Unknown """ import array a = array.array("b", (1, 2, 3)) del a[1] print(a)
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/modules_array_deletion.py
Python
apache-2.0
177
""" categories: Modules,array description: Subscript with step != 1 is not yet implemented cause: Unknown workaround: Unknown """ import array a = array.array("b", (1, 2, 3)) print(a[3:2:2])
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/modules_array_subscrstep.py
Python
apache-2.0
192
""" categories: Modules,deque description: Deque not implemented cause: Unknown workaround: Use regular lists. micropython-lib has implementation of collections.deque. """ import collections D = collections.deque() print(D)
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/modules_deque.py
Python
apache-2.0
225
""" categories: Modules,json description: JSON module does not throw exception when object is not serialisable cause: Unknown workaround: Unknown """ import json a = bytes(x for x in range(256)) try: z = json.dumps(a) x = json.loads(z) print("Should not get here") except TypeError: print("TypeError")
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/modules_json_nonserializable.py
Python
apache-2.0
319
""" categories: Modules,os description: ``environ`` attribute is not implemented cause: Unknown workaround: Use ``getenv``, ``putenv`` and ``unsetenv`` """ import os try: print(os.environ.get("NEW_VARIABLE")) os.environ["NEW_VARIABLE"] = "VALUE" print(os.environ["NEW_VARIABLE"]) except AttributeError: print("should not get here") print(os.getenv("NEW_VARIABLE")) os.putenv("NEW_VARIABLE", "VALUE") print(os.getenv("NEW_VARIABLE"))
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/modules_os_environ.py
Python
apache-2.0
462
""" categories: Modules,os description: ``getenv`` returns actual value instead of cached value cause: The ``environ`` attribute is not implemented workaround: Unknown """ import os print(os.getenv("NEW_VARIABLE")) os.putenv("NEW_VARIABLE", "VALUE") print(os.getenv("NEW_VARIABLE"))
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/modules_os_getenv.py
Python
apache-2.0
284
""" categories: Modules,os description: ``getenv`` only allows one argument cause: Unknown workaround: Test that the return value is ``None`` """ import os try: print(os.getenv("NEW_VARIABLE", "DEFAULT")) except TypeError: print("should not get here") # this assumes NEW_VARIABLE is never an empty variable print(os.getenv("NEW_VARIABLE") or "DEFAULT")
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/modules_os_getenv_argcount.py
Python
apache-2.0
370
""" categories: Modules,random description: ``getrandbits`` method can only return a maximum of 32 bits at a time. cause: PRNG's internal state is only 32bits so it can only return a maximum of 32 bits of data at a time. workaround: If you need a number that has more than 32 bits then utilize the random module from micropython-lib. """ import random x = random.getrandbits(64) print("{}".format(x))
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/modules_random_getrandbits.py
Python
apache-2.0
404
""" categories: Modules,random description: ``randint`` method can only return an integer that is at most the native word size. cause: PRNG is only able to generate 32 bits of state at a time. The result is then cast into a native sized int instead of a full int object. workaround: If you need integers larger than native wordsize use the random module from micropython-lib. """ import random x = random.randint(2 ** 128 - 1, 2 ** 128) print("x={}".format(x))
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/modules_random_randint.py
Python
apache-2.0
464
""" categories: Modules,struct description: Struct pack with too few args, not checked by uPy cause: Unknown workaround: Unknown """ import struct try: print(struct.pack("bb", 1)) print("Should not get here") except: print("struct.error")
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/modules_struct_fewargs.py
Python
apache-2.0
252
""" categories: Modules,struct description: Struct pack with too many args, not checked by uPy cause: Unknown workaround: Unknown """ import struct try: print(struct.pack("bb", 1, 2, 3)) print("Should not get here") except: print("struct.error")
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/modules_struct_manyargs.py
Python
apache-2.0
259
""" categories: Modules,struct description: Struct pack with whitespace in format, whitespace ignored by CPython, error on uPy cause: MicroPython is optimised for code size. workaround: Don't use spaces in format strings. """ import struct try: print(struct.pack("b b", 1, 2)) print("Should have worked") except: print("struct.error")
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/modules_struct_whitespace_in_format.py
Python
apache-2.0
348
""" categories: Modules,sys description: Overriding sys.stdin, sys.stdout and sys.stderr not possible cause: They are stored in read-only memory. workaround: Unknown """ import sys sys.stdin = None print(sys.stdin)
YifuLiu/AliOS-Things
components/py_engine/tests/cpydiff/modules_sys_stdassign.py
Python
apache-2.0
216