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

Method repr

crates/vm/src/builtins/union.rs:60–97  ·  view source on GitHub ↗
(&self, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

58 }
59
60 fn repr(&self, vm: &VirtualMachine) -> PyResult<String> {
61 fn repr_item(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
62 if obj.is(vm.ctx.types.none_type) {
63 return Ok("None".to_string());
64 }
65
66 if vm
67 .get_attribute_opt(obj.clone(), identifier!(vm, __origin__))?
68 .is_some()
69 && vm
70 .get_attribute_opt(obj.clone(), identifier!(vm, __args__))?
71 .is_some()
72 {
73 return Ok(obj.repr(vm)?.to_string());
74 }
75
76 match (
77 vm.get_attribute_opt(obj.clone(), identifier!(vm, __qualname__))?
78 .and_then(|o| o.downcast_ref::<PyStr>().map(|n| n.to_string())),
79 vm.get_attribute_opt(obj.clone(), identifier!(vm, __module__))?
80 .and_then(|o| o.downcast_ref::<PyStr>().map(|m| m.to_string())),
81 ) {
82 (None, _) | (_, None) => Ok(obj.repr(vm)?.to_string()),
83 (Some(qualname), Some(module)) => Ok(if module == "builtins" {
84 qualname
85 } else {
86 format!("{module}.{qualname}")
87 }),
88 }
89 }
90
91 Ok(self
92 .args
93 .iter()
94 .map(|o| repr_item(o.clone(), vm))
95 .collect::<PyResult<Vec<_>>>()?
96 .join(" | "))
97 }
98}
99
100#[pyclass(

Callers 2

repr_itemMethod · 0.45
repr_strMethod · 0.45

Calls 4

joinMethod · 0.45
mapMethod · 0.45
iterMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected