| 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( |