MCPcopy Index your code
hub / github.com/RustPython/RustPython / get_condition_matcher

Function get_condition_matcher

crates/vm/src/exception_group.rs:391–429  ·  view source on GitHub ↗
(
        condition: &PyObject,
        vm: &VirtualMachine,
    )

Source from the content-addressed store, hash-verified

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> {

Callers 2

subgroupMethod · 0.85
splitMethod · 0.85

Calls 14

newFunction · 0.85
TypesClass · 0.85
fast_issubclassMethod · 0.80
try_into_valueMethod · 0.80
is_callableMethod · 0.80
TypeEnum · 0.50
ErrClass · 0.50
CallableInterface · 0.50
to_ownedMethod · 0.45
iterMethod · 0.45
cloneMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected