diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-08-27 21:04:37 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-08-27 21:04:37 +0200 |
| commit | 28f47b8399c4f3c4a64921fb16e38e44e8ce749e (patch) | |
| tree | 53cde744130f2adf9c293fafde3c0a9c6a990d4b /src/Object.zig | |
first commit
Diffstat (limited to 'src/Object.zig')
| -rw-r--r-- | src/Object.zig | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/Object.zig b/src/Object.zig new file mode 100644 index 0000000..782071f --- /dev/null +++ b/src/Object.zig @@ -0,0 +1,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}); +} |