Merge pull request #3649 from davidhewitt/module-name-lifetime

hold onto module name properly in `PyCFunction::internal_new`
This commit is contained in:
Adam Reichold 2023-12-14 16:54:23 +00:00 committed by GitHub
commit 763ecb381b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 6 deletions

View File

@ -4,7 +4,7 @@ use crate::prelude::*;
use crate::{
ffi,
impl_::pymethods::{self, PyMethodDef},
types::{PyCapsule, PyDict, PyTuple},
types::{PyCapsule, PyDict, PyString, PyTuple},
};
use std::cell::UnsafeCell;
use std::ffi::CStr;
@ -108,12 +108,11 @@ impl PyCFunction {
py_or_module: PyFunctionArguments<'py>,
) -> PyResult<&'py Self> {
let (py, module) = py_or_module.into_py_and_maybe_module();
let (mod_ptr, module_name) = if let Some(m) = module {
let (mod_ptr, module_name): (_, Option<Py<PyString>>) = if let Some(m) = module {
let mod_ptr = m.as_ptr();
let name: Py<PyAny> = m.name()?.into_py(py);
(mod_ptr, name.as_ptr())
(mod_ptr, Some(m.name()?.into_py(py)))
} else {
(std::ptr::null_mut(), std::ptr::null_mut())
(std::ptr::null_mut(), None)
};
let (def, destructor) = method_def.as_method_def()?;
@ -121,11 +120,15 @@ impl PyCFunction {
let def = Box::into_raw(Box::new(def));
std::mem::forget(destructor);
let module_name_ptr = module_name
.as_ref()
.map_or(std::ptr::null_mut(), Py::as_ptr);
unsafe {
py.from_owned_ptr_or_err::<PyCFunction>(ffi::PyCFunction_NewEx(
def,
mod_ptr,
module_name,
module_name_ptr,
))
}
}