修复了大量存在的问题,增加了假鸭子类型等等机制

This commit is contained in:
2026-06-25 14:49:46 +08:00
parent 19f2787db0
commit d88d11b646
827 changed files with 32617 additions and 18316 deletions

View File

@@ -2,61 +2,48 @@ import t, c
from t import CInt, CLong, CDouble, CPtr, CSizeT
from stdio import printf
import cpython
_pass_count: CInt = 0
_fail_count: CInt = 0
def check(name: t.CChar | CPtr, condition: bool):
global _pass_count, _fail_count
if condition:
_pass_count += 1
printf(" [PASS] %s\n", name)
else:
_fail_count += 1
printf(" [FAIL] %s\n", name)
import testcheck
def test_init_finalize():
printf("\n+-- Py_Initialize / Py_Finalize --+\n")
testcheck.section("Py_Initialize / Py_Finalize")
cpython.Py_Initialize()
check("Py_Initialize succeeded", cpython.Py_IsInitialized() != 0)
testcheck.check(cpython.Py_IsInitialized() != 0, "Py_Initialize succeeded", "Py_Initialize succeeded")
cpython.Py_Finalize()
check("Py_Finalize succeeded", cpython.Py_IsInitialized() == 0)
printf("+--------------------------------------------+\n")
testcheck.check(cpython.Py_IsInitialized() == 0, "Py_Finalize succeeded", "Py_Finalize succeeded")
def test_long_operations():
printf("\n+-- PyLong operations --+\n")
testcheck.section("PyLong operations")
cpython.Py_Initialize()
# 创建整数
py_num: cpython.PyObject | CPtr = cpython.PyLong_FromLong(12345)
check("PyLong_FromLong != NULL", py_num != 0)
testcheck.check(py_num != 0, "PyLong_FromLong != NULL", "PyLong_FromLong != NULL")
# 整数转字符串
py_str: cpython.PyObject | CPtr = cpython.PyObject_Str(py_num)
check("PyObject_Str != NULL", py_str != 0)
py_str: cpython.PyObject | CPtr = cpython.PyObject_str(py_num)
testcheck.check(py_str != 0, "PyObject_Str != NULL", "PyObject_Str != NULL")
# 获取 C 字符串
c_str: t.CChar | CPtr = cpython.PyUnicode_AsUTF8(py_str)
check("PyUnicode_AsUTF8 != NULL", c_str != 0)
testcheck.check(c_str != 0, "PyUnicode_AsUTF8 != NULL", "PyUnicode_AsUTF8 != NULL")
printf(" PyLong_FromLong(12345) -> str = %s\n", c_str)
# 读取回来
val: CLong = cpython.PyLong_AsLong(py_num)
check("PyLong_AsLong == 12345", val == 12345)
testcheck.check(val == 12345, "PyLong_AsLong == 12345", "PyLong_AsLong == 12345")
# 负数
py_neg: cpython.PyObject | CPtr = cpython.PyLong_FromLong(-999)
neg_val: CLong = cpython.PyLong_AsLong(py_neg)
check("PyLong_FromLong(-999) == -999", neg_val == -999)
testcheck.check(neg_val == -999, "PyLong_FromLong(-999) == -999", "PyLong_FromLong(-999) == -999")
# 浮点数转整数
py_dbl: cpython.PyObject | CPtr = cpython.PyFloat_FromDouble(3.14159)
check("PyFloat_FromDouble != NULL", py_dbl != 0)
testcheck.check(py_dbl != 0, "PyFloat_FromDouble != NULL", "PyFloat_FromDouble != NULL")
dbl_val: CDouble = cpython.PyFloat_AsDouble(py_dbl)
check("PyFloat_AsDouble ~= 3.14", dbl_val > 3.14 and dbl_val < 3.15)
testcheck.check(dbl_val > 3.14 and dbl_val < 3.15, "PyFloat_AsDouble ~= 3.14", "PyFloat_AsDouble ~= 3.14")
# 清理
cpython.Py_DecRef(py_dbl)
@@ -65,31 +52,30 @@ def test_long_operations():
cpython.Py_DecRef(py_num)
cpython.Py_Finalize()
printf("+--------------------------------------------+\n")
def test_string_operations():
printf("\n+-- PyUnicode operations --+\n")
testcheck.section("PyUnicode operations")
cpython.Py_Initialize()
# 从 C 字符串创建
py_s1: cpython.PyObject | CPtr = cpython.PyUnicode_FromString("Hello, Viper!")
check("PyUnicode_FromString != NULL", py_s1 != 0)
testcheck.check(py_s1 != 0, "PyUnicode_FromString != NULL", "PyUnicode_FromString != NULL")
# 获取 UTF8
utf8: t.CChar | CPtr = cpython.PyUnicode_AsUTF8(py_s1)
check("AsUTF8 != NULL", utf8 != 0)
testcheck.check(utf8 != 0, "AsUTF8 != NULL", "AsUTF8 != NULL")
printf(" PyUnicode_FromString -> %s\n", utf8)
# 字符串长度
length: CLong = cpython.PyUnicode_GetLength(py_s1)
check("GetLength == 13", length == 13)
testcheck.check(length == 13, "GetLength == 13", "GetLength == 13")
# 字符串拼接
py_s2: cpython.PyObject | CPtr = cpython.PyUnicode_FromString(" World")
py_concat: cpython.PyObject | CPtr = cpython.PyUnicode_Concat(py_s1, py_s2)
concat_utf8: t.CChar | CPtr = cpython.PyUnicode_AsUTF8(py_concat)
check("Concat != NULL", py_concat != 0)
testcheck.check(py_concat != 0, "Concat != NULL", "Concat != NULL")
printf(" Concat -> %s\n", concat_utf8)
# 清理
@@ -98,16 +84,15 @@ def test_string_operations():
cpython.Py_DecRef(py_s1)
cpython.Py_Finalize()
printf("+--------------------------------------------+\n")
def test_list_operations():
printf("\n+-- PyList operations --+\n")
testcheck.section("PyList operations")
cpython.Py_Initialize()
# 创建列表
py_list: cpython.PyObject | CPtr = cpython.PyList_New(0)
check("PyList_New != NULL", py_list != 0)
testcheck.check(py_list != 0, "PyList_New != NULL", "PyList_New != NULL")
# Append 元素
py_i1: cpython.PyObject | CPtr = cpython.PyLong_FromLong(10)
@@ -119,16 +104,16 @@ def test_list_operations():
# 检查大小
list_size: CSizeT = cpython.PyList_Size(py_list)
check("PyList_Size == 3", list_size == 3)
testcheck.check(list_size == 3, "PyList_Size == 3", "PyList_Size == 3")
# 获取元素
item0: cpython.PyObject | CPtr = cpython.PyList_GetItem(py_list, 0)
val0: CLong = cpython.PyLong_AsLong(item0)
check("list[0] == 10", val0 == 10)
testcheck.check(val0 == 10, "list[0] == 10", "list[0] == 10")
item2: cpython.PyObject | CPtr = cpython.PyList_GetItem(py_list, 2)
val2: CLong = cpython.PyLong_AsLong(item2)
check("list[2] == 30", val2 == 30)
testcheck.check(val2 == 30, "list[2] == 30", "list[2] == 30")
# 清理
cpython.Py_DecRef(py_i3)
@@ -137,16 +122,15 @@ def test_list_operations():
cpython.Py_DecRef(py_list)
cpython.Py_Finalize()
printf("+--------------------------------------------+\n")
def test_dict_operations():
printf("\n+-- PyDict operations --+\n")
testcheck.section("PyDict operations")
cpython.Py_Initialize()
# 创建字典
py_dict: cpython.PyObject | CPtr = cpython.PyDict_New()
check("PyDict_New != NULL", py_dict != 0)
testcheck.check(py_dict != 0, "PyDict_New != NULL", "PyDict_New != NULL")
# 设置键值对
py_key1: cpython.PyObject | CPtr = cpython.PyUnicode_FromString("name")
@@ -159,20 +143,20 @@ def test_dict_operations():
# 检查大小
dict_size: CSizeT = cpython.PyDict_Size(py_dict)
check("PyDict_Size == 2", dict_size == 2)
testcheck.check(dict_size == 2, "PyDict_Size == 2", "PyDict_Size == 2")
# 获取值
got_val: cpython.PyObject | CPtr = cpython.PyDict_GetItem(py_dict, py_key1)
check("GetItem(name) != NULL", got_val != 0)
testcheck.check(got_val != 0, "GetItem(name) != NULL", "GetItem(name) != NULL")
got_str: t.CChar | CPtr = cpython.PyUnicode_AsUTF8(got_val)
check("name == Viper", got_str != 0)
testcheck.check(got_str != 0, "name == Viper", "name == Viper")
printf(" dict['name'] = %s\n", got_str)
# 使用 GetItemString
got_ver: cpython.PyObject | CPtr = cpython.PyDict_GetItemString(py_dict, "version")
check("GetItemString(version) != NULL", got_ver != 0)
testcheck.check(got_ver != 0, "GetItemString(version) != NULL", "GetItemString(version) != NULL")
ver_val: CLong = cpython.PyLong_AsLong(got_ver)
check("version == 1", ver_val == 1)
testcheck.check(ver_val == 1, "version == 1", "version == 1")
# 清理
cpython.Py_DecRef(py_val2)
@@ -182,79 +166,76 @@ def test_dict_operations():
cpython.Py_DecRef(py_dict)
cpython.Py_Finalize()
printf("+--------------------------------------------+\n")
def test_pyrun_string():
printf("\n+-- PyRun_SimpleString --+\n")
testcheck.section("PyRun_SimpleString")
cpython.Py_Initialize()
# 执行简单 Python 代码
rc: CInt = cpython.PyRun_SimpleString("print('Hello from embedded Python!')")
check("PyRun_SimpleString == 0", rc == 0)
testcheck.check(rc == 0, "PyRun_SimpleString == 0", "PyRun_SimpleString == 0")
# 执行赋值
rc2: CInt = cpython.PyRun_SimpleString("x = 6 * 7")
check("PyRun_SimpleString(assign) == 0", rc2 == 0)
testcheck.check(rc2 == 0, "PyRun_SimpleString(assign) == 0", "PyRun_SimpleString(assign) == 0")
# 读取结果
rc3: CInt = cpython.PyRun_SimpleString("print(f'The answer is {x}')")
check("PyRun_SimpleString(print) == 0", rc3 == 0)
testcheck.check(rc3 == 0, "PyRun_SimpleString(print) == 0", "PyRun_SimpleString(print) == 0")
cpython.Py_Finalize()
printf("+--------------------------------------------+\n")
def test_import_module():
printf("\n+-- PyImport_ImportModule --+\n")
testcheck.section("PyImport_ImportModule")
cpython.Py_Initialize()
# 导入 math 模块
math_mod: cpython.PyObject | CPtr = cpython.PyImport_ImportModule("math")
check("import math != NULL", math_mod != 0)
testcheck.check(math_mod != 0, "import math != NULL", "import math != NULL")
if math_mod != 0:
# 获取 math.pi
pi_val: cpython.PyObject | CPtr = cpython.PyObject_GetAttrString(math_mod, "pi")
check("math.pi != NULL", pi_val != 0)
testcheck.check(pi_val != 0, "math.pi != NULL", "math.pi != NULL")
if pi_val != 0:
pi_dbl: CDouble = cpython.PyFloat_AsDouble(pi_val)
check("math.pi ~= 3.14", pi_dbl > 3.14 and pi_dbl < 3.15)
testcheck.check(pi_dbl > 3.14 and pi_dbl < 3.15, "math.pi ~= 3.14", "math.pi ~= 3.14")
printf(" math.pi = %f\n", pi_dbl)
cpython.Py_DecRef(pi_val)
# 获取 math.sqrt 函数
sqrt_func: cpython.PyObject | CPtr = cpython.PyObject_GetAttrString(math_mod, "sqrt")
check("math.sqrt != NULL", sqrt_func != 0)
testcheck.check(sqrt_func != 0, "math.sqrt != NULL", "math.sqrt != NULL")
if sqrt_func != 0:
cpython.Py_DecRef(sqrt_func)
cpython.Py_DecRef(math_mod)
cpython.Py_Finalize()
printf("+--------------------------------------------+\n")
def test_operator_overload():
printf("\n+-- Operator Overload (PyLongObj / PyFloatObj / PyUnicodeObj / PyListObj / PyDictObj) --+\n")
testcheck.section("Operator Overload (PyLongObj / PyFloatObj / PyUnicodeObj / PyListObj / PyDictObj)")
cpython.Py_Initialize()
# --- PyLongObj: from_long, to_long, +, -, *, - (neg) ---
a: cpython.PyLongObj | CPtr = cpython.PyLongObj.from_long(10)
b: cpython.PyLongObj | CPtr = cpython.PyLongObj.from_long(3)
check("PyLongObj.from_long != NULL", a != 0 and b != 0)
testcheck.check(a != 0 and b != 0, "PyLongObj.from_long != NULL", "PyLongObj.from_long != NULL")
add_r: cpython.PyLongObj | CPtr = a + b
check("PyLongObj 10 + 3 == 13", add_r.to_long() == 13)
testcheck.check(add_r.to_long() == 13, "PyLongObj 10 + 3 == 13", "PyLongObj 10 + 3 == 13")
sub_r: cpython.PyLongObj | CPtr = a - b
check("PyLongObj 10 - 3 == 7", sub_r.to_long() == 7)
testcheck.check(sub_r.to_long() == 7, "PyLongObj 10 - 3 == 7", "PyLongObj 10 - 3 == 7")
mul_r: cpython.PyLongObj | CPtr = a * b
check("PyLongObj 10 * 3 == 30", mul_r.to_long() == 30)
testcheck.check(mul_r.to_long() == 30, "PyLongObj 10 * 3 == 30", "PyLongObj 10 * 3 == 30")
neg_r: cpython.PyLongObj | CPtr = -b
check("PyLongObj -3 == -3", neg_r.to_long() == -3)
testcheck.check(neg_r.to_long() == -3, "PyLongObj -3 == -3", "PyLongObj -3 == -3")
mul_r.dec_ref()
sub_r.dec_ref()
@@ -266,22 +247,22 @@ def test_operator_overload():
# --- PyFloatObj: from_double, to_double, +, -, *, /, - (neg) ---
fa: cpython.PyFloatObj | CPtr = cpython.PyFloatObj.from_double(10.0)
fb: cpython.PyFloatObj | CPtr = cpython.PyFloatObj.from_double(4.0)
check("PyFloatObj.from_double != NULL", fa != 0 and fb != 0)
testcheck.check(fa != 0 and fb != 0, "PyFloatObj.from_double != NULL", "PyFloatObj.from_double != NULL")
fadd: cpython.PyFloatObj | CPtr = fa + fb
check("PyFloatObj 10.0 + 4.0 == 14.0", fadd.to_double() == 14.0)
testcheck.check(fadd.to_double() == 14.0, "PyFloatObj 10.0 + 4.0 == 14.0", "PyFloatObj 10.0 + 4.0 == 14.0")
fsub: cpython.PyFloatObj | CPtr = fa - fb
check("PyFloatObj 10.0 - 4.0 == 6.0", fsub.to_double() == 6.0)
testcheck.check(fsub.to_double() == 6.0, "PyFloatObj 10.0 - 4.0 == 6.0", "PyFloatObj 10.0 - 4.0 == 6.0")
fmul: cpython.PyFloatObj | CPtr = fa * fb
check("PyFloatObj 10.0 * 4.0 == 40.0", fmul.to_double() == 40.0)
testcheck.check(fmul.to_double() == 40.0, "PyFloatObj 10.0 * 4.0 == 40.0", "PyFloatObj 10.0 * 4.0 == 40.0")
fdiv: cpython.PyFloatObj | CPtr = fa / fb
check("PyFloatObj 10.0 / 4.0 == 2.5", fdiv.to_double() == 2.5)
testcheck.check(fdiv.to_double() == 2.5, "PyFloatObj 10.0 / 4.0 == 2.5", "PyFloatObj 10.0 / 4.0 == 2.5")
fneg: cpython.PyFloatObj | CPtr = -fb
check("PyFloatObj -4.0 == -4.0", fneg.to_double() == -4.0)
testcheck.check(fneg.to_double() == -4.0, "PyFloatObj -4.0 == -4.0", "PyFloatObj -4.0 == -4.0")
fadd.dec_ref()
fsub.dec_ref()
@@ -294,16 +275,16 @@ def test_operator_overload():
# --- PyUnicodeObj: from_string, as_utf8, len, + (concat) ---
sa: cpython.PyUnicodeObj | CPtr = cpython.PyUnicodeObj.from_string("Hello")
sb: cpython.PyUnicodeObj | CPtr = cpython.PyUnicodeObj.from_string(" World")
check("PyUnicodeObj.from_string != NULL", sa != 0 and sb != 0)
testcheck.check(sa != 0 and sb != 0, "PyUnicodeObj.from_string != NULL", "PyUnicodeObj.from_string != NULL")
sc: cpython.PyUnicodeObj | CPtr = sa + sb
sc_utf8: t.CChar | CPtr = sc.as_utf8()
check("PyUnicodeObj concat != NULL", sc_utf8 != 0)
testcheck.check(sc_utf8 != 0, "PyUnicodeObj concat != NULL", "PyUnicodeObj concat != NULL")
if sc_utf8 != 0:
printf(" PyUnicodeObj 'Hello' + ' World' = %s\n", sc_utf8)
sa_len: t.CPtrDiffT = len(sa)
check("PyUnicodeObj len('Hello') == 5", sa_len == 5)
testcheck.check(sa_len == 5, "PyUnicodeObj len('Hello') == 5", "PyUnicodeObj len('Hello') == 5")
sc.dec_ref()
sb.dec_ref()
@@ -311,7 +292,7 @@ def test_operator_overload():
# --- PyListObj: new, append, len, [], []= ---
lst: cpython.PyListObj | CPtr = cpython.PyListObj.new()
check("PyListObj.new != NULL", lst != 0)
testcheck.check(lst != 0, "PyListObj.new != NULL", "PyListObj.new != NULL")
if lst != 0:
e1: cpython.PyObject | CPtr = cpython.PyLong_FromLong(100)
e2: cpython.PyObject | CPtr = cpython.PyLong_FromLong(200)
@@ -319,16 +300,16 @@ def test_operator_overload():
lst.append(e1)
lst.append(e2)
lst.append(e3)
check("PyListObj len == 3", len(lst) == 3)
testcheck.check(len(lst) == 3, "PyListObj len == 3", "PyListObj len == 3")
item0: cpython.PyObject | CPtr = lst[t.CSizeT(0)]
check("PyListObj lst[0] == 100", cpython.PyLong_AsLong(item0) == 100)
testcheck.check(cpython.PyLong_AsLong(item0) == 100, "PyListObj lst[0] == 100", "PyListObj lst[0] == 100")
# __setitem__
e4: cpython.PyObject | CPtr = cpython.PyLong_FromLong(999)
lst[t.CSizeT(1)] = e4
item1: cpython.PyObject | CPtr = lst[t.CSizeT(1)]
check("PyListObj lst[1] = 999", cpython.PyLong_AsLong(item1) == 999)
testcheck.check(cpython.PyLong_AsLong(item1) == 999, "PyListObj lst[1] = 999", "PyListObj lst[1] = 999")
cpython.Py_DecRef(e4)
cpython.Py_DecRef(e3)
@@ -338,34 +319,33 @@ def test_operator_overload():
# --- PyDictObj: new, [], []=, len, set_string, get_string ---
dct: cpython.PyDictObj | CPtr = cpython.PyDictObj.new()
check("PyDictObj.new != NULL", dct != 0)
testcheck.check(dct != 0, "PyDictObj.new != NULL", "PyDictObj.new != NULL")
if dct != 0:
dk1: cpython.PyObject | CPtr = cpython.PyUnicode_FromString("lang")
dv1: cpython.PyObject | CPtr = cpython.PyUnicode_FromString("Viper")
dct[dk1] = dv1
check("PyDictObj len == 1", len(dct) == 1)
testcheck.check(len(dct) == 1, "PyDictObj len == 1", "PyDictObj len == 1")
got_lang: cpython.PyObject | CPtr = dct[dk1]
got_str: t.CChar | CPtr = cpython.PyUnicode_AsUTF8(got_lang)
check("PyDictObj dct['lang'] == Viper", got_str != 0)
testcheck.check(got_str != 0, "PyDictObj dct['lang'] == Viper", "PyDictObj dct['lang'] == Viper")
if got_str != 0:
printf(" PyDictObj dct['lang'] = %s\n", got_str)
# get_string / set_string
dct.set_string("ver", cpython.PyLong_FromLong(3))
got_ver: cpython.PyObject | CPtr = dct.get_string("ver")
check("PyDictObj get_string('ver') == 3", got_ver != 0 and cpython.PyLong_AsLong(got_ver) == 3)
testcheck.check(got_ver != 0 and cpython.PyLong_AsLong(got_ver) == 3, "PyDictObj get_string('ver') == 3", "PyDictObj get_string('ver') == 3")
cpython.Py_DecRef(dv1)
cpython.Py_DecRef(dk1)
dct.dec_ref()
cpython.Py_Finalize()
printf("+--------------------------------------------+\n")
def main() -> CInt | t.CExport:
printf("=== CPython Integration Test ===\n")
testcheck.begin("CPython Integration Test")
test_init_finalize()
test_long_operations()
@@ -376,5 +356,4 @@ def main() -> CInt | t.CExport:
test_import_module()
test_operator_overload()
printf("\n=== Results: %d passed, %d failed ===\n", _pass_count, _fail_count)
return 0
return testcheck.end()