fix(stubgen): vpi 生成 CArray count 数字常量输出数值而非 t.CInt

_PyiTypeStr 的 Constant 分支对 CONST_INT/CONST_CHAR 默认回退 "t.CInt",
t.CArray[t.CUInt32T, 64] 的 count 被生成成 t.CInt,加载 stub 时触发
TypeError(count 必须是整数常量)。修复:输出数值本身。
This commit is contained in:
TermiNexus committed 2026-08-13 08:11:41 +08:00
1 parent 1f4b231134
commit 97f765fcb3
1 file changed
+12 -1
+12 -1
View File
@@ -205,7 +205,7 @@ def _PyiTypeStr(buf: bytes, buf_size: t.CSizeT, pos: t.CSizeT,
pos = _PyiTypeStr(buf, buf_size, pos, bop.right)
return pos
# Constant: 字符串前向引用 (如 'MyClass') 或 None
# Constant: 字符串前向引用 (如 'MyClass')、None 或数字常量(CArray count)
if k == ast.ASTKind.Constant:
cn: ast.Constant | t.CPtr = (ast.Constant | t.CPtr)(annot)
if cn.const_kind == ast.CONST_STR:
@@ -216,6 +216,17 @@ def _PyiTypeStr(buf: bytes, buf_size: t.CSizeT, pos: t.CSizeT,
return pos
if cn.const_kind == ast.CONST_NONE:
return _PyiEmit(buf, buf_size, pos, "None")
# 数字常量(t.CArray[t.CUInt32T, 64] 的 count 等):输出数值本身,
# 而不是回退 t.CInt——否则 vpi 生成 "t.CArray[..., t.CInt]" 在加载
# stub 时触发 TypeError(count 必须是整数常量、None、CDefine 常量名
# 或常量表达式)。
if cn.const_kind == ast.CONST_INT or cn.const_kind == ast.CONST_CHAR:
num_buf_ct: str = stdlib.malloc(24)
if num_buf_ct is not None:
viperlib.snprintf(num_buf_ct, 24, "%d", cn.int_val)
pos = _PyiEmit(buf, buf_size, pos, num_buf_ct)
stdlib.free(num_buf_ct)
return pos
return _PyiEmit(buf, buf_size, pos, "t.CInt")
# Tuple: 多参数下标 (如 Dict[str, int])