Write unraisable exception to stderr during finalization. Similar to _PyErr_WriteUnraisableDefaultHook in CPython.
(
&self,
e: &PyBaseExceptionRef,
msg: Option<&str>,
object: &PyObjectRef,
)
| 1141 | /// Write unraisable exception to stderr during finalization. |
| 1142 | /// Similar to _PyErr_WriteUnraisableDefaultHook in CPython. |
| 1143 | fn write_unraisable_to_stderr( |
| 1144 | &self, |
| 1145 | e: &PyBaseExceptionRef, |
| 1146 | msg: Option<&str>, |
| 1147 | object: &PyObjectRef, |
| 1148 | ) { |
| 1149 | // Get stderr once and reuse it |
| 1150 | let stderr = crate::stdlib::sys::get_stderr(self).ok(); |
| 1151 | |
| 1152 | let write_to_stderr = |s: &str, stderr: &Option<PyObjectRef>, vm: &VirtualMachine| { |
| 1153 | if let Some(stderr) = stderr { |
| 1154 | let _ = vm.call_method(stderr, "write", (s.to_owned(),)); |
| 1155 | } else { |
| 1156 | eprint!("{}", s); |
| 1157 | } |
| 1158 | }; |
| 1159 | |
| 1160 | let msg_str = if let Some(msg) = msg { |
| 1161 | format!("{msg}: ") |
| 1162 | } else { |
| 1163 | "Exception ignored in: ".to_owned() |
| 1164 | }; |
| 1165 | write_to_stderr(&msg_str, &stderr, self); |
| 1166 | |
| 1167 | let repr_result = object.repr(self); |
| 1168 | let repr_wtf8 = repr_result |
| 1169 | .as_ref() |
| 1170 | .map_or("<object repr failed>".as_ref(), |s| s.as_wtf8()); |
| 1171 | write_to_stderr(&format!("{repr_wtf8}\n"), &stderr, self); |
| 1172 | |
| 1173 | // Write exception type and message |
| 1174 | let exc_type_name = e.class().name(); |
| 1175 | let msg = match e.as_object().str(self) { |
| 1176 | Ok(exc_str) if !exc_str.as_wtf8().is_empty() => { |
| 1177 | format!("{}: {}\n", exc_type_name, exc_str.as_wtf8()) |
| 1178 | } |
| 1179 | _ => format!("{}\n", exc_type_name), |
| 1180 | }; |
| 1181 | write_to_stderr(&msg, &stderr, self); |
| 1182 | |
| 1183 | // Flush stderr to ensure output is visible |
| 1184 | if let Some(ref stderr) = stderr { |
| 1185 | let _ = self.call_method(stderr, "flush", ()); |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | #[inline(always)] |
| 1190 | pub fn run_frame(&self, frame: FrameRef) -> PyResult { |
no test coverage detected