From 6c2e06ed20cdea1a67629554d8aa1d5fcda030f9 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Wed, 2 Sep 2026 08:17:42 +0200 Subject: Implement sequences and tuples --- src/type/Watcher.zig | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/type/Watcher.zig (limited to 'src/type/Watcher.zig') diff --git a/src/type/Watcher.zig b/src/type/Watcher.zig new file mode 100644 index 0000000..6369df5 --- /dev/null +++ b/src/type/Watcher.zig @@ -0,0 +1,43 @@ +const std = @import("std"); +const c = @import("c"); +const zpy = @import("../root.zig"); + +const Self = @This(); + +id: c_int, + +pub const Callback = fn (*zpy.Type) error{Exception}!void; + +pub fn add(callback: Callback) !Self { + const wrapper = struct { + pub fn wrapper(t: [*c]c.PyObject) callconv(.c) c_int { + return if (std.meta.isError(callback(@ptrCast(t)))) -1 else 0; + } + }; + + const result = c.PyType_AddWatcher(&wrapper); + + if (result == -1) { + return error.Exception; + } + + return .{ .id = result }; +} + +pub fn clear(self: Self) !void { + if (c.PyType_Clear(self.id) == -1) { + return error.Exception; + } +} + +pub fn watch(self: Self, t: *zpy.Dict) !void { + if (c.PyType_Watch(self.id, t) == -1) { + return error.Exception; + } +} + +pub fn unwatch(self: Self, t: *zpy.Dict) !void { + if (c.PyType_Unwatch(self.id, t) == -1) { + return error.Exception; + } +} -- cgit v1.2.3-70-g09d2