Collect all leaf exception IDs from an exception (group).
(
exc: &PyObjectRef,
leaf_ids: &mut HashSet<usize>,
vm: &VirtualMachine,
)
| 2909 | |
| 2910 | /// Collect all leaf exception IDs from an exception (group). |
| 2911 | fn collect_exception_group_leaf_ids( |
| 2912 | exc: &PyObjectRef, |
| 2913 | leaf_ids: &mut HashSet<usize>, |
| 2914 | vm: &VirtualMachine, |
| 2915 | ) { |
| 2916 | if vm.is_none(exc) { |
| 2917 | return; |
| 2918 | } |
| 2919 | |
| 2920 | // If not an exception group, it's a leaf - add its ID |
| 2921 | if !exc.fast_isinstance(vm.ctx.exceptions.base_exception_group) { |
| 2922 | leaf_ids.insert(exc.get_id()); |
| 2923 | return; |
| 2924 | } |
| 2925 | |
| 2926 | // Recurse into exception group's exceptions |
| 2927 | if let Ok(excs_attr) = exc.get_attr("exceptions", vm) |
| 2928 | && let Ok(tuple) = excs_attr.downcast::<PyTuple>() |
| 2929 | { |
| 2930 | for e in tuple.iter() { |
| 2931 | collect_exception_group_leaf_ids(e, leaf_ids, vm); |
| 2932 | } |
| 2933 | } |
| 2934 | } |
| 2935 | |
| 2936 | /// Project orig onto keep list, preserving nested structure. |
| 2937 | /// Returns an exception group containing only the exceptions from orig |