summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSabas <sabasjimenez@gmail.com>2020-04-09 17:05:11 -0500
committerGitHub <noreply@github.com>2020-04-09 17:05:11 -0500
commitcac4fb0318015f344e191e3077e99ffdce347515 (patch)
treed96c5d7010563f9db3f4e5fb2f981e5bb75c6345 /tests
parent2a47623283911c8ebac6152560affc8c419dbddf (diff)
parentb3b8b5ca71dc58415083180d04b407185514c216 (diff)
Merge branch 'master' into nfc_copy
Diffstat (limited to 'tests')
-rw-r--r--tests/basics/dict1.py2
-rw-r--r--tests/basics/exception1.py1
-rw-r--r--tests/basics/gen_yield_from.py.exp14
-rw-r--r--tests/basics/gen_yield_from_close.py.exp20
-rw-r--r--tests/basics/gen_yield_from_throw.py.exp6
-rw-r--r--tests/basics/generator_close.py.exp10
-rw-r--r--tests/basics/generator_return.py2
-rw-r--r--tests/basics/int_big_error.py4
-rw-r--r--tests/basics/int_bytes.py12
-rw-r--r--tests/basics/int_bytes_intbig.py8
-rw-r--r--tests/basics/int_longint_bytes.py1
-rw-r--r--tests/basics/parser.py.exp3
-rw-r--r--tests/basics/python34.py6
-rw-r--r--tests/basics/python34.py.exp1
-rw-r--r--tests/basics/set_pop.py2
-rw-r--r--tests/basics/string_pep498_fstring.py113
-rw-r--r--tests/basics/string_pep498_fstring.py.exp0
-rw-r--r--tests/basics/subclass_native3.py4
-rw-r--r--tests/basics/try_as_var.py2
-rw-r--r--tests/cmdline/cmd_parsetree.py.exp4
-rw-r--r--tests/import/import_override.py.exp2
-rw-r--r--tests/misc/sys_exc_info.py2
-rwxr-xr-xtests/run-tests2
23 files changed, 205 insertions, 16 deletions
diff --git a/tests/basics/dict1.py b/tests/basics/dict1.py
index 20fa9def3..0cec51173 100644
--- a/tests/basics/dict1.py
+++ b/tests/basics/dict1.py
@@ -27,7 +27,7 @@ print({1:1} == {2:1})
try:
{}[0]
except KeyError as er:
- print('KeyError', er, repr(er), er.args)
+ print('KeyError', er, er.args)
# unsupported unary op
try:
diff --git a/tests/basics/exception1.py b/tests/basics/exception1.py
index 739dd3275..d83764cb9 100644
--- a/tests/basics/exception1.py
+++ b/tests/basics/exception1.py
@@ -1,7 +1,6 @@
print(repr(IndexError()))
print(str(IndexError()))
-print(repr(IndexError("foo")))
print(str(IndexError("foo")))
a = IndexError(1, "test", [100, 200])
diff --git a/tests/basics/gen_yield_from.py.exp b/tests/basics/gen_yield_from.py.exp
new file mode 100644
index 000000000..507f2b9ca
--- /dev/null
+++ b/tests/basics/gen_yield_from.py.exp
@@ -0,0 +1,14 @@
+here1
+3
+here2
+[1, 2]
+here1
+None
+here2
+[1, 2]
+here1
+123
+here2
+[1, 2]
+444
+[0, 1, 2]
diff --git a/tests/basics/gen_yield_from_close.py.exp b/tests/basics/gen_yield_from_close.py.exp
new file mode 100644
index 000000000..a44d1353d
--- /dev/null
+++ b/tests/basics/gen_yield_from_close.py.exp
@@ -0,0 +1,20 @@
+-1
+1
+StopIteration
+-1
+1
+2
+leaf caught GeneratorExit and swallowed it
+delegating caught GeneratorExit
+StopIteration
+-1
+1
+2
+leaf caught GeneratorExit and raised StopIteration instead
+delegating caught GeneratorExit
+StopIteration
+123
+RuntimeError
+0
+1
+close
diff --git a/tests/basics/gen_yield_from_throw.py.exp b/tests/basics/gen_yield_from_throw.py.exp
new file mode 100644
index 000000000..6ce97ad86
--- /dev/null
+++ b/tests/basics/gen_yield_from_throw.py.exp
@@ -0,0 +1,6 @@
+1
+got ValueError from upstream!
+str1
+got TypeError from downstream!
+123
+got StopIteration from downstream!
diff --git a/tests/basics/generator_close.py.exp b/tests/basics/generator_close.py.exp
new file mode 100644
index 000000000..fcd583935
--- /dev/null
+++ b/tests/basics/generator_close.py.exp
@@ -0,0 +1,10 @@
+None
+StopIteration
+1
+None
+StopIteration
+[1, 2]
+None
+StopIteration
+None
+ValueError
diff --git a/tests/basics/generator_return.py b/tests/basics/generator_return.py
index a3ac88575..5814ce837 100644
--- a/tests/basics/generator_return.py
+++ b/tests/basics/generator_return.py
@@ -7,4 +7,4 @@ print(next(g))
try:
print(next(g))
except StopIteration as e:
- print(repr(e))
+ print(type(e), e.args)
diff --git a/tests/basics/int_big_error.py b/tests/basics/int_big_error.py
index e036525d1..79809aef1 100644
--- a/tests/basics/int_big_error.py
+++ b/tests/basics/int_big_error.py
@@ -17,9 +17,9 @@ try:
except TypeError:
print("TypeError")
-# overflow because rhs of >> is being converted to machine int
+# overflow because arg of bytearray is being converted to machine int
try:
- 1 >> i
+ bytearray(i)
except OverflowError:
print('OverflowError')
diff --git a/tests/basics/int_bytes.py b/tests/basics/int_bytes.py
index aaf440081..d42afac1f 100644
--- a/tests/basics/int_bytes.py
+++ b/tests/basics/int_bytes.py
@@ -1,8 +1,11 @@
print((10).to_bytes(1, "little"))
+print((-10).to_bytes(1, "little", signed=True))
# Test fitting in length that's not a power of two.
print((0x10000).to_bytes(3, 'little'))
print((111111).to_bytes(4, "little"))
+print((-111111).to_bytes(4, "little", signed=True))
print((100).to_bytes(10, "little"))
+print((-100).to_bytes(10, "little", signed=True))
# check that extra zero bytes don't change the internal int value
print(int.from_bytes(bytes(20), "little") == 0)
@@ -10,7 +13,9 @@ print(int.from_bytes(b"\x01" + bytes(20), "little") == 1)
# big-endian conversion
print((10).to_bytes(1, "big"))
+print((-10).to_bytes(1, "big", signed=True))
print((100).to_bytes(10, "big"))
+print((-100).to_bytes(10, "big", signed=True))
print(int.from_bytes(b"\0\0\0\0\0\0\0\0\0\x01", "big"))
print(int.from_bytes(b"\x01\0", "big"))
@@ -26,8 +31,13 @@ try:
except OverflowError:
print("OverflowError")
-# negative numbers should raise an error
+# negative numbers should raise an error if signed=False
try:
(-256).to_bytes(2, "little")
except OverflowError:
print("OverflowError")
+
+try:
+ (-256).to_bytes(2, "little", signed=False)
+except OverflowError:
+ print("OverflowError")
diff --git a/tests/basics/int_bytes_intbig.py b/tests/basics/int_bytes_intbig.py
index b19c8ae53..a9f069902 100644
--- a/tests/basics/int_bytes_intbig.py
+++ b/tests/basics/int_bytes_intbig.py
@@ -2,7 +2,9 @@ import skip_if
skip_if.no_bigint()
print((2**64).to_bytes(9, "little"))
+print((-2**64).to_bytes(9, "little", signed=True))
print((2**64).to_bytes(9, "big"))
+print((-2**64).to_bytes(9, "big", signed=True))
b = bytes(range(20))
@@ -22,8 +24,12 @@ try:
except OverflowError:
print("OverflowError")
-# negative numbers should raise an error
+# negative numbers should raise an error if signed=False
try:
(-2**64).to_bytes(9, "little")
except OverflowError:
print("OverflowError")
+try:
+ (-2**64).to_bytes(9, "little", signed=False)
+except OverflowError:
+ print("OverflowError")
diff --git a/tests/basics/int_longint_bytes.py b/tests/basics/int_longint_bytes.py
index 42e445f53..90cbff2f8 100644
--- a/tests/basics/int_longint_bytes.py
+++ b/tests/basics/int_longint_bytes.py
@@ -3,6 +3,7 @@ import skip_if
skip_if.no_bigint()
print((2**64).to_bytes(9, "little"))
+print((-2**64).to_bytes(9, "little", signed=True))
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"))
diff --git a/tests/basics/parser.py.exp b/tests/basics/parser.py.exp
new file mode 100644
index 000000000..4d9886a09
--- /dev/null
+++ b/tests/basics/parser.py.exp
@@ -0,0 +1,3 @@
+SyntaxError
+SyntaxError
+SyntaxError
diff --git a/tests/basics/python34.py b/tests/basics/python34.py
index 36531f11c..4030db143 100644
--- a/tests/basics/python34.py
+++ b/tests/basics/python34.py
@@ -1,4 +1,4 @@
-# tests that differ when running under Python 3.4 vs 3.5/3.6
+# tests that differ when running under Python 3.4 vs 3.5/3.6/3.7
try:
exec
@@ -36,3 +36,7 @@ test_syntax("del ()") # can't delete empty tuple (in 3.6 we can)
import sys
print(sys.version[:3])
print(sys.version_info[0], sys.version_info[1])
+
+# from basics/exception1.py
+# in 3.7 no comma is printed if there is only 1 arg (in 3.4-3.6 one is printed)
+print(repr(IndexError("foo")))
diff --git a/tests/basics/python34.py.exp b/tests/basics/python34.py.exp
index 590fc364f..848017130 100644
--- a/tests/basics/python34.py.exp
+++ b/tests/basics/python34.py.exp
@@ -11,3 +11,4 @@ SyntaxError
SyntaxError
3.4
3 4
+IndexError('foo',)
diff --git a/tests/basics/set_pop.py b/tests/basics/set_pop.py
index 5e1196c9f..e951ca593 100644
--- a/tests/basics/set_pop.py
+++ b/tests/basics/set_pop.py
@@ -15,4 +15,4 @@ while s:
print(s.pop()) # last pop() should trigger the optimisation
for i in range(N):
s.add(i) # check that we can add the numbers back to the set
-print(list(s))
+print(sorted(s))
diff --git a/tests/basics/string_pep498_fstring.py b/tests/basics/string_pep498_fstring.py
new file mode 100644
index 000000000..c645a730c
--- /dev/null
+++ b/tests/basics/string_pep498_fstring.py
@@ -0,0 +1,113 @@
+# Tests against https://www.python.org/dev/peps/pep-0498/
+
+assert f'no interpolation' == 'no interpolation'
+assert f"no interpolation" == 'no interpolation'
+
+# Quoth the PEP:
+# Backslashes may not appear anywhere within expressions. Comments, using the
+# '#' character, are not allowed inside an expression
+#
+# CPython (3.7.4 on Linux) raises a SyntaxError here:
+# >>> f'{#}'
+# File "<stdin>", line 1
+# SyntaxError: f-string expression part cannot include '#'
+# >>> f'{\}'
+# File "<stdin>", line 1
+# SyntaxError: f-string expression part cannot include a backslash
+# >>> f'{\\}'
+# File "<stdin>", line 1
+# SyntaxError: f-string expression part cannot include a backslash
+# >>> f'{\#}'
+# File "<stdin>", line 1
+# SyntaxError: f-string expression part cannot include a backslash
+
+# Backslashes and comments allowed outside expression
+assert f"\\" == "\\"
+assert f'#' == '#'
+
+## But not inside
+try:
+ eval("f'{\}'")
+except SyntaxError:
+ pass
+else:
+ raise AssertionError('f-string with backslash in expression did not raise SyntaxError')
+
+try:
+ eval("f'{#}'")
+except SyntaxError:
+ pass
+else:
+ raise AssertionError('f-string with \'#\' in expression did not raise SyntaxError')
+
+# Quoth the PEP:
+# While scanning the string for expressions, any doubled braces '{{' or '}}'
+# inside literal portions of an f-string are replaced by the corresponding
+# single brace. Doubled literal opening braces do not signify the start of an
+# expression. A single closing curly brace '}' in the literal portion of a
+# string is an error: literal closing curly braces must be doubled '}}' in
+# order to represent a single closing brace.
+#
+# CPython (3.7.4 on Linux) raises a SyntaxError for the last case:
+# >>> f'{{}'
+# File "<stdin>", line 1
+# SyntaxError: f-string: single '}' is not allowed
+
+assert f'{{}}' == '{}'
+
+try:
+ eval("f'{{}'")
+except ValueError:
+ pass
+else:
+ raise RuntimeError('Expected ValueError for invalid f-string literal bracing')
+
+x = 1
+assert f'{x}' == '1'
+
+# Quoth the PEP:
+# The expressions that are extracted from the string are evaluated in the
+# context where the f-string appeared. This means the expression has full
+# access to local and global variables. Any valid Python expression can be
+# used, including function and method calls. Because the f-strings are
+# evaluated where the string appears in the source code, there is no additional
+# expressiveness available with f-strings. There are also no additional
+# security concerns: you could have also just written the same expression, not
+# inside of an f-string:
+
+def foo():
+ return 20
+
+assert f'result={foo()}' == 'result=20'
+assert f'result={foo()}' == 'result={}'.format(foo())
+assert f'result={foo()}' == 'result={result}'.format(result=foo())
+
+
+# Quoth the PEP:
+# Adjacent f-strings and regular strings are concatenated. Regular strings are
+# concatenated at compile time, and f-strings are concatenated at run time. For
+# example, the expression:
+#
+# >>> x = 10
+# >>> y = 'hi'
+# >>> 'a' 'b' f'{x}' '{c}' f'str<{y:^4}>' 'd' 'e'
+#
+# yields the value: 'ab10{c}str< hi >de'
+#
+# Because strings are concatenated at lexer time rather than parser time in
+# MicroPython for mostly RAM efficiency reasons (see
+# https://github.com/micropython/micropython/commit/534b7c368dc2af7720f3aaed0c936ef46d773957),
+# and because f-strings here are implemented as a syntax translation
+# (f'{something}' => '{}'.format(something)), this particular functionality is unimplemented,
+# and in the above example, the '{c}' portion will trigger a KeyError on String.format()
+
+x = 10
+y = 'hi'
+assert (f'h' f'i') == 'hi'
+#assert (f'h' 'i') == 'hi'
+#assert ('h' f'i') == 'hi'
+assert f'{x:^4}' == ' 10 '
+#assert ('a' 'b' f'{x}' f'str<{y:^4}>' 'd' 'e') == 'ab10str< hi >de'
+
+# Other tests
+assert f'{{{4*10}}}' == '{40}'
diff --git a/tests/basics/string_pep498_fstring.py.exp b/tests/basics/string_pep498_fstring.py.exp
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/tests/basics/string_pep498_fstring.py.exp
diff --git a/tests/basics/subclass_native3.py b/tests/basics/subclass_native3.py
index bd99ab0d6..6745b77bb 100644
--- a/tests/basics/subclass_native3.py
+++ b/tests/basics/subclass_native3.py
@@ -7,12 +7,12 @@ print(repr(e))
print(e.args)
try:
- raise MyExc("Some error")
+ raise MyExc("Some error", 1)
except MyExc as e:
print("Caught exception:", repr(e))
try:
- raise MyExc("Some error2")
+ raise MyExc("Some error2", 2)
except Exception as e:
print("Caught exception:", repr(e))
diff --git a/tests/basics/try_as_var.py b/tests/basics/try_as_var.py
index 0a92f1cae..4f02f9c10 100644
--- a/tests/basics/try_as_var.py
+++ b/tests/basics/try_as_var.py
@@ -1,7 +1,7 @@
try:
raise ValueError(534)
except ValueError as e:
- print(repr(e))
+ print(type(e), e.args)
# Var bound in except block is automatically deleted
try:
diff --git a/tests/cmdline/cmd_parsetree.py.exp b/tests/cmdline/cmd_parsetree.py.exp
index 12a1bfbe9..c06ce4e31 100644
--- a/tests/cmdline/cmd_parsetree.py.exp
+++ b/tests/cmdline/cmd_parsetree.py.exp
@@ -1,6 +1,6 @@
----------------
[ 4] rule(1) (n=9)
- tok(4)
+ tok(10)
[ 4] rule(22) (n=4)
id(i)
[ 4] rule(44) (n=1)
@@ -9,7 +9,7 @@
NULL
[ 6] rule(5) (n=2)
id(a)
- tok(14)
+ tok(20)
[ 7] rule(5) (n=2)
id(b)
str(str)
diff --git a/tests/import/import_override.py.exp b/tests/import/import_override.py.exp
new file mode 100644
index 000000000..365248da6
--- /dev/null
+++ b/tests/import/import_override.py.exp
@@ -0,0 +1,2 @@
+import import1b None 0
+456
diff --git a/tests/misc/sys_exc_info.py b/tests/misc/sys_exc_info.py
index 4bb2c61e8..bf9438e46 100644
--- a/tests/misc/sys_exc_info.py
+++ b/tests/misc/sys_exc_info.py
@@ -9,7 +9,7 @@ def f():
print(sys.exc_info()[0:2])
try:
- 1/0
+ raise ValueError('value', 123)
except:
print(sys.exc_info()[0:2])
f()
diff --git a/tests/run-tests b/tests/run-tests
index 94e1591e8..f6c727a8a 100755
--- a/tests/run-tests
+++ b/tests/run-tests
@@ -579,7 +579,7 @@ the last matching regex is used:
# run PC tests
test_dirs = (
'basics', 'micropython', 'float', 'import', 'io', 'misc',
- 'stress', 'unicode', 'extmod', 'unix', 'cmdline',
+ 'stress', 'unicode', 'extmod', '../extmod/ulab/tests', 'unix', 'cmdline',
)
else:
# run tests from these directories