Skip to content

Commit 7af58dd

Browse files
authored
Merge pull request RustPython#3328 from youknowone/del-error-cold
__del__ failure is a cold function
2 parents e1bd36d + ecf83ec commit 7af58dd

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

vm/src/pyobjectrc.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::common::{
33
rc::{PyRc, PyWeak},
44
};
55
use crate::{
6-
builtins::{PyDictRef, PyTypeRef},
6+
builtins::{PyBaseExceptionRef, PyDictRef, PyTypeRef},
77
IdProtocol, PyObjectPayload, TypeProtocol, VirtualMachine,
88
};
99
use std::any::TypeId;
@@ -353,22 +353,7 @@ impl Drop for PyObjectRef {
353353
if let Some(del_slot) = self.class().mro_find_map(|cls| cls.slots.del.load()) {
354354
let ret = crate::vm::thread::with_vm(&zelf, |vm| {
355355
if let Err(e) = del_slot(&zelf, vm) {
356-
// exception in del will be ignored but printed
357-
print!("Exception ignored in: ",);
358-
let del_method = zelf.get_class_attr("__del__").unwrap();
359-
let repr = vm.to_repr(&del_method);
360-
match repr {
361-
Ok(v) => println!("{}", v.to_string()),
362-
Err(_) => println!("{}", del_method.class().name()),
363-
}
364-
let tb_module = vm.import("traceback", None, 0).unwrap();
365-
// TODO: set exc traceback
366-
let print_stack = vm.get_attribute(tb_module, "print_stack").unwrap();
367-
vm.invoke(&print_stack, ()).unwrap();
368-
369-
if let Ok(repr) = vm.to_repr(e.as_object()) {
370-
println!("{}", repr.as_str());
371-
}
356+
print_del_error(e, &zelf, vm);
372357
}
373358
});
374359
if ret.is_none() {
@@ -381,6 +366,26 @@ impl Drop for PyObjectRef {
381366
}
382367
}
383368

369+
#[cold]
370+
fn print_del_error(e: PyBaseExceptionRef, zelf: &PyObjectRef, vm: &VirtualMachine) {
371+
// exception in del will be ignored but printed
372+
print!("Exception ignored in: ",);
373+
let del_method = zelf.get_class_attr("__del__").unwrap();
374+
let repr = vm.to_repr(&del_method);
375+
match repr {
376+
Ok(v) => println!("{}", v.to_string()),
377+
Err(_) => println!("{}", del_method.class().name()),
378+
}
379+
let tb_module = vm.import("traceback", None, 0).unwrap();
380+
// TODO: set exc traceback
381+
let print_stack = vm.get_attribute(tb_module, "print_stack").unwrap();
382+
vm.invoke(&print_stack, ()).unwrap();
383+
384+
if let Ok(repr) = vm.to_repr(e.as_object()) {
385+
println!("{}", repr.as_str());
386+
}
387+
}
388+
384389
impl fmt::Debug for PyObjectRef {
385390
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
386391
// SAFETY: the vtable contains functions that accept payload types that always match up

0 commit comments

Comments
 (0)