(
zelf: PyRef<PyBaseException>,
condition: PyObjectRef,
vm: &VirtualMachine,
)
| 126 | |
| 127 | #[pymethod] |
| 128 | fn split( |
| 129 | zelf: PyRef<PyBaseException>, |
| 130 | condition: PyObjectRef, |
| 131 | vm: &VirtualMachine, |
| 132 | ) -> PyResult<PyTupleRef> { |
| 133 | let matcher = get_condition_matcher(&condition, vm)?; |
| 134 | |
| 135 | // If self matches the condition entirely |
| 136 | let zelf_obj: PyObjectRef = zelf.clone().into(); |
| 137 | if matcher.check(&zelf_obj, vm)? { |
| 138 | return Ok(vm.ctx.new_tuple(vec![zelf_obj, vm.ctx.none()])); |
| 139 | } |
| 140 | |
| 141 | let exceptions = get_exceptions_tuple(&zelf, vm)?; |
| 142 | let mut matching: Vec<PyObjectRef> = Vec::new(); |
| 143 | let mut rest: Vec<PyObjectRef> = Vec::new(); |
| 144 | |
| 145 | for exc in exceptions { |
| 146 | if is_base_exception_group(&exc, vm) { |
| 147 | let result = vm.call_method(&exc, "split", (condition.clone(),))?; |
| 148 | let result_tuple: PyTupleRef = result.try_into_value(vm)?; |
| 149 | let match_part = result_tuple |
| 150 | .first() |
| 151 | .cloned() |
| 152 | .unwrap_or_else(|| vm.ctx.none()); |
| 153 | let rest_part = result_tuple |
| 154 | .get(1) |
| 155 | .cloned() |
| 156 | .unwrap_or_else(|| vm.ctx.none()); |
| 157 | |
| 158 | if !vm.is_none(&match_part) { |
| 159 | matching.push(match_part); |
| 160 | } |
| 161 | if !vm.is_none(&rest_part) { |
| 162 | rest.push(rest_part); |
| 163 | } |
| 164 | } else if matcher.check(&exc, vm)? { |
| 165 | matching.push(exc); |
| 166 | } else { |
| 167 | rest.push(exc); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | let match_group = if matching.is_empty() { |
| 172 | vm.ctx.none() |
| 173 | } else { |
| 174 | derive_and_copy_attributes(&zelf, matching, vm)? |
| 175 | }; |
| 176 | |
| 177 | let rest_group = if rest.is_empty() { |
| 178 | vm.ctx.none() |
| 179 | } else { |
| 180 | derive_and_copy_attributes(&zelf, rest, vm)? |
| 181 | }; |
| 182 | |
| 183 | Ok(vm.ctx.new_tuple(vec![match_group, rest_group])) |
| 184 | } |
| 185 |
nothing calls this directly
no test coverage detected