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

Method str

crates/vm/src/protocol/object.rs:387–411  ·  view source on GitHub ↗
(&self, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

385 self.str(vm)?.try_into_utf8(vm)
386 }
387 pub fn str(&self, vm: &VirtualMachine) -> PyResult<PyRef<PyStr>> {
388 let obj = match self.to_owned().downcast_exact::<PyStr>(vm) {
389 Ok(s) => return Ok(s.into_pyref()),
390 Err(obj) => obj,
391 };
392 // Fast path for exact int: skip __str__ method resolution
393 let obj = match obj.downcast_exact::<PyInt>(vm) {
394 Ok(int) => {
395 return Ok(vm.ctx.new_str(int.to_str_radix_10()));
396 }
397 Err(obj) => obj,
398 };
399 // TODO: replace to obj.class().slots.str
400 let str_method = match vm.get_special_method(&obj, identifier!(vm, __str__))? {
401 Some(str_method) => str_method,
402 None => return obj.repr(vm),
403 };
404 let s = str_method.invoke((), vm)?;
405 s.downcast::<PyStr>().map_err(|obj| {
406 vm.new_type_error(format!(
407 "__str__ returned non-string (type {})",
408 obj.class().name()
409 ))
410 })
411 }
412
413 // Equivalent to CPython's check_class. Returns Ok(()) if cls is a valid class,
414 // Err with TypeError if not. Uses abstract_get_bases internally.

Callers 15

run_shellFunction · 0.45
py_err_to_js_errFunction · 0.45
fetchFunction · 0.45
get_future_repr_infoFunction · 0.45
get_task_repr_infoFunction · 0.45
py_initMethod · 0.45
set_nameMethod · 0.45
repr_wtf8Method · 0.45
__str__Method · 0.45
format_internalFunction · 0.45
write_syntaxerrorMethod · 0.45

Calls 7

into_pyrefMethod · 0.80
to_str_radix_10Method · 0.80
get_special_methodMethod · 0.80
to_ownedMethod · 0.45
new_strMethod · 0.45
reprMethod · 0.45
invokeMethod · 0.45

Tested by

no test coverage detected