summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/dict/Watcher.zig (renamed from src/Watcher.zig)2
-rw-r--r--src/dict/root.zig (renamed from src/dict.zig)4
-rw-r--r--src/main.zig14
-rw-r--r--src/root.zig6
-rw-r--r--src/sequence/fast.zig20
-rw-r--r--src/sequence/root.zig113
-rw-r--r--src/str.zig2
-rw-r--r--src/struct-sequence.zig25
-rw-r--r--src/tuple.zig19
-rw-r--r--src/type/Watcher.zig43
-rw-r--r--src/type/root.zig58
11 files changed, 298 insertions, 8 deletions
diff --git a/src/Watcher.zig b/src/dict/Watcher.zig
index 4a0a7c3..317a269 100644
--- a/src/Watcher.zig
+++ b/src/dict/Watcher.zig
@@ -1,6 +1,6 @@
const std = @import("std");
const c = @import("c");
-const zpy = @import("root.zig");
+const zpy = @import("../root.zig");
const Self = @This();
diff --git a/src/dict.zig b/src/dict/root.zig
index a38d18d..169add1 100644
--- a/src/dict.zig
+++ b/src/dict/root.zig
@@ -1,13 +1,15 @@
const std = @import("std");
const c = @import("c");
-const zpy = @import("root.zig");
+const zpy = @import("../root.zig");
pub const Dict = extern struct {
const Self = @This();
data: c.PyObject,
+ pub const Watcher = @import("Watcher.zig");
+
pub const Key = union(enum) {
string: [*c]const u8,
object: *zpy.Object,
diff --git a/src/main.zig b/src/main.zig
index fa6fe82..c87075e 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -31,4 +31,18 @@ pub fn main() !void {
while (global_iterator.next()) |pair| {
std.debug.print("{f}\n", .{pair.key});
}
+
+ const structtype: *zpy.Type = try .new(
+ .{ .name = "MyStruct" },
+ &.{
+ .{ .name = "first" },
+ .{ .name = "second" },
+ }
+ );
+
+ var struct_seq: *zpy.StructSequence = try .new(structtype);
+ struct_seq.set(0, (try zpy.Int.from(42)).asObject());
+ struct_seq.set(1, (zpy.Str.from("hello")).asObject());
+
+ std.debug.print("struct = {f}\n", .{struct_seq.asObject()});
}
diff --git a/src/root.zig b/src/root.zig
index c45470f..0f4d9a2 100644
--- a/src/root.zig
+++ b/src/root.zig
@@ -4,13 +4,15 @@ const err = @import("error.zig");
pub const Exception = err.Exception;
pub const Object = @import("object.zig").Object;
-pub const Dict = @import("dict.zig").Dict;
-pub const Watcher = @import("Watcher.zig");
+pub const Type = @import("type/root.zig").Type;
+pub const Dict = @import("dict/root.zig").Dict;
pub const Str = @import("str.zig").Str;
pub const Tuple = @import("tuple.zig").Tuple;
pub const Int = @import("int.zig").Int;
pub const Float = @import("float.zig").Float;
pub const List = @import("list.zig").List;
+pub const Sequence = @import("sequence/root.zig").Sequence;
+pub const StructSequence = @import("struct-sequence.zig").StructSequence;
pub const Interpreter = @import("Interpreter.zig");
diff --git a/src/sequence/fast.zig b/src/sequence/fast.zig
new file mode 100644
index 0000000..07f5de6
--- /dev/null
+++ b/src/sequence/fast.zig
@@ -0,0 +1,20 @@
+const c = @import("c");
+const zpy = @import("../root.zig");
+
+pub const Fast = extern struct {
+ const Self = @This();
+
+ data: c.PyObject,
+
+ pub fn size(self: *Self) !usize {
+ return @intCast(c.PySequence_Fast_GET_SIZE(@ptrCast(self)));
+ }
+
+ pub fn get(self: *Self, index: usize) *zpy.Object {
+ return @ptrCast(c.PySequence_Fast_GET_ITEM(@ptrCast(self), @intCast(index)) orelse return error.Exception);
+ }
+
+ pub fn items(self: *Self) [*:null][*c]zpy.Object {
+ return @ptrCast(c.PySequence_Fast_ITEMS(@ptrCast(self)));
+ }
+};
diff --git a/src/sequence/root.zig b/src/sequence/root.zig
new file mode 100644
index 0000000..107233b
--- /dev/null
+++ b/src/sequence/root.zig
@@ -0,0 +1,113 @@
+const c = @import("c");
+const zpy = @import("../root.zig");
+
+pub const Sequence = extern struct {
+ const Self = @This();
+ pub const Fast = @import("fast.zig").Fast;
+
+ data: c.PyObject,
+
+ pub fn size(self: *Self) usize {
+ return @intCast(c.PySequence_Size(@ptrCast(self)));
+ }
+
+ pub fn concat(self: *Self, tail: *Self) !*Self {
+ return @ptrCast(c.PySequence_Concat(
+ @ptrCast(self),
+ @ptrCast(tail),
+ ) orelse return error.Exception);
+ }
+
+ pub fn repeat(self: *Self, n: usize) !*Self {
+ return @ptrCast(c.PySequence_Repeat(
+ @ptrCast(self),
+ @intCast(n),
+ ) orelse return error.Exception);
+ }
+
+ pub fn inPlaceConcat(self: *Self, tail: *Self) !*Self {
+ return @ptrCast(c.PySequence_InPlaceConcat(
+ @ptrCast(self),
+ @ptrCast(tail),
+ ) orelse return error.Exception);
+ }
+
+ pub fn inPlaceRepeat(self: *Self, n: usize) !*Self {
+ return @ptrCast(c.PySequence_InPlaceRepeat(
+ @ptrCast(self),
+ @intCast(n),
+ ) orelse return error.Exception);
+ }
+
+ pub fn get(self: *Self, index: usize) ?*zpy.Object {
+ return @ptrCast(c.PySequence_GetItem(@ptrCast(self), @intCast(index)));
+ }
+
+ pub fn getSlice(self: *Self, from: usize, to: usize) ?*Self {
+ return @ptrCast(c.PySequence_GetSlice(@ptrCast(self), @intCast(from), @intCast(to)));
+ }
+
+ pub fn set(self: *Self, index: usize, value: *zpy.Object) !void {
+ if (c.PySequence_SetItem(@ptrCast(self), @intCast(index), @ptrCast(value)) == -1) {
+ return error.Exception;
+ }
+ }
+
+ pub fn delete(self: *Self, index: usize) !void {
+ if (c.PySequence_DelItem(@ptrCast(self), @intCast(index)) == -1) {
+ return error.Exception;
+ }
+ }
+
+ pub fn deleteSlice(self: *Self, from: usize, to: usize) !void {
+ if (c.PySequence_DelSlice(@ptrCast(self), @intCast(from), @intCast(to)) == -1) {
+ return error.Exception;
+ }
+ }
+
+ pub fn count(self: *const Self, value: *zpy.Object) !usize {
+ const result = c.PySequence_Count(@ptrCast(@constCast(self)), @ptrCast(value));
+
+ if (result == -1) {
+ return error.Exception;
+ }
+
+ return result;
+ }
+
+ pub fn contains(self: *const Self, value: *zpy.Object) !bool {
+ const result = c.PySequence_Contains(@ptrCast(@constCast(self)), @ptrCast(value));
+
+ if (result == -1) {
+ return error.Exception;
+ }
+
+ return result != 0;
+ }
+
+ pub fn indexOf(self: *const Self, value: *zpy.Object) !usize {
+ const index = c.PySequence_Index(@ptrCast(@constCast(self)), @ptrCast(value));
+
+ if (index == -1) {
+ return error.Exception;
+ }
+
+ return index;
+ }
+
+ pub fn toList(self: *Self) !*zpy.List {
+ return @ptrCast(c.PySequence_List(@ptrCast(self)) orelse return error.Exception);
+ }
+
+ pub fn toTuple(self: *Self) !*zpy.Tuple {
+ return @ptrCast(c.PySequence_Tuple(@ptrCast(self)) orelse return error.Exception);
+ }
+
+ pub fn asFast(self: *Self) !*Fast {
+ return @ptrCast(c.PySequence_Fast(@ptrCast(self), null) orelse return error.Exception);
+ }
+
+ pub fn asObject(self: *Self) *zpy.Object {
+ return @ptrCast(self);
+ }
+};
diff --git a/src/str.zig b/src/str.zig
index 7dde4f9..c66bd7e 100644
--- a/src/str.zig
+++ b/src/str.zig
@@ -7,7 +7,7 @@ pub const Str = extern struct {
data: c.PyObject,
- pub fn fromString(buffer: []const u8) *Self {
+ pub fn from(buffer: []const u8) *Self {
return @ptrCast(c.PyUnicode_FromStringAndSize(@ptrCast(buffer), @intCast(buffer.len)));
}
diff --git a/src/struct-sequence.zig b/src/struct-sequence.zig
new file mode 100644
index 0000000..bc1b8e7
--- /dev/null
+++ b/src/struct-sequence.zig
@@ -0,0 +1,25 @@
+const c = @import("c");
+
+const zpy = @import("root.zig");
+
+pub const StructSequence = extern struct {
+ const Self = @This();
+
+ data: c.PyObject,
+
+ pub fn new(t: *zpy.Type) !*Self {
+ return @ptrCast(c.PyStructSequence_New(@ptrCast(@constCast(t))) orelse return error.Exception);
+ }
+
+ pub fn get(self: *Self, index: usize) *Self {
+ return @ptrCast(c.PyStructSequence_GetItem(@ptrCast(self), @intCast(index)));
+ }
+
+ pub fn set(self: *Self, index: usize, value: *zpy.Object) void {
+ c.PyStructSequence_SetItem(@ptrCast(self), @intCast(index), @ptrCast(value));
+ }
+
+ pub fn asObject(self: *Self) *zpy.Object {
+ return @ptrCast(self);
+ }
+};
diff --git a/src/tuple.zig b/src/tuple.zig
index ba82003..4e53d3f 100644
--- a/src/tuple.zig
+++ b/src/tuple.zig
@@ -22,24 +22,37 @@ pub const Tuple = extern struct {
}
pub fn size(self: *const Self) usize {
- return c.PyTuple_GET_SIZE(self.object.ptr);
+ return c.PyTuple_GET_SIZE(@ptrCast(self));
}
- pub fn get(self: *Self, index: usize) !*zpy.Object {
+ pub fn get(self: *const Self, index: usize) !*zpy.Object {
return @ptrCast(
c.PyTuple_GetItem(
- self.object.ptr,
+ @ptrCast(self),
@intCast(index),
) orelse return error.Exception,
);
}
+ pub fn getUnchecked(self: *Self, index: usize) *zpy.Object {
+ return @ptrCast(c.PyTuple.GET_ITEM(
+ @ptrCast(self),
+ @intCast(index),
+ ));
+ }
+
pub fn set(self: *Self, index: usize, value: *zpy.Object) !void {
if (c.PyTuple_SetItem(@ptrCast(self), @intCast(index), @ptrCast(value)) == -1) {
return error.Exception;
}
}
+ pub fn resize(self: *Self, newlen: usize) !void {
+ if (c._PyTuple_Resize(@ptrCast(&self), @intCast(newlen)) == -1) {
+ return error.Exception;
+ }
+ }
+
pub fn asObject(self: *Self) *zpy.Object {
return @ptrCast(self);
}
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);
+ }
+};