micropython: add micropython component
This commit is contained in:
@@ -0,0 +1 @@
|
||||
Привет
|
@@ -0,0 +1,2 @@
|
||||
aαbβcγdδ
|
||||
ぁ🙐
|
4
components/language/micropython/tests/unicode/file1.py
Normal file
4
components/language/micropython/tests/unicode/file1.py
Normal file
@@ -0,0 +1,4 @@
|
||||
f = open("unicode/data/utf-8_1.txt", encoding="utf-8")
|
||||
l = f.readline()
|
||||
print(l)
|
||||
print(len(l))
|
28
components/language/micropython/tests/unicode/file2.py
Normal file
28
components/language/micropython/tests/unicode/file2.py
Normal file
@@ -0,0 +1,28 @@
|
||||
# test reading a given number of characters
|
||||
|
||||
|
||||
def do(mode):
|
||||
if mode == "rb":
|
||||
enc = None
|
||||
else:
|
||||
enc = "utf-8"
|
||||
f = open("unicode/data/utf-8_2.txt", mode=mode, encoding=enc)
|
||||
print(f.read(1))
|
||||
print(f.read(1))
|
||||
print(f.read(2))
|
||||
print(f.read(4))
|
||||
|
||||
# skip to end of line
|
||||
f.readline()
|
||||
|
||||
# check 3-byte utf-8 char
|
||||
print(f.read(1 if mode == "rt" else 3))
|
||||
|
||||
# check 4-byte utf-8 char
|
||||
print(f.read(1 if mode == "rt" else 4))
|
||||
|
||||
f.close()
|
||||
|
||||
|
||||
do("rb")
|
||||
do("rt")
|
53
components/language/micropython/tests/unicode/unicode.py
Normal file
53
components/language/micropython/tests/unicode/unicode.py
Normal file
@@ -0,0 +1,53 @@
|
||||
# Test a UTF-8 encoded literal
|
||||
s = "asdf©qwer"
|
||||
for i in range(len(s)):
|
||||
print("s[%d]: %s %X" % (i, s[i], ord(s[i])))
|
||||
|
||||
# Test all three forms of Unicode escape, and
|
||||
# all blocks of UTF-8 byte patterns
|
||||
s = "a\xA9\xFF\u0123\u0800\uFFEE\U0001F44C"
|
||||
for i in range(-len(s), len(s)):
|
||||
print("s[%d]: %s %X" % (i, s[i], ord(s[i])))
|
||||
print("s[:%d]: %d chars, '%s'" % (i, len(s[:i]), s[:i]))
|
||||
for j in range(i, len(s)):
|
||||
print("s[%d:%d]: %d chars, '%s'" % (i, j, len(s[i:j]), s[i:j]))
|
||||
print("s[%d:]: %d chars, '%s'" % (i, len(s[i:]), s[i:]))
|
||||
|
||||
# Test UTF-8 encode and decode
|
||||
enc = s.encode()
|
||||
print(enc, enc.decode() == s)
|
||||
|
||||
# printing of unicode chars using repr
|
||||
# NOTE: for some characters (eg \u10ff) we differ to CPython
|
||||
print(repr("a\uffff"))
|
||||
print(repr("a\U0001ffff"))
|
||||
|
||||
# test invalid escape code
|
||||
try:
|
||||
eval('"\\U00110000"')
|
||||
except SyntaxError:
|
||||
print("SyntaxError")
|
||||
|
||||
# test unicode string given to int
|
||||
try:
|
||||
int("\u0200")
|
||||
except ValueError:
|
||||
print("ValueError")
|
||||
|
||||
# test invalid UTF-8 string
|
||||
try:
|
||||
str(b"ab\xa1", "utf8")
|
||||
except UnicodeError:
|
||||
print("UnicodeError")
|
||||
try:
|
||||
str(b"ab\xf8", "utf8")
|
||||
except UnicodeError:
|
||||
print("UnicodeError")
|
||||
try:
|
||||
str(bytearray(b"ab\xc0a"), "utf8")
|
||||
except UnicodeError:
|
||||
print("UnicodeError")
|
||||
try:
|
||||
str(b"\xf0\xe0\xed\xe8", "utf8")
|
||||
except UnicodeError:
|
||||
print("UnicodeError")
|
@@ -0,0 +1,5 @@
|
||||
# test builtin chr with unicode characters
|
||||
|
||||
print(chr(945))
|
||||
print(chr(0x800))
|
||||
print(chr(0x10000))
|
32
components/language/micropython/tests/unicode/unicode_id.py
Normal file
32
components/language/micropython/tests/unicode/unicode_id.py
Normal file
@@ -0,0 +1,32 @@
|
||||
# test unicode in identifiers
|
||||
|
||||
# comment
|
||||
# αβγδϵφζ
|
||||
|
||||
# global identifiers
|
||||
α = 1
|
||||
αβγ = 2
|
||||
bβ = 3
|
||||
βb = 4
|
||||
print(α, αβγ, bβ, βb)
|
||||
|
||||
# function, argument, local identifiers
|
||||
def α(β, γ):
|
||||
δ = β + γ
|
||||
print(β, γ, δ)
|
||||
|
||||
|
||||
α(1, 2)
|
||||
|
||||
# class, method identifiers
|
||||
class φ:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def δ(self, ϵ):
|
||||
print(ϵ)
|
||||
|
||||
|
||||
zζzζz = φ()
|
||||
if hasattr(zζzζz, "δ"):
|
||||
zζzζz.δ(ϵ=123)
|
@@ -0,0 +1,6 @@
|
||||
print("Привет".find("т"))
|
||||
print("Привет".find("П"))
|
||||
print("Привет".rfind("т"))
|
||||
print("Привет".rfind("П"))
|
||||
print("Привет".index("т"))
|
||||
print("Привет".index("П"))
|
@@ -0,0 +1,4 @@
|
||||
for c in "Hello":
|
||||
print(c)
|
||||
for c in "Привет":
|
||||
print(c)
|
@@ -0,0 +1,3 @@
|
||||
# test builtin ord with unicode characters
|
||||
|
||||
print(ord("α"))
|
@@ -0,0 +1,5 @@
|
||||
# str methods with explicit start/end pos
|
||||
print("Привет".startswith("П"))
|
||||
print("Привет".startswith("р", 1))
|
||||
print("абвба".find("а", 1))
|
||||
print("абвба".find("а", 1, -1))
|
@@ -0,0 +1,12 @@
|
||||
# Test slicing of Unicode strings
|
||||
|
||||
s = "Привет"
|
||||
|
||||
print(s[:])
|
||||
print(s[2:])
|
||||
print(s[:5])
|
||||
print(s[2:5])
|
||||
print(s[2:5:1])
|
||||
print(s[2:10])
|
||||
print(s[-3:10])
|
||||
print(s[-4:10])
|
@@ -0,0 +1,4 @@
|
||||
# test handling of unicode chars in format strings
|
||||
|
||||
print("α".format())
|
||||
print("{α}".format(α=1))
|
@@ -0,0 +1,3 @@
|
||||
# test handling of unicode chars in string % formatting
|
||||
|
||||
print("α" % ())
|
@@ -0,0 +1,23 @@
|
||||
a = "¢пр"
|
||||
|
||||
print(a[0], a[0:1])
|
||||
print(a[1], a[1:2])
|
||||
print(a[2], a[2:3])
|
||||
try:
|
||||
print(a[3])
|
||||
except IndexError:
|
||||
print("IndexError")
|
||||
print(a[3:4])
|
||||
|
||||
print(a[-1])
|
||||
print(a[-2], a[-2:-1])
|
||||
print(a[-3], a[-3:-2])
|
||||
try:
|
||||
print(a[-4])
|
||||
except IndexError:
|
||||
print("IndexError")
|
||||
print(a[-4:-3])
|
||||
|
||||
print(a[0:2])
|
||||
print(a[1:3])
|
||||
print(a[2:4])
|
Reference in New Issue
Block a user