Skip to content

Commit

Permalink
PyFrame: basic methods to get started
Browse files Browse the repository at this point in the history
Signed-off-by: daniel sonbolian <[email protected]>
  • Loading branch information
dsal3389 committed Jan 29, 2025
1 parent 0a97d53 commit 8ac36cf
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
1 change: 0 additions & 1 deletion pyo3-ffi/src/pyframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ extern "C" {
#[cfg(Py_3_11)]
pub fn PyFrame_GetBuiltins(frame: *mut PyFrameObject) -> *mut PyObject;

#[cfg(Py_3_11)]
pub fn PyFrame_GetLocals(frame: *mut PyFrameObject) -> *mut PyObject;

#[cfg(Py_3_11)]
Expand Down
1 change: 1 addition & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ pub use crate::types::traceback::PyTracebackMethods;
pub use crate::types::tuple::PyTupleMethods;
pub use crate::types::typeobject::PyTypeMethods;
pub use crate::types::weakref::PyWeakrefMethods;
pub use crate::types::frame::PyFrameMethods;
20 changes: 20 additions & 0 deletions src/types/frame.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::ffi;
use crate::ffi_ptr_ext::FfiPtrExt;
use crate::PyAny;
use crate::{Bound, PyObject, PyResult};

/// Represents a Python frame.
///
Expand All @@ -13,3 +15,21 @@ pyobject_native_type_core!(
pyobject_native_static_type_object!(ffi::PyFrame_Type),
#checkfunction=ffi::PyFrame_Check
);

pub trait PyFrameMethods<'py> {
fn get_line(&self) -> usize;

fn get_locals(&self) -> PyResult<Bound<'py, PyAny>>;
}

impl<'py> PyFrameMethods<'py> for Bound<'py, PyFrame> {
fn get_line(&self) -> usize {
(unsafe { ffi::PyFrame_GetLineNumber(self.as_ptr() as *mut ffi::PyFrameObject) }) as usize
}

fn get_locals(&self) -> PyResult<Bound<'py, PyAny>> {
unsafe {
ffi::PyFrame_GetLocals(self.as_ptr() as *mut ffi::PyFrameObject).assume_owned_or_err(self.py())
}
}
}
4 changes: 2 additions & 2 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use self::dict::{PyDictItems, PyDictKeys, PyDictValues};
pub use self::ellipsis::PyEllipsis;
pub use self::float::{PyFloat, PyFloatMethods};
#[cfg(all(not(Py_LIMITED_API), not(PyPy), not(GraalPy)))]
pub use self::frame::PyFrame;
pub use self::frame::{PyFrame, PyFrameMethods};
pub use self::frozenset::{PyFrozenSet, PyFrozenSetBuilder, PyFrozenSetMethods};
pub use self::function::PyCFunction;
#[cfg(all(not(Py_LIMITED_API), not(all(PyPy, not(Py_3_8)))))]
Expand Down Expand Up @@ -245,7 +245,7 @@ pub(crate) mod dict;
mod ellipsis;
pub(crate) mod float;
#[cfg(all(not(Py_LIMITED_API), not(PyPy), not(GraalPy)))]
mod frame;
pub mod frame;
pub(crate) mod frozenset;
mod function;
pub(crate) mod iterator;
Expand Down

0 comments on commit 8ac36cf

Please sign in to comment.