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

Function get_category

crates/vm/src/stdlib/_warnings.rs:99–130  ·  view source on GitHub ↗

Validate and resolve the category argument, matching get_category() in C.

(
        message: &PyObjectRef,
        category: Option<PyObjectRef>,
        vm: &VirtualMachine,
    )

Source from the content-addressed store, hash-verified

97
98 /// Validate and resolve the category argument, matching get_category() in C.
99 fn get_category(
100 message: &PyObjectRef,
101 category: Option<PyObjectRef>,
102 vm: &VirtualMachine,
103 ) -> PyResult<Option<PyTypeRef>> {
104 let cat_obj = match category {
105 Some(c) if !vm.is_none(&c) => c,
106 _ => {
107 if message.fast_isinstance(vm.ctx.exceptions.warning) {
108 return Ok(Some(message.class().to_owned()));
109 } else {
110 return Ok(None); // will default to UserWarning in warn_explicit
111 }
112 }
113 };
114
115 let cat = PyTypeRef::try_from_object(vm, cat_obj.clone()).map_err(|_| {
116 vm.new_type_error(format!(
117 "category must be a Warning subclass, not '{}'",
118 cat_obj.class().name()
119 ))
120 })?;
121
122 if !cat.fast_issubclass(vm.ctx.exceptions.warning) {
123 return Err(vm.new_type_error(format!(
124 "category must be a Warning subclass, not '{}'",
125 cat.class().name()
126 )));
127 }
128
129 Ok(Some(cat))
130 }
131
132 #[pyfunction]
133 fn warn(args: WarnArgs, vm: &VirtualMachine) -> PyResult<()> {

Callers 1

warnFunction · 0.85

Calls 8

fast_isinstanceMethod · 0.80
fast_issubclassMethod · 0.80
SomeClass · 0.50
ErrClass · 0.50
is_noneMethod · 0.45
to_ownedMethod · 0.45
classMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected