summaryrefslogtreecommitdiff
path: root/src/object.zig
blob: 70392c01685fccf40ff4dfa3dbd13f36904cd55e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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});
    }
};