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

Method format

crates/vm/src/vm/vm_ops.rs:551–576  ·  view source on GitHub ↗

PyObject_Format

(&self, obj: &PyObject, format_spec: PyStrRef)

Source from the content-addressed store, hash-verified

549
550 // PyObject_Format
551 pub fn format(&self, obj: &PyObject, format_spec: PyStrRef) -> PyResult<PyStrRef> {
552 if format_spec.is_empty() {
553 let obj = match obj.to_owned().downcast_exact::<PyStr>(self) {
554 Ok(s) => return Ok(s.into_pyref()),
555 Err(obj) => obj,
556 };
557 if obj.class().is(self.ctx.types.int_type) {
558 return obj.str(self);
559 }
560 }
561 let bound_format = self
562 .get_special_method(obj, identifier!(self, __format__))?
563 .ok_or_else(|| {
564 self.new_type_error(format!(
565 "Type {} doesn't define __format__",
566 obj.class().name()
567 ))
568 })?;
569 let formatted = bound_format.invoke((format_spec,), self)?;
570 formatted.downcast().map_err(|result| {
571 self.new_type_error(format!(
572 "__format__ must return a str, not {}",
573 &result.class().name()
574 ))
575 })
576 }
577 pub fn format_utf8(&self, obj: &PyObject, format_spec: PyStrRef) -> PyResult<PyRef<PyUtf8Str>> {
578 self.format(obj, format_spec)?.try_into_utf8(self)
579 }

Callers 12

format_sizeFunction · 0.45
_search_registryMethod · 0.45
_verbose_messageFunction · 0.45
format_internalFunction · 0.45
get_git_dateFunction · 0.45
get_git_timeFunction · 0.45
execute_instructionMethod · 0.45
formatFunction · 0.45
asctimeFunction · 0.45
ctimeFunction · 0.45
new_localMethod · 0.45
format_utf8Method · 0.45

Calls 10

into_pyrefMethod · 0.80
isMethod · 0.80
ok_or_elseMethod · 0.80
get_special_methodMethod · 0.80
downcastMethod · 0.80
is_emptyMethod · 0.45
to_ownedMethod · 0.45
classMethod · 0.45
strMethod · 0.45
invokeMethod · 0.45

Tested by

no test coverage detected