Skip to content
This repository was archived by the owner on Apr 3, 2026. It is now read-only.

Commit a43094b

Browse files
committed
Make errorObject's provide the return value
1 parent 67fecf6 commit a43094b

2 files changed

Lines changed: 17 additions & 16 deletions

File tree

example/pyzigtest.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ const Module = py.Module;
99
pub fn add(mod: *Module, args: [*]*Object, n: isize) ?*Object {
1010
_ = mod;
1111
if (n != 2) {
12-
return py.typeErrorObject("sum requires 2 arguments", .{});
12+
return py.typeErrorObject(null, "sum requires 2 arguments", .{});
1313
}
1414
// We can now safely access indexes 0 and 1
1515
if (!Int.check(args[0]) or !Int.check(args[1])) {
16-
return py.typeErrorObject("both arguments must be ints!", .{});
16+
return py.typeErrorObject(null, "both arguments must be ints!", .{});
1717
}
1818

1919
// We can now safely cast to Int objects and access their methods

py.zig

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,48 +110,49 @@ pub inline fn typeError(msg: [:0]const u8, args: anytype) !void {
110110
return errorFormat(@ptrCast(c.PyExc_TypeError), msg, args);
111111
}
112112

113-
pub inline fn typeErrorObject(msg: [:0]const u8, args: anytype) ?*Object {
114-
typeError(msg, args) catch return null;
115-
unreachable;
113+
pub inline fn typeErrorObject(comptime value: anytype, msg: [:0]const u8, args: anytype) @TypeOf(value) {
114+
typeError(msg, args) catch {};
115+
return value;
116116
}
117117

118118
// Helper that is the equivalent to `SystemError(msg)`
119119
pub inline fn systemError(msg: [:0]const u8, args: anytype) !void {
120120
return errorFormat(@ptrCast(c.PyExc_SystemError), msg, args);
121121
}
122122

123-
pub inline fn systemErrorObject(msg: [:0]const u8, args: anytype) ?*Object {
124-
systemError(msg, args) catch return null;
125-
unreachable;
123+
pub inline fn systemErrorObject(comptime value: anytype, msg: [:0]const u8, args: anytype) @TypeOf(value) {
124+
systemError(msg, args) catch {};
125+
return value;
126126
}
127127

128128
// Helper that is the equivalent to `ValueError(msg)`
129129
pub inline fn valueError(msg: [:0]const u8, args: anytype) !void {
130130
return errorFormat(@ptrCast(c.PyExc_ValueError), msg, args);
131131
}
132132

133-
pub inline fn valueErrorObject(msg: [:0]const u8, args: anytype) ?*Object {
134-
valueError(msg, args) catch return null;
135-
unreachable;
133+
pub inline fn valueErrorObject(comptime value: anytype, msg: [:0]const u8, args: anytype) @TypeOf(value) {
134+
valueError(msg, args) catch {};
135+
return value;
136136
}
137137

138138
// Helper that is the equivalent to `AttributeError(msg)`
139139
pub inline fn attributeError(msg: [:0]const u8, args: anytype) !void {
140140
return errorFormat(@ptrCast(c.PyExc_AttributeError), msg, args);
141141
}
142142

143-
pub inline fn attributeErrorObject(msg: [:0]const u8, args: anytype) ?*Object {
144-
attributeError(msg, args) catch return null;
145-
unreachable;
143+
pub inline fn attributeErrorObject(comptime value: anytype, msg: [:0]const u8, args: anytype) @TypeOf(value) {
144+
attributeError(msg, args) catch {};
145+
return value;
146146
}
147147

148148
pub inline fn memoryError() !void {
149149
_ = c.PyErr_NoMemory();
150150
return error.PyError;
151151
}
152152

153-
pub inline fn memoryErrorObject() ?*Object {
154-
return @ptrCast(c.PyErr_NoMemory());
153+
pub inline fn memoryErrorObject(comptime value: anytype) @TypeOf(value) {
154+
memoryError() catch {};
155+
return value;
155156
}
156157

157158
// Clear a reference to **Object or *?*Object

0 commit comments

Comments
 (0)