Merge pull request #417 from janczer/fix_lt_le_ge_gt_float

float.{__lt__, __le__, __ge__, __gt__} fix comparing with non float type
This commit is contained in:
Windel Bouwman
2019-02-10 12:10:58 +01:00
committed by GitHub
2 changed files with 69 additions and 32 deletions

View File

@@ -18,3 +18,24 @@ assert not a >= b
assert a + b == 2.5
assert a - c == 0
assert a / c == 1
assert a < 5
assert a <= 5
try:
assert a < 'a'
except TypeError:
pass
try:
assert a <= 'a'
except TypeError:
pass
assert a > 1
assert a >= 1
try:
assert a > 'a'
except TypeError:
pass
try:
assert a >= 'a'
except TypeError:
pass

View File

@@ -83,60 +83,76 @@ fn float_lt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [
(zelf, Some(vm.ctx.float_type())),
(other, Some(vm.ctx.float_type()))
]
required = [(i, Some(vm.ctx.float_type())), (i2, None)]
);
let zelf = get_value(zelf);
let other = get_value(other);
let result = zelf < other;
Ok(vm.ctx.new_bool(result))
let v1 = get_value(i);
if objtype::isinstance(i2, &vm.ctx.float_type()) {
Ok(vm.ctx.new_bool(v1 < get_value(i2)))
} else if objtype::isinstance(i2, &vm.ctx.int_type()) {
Ok(vm
.ctx
.new_bool(v1 < objint::get_value(i2).to_f64().unwrap()))
} else {
Err(vm.new_type_error(format!("Cannot compare {} and {}", i.borrow(), i2.borrow())))
}
}
fn float_le(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [
(zelf, Some(vm.ctx.float_type())),
(other, Some(vm.ctx.float_type()))
]
required = [(i, Some(vm.ctx.float_type())), (i2, None)]
);
let zelf = get_value(zelf);
let other = get_value(other);
let result = zelf <= other;
Ok(vm.ctx.new_bool(result))
let v1 = get_value(i);
if objtype::isinstance(i2, &vm.ctx.float_type()) {
Ok(vm.ctx.new_bool(v1 <= get_value(i2)))
} else if objtype::isinstance(i2, &vm.ctx.int_type()) {
Ok(vm
.ctx
.new_bool(v1 <= objint::get_value(i2).to_f64().unwrap()))
} else {
Err(vm.new_type_error(format!("Cannot compare {} and {}", i.borrow(), i2.borrow())))
}
}
fn float_gt(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [
(zelf, Some(vm.ctx.float_type())),
(other, Some(vm.ctx.float_type()))
]
required = [(i, Some(vm.ctx.float_type())), (i2, None)]
);
let zelf = get_value(zelf);
let other = get_value(other);
let result = zelf > other;
Ok(vm.ctx.new_bool(result))
let v1 = get_value(i);
if objtype::isinstance(i2, &vm.ctx.float_type()) {
Ok(vm.ctx.new_bool(v1 > get_value(i2)))
} else if objtype::isinstance(i2, &vm.ctx.int_type()) {
Ok(vm
.ctx
.new_bool(v1 > objint::get_value(i2).to_f64().unwrap()))
} else {
Err(vm.new_type_error(format!("Cannot compare {} and {}", i.borrow(), i2.borrow())))
}
}
fn float_ge(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
arg_check!(
vm,
args,
required = [
(zelf, Some(vm.ctx.float_type())),
(other, Some(vm.ctx.float_type()))
]
required = [(i, Some(vm.ctx.float_type())), (i2, None)]
);
let zelf = get_value(zelf);
let other = get_value(other);
let result = zelf >= other;
Ok(vm.ctx.new_bool(result))
let v1 = get_value(i);
if objtype::isinstance(i2, &vm.ctx.float_type()) {
Ok(vm.ctx.new_bool(v1 >= get_value(i2)))
} else if objtype::isinstance(i2, &vm.ctx.int_type()) {
Ok(vm
.ctx
.new_bool(v1 >= objint::get_value(i2).to_f64().unwrap()))
} else {
Err(vm.new_type_error(format!("Cannot compare {} and {}", i.borrow(), i2.borrow())))
}
}
fn float_abs(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {