Skip to content

Commit 22d84b6

Browse files
Add pyframe functions to c-api
1 parent 3f9d80b commit 22d84b6

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎crates/capi/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub mod object;
2828
pub mod osmodule;
2929
pub mod pycapsule;
3030
pub mod pyerrors;
31+
pub mod pyframe;
3132
pub mod pylifecycle;
3233
pub mod pymem;
3334
pub mod pystate;

‎crates/capi/src/pyframe.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use crate::PyObject;
2+
use crate::pystate::with_vm;
3+
use rustpython_vm::frame::Frame;
4+
5+
#[unsafe(no_mangle)]
6+
pub unsafe extern "C" fn PyFrame_GetCode(frame: *mut PyObject) -> *mut PyObject {
7+
with_vm(|vm| {
8+
let frame = unsafe { &*frame }.try_downcast_ref::<Frame>(vm)?;
9+
Ok(frame.code.clone())
10+
})
11+
}
12+
13+
#[unsafe(no_mangle)]
14+
pub unsafe extern "C" fn PyFrame_GetLineNumber(frame: *mut PyObject) -> core::ffi::c_int {
15+
with_vm(|vm| {
16+
let frame = unsafe { &*frame }.try_downcast_ref::<Frame>(vm)?;
17+
let lineno = if frame.lasti() == 0 {
18+
frame.code.first_line_number.map_or(1, |n| n.get())
19+
} else {
20+
frame.current_location().line.get()
21+
};
22+
Ok(lineno as core::ffi::c_int)
23+
})
24+
}

0 commit comments

Comments
 (0)