Fix thread UnraisableException object to be None (#8326)

Assisted-by: Codex:gpt-5
This commit is contained in:
Jeaeun Kim authored and GitHub committed 2026-07-22 20:34:20 +09:00
1 parent a9dd9e5264
commit 9c19f4efec
3 files changed
+35 -22

No files matched your search

-1
View File
@@ -151,7 +151,6 @@ class ThreadRunningTests(BasicThreadTest):
support.gc_collect() # For PyPy or other GCs.
self.assertEqual(thread._count(), orig)
@unittest.expectedFailure # TODO: RUSTPYTHON
def test_unraisable_exception(self):
def task():
started.release()
+19 -11
View File
@@ -20,7 +20,10 @@ pub(crate) mod _thread {
use crate::{
AsObject, Py, PyPayload, PyRef, PyResult, VirtualMachine,
builtins::{PyDictRef, PyIntRef, PyStr, PyTupleRef, PyType, PyTypeRef, PyUtf8StrRef},
builtins::{
PyBaseExceptionRef, PyDictRef, PyIntRef, PyStr, PyTupleRef, PyType, PyTypeRef,
PyUtf8StrRef,
},
common::wtf8::Wtf8Buf,
frame::FrameRef,
function::{ArgCallable, FuncArgs, KwArgs, OptionalArg, PySetterValue, TimeoutSeconds},
@@ -558,6 +561,19 @@ pub(crate) mod _thread {
.map_err(|_err| vm.new_runtime_error("can't start new thread"))
}
fn report_unraisable_thread_exception(
exc: PyBaseExceptionRef,
func: &ArgCallable,
vm: &VirtualMachine,
) {
let msg = func
.as_ref()
.repr(vm)
.ok()
.map(|repr| format!("Exception ignored in thread started by {}", repr.as_wtf8()));
vm.run_unraisable(exc, msg, vm.ctx.none());
}
fn run_thread(func: ArgCallable, args: FuncArgs, vm: &VirtualMachine) {
// Increment thread count when thread actually starts executing
vm.state.thread_count.fetch_add(1);
@@ -572,11 +588,7 @@ pub(crate) mod _thread {
if let Err(exc) = func.invoke(args, vm)
&& !exc.fast_isinstance(vm.ctx.exceptions.system_exit)
{
vm.run_unraisable(
exc,
Some("Exception ignored in thread started by".to_owned()),
func.into(),
);
report_unraisable_thread_exception(exc, &func, vm);
}
}
for lock in SENTINELS.take() {
@@ -1755,11 +1767,7 @@ pub(crate) mod _thread {
if let Err(exc) = func.invoke((), vm)
&& !exc.fast_isinstance(vm.ctx.exceptions.system_exit)
{
vm.run_unraisable(
exc,
Some("Exception ignored in thread started by".to_owned()),
func.into(),
);
report_unraisable_thread_exception(exc, &func, vm);
}
}
}))
+16 -10
View File
@@ -1296,18 +1296,24 @@ impl VirtualMachine {
}
};
let msg_str = if let Some(msg) = msg {
format!("{msg}: ")
if self.is_none(object) {
if let Some(msg) = msg {
write_to_stderr(&format!("{msg}:\n"), &stderr, self);
}
} else {
"Exception ignored in: ".to_owned()
};
write_to_stderr(&msg_str, &stderr, self);
let msg_str = if let Some(msg) = msg {
format!("{msg}: ")
} else {
"Exception ignored in: ".to_owned()
};
write_to_stderr(&msg_str, &stderr, self);
let repr_result = object.repr(self);
let repr_wtf8 = repr_result
.as_ref()
.map_or_else(|_| "<object repr failed>".as_ref(), |s| s.as_wtf8());
write_to_stderr(&format!("{repr_wtf8}\n"), &stderr, self);
let repr_result = object.repr(self);
let repr_wtf8 = repr_result
.as_ref()
.map_or_else(|_| "<object repr failed>".as_ref(), |s| s.as_wtf8());
write_to_stderr(&format!("{repr_wtf8}\n"), &stderr, self);
}
// Write exception type and message
let exc_type_name = e.class().name();