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

Fix trampoline optionals #105

Merged
merged 1 commit into from
Sep 16, 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
2 changes: 1 addition & 1 deletion pydust/src/functions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ fn valueToStr(comptime T: type, value: *const anyopaque) []const u8 {
},
inline .Bool => if (@as(*const bool, @ptrCast(value)).*) "True" else "False",
inline .Struct => "...",
inline .Optional => if (value == null) "None" else "...",
inline .Optional => |o| if (@as(*const ?o.child, @alignCast(@ptrCast(value))).* == null) "None" else "...",
inline else => std.fmt.comptimePrint("{any}", .{@as(*const T, @alignCast(@ptrCast(value))).*}),
};
}
2 changes: 1 addition & 1 deletion pydust/src/pydust.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ const pytypes = @import("pytypes.zig");
const ClassDef = pytypes.ClassDef;
const funcs = @import("functions.zig");
const tramp = @import("trampoline.zig");
const PyError = @import("errors.zig").PyError;

// Export some useful things for users
pub usingnamespace builtins;
pub usingnamespace conversions;
pub usingnamespace types;
pub const ffi = @import("ffi.zig");
pub const PyError = @import("errors.zig").PyError;
pub const allocator: std.mem.Allocator = mem.PyMemAllocator.allocator();

const Self = @This();
Expand Down
6 changes: 3 additions & 3 deletions pydust/src/trampoline.zig
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,20 @@ pub fn Trampoline(comptime T: type) type {

/// Unwrap a Python object into a Zig object. Does not steal a reference.
/// The Python object must be the correct corresponding type (vs a cast which coerces values).
pub inline fn unwrap(object: ?py.PyObject) !T {
pub inline fn unwrap(object: ?py.PyObject) py.PyError!T {
// Handle the error case explicitly, then we can unwrap the error case entirely.
const typeInfo = @typeInfo(T);

// Early return to handle errors
if (typeInfo == .ErrorUnion) {
const value = object catch |err| return err;
return Trampoline(typeInfo.ErrorUnion.payload).unwrap(value);
return @as(T, Trampoline(typeInfo.ErrorUnion.payload).unwrap(value));
}

// Early return to handle optionals
if (typeInfo == .Optional) {
const value = object orelse return null;
return Trampoline(typeInfo.Optional.child).unwrap(value);
return @as(T, try Trampoline(typeInfo.Optional.child).unwrap(value));
}

// Otherwise we can unwrap the object.
Expand Down
7 changes: 7 additions & 0 deletions pydust/src/types/str.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ pub const PyString = extern struct {
return .{ .obj = .{ .py = unicode } };
}

pub fn fmt(comptime format: []const u8, args: anytype) !py.PyString {
const str = try std.fmt.allocPrint(py.allocator, format, args);
defer py.allocator.free(str);

return create(str);
}

/// Append other to self.
///
/// Warning: a reference to self is stolen. Use concat, or self.incref(), if you don't own a reference to self.
Expand Down