|
| 1 | +use crate::PyObject; |
| 2 | +use crate::object::define_py_check; |
| 3 | +use crate::pystate::with_vm; |
| 4 | +use core::ffi::c_int; |
| 5 | +use rustpython_vm::builtins::{PyWeak, PyWeakProxy}; |
| 6 | + |
| 7 | +define_py_check!(fn PyWeakref_CheckProxy, types.weakproxy_type); |
| 8 | +define_py_check!(fn PyWeakref_CheckRef, types.weakref_type); |
| 9 | + |
| 10 | +#[unsafe(no_mangle)] |
| 11 | +pub unsafe extern "C" fn PyWeakref_GetRef( |
| 12 | + reference: *mut PyObject, |
| 13 | + result: *mut *mut PyObject, |
| 14 | +) -> c_int { |
| 15 | + with_vm(|vm| { |
| 16 | + unsafe { |
| 17 | + *result = core::ptr::null_mut(); |
| 18 | + } |
| 19 | + |
| 20 | + let reference = unsafe { &*reference }; |
| 21 | + let upgraded = if let Some(weak) = reference.downcast_ref::<PyWeak>() { |
| 22 | + weak.upgrade() |
| 23 | + } else if let Some(proxy) = reference.downcast_ref::<PyWeakProxy>() { |
| 24 | + proxy.get_weak().upgrade() |
| 25 | + } else { |
| 26 | + return Err(vm.new_type_error("expected a weakref")); |
| 27 | + }; |
| 28 | + |
| 29 | + if let Some(obj) = upgraded { |
| 30 | + unsafe { |
| 31 | + *result = obj.into_raw().as_ptr(); |
| 32 | + } |
| 33 | + Ok(true) |
| 34 | + } else { |
| 35 | + Ok(false) |
| 36 | + } |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +#[unsafe(no_mangle)] |
| 41 | +pub unsafe extern "C" fn PyWeakref_NewProxy( |
| 42 | + ob: *mut PyObject, |
| 43 | + callback: *mut PyObject, |
| 44 | +) -> *mut PyObject { |
| 45 | + with_vm(|vm| { |
| 46 | + let ob = unsafe { &*ob }; |
| 47 | + let callback = unsafe { callback.as_ref() } |
| 48 | + .filter(|callback| !vm.is_none(callback)) |
| 49 | + .map(ToOwned::to_owned); |
| 50 | + PyWeakProxy::new_weakproxy(ob, callback, vm) |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +#[unsafe(no_mangle)] |
| 55 | +pub unsafe extern "C" fn PyWeakref_NewRef( |
| 56 | + ob: *mut PyObject, |
| 57 | + callback: *mut PyObject, |
| 58 | +) -> *mut PyObject { |
| 59 | + with_vm(|vm| { |
| 60 | + let ob = unsafe { &*ob }; |
| 61 | + let callback = unsafe { callback.as_ref() } |
| 62 | + .filter(|callback| !vm.is_none(callback)) |
| 63 | + .map(ToOwned::to_owned); |
| 64 | + ob.downgrade(callback, vm) |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +#[cfg(false)] |
| 69 | +mod tests { |
| 70 | + use pyo3::prelude::*; |
| 71 | + use pyo3::types::PyAnyMethods; |
| 72 | + use pyo3::types::{PyInt, PyWeakrefMethods, PyWeakrefProxy, PyWeakrefReference}; |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn check_ref_and_proxy() { |
| 76 | + Python::attach(|py| { |
| 77 | + let object_ty = py.get_type::<PyInt>(); |
| 78 | + |
| 79 | + let weak_ref = PyWeakrefReference::new(&object_ty).unwrap(); |
| 80 | + let weak_proxy = PyWeakrefProxy::new(&object_ty).unwrap(); |
| 81 | + |
| 82 | + assert!(weak_ref.is_instance_of::<PyWeakrefReference>()); |
| 83 | + assert!(weak_proxy.is_instance_of::<PyWeakrefProxy>()); |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn new_ref_and_get_ref() { |
| 89 | + Python::attach(|py| { |
| 90 | + let object_ty = py.get_type::<PyInt>(); |
| 91 | + let weak_ref = PyWeakrefReference::new(&object_ty).unwrap(); |
| 92 | + |
| 93 | + assert!(weak_ref.upgrade().is_some_and(|obj| obj.is(&object_ty))); |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn new_proxy() { |
| 99 | + Python::attach(|py| { |
| 100 | + let object_ty = py.get_type::<PyInt>(); |
| 101 | + let weak_proxy = PyWeakrefProxy::new(&object_ty).unwrap(); |
| 102 | + |
| 103 | + assert!(weak_proxy.upgrade().is_some_and(|obj| obj.is(&object_ty))); |
| 104 | + }); |
| 105 | + } |
| 106 | +} |
0 commit comments