|
| 1 | +use crate::PyObject; |
| 2 | +use crate::pystate::with_vm; |
| 3 | +use core::ffi::{CStr, c_char, c_int, c_void}; |
| 4 | +use core::ptr::NonNull; |
| 5 | +use rustpython_vm::builtins::PyCapsule; |
| 6 | +use rustpython_vm::{PyObjectRef, PyResult, VirtualMachine}; |
| 7 | + |
| 8 | +#[allow(non_camel_case_types)] |
| 9 | +pub type PyCapsule_Destructor = unsafe extern "C" fn(capsule: *mut PyObject); |
| 10 | + |
| 11 | +#[unsafe(no_mangle)] |
| 12 | +pub extern "C" fn PyCapsule_New( |
| 13 | + pointer: *mut c_void, |
| 14 | + name: *const c_char, |
| 15 | + destructor: Option<PyCapsule_Destructor>, |
| 16 | +) -> *mut PyObject { |
| 17 | + with_vm(|vm| { |
| 18 | + if pointer.is_null() { |
| 19 | + return Err(vm.new_value_error("PyCapsule_New called with null pointer")); |
| 20 | + } |
| 21 | + let name = NonNull::new(name.cast_mut()).map(|ptr| unsafe { CStr::from_ptr(ptr.as_ptr()) }); |
| 22 | + Ok(vm.ctx.new_capsule(pointer, name, destructor)) |
| 23 | + }) |
| 24 | +} |
| 25 | + |
| 26 | +#[unsafe(no_mangle)] |
| 27 | +pub unsafe extern "C" fn PyCapsule_GetPointer( |
| 28 | + capsule: *mut PyObject, |
| 29 | + name: *const c_char, |
| 30 | +) -> *mut c_void { |
| 31 | + with_vm(|vm| Ok(checked_capsule(vm, unsafe { &*capsule }, name)?.pointer())) |
| 32 | +} |
| 33 | + |
| 34 | +#[unsafe(no_mangle)] |
| 35 | +pub unsafe extern "C" fn PyCapsule_GetName(capsule: *mut PyObject) -> *const c_char { |
| 36 | + with_vm(|vm| { |
| 37 | + let capsule = unsafe { &*capsule } |
| 38 | + .downcast_ref_if_exact::<PyCapsule>(vm) |
| 39 | + .ok_or_else(|| vm.new_value_error("Invalid capsule"))?; |
| 40 | + Ok(capsule.name().map(CStr::as_ptr).unwrap_or_default()) |
| 41 | + }) |
| 42 | +} |
| 43 | + |
| 44 | +#[unsafe(no_mangle)] |
| 45 | +pub unsafe extern "C" fn PyCapsule_GetContext(capsule: *mut PyObject) -> *mut c_void { |
| 46 | + with_vm(|vm| { |
| 47 | + let capsule = unsafe { &*capsule } |
| 48 | + .downcast_ref_if_exact::<PyCapsule>(vm) |
| 49 | + .ok_or_else(|| vm.new_value_error("Invalid capsule"))?; |
| 50 | + Ok(capsule.context()) |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +#[unsafe(no_mangle)] |
| 55 | +pub unsafe extern "C" fn PyCapsule_SetContext( |
| 56 | + capsule: *mut PyObject, |
| 57 | + context: *mut c_void, |
| 58 | +) -> c_int { |
| 59 | + with_vm(|vm| { |
| 60 | + let capsule = unsafe { &*capsule } |
| 61 | + .downcast_ref_if_exact::<PyCapsule>(vm) |
| 62 | + .ok_or_else(|| vm.new_value_error("Invalid capsule"))?; |
| 63 | + let _: () = capsule.set_context(context); |
| 64 | + Ok(()) |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +#[unsafe(no_mangle)] |
| 69 | +pub unsafe extern "C" fn PyCapsule_SetPointer( |
| 70 | + capsule: *mut PyObject, |
| 71 | + pointer: *mut c_void, |
| 72 | +) -> c_int { |
| 73 | + with_vm(|vm| { |
| 74 | + let capsule = unsafe { &*capsule } |
| 75 | + .downcast_ref_if_exact::<PyCapsule>(vm) |
| 76 | + .ok_or_else(|| vm.new_value_error("Invalid capsule"))?; |
| 77 | + let _: () = capsule.set_pointer(pointer); |
| 78 | + Ok(()) |
| 79 | + }) |
| 80 | +} |
| 81 | + |
| 82 | +#[unsafe(no_mangle)] |
| 83 | +pub unsafe extern "C" fn PyCapsule_IsValid(capsule: *mut PyObject, name: *const c_char) -> c_int { |
| 84 | + with_vm(|vm| { |
| 85 | + if capsule.is_null() { |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + checked_capsule(vm, unsafe { &*capsule }, name).is_ok() |
| 90 | + }) |
| 91 | +} |
| 92 | + |
| 93 | +#[unsafe(no_mangle)] |
| 94 | +pub unsafe extern "C" fn PyCapsule_Import(name: *const c_char, _no_block: c_int) -> *mut c_void { |
| 95 | + with_vm(|vm| { |
| 96 | + let capsule_name = unsafe { CStr::from_ptr(name) } |
| 97 | + .to_str() |
| 98 | + .map_err(|_| vm.new_system_error("capsule name is not valid UTF-8"))?; |
| 99 | + let (module_name, attrs_path) = capsule_name.split_once('.').ok_or_else(|| { |
| 100 | + vm.new_import_error( |
| 101 | + "capsule name is missing attribute path", |
| 102 | + vm.ctx.new_str(capsule_name), |
| 103 | + ) |
| 104 | + })?; |
| 105 | + let mut obj: PyObjectRef = vm.import(module_name, 0)?; |
| 106 | + |
| 107 | + for attr in attrs_path.split('.') { |
| 108 | + obj = obj.get_attr(attr, vm)?; |
| 109 | + } |
| 110 | + |
| 111 | + Ok(checked_capsule(vm, &obj, name)?.pointer()) |
| 112 | + }) |
| 113 | +} |
| 114 | + |
| 115 | +#[inline] |
| 116 | +fn names_match(stored_name: *const c_char, expected_name: *const c_char) -> bool { |
| 117 | + if stored_name.is_null() || expected_name.is_null() { |
| 118 | + stored_name.is_null() && expected_name.is_null() |
| 119 | + } else { |
| 120 | + unsafe { CStr::from_ptr(stored_name) == CStr::from_ptr(expected_name) } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +#[inline] |
| 125 | +fn checked_capsule<'a>( |
| 126 | + vm: &VirtualMachine, |
| 127 | + obj: &'a PyObject, |
| 128 | + name: *const c_char, |
| 129 | +) -> PyResult<&'a PyCapsule> { |
| 130 | + let capsule = obj |
| 131 | + .downcast_ref_if_exact::<PyCapsule>(vm) |
| 132 | + .ok_or_else(|| vm.new_value_error("Invalid capsule"))?; |
| 133 | + |
| 134 | + if !names_match(capsule.name().map(CStr::as_ptr).unwrap_or_default(), name) { |
| 135 | + return Err(vm.new_value_error("Capsule name does not match")); |
| 136 | + } |
| 137 | + |
| 138 | + if capsule.pointer().is_null() { |
| 139 | + return Err(vm.new_value_error("Capsule has null pointer")); |
| 140 | + } |
| 141 | + |
| 142 | + Ok(capsule) |
| 143 | +} |
| 144 | + |
| 145 | +#[cfg(false)] |
| 146 | +mod tests { |
| 147 | + use pyo3::prelude::*; |
| 148 | + use pyo3::types::PyCapsule; |
| 149 | + |
| 150 | + #[test] |
| 151 | + fn test_capsule_new() { |
| 152 | + Python::attach(|py| { |
| 153 | + let value = String::from("Some data"); |
| 154 | + let capsule = PyCapsule::new_with_value(py, value, c"my_capsule").unwrap(); |
| 155 | + assert!(capsule.is_valid_checked(Some(c"my_capsule"))); |
| 156 | + let ptr = capsule.pointer_checked(Some(c"my_capsule")).unwrap(); |
| 157 | + assert_eq!(unsafe { ptr.cast::<String>().as_ref() }, "Some data"); |
| 158 | + }) |
| 159 | + } |
| 160 | +} |
0 commit comments