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

fix(pymodule): improve safety of PyModule::from_code #4777

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
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
9 changes: 9 additions & 0 deletions src/types/module.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use pyo3_ffi::c_str;

use crate::err::{PyErr, PyResult};
use crate::ffi_ptr_ext::FfiPtrExt;
use crate::impl_::callback::IntoPyCallbackOutput;
Expand Down Expand Up @@ -114,6 +116,8 @@ impl PyModule {
/// containing the Python code passed to `code`
/// and pretending to live at `file_name`.
///
/// If `file_name` is empty, it will be set to `<string>`.
///
/// <div class="information">
/// <div class="tooltip compile_fail" style="">&#x26a0; &#xfe0f;</div>
/// </div><div class="example-wrap" style="display:inline-block"><pre class="compile_fail" style="white-space:normal;font:inherit;">
Expand Down Expand Up @@ -173,6 +177,11 @@ impl PyModule {
file_name: &CStr,
module_name: &CStr,
) -> PyResult<Bound<'py, PyModule>> {
let file_name = if file_name.is_empty() {
c_str!("<string>")
} else {
file_name
};
unsafe {
let code = ffi::Py_CompileString(code.as_ptr(), file_name.as_ptr(), ffi::Py_file_input)
.assume_owned_or_err(py)?;
Expand Down
Loading