forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweakref.rs
More file actions
37 lines (31 loc) · 1.24 KB
/
weakref.rs
File metadata and controls
37 lines (31 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//! Implementation in line with the python `weakref` module.
//!
//! See also:
//! - [python weakref module](https://docs.python.org/3/library/weakref.html)
//! - [rust weak struct](https://doc.rust-lang.org/std/rc/struct.Weak.html)
//!
use crate::pyobject::PyObjectRef;
use crate::vm::VirtualMachine;
fn weakref_getweakrefcount(obj: PyObjectRef) -> usize {
PyObjectRef::weak_count(&obj)
}
fn weakref_getweakrefs(_obj: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
// TODO: implement this, may require a different gc
vm.ctx.new_list(vec![])
}
fn weakref_remove_dead_weakref(_obj: PyObjectRef, _key: PyObjectRef) {
// TODO
}
pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
let ctx = &vm.ctx;
py_module!(vm, "_weakref", {
"ref" => ctx.types.weakref_type.clone(),
"proxy" => ctx.types.weakproxy_type.clone(),
"getweakrefcount" => ctx.new_function(weakref_getweakrefcount),
"getweakrefs" => ctx.new_function(weakref_getweakrefs),
"ReferenceType" => ctx.types.weakref_type.clone(),
"ProxyType" => ctx.types.weakproxy_type.clone(),
"CallableProxyType" => ctx.types.weakproxy_type.clone(),
"_remove_dead_weakref" => ctx.new_function(weakref_remove_dead_weakref),
})
}