Skip to content

Commit c2db23d

Browse files
Merge pull request #417 from janczer/fix_lt_le_ge_gt_float
float.{__lt__, __le__, __ge__, __gt__} fix comparing with non float type
2 parents fc38d55 + 454837f commit c2db23d

2 files changed

Lines changed: 69 additions & 32 deletions

File tree

tests/snippets/floats.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,24 @@
1818
assert a + b == 2.5
1919
assert a - c == 0
2020
assert a / c == 1
21+
22+
assert a < 5
23+
assert a <= 5
24+
try:
25+
assert a < 'a'
26+
except TypeError:
27+
pass
28+
try:
29+
assert a <= 'a'
30+
except TypeError:
31+
pass
32+
assert a > 1
33+
assert a >= 1
34+
try:
35+
assert a > 'a'
36+
except TypeError:
37+
pass
38+
try:
39+
assert a >= 'a'
40+
except TypeError:
41+
pass

vm/src/obj/objfloat.rs

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -83,60 +83,76 @@ fn float_lt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
8383
arg_check!(
8484
vm,
8585
args,
86-
required = [
87-
(zelf, Some(vm.ctx.float_type())),
88-
(other, Some(vm.ctx.float_type()))
89-
]
86+
required = [(i, Some(vm.ctx.float_type())), (i2, None)]
9087
);
91-
let zelf = get_value(zelf);
92-
let other = get_value(other);
93-
let result = zelf < other;
94-
Ok(vm.ctx.new_bool(result))
88+
89+
let v1 = get_value(i);
90+
if objtype::isinstance(i2, &vm.ctx.float_type()) {
91+
Ok(vm.ctx.new_bool(v1 < get_value(i2)))
92+
} else if objtype::isinstance(i2, &vm.ctx.int_type()) {
93+
Ok(vm
94+
.ctx
95+
.new_bool(v1 < objint::get_value(i2).to_f64().unwrap()))
96+
} else {
97+
Err(vm.new_type_error(format!("Cannot compare {} and {}", i.borrow(), i2.borrow())))
98+
}
9599
}
96100

97101
fn float_le(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
98102
arg_check!(
99103
vm,
100104
args,
101-
required = [
102-
(zelf, Some(vm.ctx.float_type())),
103-
(other, Some(vm.ctx.float_type()))
104-
]
105+
required = [(i, Some(vm.ctx.float_type())), (i2, None)]
105106
);
106-
let zelf = get_value(zelf);
107-
let other = get_value(other);
108-
let result = zelf <= other;
109-
Ok(vm.ctx.new_bool(result))
107+
108+
let v1 = get_value(i);
109+
if objtype::isinstance(i2, &vm.ctx.float_type()) {
110+
Ok(vm.ctx.new_bool(v1 <= get_value(i2)))
111+
} else if objtype::isinstance(i2, &vm.ctx.int_type()) {
112+
Ok(vm
113+
.ctx
114+
.new_bool(v1 <= objint::get_value(i2).to_f64().unwrap()))
115+
} else {
116+
Err(vm.new_type_error(format!("Cannot compare {} and {}", i.borrow(), i2.borrow())))
117+
}
110118
}
111119

112120
fn float_gt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
113121
arg_check!(
114122
vm,
115123
args,
116-
required = [
117-
(zelf, Some(vm.ctx.float_type())),
118-
(other, Some(vm.ctx.float_type()))
119-
]
124+
required = [(i, Some(vm.ctx.float_type())), (i2, None)]
120125
);
121-
let zelf = get_value(zelf);
122-
let other = get_value(other);
123-
let result = zelf > other;
124-
Ok(vm.ctx.new_bool(result))
126+
127+
let v1 = get_value(i);
128+
if objtype::isinstance(i2, &vm.ctx.float_type()) {
129+
Ok(vm.ctx.new_bool(v1 > get_value(i2)))
130+
} else if objtype::isinstance(i2, &vm.ctx.int_type()) {
131+
Ok(vm
132+
.ctx
133+
.new_bool(v1 > objint::get_value(i2).to_f64().unwrap()))
134+
} else {
135+
Err(vm.new_type_error(format!("Cannot compare {} and {}", i.borrow(), i2.borrow())))
136+
}
125137
}
126138

127139
fn float_ge(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
128140
arg_check!(
129141
vm,
130142
args,
131-
required = [
132-
(zelf, Some(vm.ctx.float_type())),
133-
(other, Some(vm.ctx.float_type()))
134-
]
143+
required = [(i, Some(vm.ctx.float_type())), (i2, None)]
135144
);
136-
let zelf = get_value(zelf);
137-
let other = get_value(other);
138-
let result = zelf >= other;
139-
Ok(vm.ctx.new_bool(result))
145+
146+
let v1 = get_value(i);
147+
if objtype::isinstance(i2, &vm.ctx.float_type()) {
148+
Ok(vm.ctx.new_bool(v1 >= get_value(i2)))
149+
} else if objtype::isinstance(i2, &vm.ctx.int_type()) {
150+
Ok(vm
151+
.ctx
152+
.new_bool(v1 >= objint::get_value(i2).to_f64().unwrap()))
153+
} else {
154+
Err(vm.new_type_error(format!("Cannot compare {} and {}", i.borrow(), i2.borrow())))
155+
}
140156
}
141157

142158
fn float_abs(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

0 commit comments

Comments
 (0)