forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwarn.rs
More file actions
611 lines (565 loc) · 18.7 KB
/
warn.rs
File metadata and controls
611 lines (565 loc) · 18.7 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyResult, VirtualMachine,
builtins::{
PyBaseExceptionRef, PyDictRef, PyListRef, PyStr, PyStrInterned, PyStrRef, PyTuple,
PyTupleRef, PyTypeRef,
},
convert::TryFromObject,
};
use core::sync::atomic::{AtomicUsize, Ordering};
use rustpython_common::lock::OnceCell;
pub struct WarningsState {
pub filters: PyListRef,
pub once_registry: PyDictRef,
pub default_action: PyStrRef,
pub filters_version: AtomicUsize,
pub context_var: OnceCell<PyObjectRef>,
lock_count: AtomicUsize,
}
impl WarningsState {
fn create_default_filters(ctx: &Context) -> PyListRef {
// init_filters(): non-debug default filter set.
ctx.new_list(vec![
ctx.new_tuple(vec![
ctx.new_str("default").into(),
ctx.none(),
ctx.exceptions.deprecation_warning.as_object().to_owned(),
ctx.new_str("__main__").into(),
ctx.new_int(0).into(),
])
.into(),
ctx.new_tuple(vec![
ctx.new_str("ignore").into(),
ctx.none(),
ctx.exceptions.deprecation_warning.as_object().to_owned(),
ctx.none(),
ctx.new_int(0).into(),
])
.into(),
ctx.new_tuple(vec![
ctx.new_str("ignore").into(),
ctx.none(),
ctx.exceptions
.pending_deprecation_warning
.as_object()
.to_owned(),
ctx.none(),
ctx.new_int(0).into(),
])
.into(),
ctx.new_tuple(vec![
ctx.new_str("ignore").into(),
ctx.none(),
ctx.exceptions.import_warning.as_object().to_owned(),
ctx.none(),
ctx.new_int(0).into(),
])
.into(),
ctx.new_tuple(vec![
ctx.new_str("ignore").into(),
ctx.none(),
ctx.exceptions.resource_warning.as_object().to_owned(),
ctx.none(),
ctx.new_int(0).into(),
])
.into(),
])
}
pub fn init_state(ctx: &Context) -> Self {
Self {
filters: Self::create_default_filters(ctx),
once_registry: ctx.new_dict(),
default_action: ctx.new_str("default"),
filters_version: AtomicUsize::new(0),
context_var: OnceCell::new(),
lock_count: AtomicUsize::new(0),
}
}
pub fn acquire_lock(&self) {
self.lock_count.fetch_add(1, Ordering::SeqCst);
}
pub fn release_lock(&self) -> bool {
let prev = self.lock_count.load(Ordering::SeqCst);
if prev == 0 {
return false;
}
self.lock_count.fetch_sub(1, Ordering::SeqCst);
true
}
pub fn filters_mutated(&self) {
self.filters_version.fetch_add(1, Ordering::SeqCst);
}
}
/// None matches everything; plain strings do exact comparison;
/// regex objects use .match().
fn check_matched(obj: &PyObject, arg: &PyObject, vm: &VirtualMachine) -> PyResult<bool> {
if vm.is_none(obj) {
return Ok(true);
}
if obj.class().is(vm.ctx.types.str_type) {
return obj.rich_compare_bool(arg, crate::types::PyComparisonOp::Eq, vm);
}
let result = vm.call_method(obj, "match", (arg.to_owned(),))?;
result.is_true(vm)
}
fn get_warnings_attr(
vm: &VirtualMachine,
attr_name: &'static PyStrInterned,
try_import: bool,
) -> PyResult<Option<PyObjectRef>> {
let module = if try_import
&& !vm
.state
.finalizing
.load(core::sync::atomic::Ordering::SeqCst)
{
match vm.import("warnings", 0) {
Ok(module) => module,
Err(_) => return Ok(None),
}
} else {
match vm.sys_module.get_attr(identifier!(vm, modules), vm) {
Ok(modules) => match modules.get_item(vm.ctx.intern_str("warnings"), vm) {
Ok(module) => module,
Err(_) => return Ok(None),
},
Err(_) => return Ok(None),
}
};
match module.get_attr(attr_name, vm) {
Ok(attr) => Ok(Some(attr)),
Err(_) => Ok(None),
}
}
/// Get the warnings filters list from sys.modules['warnings'].filters,
/// falling back to vm.state.warnings.filters.
fn get_warnings_filters(vm: &VirtualMachine) -> PyResult<PyListRef> {
if let Some(filters_obj) = get_warnings_attr(vm, identifier!(&vm.ctx, filters), false)?
&& let Ok(filters) = filters_obj.try_into_value::<PyListRef>(vm)
{
return Ok(filters);
}
Ok(vm.state.warnings.filters.clone())
}
/// Get the default action from sys.modules['warnings']._defaultaction,
/// falling back to vm.state.warnings.default_action.
fn get_default_action(vm: &VirtualMachine) -> PyResult<PyObjectRef> {
if let Some(action) = get_warnings_attr(vm, identifier!(&vm.ctx, defaultaction), false)? {
if !action.class().is(vm.ctx.types.str_type) {
return Err(vm.new_type_error(format!(
"_warnings.defaultaction must be a string, not '{}'",
action.class().name()
)));
}
return Ok(action);
}
Ok(vm.state.warnings.default_action.clone().into())
}
/// Get the once registry from sys.modules['warnings']._onceregistry,
/// falling back to vm.state.warnings.once_registry.
fn get_once_registry(vm: &VirtualMachine) -> PyResult<PyObjectRef> {
if let Some(registry) = get_warnings_attr(vm, identifier!(&vm.ctx, onceregistry), false)? {
if !registry.class().is(vm.ctx.types.dict_type) {
return Err(vm.new_type_error(format!(
"_warnings.onceregistry must be a dict, not '{}'",
registry.class().name()
)));
}
return Ok(registry);
}
Ok(vm.state.warnings.once_registry.clone().into())
}
fn already_warned(
registry: &PyObject,
key: PyObjectRef,
should_set: bool,
vm: &VirtualMachine,
) -> PyResult<bool> {
if vm.is_none(registry) {
return Ok(false);
}
let current_version = vm.state.warnings.filters_version.load(Ordering::SeqCst);
let version_obj = registry.get_item(identifier!(&vm.ctx, version), vm).ok();
let version_matches = version_obj.as_ref().is_some_and(|v| {
v.try_int(vm)
.map(|i| i.as_u32_mask() as usize == current_version)
.unwrap_or(false)
});
if version_matches {
if let Ok(val) = registry.get_item(key.as_ref(), vm)
&& val.is_true(vm)?
{
return Ok(true);
}
} else if let Ok(dict) = PyDictRef::try_from_object(vm, registry.to_owned()) {
dict.clear();
dict.set_item(
identifier!(&vm.ctx, version),
vm.ctx.new_int(current_version).into(),
vm,
)?;
}
if should_set {
registry.set_item(key.as_ref(), vm.ctx.true_value.clone().into(), vm)?;
}
Ok(false)
}
/// Create a `(text, category)` or `(text, category, 0)` key and record
/// it in the registry via `already_warned`.
fn update_registry(
registry: &PyObject,
text: &PyObject,
category: &PyObject,
add_zero: bool,
vm: &VirtualMachine,
) -> PyResult<bool> {
let altkey: PyObjectRef = if add_zero {
PyTuple::new_ref(
vec![
text.to_owned(),
category.to_owned(),
vm.ctx.new_int(0).into(),
],
&vm.ctx,
)
.into()
} else {
PyTuple::new_ref(vec![text.to_owned(), category.to_owned()], &vm.ctx).into()
};
already_warned(registry, altkey, true, vm)
}
fn normalize_module(filename: &Py<PyStr>, vm: &VirtualMachine) -> PyObjectRef {
match filename.byte_len() {
0 => vm.new_pyobj("<unknown>"),
len if len >= 3 && filename.as_bytes().ends_with(b".py") => {
vm.new_pyobj(&filename.as_wtf8()[..len - 3])
}
_ => filename.as_object().to_owned(),
}
}
/// Search the global filters list for a matching action.
// TODO: split into filter_search() + get_filter() and support
// context-aware filters (get_warnings_context_filters).
fn get_filter(
category: PyObjectRef,
text: PyObjectRef,
lineno: usize,
module: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult {
let filters = get_warnings_filters(vm)?;
// filters could change while we are iterating over it.
// Re-check list length each iteration (matches C behavior).
let mut i = 0;
while i < filters.borrow_vec().len() {
let Some(tmp_item) = filters.borrow_vec().get(i).cloned() else {
break;
};
let tmp_item = PyTupleRef::try_from_object(vm, tmp_item)
.ok()
.filter(|t| t.len() == 5)
.ok_or_else(|| {
vm.new_value_error(format!("_warnings.filters item {i} isn't a 5-tuple"))
})?;
/* action, msg, cat, mod, ln = item */
let action = &tmp_item[0];
let good_msg = check_matched(&tmp_item[1], &text, vm)?;
let is_subclass = category.is_subclass(&tmp_item[2], vm)?;
let good_mod = check_matched(&tmp_item[3], &module, vm)?;
let ln: usize = tmp_item[4].try_int(vm).map_or(0, |v| v.as_u32_mask() as _);
if good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln) {
return Ok(action.to_owned());
}
i += 1;
}
get_default_action(vm)
}
pub fn warn(
message: PyObjectRef,
category: Option<PyTypeRef>,
stack_level: isize,
source: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<()> {
warn_with_skip(message, category, stack_level, source, None, vm)
}
/// do_warn: resolve context via setup_context, then call warn_explicit.
pub fn warn_with_skip(
message: PyObjectRef,
category: Option<PyTypeRef>,
mut stack_level: isize,
source: Option<PyObjectRef>,
skip_file_prefixes: Option<PyTupleRef>,
vm: &VirtualMachine,
) -> PyResult<()> {
if let Some(ref prefixes) = skip_file_prefixes
&& !prefixes.is_empty()
&& stack_level < 2
{
stack_level = 2;
}
let (filename, lineno, module, registry) =
setup_context(stack_level, skip_file_prefixes.as_ref(), vm)?;
warn_explicit(
category, message, filename, lineno, module, registry, None, source, vm,
)
}
/// Core warning logic matching `warn_explicit()` in `_warnings.c`.
#[allow(clippy::too_many_arguments)]
pub(crate) fn warn_explicit(
category: Option<PyTypeRef>,
message: PyObjectRef,
filename: PyStrRef,
lineno: usize,
module: Option<PyObjectRef>,
registry: PyObjectRef,
source_line: Option<PyObjectRef>,
source: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<()> {
// Normalize module. None → silent return (late-shutdown safety).
let module = module.unwrap_or_else(|| normalize_module(&filename, vm));
if vm.is_none(&module) {
return Ok(());
}
// Normalize message.
let is_warning = message.fast_isinstance(vm.ctx.exceptions.warning);
let (text, category, message) = if is_warning {
let text = message.str(vm)?;
let cat = message.class().to_owned();
(text, cat, message)
} else {
// For non-Warning messages, convert to string via str()
let text = message.str(vm)?;
let cat = category.unwrap_or_else(|| vm.ctx.exceptions.user_warning.to_owned());
let instance = cat.as_object().call((text.clone(),), vm)?;
(text, cat, instance)
};
let lineno_obj: PyObjectRef = vm.ctx.new_int(lineno).into();
// key = (text, category, lineno)
let key: PyObjectRef = PyTuple::new_ref(
vec![
text.clone().into(),
category.as_object().to_owned(),
lineno_obj.clone(),
],
&vm.ctx,
)
.into();
// Check if already warned
if !vm.is_none(®istry) && already_warned(®istry, key.clone(), false, vm)? {
return Ok(());
}
// Get filter action
let action = get_filter(
category.as_object().to_owned(),
text.clone().into(),
lineno,
module,
vm,
)?;
let action_str = PyStrRef::try_from_object(vm, action)
.map_err(|_| vm.new_type_error("action must be a string"))?;
if action_str.as_bytes() == b"error" {
let exc = PyBaseExceptionRef::try_from_object(vm, message)?;
return Err(exc);
}
if action_str.as_bytes() == b"ignore" {
return Ok(());
}
// For everything except "always"/"all", record in registry then
// check per-action registries.
let already = if action_str.as_wtf8() != "always" && action_str.as_wtf8() != "all" {
if !vm.is_none(®istry) {
registry.set_item(&*key, vm.ctx.true_value.clone().into(), vm)?;
}
let action_s = action_str.to_str();
match action_s {
Some("once") => {
let reg = if vm.is_none(®istry) {
get_once_registry(vm)?
} else {
registry.clone()
};
update_registry(®, text.as_ref(), category.as_object(), false, vm)?
}
Some("module") => {
if !vm.is_none(®istry) {
update_registry(®istry, text.as_ref(), category.as_object(), false, vm)?
} else {
false
}
}
Some("default") => false,
_ => {
return Err(vm.new_runtime_error(format!(
"Unrecognized action ({action_str}) in warnings.filters:\n {action_str}"
)));
}
}
} else {
false
};
if already {
return Ok(());
}
call_show_warning(
category,
text,
message,
filename,
lineno,
lineno_obj,
source_line,
source,
vm,
)
}
#[allow(clippy::too_many_arguments)]
fn call_show_warning(
category: PyTypeRef,
text: PyStrRef,
message: PyObjectRef,
filename: PyStrRef,
lineno: usize,
lineno_obj: PyObjectRef,
source_line: Option<PyObjectRef>,
source: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<()> {
let Some(show_fn) =
get_warnings_attr(vm, identifier!(&vm.ctx, _showwarnmsg), source.is_some())?
else {
return show_warning(filename, lineno, text, category, source_line, vm);
};
if !show_fn.is_callable() {
return Err(vm.new_type_error("warnings._showwarnmsg() must be set to a callable"));
}
let Some(warnmsg_cls) = get_warnings_attr(vm, identifier!(&vm.ctx, WarningMessage), false)?
else {
return Err(vm.new_runtime_error("unable to get warnings.WarningMessage"));
};
let msg = warnmsg_cls.call(
vec![
message,
category.into(),
filename.into(),
lineno_obj,
vm.ctx.none(),
vm.ctx.none(),
vm.unwrap_or_none(source),
],
vm,
)?;
show_fn.call((msg,), vm)?;
Ok(())
}
fn show_warning(
filename: PyStrRef,
lineno: usize,
text: PyStrRef,
category: PyTypeRef,
_source_line: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<()> {
let stderr = crate::stdlib::sys::PyStderr(vm);
writeln!(
stderr,
"{}:{}: {}: {}",
filename,
lineno,
category.name(),
text
);
Ok(())
}
/// Check if a frame's filename starts with any of the given prefixes.
fn is_filename_to_skip(frame: &crate::frame::Frame, prefixes: &PyTupleRef) -> bool {
let filename = frame.f_code().co_filename();
let filename_bytes = filename.as_bytes();
prefixes.iter().any(|prefix| {
prefix
.downcast_ref::<PyStr>()
.is_some_and(|s| filename_bytes.starts_with(s.as_bytes()))
})
}
/// Like Frame::next_external_frame but also skips frames matching prefixes.
fn next_external_frame_with_skip(
frame: &crate::frame::FrameRef,
skip_file_prefixes: Option<&PyTupleRef>,
vm: &VirtualMachine,
) -> Option<crate::frame::FrameRef> {
let mut f = frame.f_back(vm);
loop {
let current: crate::frame::FrameRef = f.take()?;
if current.is_internal_frame()
|| skip_file_prefixes.is_some_and(|p| is_filename_to_skip(¤t, p))
{
f = current.f_back(vm);
} else {
return Some(current);
}
}
}
/// filename, module, and registry are new refs, globals is borrowed
/// Returns `Ok` on success, or `Err` on error (no new refs)
fn setup_context(
mut stack_level: isize,
skip_file_prefixes: Option<&PyTupleRef>,
vm: &VirtualMachine,
) -> PyResult<(PyStrRef, usize, Option<PyObjectRef>, PyObjectRef)> {
let mut f = vm.current_frame();
// Stack level comparisons to Python code is off by one as there is no
// warnings-related stack level to avoid.
if stack_level <= 0 || f.as_ref().is_some_and(|frame| frame.is_internal_frame()) {
while {
stack_level -= 1;
stack_level > 0
} {
match f {
Some(tmp) => f = tmp.f_back(vm),
None => break,
}
}
} else {
while {
stack_level -= 1;
stack_level > 0
} {
match f {
Some(tmp) => f = next_external_frame_with_skip(&tmp, skip_file_prefixes, vm),
None => break,
}
}
}
let (globals, filename, lineno) = if let Some(f) = f {
(f.globals.clone(), f.code.source_path(), f.f_lineno())
} else if let Some(frame) = vm.current_frame() {
// We have a frame but it wasn't found during stack walking
(frame.globals.clone(), vm.ctx.intern_str("<sys>"), 1)
} else {
// No frames on the stack - use sys.__dict__ (interp->sysdict)
let globals = vm
.sys_module
.as_object()
.get_attr(identifier!(vm, __dict__), vm)
.and_then(|d| {
d.downcast::<crate::builtins::PyDict>()
.map_err(|_| vm.new_type_error("sys.__dict__ is not a dictionary"))
})?;
(globals, vm.ctx.intern_str("<sys>"), 0)
};
let registry = match globals.get_item("__warningregistry__", vm) {
Ok(r) => r,
Err(_) => {
let r = vm.ctx.new_dict();
globals.set_item("__warningregistry__", r.clone().into(), vm)?;
r.into()
}
};
// Setup module.
let module = globals
.get_item("__name__", vm)
.unwrap_or_else(|_| vm.new_pyobj("<string>"));
Ok((filename.to_owned(), lineno, Some(module), registry))
}