chore(comments): Test 仓注释英文化(R11)
把 81 个 .vp 文件的中文 # 注释与三引号文档串改写为简洁英文,每个连续注释块 <=2 行;运行时中文字符串字面量(testcheck.begin/print 等用户可见消息)保持原样。骨架比对 HEAD 与工作区 112 文件漂移 0,全量 77/77 顺序回归通过。
This commit is contained in:
1 parent
aa5a7e7250
commit
ee6461be46
81 files changed
+1231
-1925
No files matched your search
+40
-110
@@ -1,17 +1,13 @@
|
||||
import t, c
|
||||
|
||||
|
||||
# ============================================================
|
||||
# PyObject — 所有 Python 对象的基类
|
||||
# ============================================================
|
||||
# --- PyObject: base class of all Python objects ---
|
||||
class PyObject:
|
||||
ob_refcnt: t.CLong
|
||||
ob_type: t.CPtr
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 引用计数
|
||||
# ============================================================
|
||||
# --- Reference counting ---
|
||||
extern def Py_INCREF(op: PyObject*)
|
||||
extern def Py_DECREF(op: PyObject*)
|
||||
extern def Py_XINCREF(op: PyObject*)
|
||||
@@ -19,9 +15,7 @@ extern def Py_XDECREF(op: PyObject*)
|
||||
extern def Py_DecRef(op: PyObject*)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 解释器初始化/终止
|
||||
# ============================================================
|
||||
# --- Interpreter init/finalize ---
|
||||
extern def Py_Initialize()
|
||||
extern def Py_Finalize()
|
||||
extern def Py_InitializeEx(initsigs: int)
|
||||
@@ -29,9 +23,7 @@ extern def Py_FinalizeEx() -> int
|
||||
extern def Py_IsInitialized() -> int
|
||||
|
||||
|
||||
# ============================================================
|
||||
# PyConfig
|
||||
# ============================================================
|
||||
# --- PyConfig ---
|
||||
class PySTATUS:
|
||||
func: const str
|
||||
err_msg: const str
|
||||
@@ -107,9 +99,7 @@ extern def Py_DecodeLocale(s: const str) -> str
|
||||
extern def PyStatus_Exception(status: PySTATUS) -> int
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 整数操作 (PyLong)
|
||||
# ============================================================
|
||||
# --- Integer ops (PyLong) ---
|
||||
extern def PyLong_FromLong(v: int) -> PyObject*
|
||||
extern def PyLong_FromUnsignedLong(v: t.CUnsignedLong) -> PyObject*
|
||||
extern def PyLong_FromLongLong(v: t.CLong) -> PyObject*
|
||||
@@ -126,17 +116,13 @@ extern def PyLong_AsSsize_t(obj: PyObject*) -> t.CPtrDiffT
|
||||
extern def PyLong_AsSize_t(obj: PyObject*) -> sizet
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 浮点数操作 (PyFloat)
|
||||
# ============================================================
|
||||
# --- Float ops (PyFloat) ---
|
||||
extern def PyFloat_FromDouble(v: double) -> PyObject*
|
||||
extern def PyFloat_AsDouble(obj: PyObject*) -> double
|
||||
extern def PyFloat_FromString(s: PyObject*) -> PyObject*
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 字符串操作 (PyUnicode / PyBytes)
|
||||
# ============================================================
|
||||
# --- String ops (PyUnicode / PyBytes) ---
|
||||
extern def PyUnicode_FromString(u: const str) -> PyObject*
|
||||
extern def PyUnicode_FromStringAndSize(u: const str, size: sizet) -> PyObject*
|
||||
extern def PyUnicode_FromFormat(fmt: const str) -> PyObject*
|
||||
@@ -161,9 +147,7 @@ extern def PyBytes_FromObject(o: PyObject*) -> PyObject*
|
||||
extern def PyBytes_FromFormat(fmt: const str) -> PyObject*
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 布尔 / None
|
||||
# ============================================================
|
||||
# --- Bool / None ---
|
||||
extern def PyBool_FromLong(v: t.CLong) -> PyObject*
|
||||
|
||||
Py_True: PyObject | t.CPtr | t.State
|
||||
@@ -171,9 +155,7 @@ Py_False: PyObject | t.CPtr | t.State
|
||||
Py_None: PyObject | t.CPtr | t.State
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 列表操作 (PyList)
|
||||
# ============================================================
|
||||
# --- List ops (PyList) ---
|
||||
extern def PyList_New(len: sizet) -> PyObject*
|
||||
extern def PyList_Size(lst: PyObject*) -> sizet
|
||||
extern def PyList_GetItem(lst: PyObject*, idx: sizet) -> PyObject*
|
||||
@@ -187,9 +169,7 @@ extern def PyList_Reverse(lst: PyObject*) -> int
|
||||
extern def PyList_AsTuple(lst: PyObject*) -> PyObject*
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 元组操作 (PyTuple)
|
||||
# ============================================================
|
||||
# --- Tuple ops (PyTuple) ---
|
||||
extern def PyTuple_New(len: sizet) -> PyObject*
|
||||
extern def PyTuple_Size(tup: PyObject*) -> sizet
|
||||
extern def PyTuple_GetItem(tup: PyObject*, idx: sizet) -> PyObject*
|
||||
@@ -198,9 +178,7 @@ extern def PyTuple_GetSlice(tup: PyObject*, lo: sizet, hi: sizet) -> PyObject*
|
||||
extern def PyTuple_Pack(n: sizet) -> PyObject*
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 字典操作 (PyDict)
|
||||
# ============================================================
|
||||
# --- Dict ops (PyDict) ---
|
||||
extern def PyDict_New() -> PyObject*
|
||||
extern def PyDict_GetItem(d: PyObject*, key: PyObject*) -> PyObject*
|
||||
extern def PyDict_GetItemString(d: PyObject*, key: const str) -> PyObject*
|
||||
@@ -220,9 +198,7 @@ extern def PyDict_Merge(d: PyObject*, other: PyObject*, override: int) -> int
|
||||
extern def PyDict_Update(d: PyObject*, other: PyObject*) -> int
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 对象通用操作
|
||||
# ============================================================
|
||||
# --- Object common ops ---
|
||||
extern def PyObject_Str(obj: PyObject*) -> PyObject*
|
||||
extern def PyObject_Repr(obj: PyObject*) -> PyObject*
|
||||
extern def PyObject_Type(obj: PyObject*) -> PyObject*
|
||||
@@ -254,9 +230,7 @@ extern def PyObject_GetIter(obj: PyObject*) -> PyObject*
|
||||
extern def PyIter_Next(obj: PyObject*) -> PyObject*
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 比较常量
|
||||
# ============================================================
|
||||
# --- Comparison constants ---
|
||||
Py_LT: int | t.State
|
||||
Py_LE: int | t.State
|
||||
Py_EQ: int | t.State
|
||||
@@ -265,9 +239,7 @@ Py_GT: int | t.State
|
||||
Py_GE: int | t.State
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 模块操作
|
||||
# ============================================================
|
||||
# --- Module ops ---
|
||||
extern def PyImport_ImportModule(name: const str) -> PyObject*
|
||||
extern def PyImport_Import(name: PyObject*) -> PyObject*
|
||||
extern def PyImport_ImportModuleLevelObject(name: PyObject*, globs: PyObject*, locs: PyObject*, fromlist: PyObject*, level: int) -> PyObject*
|
||||
@@ -277,9 +249,7 @@ extern def PyImport_GetModuleDict() -> PyObject*
|
||||
extern def PyImport_ImportModuleEx(name: const str, globs: PyObject*, locs: PyObject*, fromlist: PyObject*) -> PyObject*
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 模块创建
|
||||
# ============================================================
|
||||
# --- Module creation ---
|
||||
class PyMethodDef:
|
||||
ml_name: const str
|
||||
ml_meth: t.CPtr
|
||||
@@ -314,9 +284,7 @@ extern def PyModule_AddIntConstant(mod: PyObject*, name: const str, val: t.CLong
|
||||
extern def PyModule_AddStringConstant(mod: PyObject*, name: const str, val: const str) -> int
|
||||
|
||||
|
||||
# ============================================================
|
||||
# GIL 管理
|
||||
# ============================================================
|
||||
# --- GIL management ---
|
||||
extern def PyGILState_Ensure() -> int
|
||||
extern def PyGILState_Release(state: int): pass
|
||||
extern def PyEval_SaveThread() -> t.CPtr
|
||||
@@ -329,9 +297,7 @@ extern def PyEval_ThreadsInitialized() -> int
|
||||
extern def PyEval_InitThreads(): pass
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 错误处理
|
||||
# ============================================================
|
||||
# --- Error handling ---
|
||||
extern def PyErr_Occurred() -> PyObject*
|
||||
extern def PyErr_Clear(): pass
|
||||
extern def PyErr_Fetch(tp: PyObject**, val: PyObject**, tb: PyObject**): pass
|
||||
@@ -346,9 +312,7 @@ extern def PyErr_ExceptionMatches(exc: PyObject*) -> int | t.State: pass
|
||||
extern def PyErr_GivenExceptionMatches(given: PyObject*, exc: PyObject*) -> int | t.State: pass
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 内置异常类型
|
||||
# ============================================================
|
||||
# --- Built-in exception types ---
|
||||
PyExc_BaseException: PyObject | t.CPtr | t.State
|
||||
PyExc_Exception: PyObject | t.CPtr | t.State
|
||||
PyExc_TypeError: PyObject | t.CPtr | t.State
|
||||
@@ -373,16 +337,12 @@ PyExc_IsADirectoryError: PyObject | t.CPtr | t.State
|
||||
PyExc_NotADirectoryError: PyObject | t.CPtr | t.State
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 内置函数
|
||||
# ============================================================
|
||||
# --- Built-in functions ---
|
||||
extern def Py_BuildValue(fmt: const str) -> PyObject*
|
||||
extern def Py_VaBuildValue(fmt: const str, va: t.CPtr) -> PyObject*
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 类型检查
|
||||
# ============================================================
|
||||
# --- Type checks ---
|
||||
extern def PyLong_Check(obj: PyObject*) -> int
|
||||
extern def PyFloat_Check(obj: PyObject*) -> int
|
||||
extern def PyUnicode_Check(obj: PyObject*) -> int
|
||||
@@ -394,9 +354,7 @@ extern def PyBool_Check(obj: PyObject*) -> int
|
||||
extern def PyNone_Check(obj: PyObject*) -> int
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 内存管理
|
||||
# ============================================================
|
||||
# --- Memory management ---
|
||||
extern def PyMem_Malloc(size: sizet) -> t.CPtr
|
||||
extern def PyMem_Realloc(ptr: t.CPtr, size: sizet) -> t.CPtr
|
||||
extern def PyMem_Free(ptr: t.CPtr)
|
||||
@@ -409,18 +367,14 @@ extern def PyObject_Init(obj: PyObject*, tp: t.CPtr) -> PyObject*
|
||||
extern def PyObject_InitVar(obj: PyObject*, tp: t.CPtr, size: sizet) -> PyObject*
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 文件操作
|
||||
# ============================================================
|
||||
# --- File ops ---
|
||||
extern def PyFile_FromString(filename: const str, mode: const str) -> PyObject*
|
||||
extern def PyFile_AsFile(file: PyObject*) -> t.CPtr
|
||||
extern def PyFile_WriteObject(obj: PyObject*, file: PyObject*, flags: int) -> int
|
||||
extern def PyFile_WriteString(s: const str, file: PyObject*) -> int
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 编译/执行
|
||||
# ============================================================
|
||||
# --- Compile/execute ---
|
||||
extern def Py_CompileString(s: const str, filename: const str, start: int) -> PyObject*
|
||||
extern def PyEval_EvalCode(code: PyObject*, globs: PyObject*, locs: PyObject*) -> PyObject*
|
||||
extern def PyEval_EvalCodeEx(code: PyObject*, globs: PyObject*, locs: PyObject*, args: PyObject**, argc: int, kws: PyObject**, kwc: int, defs: PyObject**, defc: int, kwdefs: PyObject*, closure: PyObject*) -> PyObject*: pass
|
||||
@@ -430,9 +384,7 @@ extern def PyEval_CallFunction(callable: PyObject*, fmt: const str) -> PyObject*
|
||||
extern def PyEval_CallMethod(obj: PyObject*, method: const str, fmt: const str) -> PyObject*: pass
|
||||
|
||||
|
||||
# ============================================================
|
||||
# PyRun
|
||||
# ============================================================
|
||||
# --- PyRun ---
|
||||
extern def PyRun_SimpleString(cmd: const str) -> int
|
||||
extern def PyRun_SimpleFile(fp: t.CPtr, filename: const str) -> int
|
||||
extern def PyRun_AnyFile(fp: t.CPtr, filename: const str) -> int
|
||||
@@ -444,9 +396,7 @@ Py_file_input: int | t.State
|
||||
Py_eval_input: int | t.State
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Capsule
|
||||
# ============================================================
|
||||
# --- Capsule ---
|
||||
extern def PyCapsule_New(ptr: t.CPtr, name: const str, dtor: t.CPtr) -> PyObject*
|
||||
extern def PyCapsule_GetPointer(cap: PyObject*, name: const str) -> t.CPtr
|
||||
extern def PyCapsule_GetName(cap: PyObject*) -> str
|
||||
@@ -456,9 +406,7 @@ extern def PyCapsule_SetName(cap: PyObject*, name: const str) -> int
|
||||
extern def PyCapsule_SetDestructor(cap: PyObject*, dtor: t.CPtr) -> int
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 序列操作
|
||||
# ============================================================
|
||||
# --- Sequence ops ---
|
||||
extern def PySequence_Check(obj: PyObject*) -> int
|
||||
extern def PySequence_Size(obj: PyObject*) -> sizet
|
||||
extern def PySequence_Length(obj: PyObject*) -> sizet
|
||||
@@ -477,9 +425,7 @@ extern def PySequence_List(obj: PyObject*) -> PyObject*
|
||||
extern def PySequence_Tuple(obj: PyObject*) -> PyObject*
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 映射操作
|
||||
# ============================================================
|
||||
# --- Mapping ops ---
|
||||
extern def PyMapping_Check(obj: PyObject*) -> int
|
||||
extern def PyMapping_Size(obj: PyObject*) -> sizet
|
||||
extern def PyMapping_Length(obj: PyObject*) -> sizet
|
||||
@@ -492,9 +438,7 @@ extern def PyMapping_Values(obj: PyObject*) -> PyObject*
|
||||
extern def PyMapping_Items(obj: PyObject*) -> PyObject*
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 数字操作
|
||||
# ============================================================
|
||||
# --- Number ops ---
|
||||
extern def PyNumber_Add(o1: PyObject*, o2: PyObject*) -> PyObject*
|
||||
extern def PyNumber_Subtract(o1: PyObject*, o2: PyObject*) -> PyObject*
|
||||
extern def PyNumber_Multiply(o1: PyObject*, o2: PyObject*) -> PyObject*
|
||||
@@ -518,17 +462,13 @@ extern def PyNumber_Float(obj: PyObject*) -> PyObject*
|
||||
extern def PyNumber_Check(obj: PyObject*) -> int
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 弱引用
|
||||
# ============================================================
|
||||
# --- Weak references ---
|
||||
extern def PyWeakref_NewRef(obj: PyObject*, cb: PyObject*) -> PyObject*
|
||||
extern def PyWeakref_GetObject(ref: PyObject*) -> PyObject*
|
||||
extern def PyWeakref_Check(obj: PyObject*) -> int
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 属性描述符
|
||||
# ============================================================
|
||||
# --- Property descriptors ---
|
||||
class PyGetSetDef:
|
||||
name: const str
|
||||
get: t.CPtr
|
||||
@@ -544,9 +484,7 @@ class PyMemberDef:
|
||||
doc: const str
|
||||
|
||||
|
||||
# ============================================================
|
||||
# sys 模块
|
||||
# ============================================================
|
||||
# --- sys module ---
|
||||
extern def PySys_GetObject(name: const str) -> PyObject*
|
||||
extern def PySys_SetObject(name: const str, val: PyObject*) -> int
|
||||
extern def PySys_ResetWarnOptions(): pass
|
||||
@@ -558,17 +496,13 @@ extern def PySys_FormatStdout(fmt: const str): pass
|
||||
extern def PySys_FormatStderr(fmt: const str): pass
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 内存视图
|
||||
# ============================================================
|
||||
# --- Memory view ---
|
||||
extern def PyMemoryView_FromObject(obj: PyObject*) -> PyObject*
|
||||
extern def PyMemoryView_GetContiguous(obj: PyObject*, btype: int, order: char) -> PyObject*
|
||||
extern def PyMemoryView_Check(obj: PyObject*) -> int
|
||||
|
||||
|
||||
# ============================================================
|
||||
# set / frozenset
|
||||
# ============================================================
|
||||
# --- set / frozenset ---
|
||||
extern def PySet_New(iterable: PyObject*) -> PyObject*
|
||||
extern def PySet_Add(s: PyObject*, key: PyObject*) -> int
|
||||
extern def PySet_Discard(s: PyObject*, key: PyObject*) -> int
|
||||
@@ -580,9 +514,7 @@ extern def PyFrozenSet_New(iterable: PyObject*) -> PyObject*
|
||||
extern def PyFrozenSet_Check(obj: PyObject*) -> int
|
||||
|
||||
|
||||
# ============================================================
|
||||
# slice
|
||||
# ============================================================
|
||||
# --- slice ---
|
||||
extern def PySlice_New(start: PyObject*, stop: PyObject*, step: PyObject*) -> PyObject*
|
||||
extern def PySlice_GetIndicesEx(sl: PyObject*, length: sizet, start: sizet*, stop: sizet*, step: sizet*, slicelen: sizet*) -> int
|
||||
extern def PySlice_Unpack(sl: PyObject*, start: t.CPtrDiffT*, stop: t.CPtrDiffT*, step: t.CPtrDiffT*): pass
|
||||
@@ -590,11 +522,9 @@ extern def PySlice_AdjustIndices(length: sizet, start: t.CPtrDiffT*, stop: t.CPt
|
||||
extern def PySlice_Check(obj: PyObject*) -> int
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 类型包装类 — 带运算符重载
|
||||
# ============================================================
|
||||
# --- Type wrapper classes: operator overloading ---
|
||||
|
||||
# --- PyLongObj: Python 整数包装 ---
|
||||
# --- PyLongObj: Python integer wrapper ---
|
||||
class PyLongObj:
|
||||
ptr: PyObject*
|
||||
|
||||
@@ -637,7 +567,7 @@ class PyLongObj:
|
||||
Py_DecRef(self.ptr)
|
||||
|
||||
|
||||
# --- PyFloatObj: Python 浮点数包装 ---
|
||||
# --- PyFloatObj: Python float wrapper ---
|
||||
class PyFloatObj:
|
||||
ptr: PyObject*
|
||||
|
||||
@@ -685,7 +615,7 @@ class PyFloatObj:
|
||||
Py_DecRef(self.ptr)
|
||||
|
||||
|
||||
# --- PyUnicodeObj: Python 字符串包装 ---
|
||||
# --- PyUnicodeObj: Python string wrapper ---
|
||||
class PyUnicodeObj:
|
||||
ptr: PyObject*
|
||||
|
||||
@@ -716,7 +646,7 @@ class PyUnicodeObj:
|
||||
Py_DecRef(self.ptr)
|
||||
|
||||
|
||||
# --- PyListObj: Python 列表包装 ---
|
||||
# --- PyListObj: Python list wrapper ---
|
||||
class PyListObj:
|
||||
ptr: PyObject*
|
||||
|
||||
@@ -745,7 +675,7 @@ class PyListObj:
|
||||
Py_DecRef(self.ptr)
|
||||
|
||||
|
||||
# --- PyDictObj: Python 字典包装 ---
|
||||
# --- PyDictObj: Python dict wrapper ---
|
||||
class PyDictObj:
|
||||
ptr: PyObject*
|
||||
|
||||
|
||||
+29
-29
@@ -17,35 +17,35 @@ def test_long_operations():
|
||||
testcheck.section("PyLong operations")
|
||||
cpython.Py_Initialize()
|
||||
|
||||
# 创建整数
|
||||
# Create int
|
||||
py_num: cpython.PyObject | CPtr = cpython.PyLong_FromLong(12345)
|
||||
testcheck.check(py_num != 0, "PyLong_FromLong != NULL", "PyLong_FromLong != NULL")
|
||||
|
||||
# 整数转字符串
|
||||
# Int to string
|
||||
py_str: cpython.PyObject | CPtr = cpython.PyObject_Str(py_num)
|
||||
testcheck.check(py_str != 0, "PyObject_Str != NULL", "PyObject_Str != NULL")
|
||||
|
||||
# 获取 C 字符串
|
||||
# Get C string
|
||||
c_str: char | CPtr = cpython.PyUnicode_AsUTF8(py_str)
|
||||
testcheck.check(c_str != 0, "PyUnicode_AsUTF8 != NULL", "PyUnicode_AsUTF8 != NULL")
|
||||
printf(" PyLong_FromLong(12345) -> str = %s\n", c_str)
|
||||
|
||||
# 读取回来
|
||||
# Read back
|
||||
val: CLong = cpython.PyLong_AsLong(py_num)
|
||||
testcheck.check(val == 12345, "PyLong_AsLong == 12345", "PyLong_AsLong == 12345")
|
||||
|
||||
# 负数
|
||||
# Negative
|
||||
py_neg: cpython.PyObject | CPtr = cpython.PyLong_FromLong(-999)
|
||||
neg_val: CLong = cpython.PyLong_AsLong(py_neg)
|
||||
testcheck.check(neg_val == -999, "PyLong_FromLong(-999) == -999", "PyLong_FromLong(-999) == -999")
|
||||
|
||||
# 浮点数转整数
|
||||
# Float to int
|
||||
py_dbl: cpython.PyObject | CPtr = cpython.PyFloat_FromDouble(3.14159)
|
||||
testcheck.check(py_dbl != 0, "PyFloat_FromDouble != NULL", "PyFloat_FromDouble != NULL")
|
||||
dbl_val: CDouble = cpython.PyFloat_AsDouble(py_dbl)
|
||||
testcheck.check(dbl_val > 3.14 and dbl_val < 3.15, "PyFloat_AsDouble ~= 3.14", "PyFloat_AsDouble ~= 3.14")
|
||||
|
||||
# 清理
|
||||
# Cleanup
|
||||
cpython.Py_DecRef(py_dbl)
|
||||
cpython.Py_DecRef(py_neg)
|
||||
cpython.Py_DecRef(py_str)
|
||||
@@ -58,27 +58,27 @@ def test_string_operations():
|
||||
testcheck.section("PyUnicode operations")
|
||||
cpython.Py_Initialize()
|
||||
|
||||
# 从 C 字符串创建
|
||||
# Create from C string
|
||||
py_s1: cpython.PyObject | CPtr = cpython.PyUnicode_FromString("Hello, Viper!")
|
||||
testcheck.check(py_s1 != 0, "PyUnicode_FromString != NULL", "PyUnicode_FromString != NULL")
|
||||
|
||||
# 获取 UTF8
|
||||
# Get UTF-8
|
||||
utf8: char | CPtr = cpython.PyUnicode_AsUTF8(py_s1)
|
||||
testcheck.check(utf8 != 0, "AsUTF8 != NULL", "AsUTF8 != NULL")
|
||||
printf(" PyUnicode_FromString -> %s\n", utf8)
|
||||
|
||||
# 字符串长度
|
||||
# String length
|
||||
length: CLong = cpython.PyUnicode_GetLength(py_s1)
|
||||
testcheck.check(length == 13, "GetLength == 13", "GetLength == 13")
|
||||
|
||||
# 字符串拼接
|
||||
# String concat
|
||||
py_s2: cpython.PyObject | CPtr = cpython.PyUnicode_FromString(" World")
|
||||
py_concat: cpython.PyObject | CPtr = cpython.PyUnicode_Concat(py_s1, py_s2)
|
||||
concat_utf8: char | CPtr = cpython.PyUnicode_AsUTF8(py_concat)
|
||||
testcheck.check(py_concat != 0, "Concat != NULL", "Concat != NULL")
|
||||
printf(" Concat -> %s\n", concat_utf8)
|
||||
|
||||
# 清理
|
||||
# Cleanup
|
||||
cpython.Py_DecRef(py_concat)
|
||||
cpython.Py_DecRef(py_s2)
|
||||
cpython.Py_DecRef(py_s1)
|
||||
@@ -90,11 +90,11 @@ def test_list_operations():
|
||||
testcheck.section("PyList operations")
|
||||
cpython.Py_Initialize()
|
||||
|
||||
# 创建列表
|
||||
# Create list
|
||||
py_list: cpython.PyObject | CPtr = cpython.PyList_New(0)
|
||||
testcheck.check(py_list != 0, "PyList_New != NULL", "PyList_New != NULL")
|
||||
|
||||
# Append 元素
|
||||
# Append elements
|
||||
py_i1: cpython.PyObject | CPtr = cpython.PyLong_FromLong(10)
|
||||
py_i2: cpython.PyObject | CPtr = cpython.PyLong_FromLong(20)
|
||||
py_i3: cpython.PyObject | CPtr = cpython.PyLong_FromLong(30)
|
||||
@@ -102,11 +102,11 @@ def test_list_operations():
|
||||
cpython.PyList_Append(py_list, py_i2)
|
||||
cpython.PyList_Append(py_list, py_i3)
|
||||
|
||||
# 检查大小
|
||||
# Check size
|
||||
list_size: CSizeT = cpython.PyList_Size(py_list)
|
||||
testcheck.check(list_size == 3, "PyList_Size == 3", "PyList_Size == 3")
|
||||
|
||||
# 获取元素
|
||||
# Get elements
|
||||
item0: cpython.PyObject | CPtr = cpython.PyList_GetItem(py_list, 0)
|
||||
val0: CLong = cpython.PyLong_AsLong(item0)
|
||||
testcheck.check(val0 == 10, "list[0] == 10", "list[0] == 10")
|
||||
@@ -115,7 +115,7 @@ def test_list_operations():
|
||||
val2: CLong = cpython.PyLong_AsLong(item2)
|
||||
testcheck.check(val2 == 30, "list[2] == 30", "list[2] == 30")
|
||||
|
||||
# 清理
|
||||
# Cleanup
|
||||
cpython.Py_DecRef(py_i3)
|
||||
cpython.Py_DecRef(py_i2)
|
||||
cpython.Py_DecRef(py_i1)
|
||||
@@ -128,11 +128,11 @@ def test_dict_operations():
|
||||
testcheck.section("PyDict operations")
|
||||
cpython.Py_Initialize()
|
||||
|
||||
# 创建字典
|
||||
# Create dict
|
||||
py_dict: cpython.PyObject | CPtr = cpython.PyDict_New()
|
||||
testcheck.check(py_dict != 0, "PyDict_New != NULL", "PyDict_New != NULL")
|
||||
|
||||
# 设置键值对
|
||||
# Set key/value pairs
|
||||
py_key1: cpython.PyObject | CPtr = cpython.PyUnicode_FromString("name")
|
||||
py_val1: cpython.PyObject | CPtr = cpython.PyUnicode_FromString("Viper")
|
||||
cpython.PyDict_SetItem(py_dict, py_key1, py_val1)
|
||||
@@ -141,24 +141,24 @@ def test_dict_operations():
|
||||
py_val2: cpython.PyObject | CPtr = cpython.PyLong_FromLong(1)
|
||||
cpython.PyDict_SetItem(py_dict, py_key2, py_val2)
|
||||
|
||||
# 检查大小
|
||||
# Check size
|
||||
dict_size: CSizeT = cpython.PyDict_Size(py_dict)
|
||||
testcheck.check(dict_size == 2, "PyDict_Size == 2", "PyDict_Size == 2")
|
||||
|
||||
# 获取值
|
||||
# Get value
|
||||
got_val: cpython.PyObject | CPtr = cpython.PyDict_GetItem(py_dict, py_key1)
|
||||
testcheck.check(got_val != 0, "GetItem(name) != NULL", "GetItem(name) != NULL")
|
||||
got_str: char | CPtr = cpython.PyUnicode_AsUTF8(got_val)
|
||||
testcheck.check(got_str != 0, "name == Viper", "name == Viper")
|
||||
printf(" dict['name'] = %s\n", got_str)
|
||||
|
||||
# 使用 GetItemString
|
||||
# Use GetItemString
|
||||
got_ver: cpython.PyObject | CPtr = cpython.PyDict_GetItemString(py_dict, "version")
|
||||
testcheck.check(got_ver != 0, "GetItemString(version) != NULL", "GetItemString(version) != NULL")
|
||||
ver_val: CLong = cpython.PyLong_AsLong(got_ver)
|
||||
testcheck.check(ver_val == 1, "version == 1", "version == 1")
|
||||
|
||||
# 清理
|
||||
# Cleanup
|
||||
cpython.Py_DecRef(py_val2)
|
||||
cpython.Py_DecRef(py_key2)
|
||||
cpython.Py_DecRef(py_val1)
|
||||
@@ -172,15 +172,15 @@ def test_pyrun_string():
|
||||
testcheck.section("PyRun_SimpleString")
|
||||
cpython.Py_Initialize()
|
||||
|
||||
# 执行简单 Python 代码
|
||||
# Run simple Python code
|
||||
rc: CInt = cpython.PyRun_SimpleString("print('Hello from embedded Python!')")
|
||||
testcheck.check(rc == 0, "PyRun_SimpleString == 0", "PyRun_SimpleString == 0")
|
||||
|
||||
# 执行赋值
|
||||
# Run assignment
|
||||
rc2: CInt = cpython.PyRun_SimpleString("x = 6 * 7")
|
||||
testcheck.check(rc2 == 0, "PyRun_SimpleString(assign) == 0", "PyRun_SimpleString(assign) == 0")
|
||||
|
||||
# 读取结果
|
||||
# Read result
|
||||
rc3: CInt = cpython.PyRun_SimpleString("print(f'The answer is {x}')")
|
||||
testcheck.check(rc3 == 0, "PyRun_SimpleString(print) == 0", "PyRun_SimpleString(print) == 0")
|
||||
|
||||
@@ -191,12 +191,12 @@ def test_import_module():
|
||||
testcheck.section("PyImport_ImportModule")
|
||||
cpython.Py_Initialize()
|
||||
|
||||
# 导入 math 模块
|
||||
# Import math module
|
||||
math_mod: cpython.PyObject | CPtr = cpython.PyImport_ImportModule("math")
|
||||
testcheck.check(math_mod != 0, "import math != NULL", "import math != NULL")
|
||||
|
||||
if math_mod != 0:
|
||||
# 获取 math.pi
|
||||
# Get math.pi
|
||||
pi_val: cpython.PyObject | CPtr = cpython.PyObject_GetAttrString(math_mod, "pi")
|
||||
testcheck.check(pi_val != 0, "math.pi != NULL", "math.pi != NULL")
|
||||
if pi_val != 0:
|
||||
@@ -205,7 +205,7 @@ def test_import_module():
|
||||
printf(" math.pi = %f\n", pi_dbl)
|
||||
cpython.Py_DecRef(pi_val)
|
||||
|
||||
# 获取 math.sqrt 函数
|
||||
# Get math.sqrt function
|
||||
sqrt_func: cpython.PyObject | CPtr = cpython.PyObject_GetAttrString(math_mod, "sqrt")
|
||||
testcheck.check(sqrt_func != 0, "math.sqrt != NULL", "math.sqrt != NULL")
|
||||
if sqrt_func != 0:
|
||||
|
||||
Reference in new issue
Block a user