blob: 018d8b3e8786565c0729a3a1feb8a7b374c9d797 (
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: zpy.Tuple, kwargs: ?zpy.Dict) zpy.Error!zpy.Object {
const kwargs_ptr: ?*c.PyObject = if (kwargs) |kw| kw.object.ptr else null;
if (c.PyObject_Call(self.ptr, args.object.ptr, kwargs_ptr)) |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});
}
|