Skip to content

Commit c6c1e7f

Browse files
authored
Merge pull request RustPython#453 from OddCoincidence/int-float-comparison
Fix int to float comparison
2 parents 392262d + 5fd912b commit c6c1e7f

File tree

2 files changed

+64
-23
lines changed

2 files changed

+64
-23
lines changed

tests/snippets/ints.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# int to int comparisons
2+
3+
assert 1 == 1
4+
assert not 1 != 1
5+
6+
assert (1).__eq__(1)
7+
assert not (1).__ne__(1)
8+
9+
# int to float comparisons
10+
11+
assert 1 == 1.0
12+
assert not 1 != 1.0
13+
assert not 1 > 1.0
14+
assert not 1 < 1.0
15+
assert 1 >= 1.0
16+
assert 1 <= 1.0
17+
18+
assert (1).__eq__(1.0) == NotImplemented
19+
assert (1).__ne__(1.0) == NotImplemented
20+
assert (1).__gt__(1.0) == NotImplemented
21+
assert (1).__ge__(1.0) == NotImplemented
22+
assert (1).__lt__(1.0) == NotImplemented
23+
assert (1).__le__(1.0) == NotImplemented

vm/src/obj/objint.rs

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,23 @@ fn int_eq(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
116116
let result = if objtype::isinstance(other, &vm.ctx.int_type()) {
117117
let other = BigInt::from_pyobj(other);
118118
zelf == other
119-
} else if objtype::isinstance(other, &vm.ctx.float_type()) {
120-
let other_float = objfloat::get_value(other);
119+
} else {
120+
return Ok(vm.ctx.not_implemented());
121+
};
122+
Ok(vm.ctx.new_bool(result))
123+
}
121124

122-
if let (Some(zelf_float), Some(other_int)) = (zelf.to_f64(), other_float.to_bigint()) {
123-
zelf_float == other_float && zelf == other_int
124-
} else {
125-
false
126-
}
125+
fn int_ne(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
126+
arg_check!(
127+
vm,
128+
args,
129+
required = [(zelf, Some(vm.ctx.int_type())), (other, None)]
130+
);
131+
132+
let zelf = BigInt::from_pyobj(zelf);
133+
let result = if objtype::isinstance(other, &vm.ctx.int_type()) {
134+
let other = BigInt::from_pyobj(other);
135+
zelf != other
127136
} else {
128137
return Ok(vm.ctx.not_implemented());
129138
};
@@ -134,11 +143,13 @@ fn int_lt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
134143
arg_check!(
135144
vm,
136145
args,
137-
required = [
138-
(zelf, Some(vm.ctx.int_type())),
139-
(other, Some(vm.ctx.int_type()))
140-
]
146+
required = [(zelf, Some(vm.ctx.int_type())), (other, None)]
141147
);
148+
149+
if !objtype::isinstance(other, &vm.ctx.int_type()) {
150+
return Ok(vm.ctx.not_implemented());
151+
}
152+
142153
let zelf = BigInt::from_pyobj(zelf);
143154
let other = BigInt::from_pyobj(other);
144155
let result = zelf < other;
@@ -149,11 +160,13 @@ fn int_le(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
149160
arg_check!(
150161
vm,
151162
args,
152-
required = [
153-
(zelf, Some(vm.ctx.int_type())),
154-
(other, Some(vm.ctx.int_type()))
155-
]
163+
required = [(zelf, Some(vm.ctx.int_type())), (other, None)]
156164
);
165+
166+
if !objtype::isinstance(other, &vm.ctx.int_type()) {
167+
return Ok(vm.ctx.not_implemented());
168+
}
169+
157170
let zelf = BigInt::from_pyobj(zelf);
158171
let other = BigInt::from_pyobj(other);
159172
let result = zelf <= other;
@@ -164,11 +177,13 @@ fn int_gt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
164177
arg_check!(
165178
vm,
166179
args,
167-
required = [
168-
(zelf, Some(vm.ctx.int_type())),
169-
(other, Some(vm.ctx.int_type()))
170-
]
180+
required = [(zelf, Some(vm.ctx.int_type())), (other, None)]
171181
);
182+
183+
if !objtype::isinstance(other, &vm.ctx.int_type()) {
184+
return Ok(vm.ctx.not_implemented());
185+
}
186+
172187
let zelf = BigInt::from_pyobj(zelf);
173188
let other = BigInt::from_pyobj(other);
174189
let result = zelf > other;
@@ -179,11 +194,13 @@ fn int_ge(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
179194
arg_check!(
180195
vm,
181196
args,
182-
required = [
183-
(zelf, Some(vm.ctx.int_type())),
184-
(other, Some(vm.ctx.int_type()))
185-
]
197+
required = [(zelf, Some(vm.ctx.int_type())), (other, None)]
186198
);
199+
200+
if !objtype::isinstance(other, &vm.ctx.int_type()) {
201+
return Ok(vm.ctx.not_implemented());
202+
}
203+
187204
let zelf = BigInt::from_pyobj(zelf);
188205
let other = BigInt::from_pyobj(other);
189206
let result = zelf >= other;
@@ -570,6 +587,7 @@ Base 0 means to interpret the base from the string as an integer literal.
570587
let int_type = &context.int_type;
571588

572589
context.set_attr(&int_type, "__eq__", context.new_rustfunc(int_eq));
590+
context.set_attr(&int_type, "__ne__", context.new_rustfunc(int_ne));
573591
context.set_attr(&int_type, "__lt__", context.new_rustfunc(int_lt));
574592
context.set_attr(&int_type, "__le__", context.new_rustfunc(int_le));
575593
context.set_attr(&int_type, "__gt__", context.new_rustfunc(int_gt));

0 commit comments

Comments
 (0)