summaryrefslogtreecommitdiff
path: root/src/type
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2026-09-02 08:17:42 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2026-09-02 08:17:42 +0200
commit6c2e06ed20cdea1a67629554d8aa1d5fcda030f9 (patch)
treee5fa1f199a35e455917f661a62b38505afa703d7 /src/type
parent804204db7cf1e8a28defd359d17fa72949e13eff (diff)
Implement sequences and tuples
Diffstat (limited to 'src/type')
-rw-r--r--src/type/Watcher.zig43
-rw-r--r--src/type/root.zig58
2 files changed, 101 insertions, 0 deletions
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;
+ }
+}
diff --git a/src/type/root.zig b/src/type/root.zig
new file mode 100644
index 0000000..128f3d9
--- /dev/null
+++ b/src/type/root.zig
@@ -0,0 +1,58 @@
+const std = @import("std");
+const c = @import("c");
+
+const zpy = @import("../root.zig");
+
+pub const Type = extern struct {
+ const Self = @This();
+
+ data: c.PyObject,
+
+ pub const Watcher = @import("Watcher.zig");
+
+ pub const Description = extern struct {
+ name: [*:0]const u8,
+ doc: ?[*:0]const u8 = null,
+ };
+
+ pub fn new(description: Description, comptime fields: []const Description) !*Self {
+ var c_desc: c.PyStructSequence_Desc = .{
+ .name = @ptrCast(description.name),
+ .doc = @ptrCast(description.doc),
+ .fields = @ptrCast(@constCast(fields)),
+ .n_in_sequence = @intCast(fields.len),
+ };
+
+ return @ptrCast(c.PyStructSequence_NewType(@ptrCast(&c_desc)) orelse return error.Exception);
+ }
+
+ pub fn getDict(self: *Self) *zpy.Dict {
+ return @ptrCast(c.PyType_GetDict(@ptrCast(self)));
+ }
+
+ pub fn modified(self: *Self) void {
+ c.PyType_Modified(@ptrCast(self));
+ }
+
+ pub fn isGc(self: *Self) bool {
+ return c.PyType_IS_GC(@ptrCast(self)) != 0;
+ }
+
+ pub fn isSubtype(self: *Self, target: *Self) bool {
+ return c.PyType_IsSubtype(@ptrCast(self), @ptrCast(target)) != 0;
+ }
+
+ pub fn ready(self: *Self) !void {
+ if (c.PyType_Ready(@ptrCast(self)) == -1) {
+ return error.Exception;
+ }
+ }
+
+ pub fn getName(self: *Self) *zpy.Str {
+ return @ptrCast(c.Pytype_GetName(@ptrCast(self)));
+ }
+
+ pub fn asObject(self: *Self) *zpy.Object {
+ return @ptrCast(self);
+ }
+};