MCPcopy Index your code
hub / github.com/RustPython/RustPython / in_weak_set

Function in_weak_set

crates/vm/src/stdlib/_abc.rs:84–112  ·  view source on GitHub ↗

Check if obj is in the weak set

(
        set_lock: &PyRwLock<Option<PyRef<PySet>>>,
        obj: &PyObject,
        vm: &VirtualMachine,
    )

Source from the content-addressed store, hash-verified

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(

Callers 3

_abc_instancecheckFunction · 0.85
_abc_subclasscheckFunction · 0.85

Calls 11

isMethod · 0.80
try_to_boolMethod · 0.80
ErrClass · 0.50
readMethod · 0.45
is_emptyMethod · 0.45
elementsMethod · 0.45
cloneMethod · 0.45
downgradeMethod · 0.45
classMethod · 0.45
call_methodMethod · 0.45
as_refMethod · 0.45

Tested by

no test coverage detected