Skip to content

Commit e8ed8aa

Browse files
authored
Fix PyFloat::py_new always returning new float object issue (RustPython#3979)
1 parent 0341e72 commit e8ed8aa

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

extra_tests/snippets/builtin_float.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,8 @@ def identical(x, y):
441441
assert float('-inf').hex() == '-inf'
442442
assert float('nan').hex() == 'nan'
443443

444+
assert float(math.nan) is float(math.nan)
445+
444446
# Test float exponent:
445447
assert 1 if 1else 0 == 1
446448

vm/src/builtins/float.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ impl Constructor for PyFloat {
142142
let float_val = match arg {
143143
OptionalArg::Missing => 0.0,
144144
OptionalArg::Present(val) => {
145+
if cls.is(vm.ctx.types.float_type) && val.class().is(vm.ctx.types.float_type) {
146+
return Ok(val);
147+
}
148+
145149
if let Some(f) = val.try_float_opt(vm)? {
146150
f.value
147151
} else {

0 commit comments

Comments
 (0)