Skip to content

Commit fd87c23

Browse files
committed
Don't panic when can't __del__ object
1 parent 8abfecf commit fd87c23

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
@@ -54,7 +54,7 @@ pub mod frame;
5454
mod frozen;
5555
pub mod function;
5656
pub mod import;
57-
mod iterator;
57+
pub mod iterator;
5858
mod py_io;
5959
pub mod py_serde;
6060
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
@@ -86,7 +86,7 @@ pub(crate) mod thread {
8686
})
8787
}
8888

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

0 commit comments

Comments
 (0)