Skip to content

Commit

Permalink
something
Browse files Browse the repository at this point in the history
  • Loading branch information
robert3005 committed Oct 30, 2023
1 parent 44b0487 commit 3b17235
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
19 changes: 7 additions & 12 deletions pydust/src/functions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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}", .{
Expand Down Expand Up @@ -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));
Expand Down
5 changes: 3 additions & 2 deletions pydust/src/trampoline.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
};
};
Expand Down

0 comments on commit 3b17235

Please sign in to comment.