Skip to content

Commit 2041b7c

Browse files
Fix int to float comparison
1 parent e5af4ca commit 2041b7c

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

tests/snippets/ints.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
assert not 1 > 1.0
2+
assert not 1 < 1.0
3+
assert 1 >= 1.0
4+
assert 1 <= 1.0
5+
6+
assert (1).__gt__(1.0) == NotImplemented
7+
assert (1).__ge__(1.0) == NotImplemented
8+
assert (1).__lt__(1.0) == NotImplemented
9+
assert (1).__le__(1.0) == NotImplemented

vm/src/obj/objint.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,13 @@ fn int_lt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
134134
arg_check!(
135135
vm,
136136
args,
137-
required = [
138-
(zelf, Some(vm.ctx.int_type())),
139-
(other, Some(vm.ctx.int_type()))
140-
]
137+
required = [(zelf, Some(vm.ctx.int_type())), (other, None)]
141138
);
139+
140+
if !objtype::isinstance(other, &vm.ctx.int_type()) {
141+
return Ok(vm.ctx.not_implemented());
142+
}
143+
142144
let zelf = BigInt::from_pyobj(zelf);
143145
let other = BigInt::from_pyobj(other);
144146
let result = zelf < other;
@@ -149,11 +151,13 @@ fn int_le(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
149151
arg_check!(
150152
vm,
151153
args,
152-
required = [
153-
(zelf, Some(vm.ctx.int_type())),
154-
(other, Some(vm.ctx.int_type()))
155-
]
154+
required = [(zelf, Some(vm.ctx.int_type())), (other, None)]
156155
);
156+
157+
if !objtype::isinstance(other, &vm.ctx.int_type()) {
158+
return Ok(vm.ctx.not_implemented());
159+
}
160+
157161
let zelf = BigInt::from_pyobj(zelf);
158162
let other = BigInt::from_pyobj(other);
159163
let result = zelf <= other;
@@ -164,11 +168,13 @@ fn int_gt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
164168
arg_check!(
165169
vm,
166170
args,
167-
required = [
168-
(zelf, Some(vm.ctx.int_type())),
169-
(other, Some(vm.ctx.int_type()))
170-
]
171+
required = [(zelf, Some(vm.ctx.int_type())), (other, None)]
171172
);
173+
174+
if !objtype::isinstance(other, &vm.ctx.int_type()) {
175+
return Ok(vm.ctx.not_implemented());
176+
}
177+
172178
let zelf = BigInt::from_pyobj(zelf);
173179
let other = BigInt::from_pyobj(other);
174180
let result = zelf > other;
@@ -179,11 +185,13 @@ fn int_ge(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
179185
arg_check!(
180186
vm,
181187
args,
182-
required = [
183-
(zelf, Some(vm.ctx.int_type())),
184-
(other, Some(vm.ctx.int_type()))
185-
]
188+
required = [(zelf, Some(vm.ctx.int_type())), (other, None)]
186189
);
190+
191+
if !objtype::isinstance(other, &vm.ctx.int_type()) {
192+
return Ok(vm.ctx.not_implemented());
193+
}
194+
187195
let zelf = BigInt::from_pyobj(zelf);
188196
let other = BigInt::from_pyobj(other);
189197
let result = zelf >= other;

0 commit comments

Comments
 (0)