From fd87c238fca2414ed1fa7a18a9ab29ecd53b0208 Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Fri, 5 Feb 2021 23:14:42 -0600 Subject: [PATCH] Don't panic when can't __del__ object --- vm/src/lib.rs | 2 +- vm/src/pyobjectrc.rs | 5 ++++- vm/src/vm.rs | 8 +++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/vm/src/lib.rs b/vm/src/lib.rs index 9764a8ce53..c511b73b1e 100644 --- a/vm/src/lib.rs +++ b/vm/src/lib.rs @@ -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; diff --git a/vm/src/pyobjectrc.rs b/vm/src/pyobjectrc.rs index bc973a2844..e7222eaaed 100644 --- a/vm/src/pyobjectrc.rs +++ b/vm/src/pyobjectrc.rs @@ -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, diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 7ab6524219..ffafe0c8c4 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -86,7 +86,7 @@ pub(crate) mod thread { }) } - pub fn with_vm(obj: &PyObjectRef, f: F) -> R + pub fn with_vm(obj: &PyObjectRef, f: F) -> Option 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)) }) } }