forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_warnings.rs
More file actions
213 lines (188 loc) · 6.34 KB
/
Copy path_warnings.rs
File metadata and controls
213 lines (188 loc) · 6.34 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
pub(crate) use _warnings::module_def;
use crate::{Py, PyResult, VirtualMachine, builtins::PyType};
pub fn warn(
category: &Py<PyType>,
message: String,
stack_level: usize,
vm: &VirtualMachine,
) -> PyResult<()> {
crate::warn::warn(
vm.new_pyobj(message),
Some(category.to_owned()),
isize::try_from(stack_level).unwrap_or(isize::MAX),
None,
vm,
)
}
#[pymodule]
mod _warnings {
use crate::{
AsObject, PyObjectRef, PyResult, VirtualMachine,
builtins::{PyDictRef, PyListRef, PyStrRef, PyTupleRef, PyTypeRef},
convert::TryFromObject,
function::OptionalArg,
};
#[pyattr]
fn filters(vm: &VirtualMachine) -> PyListRef {
vm.state.warnings.filters.clone()
}
#[pyattr]
fn _defaultaction(vm: &VirtualMachine) -> PyStrRef {
vm.state.warnings.default_action.clone()
}
#[pyattr]
fn _onceregistry(vm: &VirtualMachine) -> PyDictRef {
vm.state.warnings.once_registry.clone()
}
#[pyattr]
fn _warnings_context(vm: &VirtualMachine) -> PyObjectRef {
vm.state
.warnings
.context_var
.get_or_init(|| {
// Try to create a real ContextVar if _contextvars is available.
// During early startup it may not be importable yet, in which
// case we fall back to None. This is safe because
// context_aware_warnings defaults to False.
if let Ok(contextvars) = vm.import("_contextvars", 0)
&& let Ok(cv_cls) = contextvars.get_attr("ContextVar", vm)
&& let Ok(cv) = cv_cls.call(("_warnings_context",), vm)
{
cv
} else {
vm.ctx.none()
}
})
.clone()
}
#[pyfunction]
fn _acquire_lock(vm: &VirtualMachine) {
vm.state.warnings.acquire_lock();
}
#[pyfunction]
fn _release_lock(vm: &VirtualMachine) -> PyResult<()> {
if !vm.state.warnings.release_lock() {
return Err(vm.new_runtime_error("cannot release un-acquired lock"));
}
Ok(())
}
#[pyfunction]
fn _filters_mutated_lock_held(vm: &VirtualMachine) {
vm.state.warnings.filters_mutated();
}
#[derive(FromArgs)]
struct WarnArgs {
#[pyarg(positional)]
message: PyObjectRef,
#[pyarg(any, optional)]
category: OptionalArg<PyObjectRef>,
#[pyarg(any, optional)]
stacklevel: OptionalArg<i32>,
#[pyarg(named, optional)]
source: OptionalArg<PyObjectRef>,
#[pyarg(named, optional)]
skip_file_prefixes: OptionalArg<PyTupleRef>,
}
/// Validate and resolve the category argument, matching get_category() in C.
fn get_category(
message: &PyObjectRef,
category: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<Option<PyTypeRef>> {
let cat_obj = match category {
Some(c) if !vm.is_none(&c) => c,
_ => {
return Ok(if message.fast_isinstance(vm.ctx.exceptions.warning) {
Some(message.class().to_owned())
} else {
None // will default to UserWarning in warn_explicit
});
}
};
let cat = PyTypeRef::try_from_object(vm, cat_obj.clone()).map_err(|_| {
vm.new_type_error(format!(
"category must be a Warning subclass, not '{}'",
cat_obj.class().name()
))
})?;
if !cat.fast_issubclass(vm.ctx.exceptions.warning) {
return Err(vm.new_type_error(format!(
"category must be a Warning subclass, not '{}'",
cat.class().name()
)));
}
Ok(Some(cat))
}
#[pyfunction]
fn warn(args: WarnArgs, vm: &VirtualMachine) -> PyResult<()> {
let level = args.stacklevel.unwrap_or(1) as isize;
let category = get_category(&args.message, args.category.into_option(), vm)?;
// Validate skip_file_prefixes: each element must be a str
let skip_prefixes = args.skip_file_prefixes.into_option();
if let Some(ref prefixes) = skip_prefixes {
for item in prefixes.iter() {
if !item.class().is(vm.ctx.types.str_type) {
return Err(vm.new_type_error("skip_file_prefixes must be a tuple of strs"));
}
}
}
crate::warn::warn_with_skip(
args.message,
category,
level,
args.source.into_option(),
skip_prefixes,
vm,
)
}
#[derive(FromArgs)]
struct WarnExplicitArgs {
#[pyarg(positional)]
message: PyObjectRef,
#[pyarg(positional)]
category: PyObjectRef,
#[pyarg(positional)]
filename: PyStrRef,
#[pyarg(positional)]
lineno: usize,
#[pyarg(any, optional)]
module: OptionalArg<PyObjectRef>,
#[pyarg(any, optional)]
registry: OptionalArg<PyObjectRef>,
#[pyarg(any, optional)]
module_globals: OptionalArg<PyObjectRef>,
#[pyarg(named, optional)]
source: OptionalArg<PyObjectRef>,
}
#[pyfunction]
fn warn_explicit(args: WarnExplicitArgs, vm: &VirtualMachine) -> PyResult<()> {
let registry = args.registry.into_option().unwrap_or_else(|| vm.ctx.none());
let module = args.module.into_option();
// Validate module_globals: must be None or a dict
if let Some(ref mg) = args.module_globals.into_option()
&& !vm.is_none(mg)
&& !mg.class().is(vm.ctx.types.dict_type)
{
return Err(vm.new_type_error("module_globals must be a dict"));
}
let category = if vm.is_none(&args.category) {
None
} else {
Some(
PyTypeRef::try_from_object(vm, args.category)
.map_err(|_| vm.new_type_error("category must be a Warning subclass"))?,
)
};
crate::warn::warn_explicit(
category,
args.message,
args.filename,
args.lineno,
module,
registry,
None, // source_line
args.source.into_option(),
vm,
)
}
}