Skip to content

Commit dcecdf9

Browse files
Allow creating PyCapsule objects
1 parent 7e637e8 commit dcecdf9

2 files changed

Lines changed: 51 additions & 11 deletions

File tree

‎crates/vm/src/builtins/capsule.rs‎

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
use super::PyType;
2-
use crate::{Context, Py, PyPayload, PyResult, class::PyClassImpl, types::Representable};
2+
use crate::{
3+
AsObject, Context, Py, PyObject, PyPayload, PyResult, VirtualMachine,
4+
class::PyClassImpl,
5+
types::{Destructor, Representable},
6+
};
7+
use core::ffi::c_void;
8+
use core::sync::atomic::AtomicPtr;
39

410
/// PyCapsule - a container for C pointers.
511
/// In RustPython, this is a minimal implementation for compatibility.
612
#[pyclass(module = false, name = "PyCapsule")]
7-
#[derive(Debug, Clone, Copy)]
13+
#[derive(Debug)]
814
pub struct PyCapsule {
9-
// Capsules store opaque pointers; we don't expose the actual pointer functionality
10-
// since RustPython doesn't have the same C extension model as CPython.
11-
_private: (),
15+
ptr: AtomicPtr<c_void>,
16+
destructor: Option<unsafe extern "C" fn(_: *mut PyObject)>,
1217
}
1318

1419
impl PyPayload for PyCapsule {
@@ -18,8 +23,26 @@ impl PyPayload for PyCapsule {
1823
}
1924
}
2025

21-
#[pyclass(with(Representable), flags(DISALLOW_INSTANTIATION))]
22-
impl PyCapsule {}
26+
#[pyclass(with(Representable, Destructor), flags(DISALLOW_INSTANTIATION))]
27+
impl PyCapsule {
28+
pub fn new(
29+
ptr: *mut c_void,
30+
destructor: Option<unsafe extern "C" fn(_: *mut PyObject)>,
31+
) -> Self {
32+
Self {
33+
ptr: ptr.into(),
34+
destructor,
35+
}
36+
}
37+
38+
pub fn pointer(&self) -> *mut c_void {
39+
self.ptr.load(core::sync::atomic::Ordering::Relaxed)
40+
}
41+
42+
fn destructor(&self) -> Option<unsafe extern "C" fn(_: *mut PyObject)> {
43+
self.destructor
44+
}
45+
}
2346

2447
impl Representable for PyCapsule {
2548
#[inline]
@@ -28,6 +51,15 @@ impl Representable for PyCapsule {
2851
}
2952
}
3053

54+
impl Destructor for PyCapsule {
55+
fn del(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<()> {
56+
if let Some(destructor) = zelf.destructor() {
57+
unsafe { destructor(zelf.as_object().as_raw().cast_mut()) };
58+
}
59+
Ok(())
60+
}
61+
}
62+
3163
pub fn init(context: &'static Context) {
3264
PyCapsule::extend_class(context, context.types.capsule_type);
3365
}

‎crates/vm/src/vm/context.rs‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::{
2-
PyResult, VirtualMachine,
2+
PyObject, PyResult, VirtualMachine,
33
builtins::{
4-
PyByteArray, PyBytes, PyComplex, PyDict, PyDictRef, PyEllipsis, PyFloat, PyFrozenSet,
5-
PyInt, PyIntRef, PyList, PyListRef, PyNone, PyNotImplemented, PyStr, PyStrInterned,
6-
PyTuple, PyTupleRef, PyType, PyTypeRef, PyUtf8Str,
4+
PyByteArray, PyBytes, PyCapsule, PyComplex, PyDict, PyDictRef, PyEllipsis, PyFloat,
5+
PyFrozenSet, PyInt, PyIntRef, PyList, PyListRef, PyNone, PyNotImplemented, PyStr,
6+
PyStrInterned, PyTuple, PyTupleRef, PyType, PyTypeRef, PyUtf8Str,
77
bool_::PyBool,
88
code::{self, PyCode},
99
descriptor::{
@@ -752,6 +752,14 @@ impl Context {
752752
let code = code.into_code_object(self);
753753
PyRef::new_ref(PyCode::new(code), self.types.code_type.to_owned(), None)
754754
}
755+
756+
pub fn new_capsule(
757+
&self,
758+
ptr: *mut core::ffi::c_void,
759+
destructor: Option<unsafe extern "C" fn(_: *mut PyObject)>,
760+
) -> PyRef<PyCapsule> {
761+
PyCapsule::new(ptr, destructor).into_ref(self)
762+
}
755763
}
756764

757765
impl AsRef<Self> for Context {

0 commit comments

Comments
 (0)