forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweakrefobject.rs
More file actions
106 lines (92 loc) · 2.98 KB
/
Copy pathweakrefobject.rs
File metadata and controls
106 lines (92 loc) · 2.98 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use crate::PyObject;
use crate::object::define_py_check;
use crate::pystate::with_vm;
use core::ffi::c_int;
use rustpython_vm::builtins::{PyWeak, PyWeakProxy};
define_py_check!(fn PyWeakref_CheckProxy, types.weakproxy_type);
define_py_check!(fn PyWeakref_CheckRef, types.weakref_type);
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyWeakref_GetRef(
reference: *mut PyObject,
result: *mut *mut PyObject,
) -> c_int {
with_vm(|vm| {
unsafe {
*result = core::ptr::null_mut();
}
let reference = unsafe { &*reference };
let upgraded = if let Some(weak) = reference.downcast_ref::<PyWeak>() {
weak.upgrade()
} else if let Some(proxy) = reference.downcast_ref::<PyWeakProxy>() {
proxy.get_weak().upgrade()
} else {
return Err(vm.new_type_error("expected a weakref"));
};
if let Some(obj) = upgraded {
unsafe {
*result = obj.into_raw().as_ptr();
}
Ok(true)
} else {
Ok(false)
}
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyWeakref_NewProxy(
ob: *mut PyObject,
callback: *mut PyObject,
) -> *mut PyObject {
with_vm(|vm| {
let ob = unsafe { &*ob };
let callback = unsafe { callback.as_ref() }
.filter(|callback| !vm.is_none(callback))
.map(ToOwned::to_owned);
PyWeakProxy::new_weakproxy(ob, callback, vm)
})
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyWeakref_NewRef(
ob: *mut PyObject,
callback: *mut PyObject,
) -> *mut PyObject {
with_vm(|vm| {
let ob = unsafe { &*ob };
let callback = unsafe { callback.as_ref() }
.filter(|callback| !vm.is_none(callback))
.map(ToOwned::to_owned);
ob.downgrade(callback, vm)
})
}
#[cfg(false)]
mod tests {
use pyo3::prelude::*;
use pyo3::types::PyAnyMethods;
use pyo3::types::{PyInt, PyWeakrefMethods, PyWeakrefProxy, PyWeakrefReference};
#[test]
fn check_ref_and_proxy() {
Python::attach(|py| {
let object_ty = py.get_type::<PyInt>();
let weak_ref = PyWeakrefReference::new(&object_ty).unwrap();
let weak_proxy = PyWeakrefProxy::new(&object_ty).unwrap();
assert!(weak_ref.is_instance_of::<PyWeakrefReference>());
assert!(weak_proxy.is_instance_of::<PyWeakrefProxy>());
});
}
#[test]
fn new_ref_and_get_ref() {
Python::attach(|py| {
let object_ty = py.get_type::<PyInt>();
let weak_ref = PyWeakrefReference::new(&object_ty).unwrap();
assert!(weak_ref.upgrade().is_some_and(|obj| obj.is(&object_ty)));
});
}
#[test]
fn new_proxy() {
Python::attach(|py| {
let object_ty = py.get_type::<PyInt>();
let weak_proxy = PyWeakrefProxy::new(&object_ty).unwrap();
assert!(weak_proxy.upgrade().is_some_and(|obj| obj.is(&object_ty)));
});
}
}