blob: 782071fecd7025ffa6eef6518313f359b9208d10 (
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
36
37
38
39
40
|
const std = @import("std");
const c = @import("c");
const zpy = @import("root.zig");
const Self = @This();
ptr: *c.PyObject,
pub fn fromPtr(ptr: *c.PyObject) Self {
return .{ .ptr = ptr };
}
pub fn fromC(ptr: [*c]c.PyObject) ?Self {
return if (ptr) |p| .fromPtr(p) else null;
}
pub fn decref(self: *Self) void {
c.Py_DecRef(self.ptr);
}
pub fn repr(self: *const Self) zpy.Str {
return .{ .object = .fromPtr(c.PyObject_Repr(self.ptr)) };
}
pub fn call(self: *Self, args: anytype) zpy.Error!zpy.Object {
_ = args;
if (c.PyObject_CallObject(self.ptr, null)) |object| {
return .fromPtr(object);
}
return error.Exception;
}
pub fn format(self: *const Self, writer: *std.Io.Writer) !void {
var str = self.repr();
defer str.deinit();
try writer.print("{s}", .{str.asUtf8() catch return error.WriteFailed});
}
|