Skip to content

Commit f3b073a

Browse files
authored
Merge pull request RustPython#1626 from youknowone/refactor-get_float_value
Refacter objint
2 parents 1005dcc + 0932d69 commit f3b073a

File tree

4 files changed

+134
-217
lines changed

4 files changed

+134
-217
lines changed

vm/src/obj/objbool.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::pyobject::{
77
};
88
use crate::vm::VirtualMachine;
99

10-
use super::objint::PyInt;
10+
use super::objint::{self, PyInt};
1111
use super::objstr::PyStringRef;
1212
use super::objtype;
1313

@@ -111,7 +111,7 @@ pub fn not(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<bool> {
111111

112112
// Retrieve inner int value:
113113
pub fn get_value(obj: &PyObjectRef) -> bool {
114-
!obj.payload::<PyInt>().unwrap().as_bigint().is_zero()
114+
!objint::get_py_int(obj).as_bigint().is_zero()
115115
}
116116

117117
fn bool_repr(obj: bool, _vm: &VirtualMachine) -> String {
@@ -142,7 +142,7 @@ fn bool_or(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult
142142
let rhs = get_value(&rhs);
143143
(lhs || rhs).into_pyobject(vm)
144144
} else {
145-
Ok(lhs.payload::<PyInt>().unwrap().or(rhs.clone(), vm))
145+
Ok(objint::get_py_int(&lhs).or(rhs.clone(), vm))
146146
}
147147
}
148148

@@ -154,7 +154,7 @@ fn bool_and(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult
154154
let rhs = get_value(&rhs);
155155
(lhs && rhs).into_pyobject(vm)
156156
} else {
157-
Ok(lhs.payload::<PyInt>().unwrap().and(rhs.clone(), vm))
157+
Ok(objint::get_py_int(&lhs).and(rhs.clone(), vm))
158158
}
159159
}
160160

@@ -166,7 +166,7 @@ fn bool_xor(lhs: PyObjectRef, rhs: PyObjectRef, vm: &VirtualMachine) -> PyResult
166166
let rhs = get_value(&rhs);
167167
(lhs ^ rhs).into_pyobject(vm)
168168
} else {
169-
Ok(lhs.payload::<PyInt>().unwrap().xor(rhs.clone(), vm))
169+
Ok(objint::get_py_int(&lhs).xor(rhs.clone(), vm))
170170
}
171171
}
172172

vm/src/obj/objfloat.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub fn try_float(value: &PyObjectRef, vm: &VirtualMachine) -> PyResult<Option<f6
5555
Ok(if objtype::isinstance(&value, &vm.ctx.float_type()) {
5656
Some(get_value(&value))
5757
} else if objtype::isinstance(&value, &vm.ctx.int_type()) {
58-
Some(objint::get_float_value(&value, vm)?)
58+
Some(from_int_value(&value, vm)?)
5959
} else {
6060
None
6161
})
@@ -678,11 +678,15 @@ fn invalid_convert(vm: &VirtualMachine, literal: &str) -> PyObjectRef {
678678
vm.new_value_error(format!("could not convert string to float: '{}'", literal))
679679
}
680680

681+
pub fn from_int_value(obj: &PyObjectRef, vm: &VirtualMachine) -> PyResult<f64> {
682+
objint::try_float(objint::get_py_int(obj).as_bigint(), vm)
683+
}
684+
681685
fn to_float(vm: &VirtualMachine, obj: &PyObjectRef) -> PyResult<f64> {
682686
let value = if objtype::isinstance(&obj, &vm.ctx.float_type()) {
683687
get_value(&obj)
684688
} else if objtype::isinstance(&obj, &vm.ctx.int_type()) {
685-
objint::get_float_value(&obj, vm)?
689+
from_int_value(&obj, vm)?
686690
} else if objtype::isinstance(&obj, &vm.ctx.str_type()) {
687691
str_to_float(vm, objstr::get_value(&obj).trim())?
688692
} else if objtype::isinstance(&obj, &vm.ctx.bytes_type()) {

0 commit comments

Comments
 (0)