Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support __len__ #25

Merged
merged 3 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions example/classes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ pub const Dog = py.subclass("Dog", &.{Animal}, struct {
self.name.decref();
}

pub fn __len__(self: *const Self) usize {
_ = self;
return 4;
}

pub fn get_name(self: *const Self) !py.PyString {
return self.name;
}
Expand Down
10 changes: 10 additions & 0 deletions pydust/src/builtins.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
const py = @import("./pydust.zig");
const ffi = @import("./ffi.zig");
const PyError = @import("./errors.zig").PyError;

/// Get the length of the given object. Equivalent to len(obj) in Python.
pub fn len(object: anytype) !usize {
const obj = try py.PyObject.from(object);
const length = ffi.PyObject_Length(obj.py);
if (length < 0) return PyError.Propagate;
return length;
}

/// Import a module by fully-qualified name returning a PyObject.
pub fn import(module_name: [:0]const u8) !py.PyObject {
Expand Down
1 change: 1 addition & 0 deletions pydust/src/functions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub const Signature = struct {
const reservedNames = .{
"__new__",
"__init__",
"__len__",
"__del__",
};

Expand Down
21 changes: 19 additions & 2 deletions pydust/src/pytypes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,14 @@ fn Slots(comptime name: [:0]const u8, comptime definition: type, comptime Instan
if (@hasDecl(definition, "__del__")) {
slots_ = slots_ ++ .{ffi.PyType_Slot{
.slot = ffi.Py_tp_finalize,
.pfunc = @ptrCast(@constCast(&finalize)),
.pfunc = @ptrCast(@constCast(&tp_finalize)),
}};
}

if (@hasDecl(definition, "__len__")) {
slots_ = slots_ ++ .{ffi.PyType_Slot{
.slot = ffi.Py_mp_length,
.pfunc = @ptrCast(@constCast(&mp_length)),
}};
}

Expand All @@ -101,11 +108,21 @@ fn Slots(comptime name: [:0]const u8, comptime definition: type, comptime Instan
break :blk slots_;
};

fn mp_length(pyself: *ffi.PyObject) callconv(.C) isize {
const lenFunc = @field(definition, "__len__");
const self: *const Instance = @ptrCast(pyself);
const result = @as(isize, @intCast(lenFunc(&self.state)));
if (@typeInfo(@typeInfo(@TypeOf(lenFunc)).Fn.return_type.?) == .ErrorUnion) {
return result catch return -1;
}
return result;
}

/// Wrapper for the user's __del__ function.
/// Note: tp_del is deprecated in favour of tp_finalize.
///
/// See https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_finalize.
fn finalize(pyself: *ffi.PyObject) void {
fn tp_finalize(pyself: *ffi.PyObject) void {
// The finalize slot shouldn't alter any exception that is currently set.
// So it's recommended we save the existing one (if any) and restore it afterwards.
// TODO(ngates): we may want to move this logic to PyErr if it happens more?
Expand Down
4 changes: 4 additions & 0 deletions test/test_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ def test_super():
assert adopted.get_name() == "Dug"
assert adopted.get_kind_name() == "Dog named Dug"
assert adopted.get_kind() == 1


def test_length():
assert len(classes.Dog("foo")) == 4