Skip to content

Commit

Permalink
setOwnedItem takes usize and anytype as arguments (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
robert3005 authored Nov 9, 2023
1 parent 50171d7 commit acdb48a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pydust/src/pytypes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
23 changes: 20 additions & 3 deletions pydust/src/types/list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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));
}
23 changes: 20 additions & 3 deletions pydust/src/types/tuple.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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));
}

0 comments on commit acdb48a

Please sign in to comment.