From cea2c235aafe5e8b92cea72a2be631363dea17a3 Mon Sep 17 00:00:00 2001 From: Nicholas Gates Date: Wed, 4 Oct 2023 12:56:35 +0100 Subject: [PATCH] Init does not use __new__ arguments (#173) `__new__` is only used when constructing the class from Python. To add factory methods in Zig, they should be wrapped in `py.zig` --- example/iterators.pyi | 6 ++++-- example/iterators.zig | 4 ---- pydust/src/pydust.zig | 22 ++-------------------- 3 files changed, 6 insertions(+), 26 deletions(-) diff --git a/example/iterators.pyi b/example/iterators.pyi index c41f3c66..a545a582 100644 --- a/example/iterators.pyi +++ b/example/iterators.pyi @@ -10,8 +10,10 @@ class Range: ... class RangeIterator: - def __init__(next, stop, step, /): - pass + """ + Range iterator + """ + def __next__(self, /): """ Implement next(self). diff --git a/example/iterators.zig b/example/iterators.zig index b050c0e8..b3186e95 100644 --- a/example/iterators.zig +++ b/example/iterators.zig @@ -40,10 +40,6 @@ pub const RangeIterator = py.class(struct { stop: i64, step: i64, - pub fn __new__(args: struct { next: i64, stop: i64, step: i64 }) Self { - return .{ .next = args.next, .stop = args.stop, .step = args.step }; - } - pub fn __next__(self: *Self) ?i64 { if (self.next >= self.stop) { return null; diff --git a/pydust/src/pydust.zig b/pydust/src/pydust.zig index f4525369..ebe3387f 100644 --- a/pydust/src/pydust.zig +++ b/pydust/src/pydust.zig @@ -40,7 +40,7 @@ pub fn finalize() void { } /// Instantiate a class defined in Pydust. -pub fn init(comptime Cls: type, args: NewArgs(Cls)) PyError!*Cls { +pub fn init(comptime Cls: type, args: Cls) PyError!*Cls { const moduleDefinition = State.getContaining(Cls, .module); const imported = try types.PyModule.import(State.getIdentifier(moduleDefinition).name); const pytype = try imported.obj.get(State.getIdentifier(Cls).name); @@ -49,29 +49,11 @@ pub fn init(comptime Cls: type, args: NewArgs(Cls)) PyError!*Cls { // NOTE(ngates): we currently don't allow users to override tp_alloc, therefore we can shortcut // using ffi.PyType_GetSlot(tp_alloc) since we know it will always return ffi.PyType_GenericAlloc const pyobj: *pytypes.PyTypeStruct(Cls) = @alignCast(@ptrCast(ffi.PyType_GenericAlloc(@ptrCast(pytype.py), 0) orelse return PyError.PyRaised)); - - if (@hasDecl(Cls, "__new__")) { - pyobj.state = try tramp.coerceError(Cls.__new__(args)); - } else if (@typeInfo(Cls).Struct.fields.len > 0) { - pyobj.state = args; - } + pyobj.state = args; return &pyobj.state; } -/// Find the type of the positional args for a class -inline fn NewArgs(comptime Cls: type) type { - if (!@hasDecl(Cls, "__new__")) { - // Default construct args are the struct fields themselves. - return Cls; - } - - const func = @field(Cls, "__new__"); - const typeInfo = @typeInfo(@TypeOf(func)); - const sig = funcs.parseSignature("__new__", typeInfo.Fn, &.{}); - return sig.argsParam orelse struct {}; -} - /// Register the root Pydust module pub fn rootmodule(comptime definition: type) void { if (!State.isEmpty()) {