From e8ed8aab9f3674fa09534486b85cb475dfd38b07 Mon Sep 17 00:00:00 2001 From: Gyubong Date: Mon, 1 Aug 2022 01:52:15 +0900 Subject: [PATCH] Fix `PyFloat::py_new` always returning new float object issue (#3979) --- extra_tests/snippets/builtin_float.py | 2 ++ vm/src/builtins/float.rs | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/extra_tests/snippets/builtin_float.py b/extra_tests/snippets/builtin_float.py index 18cbc2f42..8d93a5e2c 100644 --- a/extra_tests/snippets/builtin_float.py +++ b/extra_tests/snippets/builtin_float.py @@ -441,6 +441,8 @@ assert float('inf').hex() == 'inf' assert float('-inf').hex() == '-inf' assert float('nan').hex() == 'nan' +assert float(math.nan) is float(math.nan) + # Test float exponent: assert 1 if 1else 0 == 1 diff --git a/vm/src/builtins/float.rs b/vm/src/builtins/float.rs index 2584b5209..54c203832 100644 --- a/vm/src/builtins/float.rs +++ b/vm/src/builtins/float.rs @@ -142,6 +142,10 @@ impl Constructor for PyFloat { let float_val = match arg { OptionalArg::Missing => 0.0, OptionalArg::Present(val) => { + if cls.is(vm.ctx.types.float_type) && val.class().is(vm.ctx.types.float_type) { + return Ok(val); + } + if let Some(f) = val.try_float_opt(vm)? { f.value } else {