Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -4162,7 +4162,6 @@ def test_global_enum_str(self):
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')
Expand Down
8 changes: 6 additions & 2 deletions crates/vm/src/builtins/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,13 @@ impl PyComplex {
}

#[pymethod]
fn __format__(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
fn __format__(zelf: &Py<Self>, spec: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
// 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))
}
}
Expand Down
8 changes: 6 additions & 2 deletions crates/vm/src/builtins/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,13 @@ fn float_from_string(val: PyObjectRef, vm: &VirtualMachine) -> PyResult<f64> {
)]
impl PyFloat {
#[pymethod]
fn __format__(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
fn __format__(zelf: &Py<Self>, spec: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
// 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))
}

Expand Down
8 changes: 6 additions & 2 deletions crates/vm/src/builtins/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,13 @@ impl PyInt {
}

#[pymethod]
fn __format__(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
fn __format__(zelf: &Py<Self>, spec: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
// 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))
}

Expand Down
Loading