Skip to content

Commit c71535d

Browse files
committed
Don't panic when can't __del__ object
1 parent f1948ef commit c71535d

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

vm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub mod frame;
5252
mod frozen;
5353
pub mod function;
5454
pub mod import;
55-
mod iterator;
55+
pub mod iterator;
5656
mod py_io;
5757
pub mod py_serde;
5858
pub mod pyobject;

vm/src/pyobjectrc.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ impl Drop for PyObjectRef {
307307
// CPython-compatible drop implementation
308308
let zelf = self.clone();
309309
if let Some(del_slot) = self.class().mro_find_map(|cls| cls.slots.del.load()) {
310-
crate::vm::thread::with_vm(&zelf, |vm| {
310+
let ret = crate::vm::thread::with_vm(&zelf, |vm| {
311311
if let Err(e) = del_slot(&zelf, vm) {
312312
// exception in del will be ignored but printed
313313
print!("Exception ignored in: ",);
@@ -327,6 +327,9 @@ impl Drop for PyObjectRef {
327327
}
328328
}
329329
});
330+
if ret.is_none() {
331+
warn!("couldn't run __del__ method for object")
332+
}
330333
}
331334

332335
// __del__ might have resurrected the object at this point, but that's fine,

vm/src/vm.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub(crate) mod thread {
8181
})
8282
}
8383

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

0 commit comments

Comments
 (0)