(
condition: &PyObject,
vm: &VirtualMachine,
)
| 389 | } |
| 390 | |
| 391 | fn get_condition_matcher( |
| 392 | condition: &PyObject, |
| 393 | vm: &VirtualMachine, |
| 394 | ) -> PyResult<ConditionMatcher> { |
| 395 | // If it's a type and subclass of BaseException |
| 396 | if let Some(typ) = condition.downcast_ref::<PyType>() |
| 397 | && typ.fast_issubclass(vm.ctx.exceptions.base_exception_type) |
| 398 | { |
| 399 | return Ok(ConditionMatcher::Type(typ.to_owned())); |
| 400 | } |
| 401 | |
| 402 | // If it's a tuple of types |
| 403 | if let Some(tuple) = condition.downcast_ref::<PyTuple>() { |
| 404 | let mut types = Vec::new(); |
| 405 | for item in tuple.iter() { |
| 406 | let typ: PyTypeRef = item.clone().try_into_value(vm).map_err(|_| { |
| 407 | vm.new_type_error( |
| 408 | "expected a function, exception type or tuple of exception types", |
| 409 | ) |
| 410 | })?; |
| 411 | if !typ.fast_issubclass(vm.ctx.exceptions.base_exception_type) { |
| 412 | return Err(vm.new_type_error( |
| 413 | "expected a function, exception type or tuple of exception types", |
| 414 | )); |
| 415 | } |
| 416 | types.push(typ); |
| 417 | } |
| 418 | if !types.is_empty() { |
| 419 | return Ok(ConditionMatcher::Types(types)); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | // If it's callable (but not a type) |
| 424 | if condition.is_callable() && condition.downcast_ref::<PyType>().is_none() { |
| 425 | return Ok(ConditionMatcher::Callable(condition.to_owned())); |
| 426 | } |
| 427 | |
| 428 | Err(vm.new_type_error("expected a function, exception type or tuple of exception types")) |
| 429 | } |
| 430 | |
| 431 | impl ConditionMatcher { |
| 432 | fn check(&self, exc: &PyObject, vm: &VirtualMachine) -> PyResult<bool> { |
no test coverage detected