我正在尝试在 Zig 中实例化一个 PyObject。在 C 中,PyObject 本质上定义为:
struct _object {
union {
Py_ssize_t ob_refcnt;
PY_UINT32_T ob_refcnt_split[2];
};
PyTypeObject *ob_type;
};
typedef struct _object PyObject;
Zig 翻译成:
const union_unnamed_11 = extern union {
ob_refcnt: Py_ssize_t,
ob_refcnt_split: [2]u32,
};
pub const struct__object = extern struct {
unnamed_0: union_unnamed_11 = @import("std").mem.zeroes(union_unnamed_11),
ob_type: [*c]PyTypeObject = @import("std").mem.zeroes([*c]PyTypeObject),
};
pub const PyObject = struct__object;
现在,我想做的只是实例化一个 PyObject,所以:
const py = @cImport({
@cInclude("Python.h");
});
var pyObject = py.PyObject{
.unnamed_0 = py.union_unnamed_11{ .ob_refcnt = 1 },
.ob_type = null,
};
// main fn and stuff..
但是当我尝试编译它时,我收到错误:
$ zig build-exe t.zig -I /usr/include/python3.13 -I /usr/include -I /usr/include/x86_64-linux-gnu
t.zig:8:20: error: 'union_unnamed_11' is not marked 'pub'
.unnamed_0 = py.union_unnamed_11{ .ob_refcnt = 1 },
这个错误当然是有道理的,但我无法真正更改联合类型
union_unnamed_11
。我怎样才能让它发挥作用?
我正在使用 Zig 0.13.0 版本。