From 3b17235f8e50e0a2d2896cdd6e0eab0e27f77ceb Mon Sep 17 00:00:00 2001 From: Robert Kruszewski Date: Mon, 30 Oct 2023 17:10:50 +0000 Subject: [PATCH] something --- pydust/src/functions.zig | 19 +++++++------------ pydust/src/trampoline.zig | 5 +++-- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/pydust/src/functions.zig b/pydust/src/functions.zig index 58fe4941..9e1543f2 100644 --- a/pydust/src/functions.zig +++ b/pydust/src/functions.zig @@ -315,17 +315,9 @@ pub fn unwrapArgs(comptime Args: type, pyargs: py.Args, pykwargs: py.Kwargs) !Ar var args: Args = undefined; const s = @typeInfo(Args).Struct; - var varargsFieldIdx: usize = undefined; - var varkwargsFieldIdx: usize = undefined; var argIdx: usize = 0; - inline for (s.fields, 0..) |field, fieldIdx| { - if (field.type == py.Args) { - // Variadic args - varargsFieldIdx = fieldIdx; - } else if (field.type == py.Kwargs) { - // Variadic kwargs - varkwargsFieldIdx = fieldIdx; - } else if (field.default_value) |def_value| { + inline for (s.fields) |field| { + if (field.default_value) |def_value| { // We have a kwarg. if (kwargs.fetchRemove(field.name)) |entry| { @field(args, field.name) = try py.as(field.type, entry.value); @@ -334,7 +326,7 @@ pub fn unwrapArgs(comptime Args: type, pyargs: py.Args, pykwargs: py.Kwargs) !Ar const defaultValue: *field.type = @alignCast(@ptrCast(@constCast(def_value))); @field(args, field.name) = defaultValue.*; } - } else { + } else if (field.type != py.Args and field.type != py.Kwargs) { // Otherwise, we have a regular argument. if (argIdx >= pyargs.len) { return py.TypeError.raiseFmt("Expected {d} arg{s}", .{ @@ -366,8 +358,11 @@ pub fn unwrapArgs(comptime Args: type, pyargs: py.Args, pykwargs: py.Kwargs) !Ar return args; } -pub fn deinitArgs(comptime Args: type, args: Args) void { +pub fn deinitArgs(comptime Args: type, args: Args, allPosArgs: []py.PyObject) void { const s = @typeInfo(Args).Struct; + if (comptime varArgsIdx(Args)) |idx| { + py.allocator.free(allPosArgs[0..idx]); + } inline for (s.fields) |field| { if (field.type == py.Args) { py.allocator.free(@field(args, field.name)); diff --git a/pydust/src/trampoline.zig b/pydust/src/trampoline.zig index 7053c275..63d163e1 100644 --- a/pydust/src/trampoline.zig +++ b/pydust/src/trampoline.zig @@ -282,6 +282,7 @@ pub fn Trampoline(comptime T: type) type { const ZigCallArgs = struct { argsStruct: T, + allPosArgs: []py.PyObject, pub fn unwrap(pyargs: ?py.PyTuple, pykwargs: ?py.PyDict) PyError!@This() { var kwargs = py.Kwargs.init(py.allocator); @@ -300,11 +301,11 @@ pub fn Trampoline(comptime T: type) type { } } - return .{ .argsStruct = try funcs.unwrapArgs(T, args, kwargs) }; + return .{ .argsStruct = try funcs.unwrapArgs(T, args, kwargs), .allPosArgs = args }; } pub fn deinit(self: @This()) void { - funcs.deinitArgs(T, self.argsStruct); + funcs.deinitArgs(T, self.argsStruct, self.allPosArgs); } }; };