Skip to content

Commit fafd843

Browse files
authored
add getformat (RustPython#3774)
1 parent ce546f6 commit fafd843

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

vm/src/builtins/float.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,24 @@ impl PyFloat {
204204
}
205205
}
206206

207+
#[pymethod(magic)]
208+
fn getformat(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
209+
if !matches!(spec.as_str(), "double" | "float") {
210+
return Err(vm.new_value_error(
211+
"__getformat__() argument 1 must be 'double' or 'float'".to_owned(),
212+
));
213+
}
214+
215+
const BIG_ENDIAN: bool = cfg!(target_endian = "big");
216+
217+
Ok(if BIG_ENDIAN {
218+
"IEEE, big-endian"
219+
} else {
220+
"IEEE, little-endian"
221+
}
222+
.to_owned())
223+
}
224+
207225
#[pymethod(magic)]
208226
fn abs(&self) -> f64 {
209227
self.value.abs()

0 commit comments

Comments
 (0)