Validate and resolve the category argument, matching get_category() in C.
(
message: &PyObjectRef,
category: Option<PyObjectRef>,
vm: &VirtualMachine,
)
| 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<()> { |
no test coverage detected