Fix PyFloat::py_new always returning new float object issue#3979
Fix PyFloat::py_new always returning new float object issue#3979youknowone merged 6 commits intoRustPython:mainfrom
PyFloat::py_new always returning new float object issue#3979Conversation
|
It seems below incompatibility make extra_test failed. class F(float):
def __int__(self):
return 3
# rustpython
>>>>> type(F(1.2))
<class 'float'>
# cpython
>>> type(F(1.2))
<class '__main__.F'> |
|
We need to move the exact float type check into |
So I tried to edit pub fn try_float_opt(&self, vm: &VirtualMachine) -> PyResult<Option<PyRef<PyFloat>>> {
let value = if let Some(float) = self.payload_if_exact::<PyFloat>(vm) {
Some(float.into_ref(vm))
} else {
let number = self.to_number();
#[allow(clippy::manual_map)]
if let Some(f) = number.float(vm)? {
Some(f)
} else if let Some(i) = self.try_index_opt(vm) {
let value = int::try_to_float(i?.as_bigint(), vm)?;
Some(vm.ctx.new_float(value))
} else if let Some(value) = self.downcast_ref::<PyFloat>() {
Some(vm.ctx.new_float(value.to_f64()))
} else {
None
}
};
Ok(value)
}I guess this is because below line returned new float instance instead of existing one. https://github.com/RustPython/RustPython/blob/main/vm/src/builtins/float.rs#L152-L154 So, I think maybe we need to update the |
|
you are right. you will find PyInt have a similar logic you are looking for |
|
looks good, would you add a related test to extra_tests? |
2eefd2c to
f894029
Compare
Fixes #3978.
Reference
https://github.com/python/cpython/blob/main/Objects/abstract.c#L1633-L1635
https://github.com/RustPython/RustPython/blob/main/vm/src/builtins/int.rs#L258-L264