Returns a display string for a callable object for use in error messages. For objects with `__qualname__`, returns "module.qualname()" or "qualname()". For other objects, returns repr(obj).
(obj: &PyObject, vm: &VirtualMachine)
| 6501 | /// For objects with `__qualname__`, returns "module.qualname()" or "qualname()". |
| 6502 | /// For other objects, returns repr(obj). |
| 6503 | fn object_function_str(obj: &PyObject, vm: &VirtualMachine) -> Wtf8Buf { |
| 6504 | let repr_fallback = || { |
| 6505 | obj.repr(vm) |
| 6506 | .as_ref() |
| 6507 | .map_or("?".as_ref(), |s| s.as_wtf8()) |
| 6508 | .to_owned() |
| 6509 | }; |
| 6510 | let Ok(qualname) = obj.get_attr(vm.ctx.intern_str("__qualname__"), vm) else { |
| 6511 | return repr_fallback(); |
| 6512 | }; |
| 6513 | let Some(qualname_str) = qualname.downcast_ref::<PyStr>() else { |
| 6514 | return repr_fallback(); |
| 6515 | }; |
| 6516 | if let Ok(module) = obj.get_attr(vm.ctx.intern_str("__module__"), vm) |
| 6517 | && let Some(module_str) = module.downcast_ref::<PyStr>() |
| 6518 | && module_str.as_bytes() != b"builtins" |
| 6519 | { |
| 6520 | return wtf8_concat!(module_str.as_wtf8(), ".", qualname_str.as_wtf8(), "()"); |
| 6521 | } |
| 6522 | wtf8_concat!(qualname_str.as_wtf8(), "()") |
| 6523 | } |
| 6524 | |
| 6525 | /// Helper function to iterate over mapping keys using the keys() method. |
| 6526 | /// This ensures proper order preservation for OrderedDict and other custom mappings. |