diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index 9f09098eb..e2dc13427 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -4162,7 +4162,6 @@ class OldTestIntFlag(unittest.TestCase): self.assertEqual(str(NoName.ONE & NoName.TWO), 'NoName(0)') self.assertEqual(str(NoName(0)), 'NoName(0)') - @unittest.expectedFailure # TODO: RUSTPYTHON; format(NewPerm.R) does not use __str__ def test_format(self): Perm = self.Perm self.assertEqual(format(Perm.R, ''), '4') diff --git a/crates/vm/src/builtins/complex.rs b/crates/vm/src/builtins/complex.rs index 78729b2f5..dd6806155 100644 --- a/crates/vm/src/builtins/complex.rs +++ b/crates/vm/src/builtins/complex.rs @@ -276,9 +276,13 @@ impl PyComplex { } #[pymethod] - fn __format__(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult { + fn __format__(zelf: &Py, spec: PyStrRef, vm: &VirtualMachine) -> PyResult { + // Empty format spec: equivalent to str(self) + if spec.is_empty() { + return Ok(zelf.as_object().str(vm)?.as_str().to_owned()); + } FormatSpec::parse(spec.as_str()) - .and_then(|format_spec| format_spec.format_complex(&self.value)) + .and_then(|format_spec| format_spec.format_complex(&zelf.value)) .map_err(|err| err.into_pyexception(vm)) } } diff --git a/crates/vm/src/builtins/float.rs b/crates/vm/src/builtins/float.rs index 9941aea9b..230d288b7 100644 --- a/crates/vm/src/builtins/float.rs +++ b/crates/vm/src/builtins/float.rs @@ -214,9 +214,13 @@ fn float_from_string(val: PyObjectRef, vm: &VirtualMachine) -> PyResult { )] impl PyFloat { #[pymethod] - fn __format__(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult { + fn __format__(zelf: &Py, spec: PyStrRef, vm: &VirtualMachine) -> PyResult { + // Empty format spec: equivalent to str(self) + if spec.is_empty() { + return Ok(zelf.as_object().str(vm)?.as_str().to_owned()); + } FormatSpec::parse(spec.as_str()) - .and_then(|format_spec| format_spec.format_float(self.value)) + .and_then(|format_spec| format_spec.format_float(zelf.value)) .map_err(|err| err.into_pyexception(vm)) } diff --git a/crates/vm/src/builtins/int.rs b/crates/vm/src/builtins/int.rs index 765f5e245..273199f69 100644 --- a/crates/vm/src/builtins/int.rs +++ b/crates/vm/src/builtins/int.rs @@ -444,9 +444,13 @@ impl PyInt { } #[pymethod] - fn __format__(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult { + fn __format__(zelf: &Py, spec: PyStrRef, vm: &VirtualMachine) -> PyResult { + // Empty format spec on a subclass: equivalent to str(self) + if spec.is_empty() && !zelf.class().is(vm.ctx.types.int_type) { + return Ok(zelf.as_object().str(vm)?.as_str().to_owned()); + } FormatSpec::parse(spec.as_str()) - .and_then(|format_spec| format_spec.format_int(&self.value)) + .and_then(|format_spec| format_spec.format_int(&zelf.value)) .map_err(|err| err.into_pyexception(vm)) }