Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for PyFrame functions #4875

Open
dsal3389 opened this issue Jan 26, 2025 · 2 comments
Open

support for PyFrame functions #4875

dsal3389 opened this issue Jan 26, 2025 · 2 comments

Comments

@dsal3389
Copy link

dsal3389 commented Jan 26, 2025

Hello!
I am in the process of writing a python profiler in Rust and I thought it would be a good starting point to
get my toes in pyo3

use case

the sys.setprofile/threading.setprofile function takes a callable that should accept Frame as first argument, the equivalent pyo3 type for frame is the PyFrame, the rust function I pass to setprofile is like so

fn handle(
    frame: Bound<'_, pyo3::types::PyFrame,
    event: ProfilerEvent,  // implementes `FromPyObject`
    args: Bound<'_, PyAny>
) {
    ...
}

problem

unfortunately Bound does not implement PyFrameMethods for Bound<'py, PyFrame> and there is no such trait as PyFrameMethods to be implemented, so I can't access frame functions

(I am still in the process of understanding pyo3 so I might be wrong here)
I tried to use the ffi function directly; PyFrame_GetLineNumber for example, but it expect *mut PyFrameObject which is the underlying ffi object, and I don't have any way to get that ffi object since it is behind the Bound struct

implementation

create PyFrameMethods and implement it on Bound<'py, PyFrame>, since as_ptr cannot be used on PyFrame because as_ptr returns ffi::PyObject and we need PyFrameObject, a new function should be implemented on PyFrame to return the ffi type, and PyFrame struct member should be ffi:PyFrameObject instead of PyAny


I would be glad to take on this implementation

Do you need mentoring?

It would be helpful, yes

@ngoldbaum
Copy link
Contributor

It looks like the existing safe rust wrappers are pretty minimal: https://github.com/PyO3/pyo3/blob/f89b5f71ddb3f482256a7a2f4da5bc8b84db19a5/src/types/frame.rs

It looks like you can also add wrappers for PyFrameLocalsProxy_Type on 3.13 and newer.

Adding more full-featured safe wrappers for the PyFrame API seems worth doing for use-cases like yours.

@ngoldbaum
Copy link
Contributor

see also #4762

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants