Skip to content

Commit

Permalink
add bindings for PyImport_AddModuleRef and use it in Python::run_code
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoldbaum committed Sep 4, 2024
1 parent 8ee5510 commit 31d7fc1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
13 changes: 13 additions & 0 deletions pyo3-ffi/src/compat/py_3_13.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,16 @@ compat_function!(
item
}
);

compat_function!(
originally_defined_for(Py_3_13);

#[inline]
pub unsafe fn PyImport_AddModuleRef(
name: *const std::os::raw::c_char,
) -> *mut crate::PyObject {
use crate::{Py_XNewRef, PyImport_AddModule};

Py_XNewRef(PyImport_AddModule(name))
}
);
3 changes: 3 additions & 0 deletions pyo3-ffi/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ extern "C" {
pub fn PyImport_AddModuleObject(name: *mut PyObject) -> *mut PyObject;
#[cfg_attr(PyPy, link_name = "PyPyImport_AddModule")]
pub fn PyImport_AddModule(name: *const c_char) -> *mut PyObject;
#[cfg(Py_3_13)]
#[cfg_attr(PyPy, link_name = "PyPyImport_AddModuleRef")]
pub fn PyImport_AddModuleRef(name: *const c_char) -> *mut PyObject;
#[cfg_attr(PyPy, link_name = "PyPyImport_ImportModule")]
pub fn PyImport_ImportModule(name: *const c_char) -> *mut PyObject;
#[cfg_attr(PyPy, link_name = "PyPyImport_ImportModuleNoBlock")]
Expand Down
3 changes: 2 additions & 1 deletion src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,14 +634,15 @@ impl<'py> Python<'py> {
locals: Option<&Bound<'py, PyDict>>,
) -> PyResult<Bound<'py, PyAny>> {
unsafe {
let mptr = ffi::PyImport_AddModule(ffi::c_str!("__main__").as_ptr());
let mptr = ffi::compat::PyImport_AddModuleRef(ffi::c_str!("__main__").as_ptr());
if mptr.is_null() {
return Err(PyErr::fetch(self));
}

let globals = globals
.map(|dict| dict.as_ptr())
.unwrap_or_else(|| ffi::PyModule_GetDict(mptr));
ffi::Py_DECREF(mptr);
let locals = locals.map(|dict| dict.as_ptr()).unwrap_or(globals);

// If `globals` don't provide `__builtins__`, most of the code will fail if Python
Expand Down

0 comments on commit 31d7fc1

Please sign in to comment.