From acdb48a8a84debe2b8992983b0f9a8a476736cce Mon Sep 17 00:00:00 2001 From: Robert Kruszewski Date: Thu, 9 Nov 2023 13:37:13 +0000 Subject: [PATCH] setOwnedItem takes usize and anytype as arguments (#247) --- pydust/src/pytypes.zig | 2 +- pydust/src/types/list.zig | 23 ++++++++++++++++++++--- pydust/src/types/tuple.zig | 23 ++++++++++++++++++++--- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/pydust/src/pytypes.zig b/pydust/src/pytypes.zig index af757281..d3c95f30 100644 --- a/pydust/src/pytypes.zig +++ b/pydust/src/pytypes.zig @@ -58,7 +58,7 @@ pub fn Type(comptime name: [:0]const u8, comptime definition: type) type { if (bases.bases.len > 0) { const basesTuple = try py.PyTuple.new(bases.bases.len); inline for (bases.bases, 0..) |base, i| { - try basesTuple.setOwnedItem(i, (try py.self(base)).obj); + try basesTuple.setOwnedItem(i, try py.self(base)); } basesPtr = basesTuple.obj.py; } diff --git a/pydust/src/types/list.zig b/pydust/src/types/list.zig index b22a7382..ca7a809f 100644 --- a/pydust/src/types/list.zig +++ b/pydust/src/types/list.zig @@ -54,15 +54,15 @@ pub const PyList = extern struct { } /// This function “steals” a reference to item and discards a reference to an item already in the list at the affected position. - pub fn setOwnedItem(self: PyList, pos: isize, value: anytype) !void { + pub fn setOwnedItem(self: PyList, pos: usize, value: anytype) !void { // Since this function steals the reference, it can only accept object-like values. - if (ffi.PyList_SetItem(self.obj.py, pos, py.object(value).py) < 0) { + if (ffi.PyList_SetItem(self.obj.py, @intCast(pos), py.object(value).py) < 0) { return PyError.PyRaised; } } /// Set the item at the given position. - pub fn setItem(self: PyList, pos: isize, value: anytype) !void { + pub fn setItem(self: PyList, pos: usize, value: anytype) !void { const valueObj = try py.create(value); return self.setOwnedItem(pos, valueObj); } @@ -138,3 +138,20 @@ test "PyList" { try std.testing.expectEqual(@as(usize, 4), tuple.length()); } + +test "PyList setOwnedItem" { + py.initialize(); + defer py.finalize(); + + var list = try PyList.new(2); + defer list.decref(); + const py1 = try py.create(1); + defer py1.decref(); + try list.setOwnedItem(0, py1); + const py2 = try py.create(2); + defer py2.decref(); + try list.setOwnedItem(1, py2); + + try std.testing.expectEqual(@as(u8, 1), try list.getItem(u8, 0)); + try std.testing.expectEqual(@as(u8, 2), try list.getItem(u8, 1)); +} diff --git a/pydust/src/types/tuple.zig b/pydust/src/types/tuple.zig index 4b303bf9..638f8f9a 100644 --- a/pydust/src/types/tuple.zig +++ b/pydust/src/types/tuple.zig @@ -82,14 +82,14 @@ pub const PyTuple = extern struct { /// Insert a reference to object o at position pos of the tuple. /// /// Warning: steals a reference to value. - pub fn setOwnedItem(self: *const PyTuple, pos: isize, value: PyObject) !void { - if (ffi.PyTuple_SetItem(self.obj.py, @intCast(pos), value.py) < 0) { + pub fn setOwnedItem(self: *const PyTuple, pos: usize, value: anytype) !void { + if (ffi.PyTuple_SetItem(self.obj.py, @intCast(pos), py.object(value).py) < 0) { return PyError.PyRaised; } } /// Insert a reference to object o at position pos of the tuple. Does not steal a reference to value. - pub fn setItem(self: *const PyTuple, pos: isize, value: PyObject) !void { + pub fn setItem(self: *const PyTuple, pos: usize, value: anytype) !void { if (ffi.PyTuple_SetItem(self.obj.py, @intCast(pos), value.py) < 0) { return PyError.PyRaised; } @@ -119,3 +119,20 @@ test "PyTuple" { try tuple.setItem(0, second.obj); try std.testing.expectEqual(@as(f64, 1.0), try tuple.getItem(f64, 0)); } + +test "PyTuple setOwnedItem" { + py.initialize(); + defer py.finalize(); + + var tuple = try PyTuple.new(2); + defer tuple.decref(); + const py1 = try py.create(1); + defer py1.decref(); + try tuple.setOwnedItem(0, py1); + const py2 = try py.create(2); + defer py2.decref(); + try tuple.setOwnedItem(1, py2); + + try std.testing.expectEqual(@as(u8, 1), try tuple.getItem(u8, 0)); + try std.testing.expectEqual(@as(u8, 2), try tuple.getItem(u8, 1)); +}