Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
PyCapsule
  • Loading branch information
youknowone committed Feb 8, 2026
commit 343e07c809f5ad9085683a22b6066e97802c968f
33 changes: 33 additions & 0 deletions crates/vm/src/builtins/capsule.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use super::PyType;
use crate::{Context, Py, PyPayload, PyResult, class::PyClassImpl, types::Representable};

/// PyCapsule - a container for C pointers.
/// In RustPython, this is a minimal implementation for compatibility.
#[pyclass(module = false, name = "PyCapsule")]
#[derive(Debug, Clone, Copy)]
pub struct PyCapsule {
// Capsules store opaque pointers; we don't expose the actual pointer functionality
// since RustPython doesn't have the same C extension model as CPython.
_private: (),
}

impl PyPayload for PyCapsule {
#[inline]
fn class(ctx: &Context) -> &'static Py<PyType> {
ctx.types.capsule_type
}
}

#[pyclass(with(Representable), flags(DISALLOW_INSTANTIATION))]
impl PyCapsule {}

impl Representable for PyCapsule {
#[inline]
fn repr_str(_zelf: &Py<Self>, _vm: &crate::VirtualMachine) -> PyResult<String> {
Ok("<capsule object>".to_string())
}
}

pub fn init(context: &Context) {
PyCapsule::extend_class(context, context.types.capsule_type);
}
2 changes: 2 additions & 0 deletions crates/vm/src/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub(crate) mod bytearray;
pub use bytearray::PyByteArray;
pub(crate) mod bytes;
pub use bytes::{PyBytes, PyBytesRef};
pub(crate) mod capsule;
pub use capsule::PyCapsule;
pub(crate) mod classmethod;
pub use classmethod::PyClassMethod;
pub(crate) mod code;
Expand Down
5 changes: 5 additions & 0 deletions crates/vm/src/stdlib/_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ mod _types {
.into()
}

#[pyattr]
fn CapsuleType(vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.types.capsule_type.to_owned().into()
}

#[pyattr]
fn CellType(vm: &VirtualMachine) -> PyObjectRef {
vm.ctx.types.cell_type.to_owned().into()
Expand Down
5 changes: 4 additions & 1 deletion crates/vm/src/types/zoo.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
Py,
builtins::{
asyncgenerator, bool_, builtin_func, bytearray, bytes, classmethod, code, complex,
asyncgenerator, bool_, builtin_func, bytearray, bytes, capsule, classmethod, code, complex,
coroutine, descriptor, dict, enumerate, filter, float, frame, function, generator,
genericalias, getset, int, interpolation, iter, list, map, mappingproxy, memory, module,
namespace, object, property, pystr, range, set, singletons, slice, staticmethod, super_,
Expand All @@ -28,6 +28,7 @@ pub struct TypeZoo {
pub bytearray_iterator_type: &'static Py<PyType>,
pub bool_type: &'static Py<PyType>,
pub callable_iterator: &'static Py<PyType>,
pub capsule_type: &'static Py<PyType>,
pub cell_type: &'static Py<PyType>,
pub classmethod_type: &'static Py<PyType>,
pub code_type: &'static Py<PyType>,
Expand Down Expand Up @@ -160,6 +161,7 @@ impl TypeZoo {
bytearray_iterator_type: bytearray::PyByteArrayIterator::init_builtin_type(),
bytes_iterator_type: bytes::PyBytesIterator::init_builtin_type(),
callable_iterator: iter::PyCallableIterator::init_builtin_type(),
capsule_type: capsule::PyCapsule::init_builtin_type(),
cell_type: function::PyCell::init_builtin_type(),
code_type: code::PyCode::init_builtin_type(),
coroutine_type: coroutine::PyCoroutine::init_builtin_type(),
Expand Down Expand Up @@ -233,6 +235,7 @@ impl TypeZoo {
complex::init(context);
bytes::init(context);
bytearray::init(context);
capsule::init(context);
property::init(context);
getset::init(context);
memory::init(context);
Expand Down