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
This commit is contained in:
youknowdot authored and GitHub committed 2026-10-10 19:36:20 +09:00
1 parent dcdd5d45c3
commit 7079e70b79
2 files changed
+21 -7

No files matched your search

+4 -7
View File
@@ -100,15 +100,12 @@ mod _warnings {
category: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<Option<PyTypeRef>> {
if message.fast_isinstance(vm.ctx.exceptions.warning) {
return Ok(Some(message.class().to_owned()));
}
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
});
}
_ => return Ok(None), // will default to UserWarning in warn_explicit
};
let cat = PyTypeRef::try_from_object(vm, cat_obj.clone()).map_err(|_| {
+17
View File
@@ -1,3 +1,20 @@
import _warnings
_warnings.warn("Test")
import warnings
class CustomWarning(UserWarning):
pass
for category in (None, DeprecationWarning, int, 42):
message = CustomWarning("sentinel")
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
warnings.warn(message, category)
assert len(caught) == 1
assert caught[0].message is message
assert caught[0].category is CustomWarning