Check if obj is in the weak set
(
set_lock: &PyRwLock<Option<PyRef<PySet>>>,
obj: &PyObject,
vm: &VirtualMachine,
)
| 82 | |
| 83 | /// Check if obj is in the weak set |
| 84 | fn in_weak_set( |
| 85 | set_lock: &PyRwLock<Option<PyRef<PySet>>>, |
| 86 | obj: &PyObject, |
| 87 | vm: &VirtualMachine, |
| 88 | ) -> PyResult<bool> { |
| 89 | let set_opt = set_lock.read(); |
| 90 | let set = match &*set_opt { |
| 91 | Some(s) if !s.elements().is_empty() => s.clone(), |
| 92 | _ => return Ok(false), |
| 93 | }; |
| 94 | drop(set_opt); |
| 95 | |
| 96 | // Create a weak reference to the object |
| 97 | let weak_ref = match obj.downgrade(None, vm) { |
| 98 | Ok(w) => w, |
| 99 | Err(e) => { |
| 100 | // If we can't create a weakref (e.g., TypeError), the object can't be in the set |
| 101 | if e.class().is(vm.ctx.exceptions.type_error) { |
| 102 | return Ok(false); |
| 103 | } |
| 104 | return Err(e); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | // Use vm.call_method to call __contains__ |
| 109 | let weak_ref_obj: PyObjectRef = weak_ref.into(); |
| 110 | vm.call_method(set.as_ref(), "__contains__", (weak_ref_obj,))? |
| 111 | .try_to_bool(vm) |
| 112 | } |
| 113 | |
| 114 | /// Add obj to the weak set |
| 115 | fn add_to_weak_set( |
no test coverage detected