(unraisable: UnraisableHookArgsData, vm: &VirtualMachine)
| 1207 | } |
| 1208 | |
| 1209 | fn _unraisablehook(unraisable: UnraisableHookArgsData, vm: &VirtualMachine) -> PyResult<()> { |
| 1210 | use super::PyStderr; |
| 1211 | |
| 1212 | let stderr = PyStderr(vm); |
| 1213 | if !vm.is_none(&unraisable.object) { |
| 1214 | if !vm.is_none(&unraisable.err_msg) { |
| 1215 | write!(stderr, "{}: ", unraisable.err_msg.str(vm)?); |
| 1216 | } else { |
| 1217 | write!(stderr, "Exception ignored in: "); |
| 1218 | } |
| 1219 | // exception in del will be ignored but printed |
| 1220 | let repr = &unraisable.object.repr(vm); |
| 1221 | let str = match repr { |
| 1222 | Ok(v) => v.to_string(), |
| 1223 | Err(_) => format!( |
| 1224 | "<object {} repr() failed>", |
| 1225 | unraisable.object.class().name() |
| 1226 | ), |
| 1227 | }; |
| 1228 | writeln!(stderr, "{str}"); |
| 1229 | } else if !vm.is_none(&unraisable.err_msg) { |
| 1230 | writeln!(stderr, "{}:", unraisable.err_msg.str(vm)?); |
| 1231 | } |
| 1232 | |
| 1233 | // Print traceback (using actual exc_traceback, not current stack) |
| 1234 | if !vm.is_none(&unraisable.exc_traceback) { |
| 1235 | let tb_module = vm.import("traceback", 0)?; |
| 1236 | let print_tb = tb_module.get_attr("print_tb", vm)?; |
| 1237 | let stderr_obj = super::get_stderr(vm)?; |
| 1238 | let kwargs: KwArgs = [("file".to_string(), stderr_obj)].into_iter().collect(); |
| 1239 | let _ = print_tb.call( |
| 1240 | FuncArgs::new(vec![unraisable.exc_traceback.clone()], kwargs), |
| 1241 | vm, |
| 1242 | ); |
| 1243 | } |
| 1244 | |
| 1245 | // Check exc_type |
| 1246 | if vm.is_none(unraisable.exc_type.as_object()) { |
| 1247 | return Ok(()); |
| 1248 | } |
| 1249 | assert!( |
| 1250 | unraisable |
| 1251 | .exc_type |
| 1252 | .fast_issubclass(vm.ctx.exceptions.base_exception_type) |
| 1253 | ); |
| 1254 | |
| 1255 | // Print module name (if not builtins or __main__) |
| 1256 | let module_name = unraisable.exc_type.__module__(vm); |
| 1257 | if let Ok(module_str) = module_name.downcast::<PyStr>() { |
| 1258 | let module = module_str.as_wtf8(); |
| 1259 | if module != "builtins" && module != "__main__" { |
| 1260 | write!(stderr, "{}.", module); |
| 1261 | } |
| 1262 | } else { |
| 1263 | write!(stderr, "<unknown>."); |
| 1264 | } |
| 1265 | |
| 1266 | // Print qualname |
no test coverage detected