forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwarnings.rs
More file actions
39 lines (36 loc) · 1.16 KB
/
warnings.rs
File metadata and controls
39 lines (36 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
pub(crate) use _warnings::make_module;
#[pymodule]
mod _warnings {
use crate::{
builtins::{PyStrRef, PyTypeRef},
function::OptionalArg,
PyResult, TypeProtocol, VirtualMachine,
};
#[derive(FromArgs)]
struct WarnArgs {
#[pyarg(positional)]
message: PyStrRef,
#[pyarg(any, optional)]
category: OptionalArg<PyTypeRef>,
#[pyarg(any, optional)]
stacklevel: OptionalArg<u32>,
}
#[pyfunction]
fn warn(args: WarnArgs, vm: &VirtualMachine) -> PyResult<()> {
// TODO: Implement correctly
let level = args.stacklevel.unwrap_or(1);
let category = if let OptionalArg::Present(category) = args.category {
if !category.issubclass(&vm.ctx.exceptions.warning) {
return Err(vm.new_type_error(format!(
"category must be a Warning subclass, not '{}'",
category.class().name()
)));
}
category
} else {
vm.ctx.exceptions.user_warning.clone()
};
eprintln!("level:{}: {}: {}", level, category.name(), args.message);
Ok(())
}
}