(
self,
value: PyObjectRef,
vm: &VirtualMachine,
)
| 445 | } |
| 446 | |
| 447 | pub fn instantiate_value( |
| 448 | self, |
| 449 | value: PyObjectRef, |
| 450 | vm: &VirtualMachine, |
| 451 | ) -> PyResult<PyBaseExceptionRef> { |
| 452 | let exc_inst = value.clone().downcast::<PyBaseException>().ok(); |
| 453 | match (self, exc_inst) { |
| 454 | // both are instances; which would we choose? |
| 455 | (Self::Instance(_exc_a), Some(_exc_b)) => { |
| 456 | Err(vm.new_type_error("instance exception may not have a separate value")) |
| 457 | } |
| 458 | // if the "type" is an instance and the value isn't, use the "type" |
| 459 | (Self::Instance(exc), None) => Ok(exc), |
| 460 | // if the value is an instance of the type, use the instance value |
| 461 | (Self::Class(cls), Some(exc)) if exc.fast_isinstance(&cls) => Ok(exc), |
| 462 | // otherwise; construct an exception of the type using the value as args |
| 463 | (Self::Class(cls), _) => { |
| 464 | let args = match_class!(match value { |
| 465 | PyNone => vec![], |
| 466 | tup @ PyTuple => tup.to_vec(), |
| 467 | exc @ PyBaseException => exc.args().to_vec(), |
| 468 | obj => vec![obj], |
| 469 | }); |
| 470 | vm.invoke_exception(cls, args) |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | #[derive(Debug)] |
no test coverage detected