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
Review
  • Loading branch information
bschoenmaeckers committed Jul 8, 2026
commit ddf148d12faf5f3673b56628b85a395bfd9a7cb9
15 changes: 9 additions & 6 deletions crates/capi/src/ceval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ pub extern "C" fn PyEval_GetBuiltins() -> *mut PyObject {
}

#[unsafe(no_mangle)]
pub extern "C" fn PyEval_GetFrame() -> *mut PyObject {
with_vm(|vm| {
pub extern "C" fn PyEval_GetFrame() -> *mut PyFrameObject {
with_vm(|vm| -> *mut PyObject {
vm.current_frame()
.map(|frame| frame.as_object().as_raw())
.map(|frame| frame.as_object().as_raw().cast_mut())
.unwrap_or_default()
})
.cast()
}

#[unsafe(no_mangle)]
Expand Down Expand Up @@ -114,9 +115,11 @@ pub extern "C" fn PyEval_GetGlobals() -> *mut PyObject {
#[unsafe(no_mangle)]
pub extern "C" fn PyEval_GetLocals() -> *mut PyObject {
with_vm(|vm| {
vm.current_frame()
.map(|frame| frame.locals.as_object(vm).as_raw())
.unwrap_or_default()
let Some(frame) = vm.current_frame() else {
return Ok(core::ptr::null_mut());
};
let _ = frame.locals(vm)?;
Ok(frame.locals.as_object(vm).as_raw().cast_mut())
})
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

Expand Down
13 changes: 9 additions & 4 deletions crates/capi/src/pyframe.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
use crate::PyObject;
use crate::pystate::with_vm;
use core::ffi::c_int;
use rustpython_vm::Py;
use rustpython_vm::builtins::PyCode;
use rustpython_vm::frame::Frame;

pub type PyFrameObject = Py<Frame>;
pub type PyCodeObject = Py<PyCode>;

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyFrame_GetCode(frame: *mut PyFrameObject) -> *mut PyObject {
pub unsafe extern "C" fn PyFrame_GetCode(frame: *mut PyFrameObject) -> *mut PyCodeObject {
with_vm(|_vm| Ok(unsafe { &*frame }.f_code()))
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn PyFrame_GetLineNumber(frame: *mut PyFrameObject) -> core::ffi::c_int {
with_vm(|_vm| Ok(unsafe { &*frame }.f_lineno() as core::ffi::c_int))
pub unsafe extern "C" fn PyFrame_GetLineNumber(frame: *mut PyFrameObject) -> c_int {
with_vm(|_vm| {
let lineno = unsafe { &*frame }.f_lineno();
Ok(lineno.try_into().unwrap_or(c_int::MAX))
})
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
13 changes: 12 additions & 1 deletion crates/capi/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::PyObject;
use core::convert::Infallible;
use core::ffi::{CStr, c_char, c_double, c_int, c_long, c_ulong, c_void};
use rustpython_vm::{PyObjectRef, PyRef, PyResult, VirtualMachine};
use rustpython_vm::{Py, PyObjectRef, PyRef, PyResult, VirtualMachine};

pub(crate) trait FfiResult<Output = Self> {
const ERR_VALUE: Output;
Expand Down Expand Up @@ -36,6 +36,17 @@ where
}
}

impl<T> FfiResult<*mut Py<T>> for PyRef<T>
where
Self: Into<PyObjectRef>,
{
const ERR_VALUE: *mut Py<T> = core::ptr::null_mut();

fn into_output(self, _vm: &VirtualMachine) -> *mut Py<T> {
self.into().into_raw().as_ptr().cast()
}
}

impl FfiResult<*mut PyObject> for PyObjectRef {
const ERR_VALUE: *mut PyObject = core::ptr::null_mut();

Expand Down
Loading