MCPcopy Index your code
hub / github.com/RustPython/RustPython / __format__

Method __format__

crates/vm/src/builtins/str.rs:991–1010  ·  view source on GitHub ↗
(
        zelf: PyRef<PyStr>,
        spec: PyUtf8StrRef,
        vm: &VirtualMachine,
    )

Source from the content-addressed store, hash-verified

989
990 #[pymethod]
991 fn __format__(
992 zelf: PyRef<PyStr>,
993 spec: PyUtf8StrRef,
994 vm: &VirtualMachine,
995 ) -> PyResult<PyRef<PyStr>> {
996 if spec.is_empty() {
997 return if zelf.class().is(vm.ctx.types.str_type) {
998 Ok(zelf)
999 } else {
1000 zelf.as_object().str(vm)
1001 };
1002 }
1003 let zelf = zelf.try_into_utf8(vm)?;
1004 let s = FormatSpec::parse(spec.as_str())
1005 .and_then(|format_spec| {
1006 format_spec.format_string(&CharLenStr(zelf.as_str(), zelf.char_len()))
1007 })
1008 .map_err(|err| err.into_pyexception(vm))?;
1009 Ok(vm.ctx.new_str(s))
1010 }
1011
1012 #[pymethod]
1013 fn title(&self) -> Wtf8Buf {

Callers

nothing calls this directly

Calls 13

CharLenStrClass · 0.85
isMethod · 0.80
try_into_utf8Method · 0.80
parseFunction · 0.50
is_emptyMethod · 0.45
classMethod · 0.45
strMethod · 0.45
as_objectMethod · 0.45
as_strMethod · 0.45
format_stringMethod · 0.45
char_lenMethod · 0.45
into_pyexceptionMethod · 0.45

Tested by

no test coverage detected