Skip to content

Expose Py_CompileString #5124

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

Open
nschloe opened this issue May 8, 2025 · 3 comments · May be fixed by #5149
Open

Expose Py_CompileString #5124

nschloe opened this issue May 8, 2025 · 3 comments · May be fixed by #5149

Comments

@nschloe
Copy link

nschloe commented May 8, 2025

When using py.run, pyo3 will always compile the code (https://pyo3.rs/main/doc/src/pyo3/marker.rs#653).

I'd like to see ffi::Py_CompileString exposed in the API so I can compile once, and py.run_code_object multiple times after that.

@davidhewitt
Copy link
Member

Seems like a reasonable addition by extending our PyCode type.

@superserious-dev
Copy link
Contributor

I'd be interested in working on this if it hasn't been picked up yet. Could you explain more about extending PyCode?

I'm also curious if the following could be a possible approach:

(1) Create 2 new methods:

pub fn compile_code_string(self, code_str: &CStr, start: c_int) -> Bound<'py, PyCode> {...}
pub fn run_code_object(self, code_obj: &Bound<'py, PyCode>, globals: Option<&Bound<'py, PyDict>>, locals: Option<&Bound<'py, PyDict>>) -> PyResult<Bound<'py, PyAny>> {...}

(2) Modify run_code so that it calls compile_code_string followed by run_code_object.

(3) Document the methods as part of the public API.

I'm still new to the codebase, so this is just an initial thought.

@mejrs
Copy link
Member

mejrs commented May 13, 2025

Yeah something like that. I'd just call them compile and run though.

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

Successfully merging a pull request may close this issue.

4 participants