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
49 lines (44 loc) · 1.19 KB
/
warnings.rs
File metadata and controls
49 lines (44 loc) · 1.19 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
40
41
42
43
44
45
46
47
48
49
pub(crate) use _warnings::make_module;
use crate::{builtins::PyType, Py, PyResult, VirtualMachine};
pub fn warn(
category: &Py<PyType>,
message: String,
stack_level: usize,
vm: &VirtualMachine,
) -> PyResult<()> {
// TODO: use rust warnings module
if let Ok(module) = vm.import("warnings", None, 0) {
if let Ok(func) = module.get_attr("warn", vm) {
let _ = vm.invoke(&func, (message, category.to_owned(), stack_level));
}
}
Ok(())
}
#[pymodule]
mod _warnings {
use crate::{
builtins::{PyStrRef, PyTypeRef},
function::OptionalArg,
PyResult, 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<()> {
let level = args.stacklevel.unwrap_or(1);
crate::warn::warn(
args.message,
args.category.into_option(),
level as isize,
None,
vm,
)
}
}