Skip to content

Commit 234bdda

Browse files
authored
Refactor warn.rs and _warnings module (RustPython#7023)
- Add already_warned() with filter version tracking - Add type validation for _defaultaction and _onceregistry - Use direct function names for _warnings pyattr/pyfunction - Route stdlib::warnings::warn through warn::warn - Remove spurious EncodingWarning from TextIOWrapper - Clean up comments and simplify check_matched error handling - Remove expectedFailure from 3 passing test_warnings tests
1 parent 8127000 commit 234bdda

5 files changed

Lines changed: 246 additions & 274 deletions

File tree

Lib/test/test_warnings/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,6 @@ def test_filter(self):
929929
self.assertRaises(UserWarning, self.module.warn,
930930
'convert to error')
931931

932-
@unittest.expectedFailure # TODO: RUSTPYTHON; AttributeError: module 'warnings' has no attribute 'onceregistry'
933932
def test_onceregistry(self):
934933
# Replacing or removing the onceregistry should be okay.
935934
global __warningregistry__
@@ -959,7 +958,6 @@ def test_onceregistry(self):
959958
finally:
960959
self.module.onceregistry = original_registry
961960

962-
@unittest.expectedFailure # TODO: RUSTPYTHON; AttributeError: module 'warnings' has no attribute 'defaultaction'
963961
def test_default_action(self):
964962
# Replacing or removing defaultaction should be okay.
965963
message = UserWarning("defaultaction test")
@@ -1631,7 +1629,6 @@ def __del__(self):
16311629
self.assertEqual(err.decode().rstrip(),
16321630
'<string>:7: UserWarning: test')
16331631

1634-
@unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: b'' doesn't start with b'<sys>:0: ResourceWarning: unclosed file '
16351632
def test_late_resource_warning(self):
16361633
# Issue #21925: Emitting a ResourceWarning late during the Python
16371634
# shutdown must be logged.

crates/vm/src/stdlib/io.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2808,14 +2808,10 @@ mod _io {
28082808
encoding: Option<PyUtf8StrRef>,
28092809
vm: &VirtualMachine,
28102810
) -> PyResult<PyUtf8StrRef> {
2811-
if encoding.is_none() && vm.state.config.settings.warn_default_encoding {
2812-
crate::stdlib::warnings::warn(
2813-
vm.ctx.exceptions.encoding_warning,
2814-
"'encoding' argument not specified".to_owned(),
2815-
1,
2816-
vm,
2817-
)?;
2818-
}
2811+
// Note: Do not issue EncodingWarning here. The warning should only
2812+
// be issued by io.text_encoding(), the public API. This function
2813+
// is used internally (e.g., for stdin/stdout/stderr initialization)
2814+
// where no warning should be emitted.
28192815
let encoding = match encoding {
28202816
None if vm.state.config.settings.utf8_mode > 0 => {
28212817
identifier_utf8!(vm, utf_8).to_owned()

crates/vm/src/stdlib/warnings.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ pub fn warn(
88
stack_level: usize,
99
vm: &VirtualMachine,
1010
) -> PyResult<()> {
11-
// TODO: use rust warnings module
12-
if let Ok(module) = vm.import("warnings", 0)
13-
&& let Ok(func) = module.get_attr("warn", vm)
14-
{
15-
func.call((message, category.to_owned(), stack_level), vm)?;
16-
}
17-
Ok(())
11+
crate::warn::warn(
12+
vm.new_pyobj(message),
13+
Some(category.to_owned()),
14+
isize::try_from(stack_level).unwrap_or(isize::MAX),
15+
None,
16+
vm,
17+
)
1818
}
1919

2020
#[pymodule]
@@ -31,18 +31,18 @@ mod _warnings {
3131
vm.state.warnings.filters.clone()
3232
}
3333

34-
#[pyattr(name = "_defaultaction")]
35-
fn default_action(vm: &VirtualMachine) -> PyStrRef {
34+
#[pyattr]
35+
fn _defaultaction(vm: &VirtualMachine) -> PyStrRef {
3636
vm.state.warnings.default_action.clone()
3737
}
3838

39-
#[pyattr(name = "_onceregistry")]
40-
fn once_registry(vm: &VirtualMachine) -> PyDictRef {
39+
#[pyattr]
40+
fn _onceregistry(vm: &VirtualMachine) -> PyDictRef {
4141
vm.state.warnings.once_registry.clone()
4242
}
4343

44-
#[pyattr(name = "_warnings_context")]
45-
fn warnings_context(vm: &VirtualMachine) -> PyObjectRef {
44+
#[pyattr]
45+
fn _warnings_context(vm: &VirtualMachine) -> PyObjectRef {
4646
vm.state
4747
.warnings
4848
.context_var
@@ -63,21 +63,21 @@ mod _warnings {
6363
.clone()
6464
}
6565

66-
#[pyfunction(name = "_acquire_lock")]
67-
fn acquire_lock(vm: &VirtualMachine) {
66+
#[pyfunction]
67+
fn _acquire_lock(vm: &VirtualMachine) {
6868
vm.state.warnings.acquire_lock();
6969
}
7070

71-
#[pyfunction(name = "_release_lock")]
72-
fn release_lock(vm: &VirtualMachine) -> PyResult<()> {
71+
#[pyfunction]
72+
fn _release_lock(vm: &VirtualMachine) -> PyResult<()> {
7373
if !vm.state.warnings.release_lock() {
7474
return Err(vm.new_runtime_error("cannot release un-acquired lock".to_owned()));
7575
}
7676
Ok(())
7777
}
7878

79-
#[pyfunction(name = "_filters_mutated_lock_held")]
80-
fn filters_mutated_lock_held(vm: &VirtualMachine) {
79+
#[pyfunction]
80+
fn _filters_mutated_lock_held(vm: &VirtualMachine) {
8181
vm.state.warnings.filters_mutated();
8282
}
8383

crates/vm/src/vm/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ declare_const_name! {
247247
_defaultaction,
248248
_onceregistry,
249249
_showwarnmsg,
250+
defaultaction,
251+
onceregistry,
250252
filters,
251253
backslashreplace,
252254
close,

0 commit comments

Comments
 (0)