Skip to content

Commit 7079e70

Browse files
authored
Honor warning instance categories before validating category arguments (#9034)
Extract the Python 3.14-compatible ordering fix from #8954. An existing Warning instance determines its own category, regardless of the category argument. Keep canonical invalid-category coverage and add only the missing instance case to the existing warnings snippet. Assisted-by: OpenAI Codex:model identifier unavailable
1 parent dcdd5d4 commit 7079e70

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

‎crates/vm/src/stdlib/_warnings.rs‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,12 @@ mod _warnings {
100100
category: Option<PyObjectRef>,
101101
vm: &VirtualMachine,
102102
) -> PyResult<Option<PyTypeRef>> {
103+
if message.fast_isinstance(vm.ctx.exceptions.warning) {
104+
return Ok(Some(message.class().to_owned()));
105+
}
103106
let cat_obj = match category {
104107
Some(c) if !vm.is_none(&c) => c,
105-
_ => {
106-
return Ok(if message.fast_isinstance(vm.ctx.exceptions.warning) {
107-
Some(message.class().to_owned())
108-
} else {
109-
None // will default to UserWarning in warn_explicit
110-
});
111-
}
108+
_ => return Ok(None), // will default to UserWarning in warn_explicit
112109
};
113110

114111
let cat = PyTypeRef::try_from_object(vm, cat_obj.clone()).map_err(|_| {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
import _warnings
22

33
_warnings.warn("Test")
4+
5+
6+
import warnings
7+
8+
9+
class CustomWarning(UserWarning):
10+
pass
11+
12+
13+
for category in (None, DeprecationWarning, int, 42):
14+
message = CustomWarning("sentinel")
15+
with warnings.catch_warnings(record=True) as caught:
16+
warnings.simplefilter("always")
17+
warnings.warn(message, category)
18+
assert len(caught) == 1
19+
assert caught[0].message is message
20+
assert caught[0].category is CustomWarning

0 commit comments

Comments
 (0)