From 713c2646caf54d7bf35801c8277780dc657fe71b Mon Sep 17 00:00:00 2001 From: Bas Schoenmaeckers <7943856+bschoenmaeckers@users.noreply.github.com> Date: Mon, 11 May 2026 20:20:01 +0200 Subject: [PATCH] Fix `ctypes.py_object` handling (#7836) * Fix `ctypes.py_object` handling * Assume owned pointer --- crates/vm/src/stdlib/_ctypes/base.rs | 12 ++++++++++++ crates/vm/src/stdlib/_ctypes/simple.rs | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/crates/vm/src/stdlib/_ctypes/base.rs b/crates/vm/src/stdlib/_ctypes/base.rs index 9eda289b5..8bd8d106e 100644 --- a/crates/vm/src/stdlib/_ctypes/base.rs +++ b/crates/vm/src/stdlib/_ctypes/base.rs @@ -2342,6 +2342,18 @@ pub(super) fn bytes_to_pyobject( } Ok(vm.ctx.new_int(val).into()) } + "O" => { + // py_object: return Python object from pointer + let ptr = read_ptr_from_buffer(bytes); + if ptr == 0 { + return Err(vm.new_value_error("PyObject is NULL")); + } + unsafe { + let obj = + PyObjectRef::from_raw(core::ptr::NonNull::new_unchecked(ptr as *mut _)); + Ok(obj) + } + } "u" => { let val = if bytes.len() >= mem::size_of::() { let wc = if mem::size_of::() == 2 { diff --git a/crates/vm/src/stdlib/_ctypes/simple.rs b/crates/vm/src/stdlib/_ctypes/simple.rs index bb00d4830..f3ef925be 100644 --- a/crates/vm/src/stdlib/_ctypes/simple.rs +++ b/crates/vm/src/stdlib/_ctypes/simple.rs @@ -466,6 +466,17 @@ impl PyCSimpleType { } } } + // py_object: pass any Python object as PyObject* + Some("O") => { + return Ok(CArgObject { + tag: b'O', + value: FfiArgValue::Pointer(value.get_id()), + obj: value, + size: 0, + offset: 0, + } + .to_pyobject(vm)); + } // c_bool Some("?") => { let bool_val = value.is_true(vm)?;