Don't panic when can't __del__ object

This commit is contained in:
Noah
2021-02-05 23:14:42 -06:00
parent 8abfecf022
commit fd87c238fc
3 changed files with 8 additions and 7 deletions

View File

@@ -54,7 +54,7 @@ pub mod frame;
mod frozen;
pub mod function;
pub mod import;
mod iterator;
pub mod iterator;
mod py_io;
pub mod py_serde;
pub mod pyobject;

View File

@@ -307,7 +307,7 @@ impl Drop for PyObjectRef {
// CPython-compatible drop implementation
let zelf = self.clone();
if let Some(del_slot) = self.class().mro_find_map(|cls| cls.slots.del.load()) {
crate::vm::thread::with_vm(&zelf, |vm| {
let ret = crate::vm::thread::with_vm(&zelf, |vm| {
if let Err(e) = del_slot(&zelf, vm) {
// exception in del will be ignored but printed
print!("Exception ignored in: ",);
@@ -327,6 +327,9 @@ impl Drop for PyObjectRef {
}
}
});
if ret.is_none() {
warn!("couldn't run __del__ method for object")
}
}
// __del__ might have resurrected the object at this point, but that's fine,

View File

@@ -86,7 +86,7 @@ pub(crate) mod thread {
})
}
pub fn with_vm<F, R>(obj: &PyObjectRef, f: F) -> R
pub fn with_vm<F, R>(obj: &PyObjectRef, f: F) -> Option<R>
where
F: Fn(&VirtualMachine) -> R,
{
@@ -101,14 +101,12 @@ pub(crate) mod thread {
debug_assert!(vm_owns_obj(x));
x
}
Err(mut others) => others
.find(|x| vm_owns_obj(*x))
.unwrap_or_else(|| panic!("can't get a vm for {:?}; none on stack", obj)),
Err(mut others) => others.find(|x| vm_owns_obj(*x))?,
};
// SAFETY: all references in VM_STACK should be valid, and should not be changed or moved
// at least until this function returns and the stack unwinds to an enter_vm() call
let vm = unsafe { intp.as_ref() };
f(vm)
Some(f(vm))
})
}
}