Skip to content

Commit a85f512

Browse files
committed
Fix Float.fromString and reorder Object.compare args
1 parent b6baddc commit a85f512

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

py.zig

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ pub inline fn ObjectProtocol(comptime T: type) type {
697697

698698
// Compare the values of o1 and o2 using the operation specified by opid, like PyObject_RichCompare(),
699699
// but returns -1 on error, 0 if the result is false, 1 otherwise.
700-
pub inline fn compare(self: *const T, other: *const Object, comptime op: CompareOp) !bool {
700+
pub inline fn compare(self: *const T, comptime op: CompareOp, other: *const Object) !bool {
701701
const flag = switch (op) {
702702
.lt => c.Py_LT,
703703
.lte => c.Py_LE,
@@ -706,24 +706,25 @@ pub inline fn ObjectProtocol(comptime T: type) type {
706706
.gt => c.Py_GT,
707707
.gte => c.Py_GE,
708708
};
709-
const r = self.compareUnchecked(other, flag);
709+
const r = self.compareUnchecked(flag, other);
710710
if (r < 0) {
711711
return error.PyError;
712712
}
713713
return r == 1;
714714
}
715715

716716
// Same as compare with no error or op checking
717-
pub inline fn compareUnchecked(self: *const T, other: *const Object, op: c_int) c_int {
718-
return @ptrCast(c.PyObject_RichCompareBool(@constCast(@ptrCast(self)), @constCast(@ptrCast(other)), op));
717+
pub inline fn compareUnchecked(self: *const T, op: c_int, other: *const Object) c_int {
718+
return c.PyObject_RichCompareBool(@constCast(@ptrCast(self)), @constCast(@ptrCast(other)), op);
719719
}
720720

721721
// Compare the values of o1 and o2 using the operation specified by opid, which must be one of Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, or Py_GE,
722722
// corresponding to <, <=, ==, !=, >, or >= respectively.
723723
// This is the equivalent of the Python expression o1 op o2,
724724
// where op is the operator corresponding to opid.
725725
// Returns the value of the comparison on success, or NULL on failure.
726-
pub inline fn compareObject(self: *const T, other: *const Object, comptime op: CompareOp) !*Object {
726+
// Returns new reference
727+
pub inline fn compareObject(self: *const T, comptime op: CompareOp, other: *const Object) !*Object {
727728
const flag = switch (op) {
728729
.lt => c.Py_LT,
729730
.lte => c.Py_LE,
@@ -732,14 +733,14 @@ pub inline fn ObjectProtocol(comptime T: type) type {
732733
.gt => c.Py_GT,
733734
.gte => c.Py_GE,
734735
};
735-
if (self.compareObjectUnchecked(self, other, flag)) |r| {
736+
if (self.compareObjectUnchecked(flag, other)) |r| {
736737
return r;
737738
}
738739
return error.PyError;
739740
}
740741

741742
// Same as compareObject with no error or op checking
742-
pub inline fn compareObjectUnchecked(self: *const T, other: *const Object, op: c_int) ?*Object {
743+
pub inline fn compareObjectUnchecked(self: *const T, op: c_int, other: *const Object) ?*Object {
743744
return @ptrCast(c.PyObject_RichCompare(@constCast(@ptrCast(self)), @constCast(@ptrCast(other)), op));
744745
}
745746

@@ -1284,15 +1285,20 @@ pub const Float = extern struct {
12841285
pub const fromDouble = new;
12851286
pub const fromDoubleUnchecked = newUnchecked;
12861287

1288+
pub inline fn fromInt(int: *Int) !*Float {
1289+
const v = try int.as(f64);
1290+
return try Float.new(v);
1291+
}
1292+
12871293
// Create a PyFloatObject object based on the string value in str, or NULL on failure.
1288-
pub inline fn fromString(value: [:0]const u8) !*Float {
1294+
pub inline fn fromString(value: *Str) !*Float {
12891295
if (fromStringUnchecked(value)) |r| {
12901296
return @ptrCast(r);
12911297
}
12921298
return error.PyError;
12931299
}
12941300

1295-
pub inline fn fromStringUnchecked(value: [:0]const u8) ?*Float {
1301+
pub inline fn fromStringUnchecked(value: *Str) ?*Float {
12961302
return @ptrCast(c.PyFloat_FromString(@ptrCast(value)));
12971303
}
12981304
};

0 commit comments

Comments
 (0)