diff options
Diffstat (limited to 'src/object.zig')
| -rw-r--r-- | src/object.zig | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/object.zig b/src/object.zig new file mode 100644 index 0000000..70392c0 --- /dev/null +++ b/src/object.zig @@ -0,0 +1,35 @@ +const std = @import("std"); +const c = @import("c"); +const zpy = @import("root.zig"); + +pub const Object = extern struct { + const Self = @This(); + + data: c.PyObject, + + pub fn decref(self: *Self) void { + c.Py_DecRef(@ptrCast(self)); + } + + pub fn repr(self: *const Self) *zpy.Str { + return @ptrCast(c.PyObject_Repr(@ptrCast(@constCast(self)))); + } + + pub fn call(self: *Self, args: *zpy.Tuple, kwargs: ?*zpy.Dict) !*zpy.Object { + if (c.PyObject_Call( + @ptrCast(self), + @ptrCast(args), + @ptrCast(kwargs), + )) |object| { + return @ptrCast(object); + } + return error.Exception; + } + + pub fn format(self: *const Self, writer: *std.Io.Writer) !void { + var str = self.repr(); + defer str.asObject().decref(); + + try writer.print("{s}", .{str.asUtf8() catch return error.WriteFailed}); + } +}; |