Skip to content

Commit

Permalink
Avoid some code duplication.
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurentMazare committed Oct 11, 2021
1 parent 4412844 commit 5a02814
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/types/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,24 @@ impl PyCFunction {
methods::PyCFunctionWithKeywords(run_closure::<F, R>),
"",
);
Self::internal_new_from_pointers(method_def, py, capsule_ptr, std::ptr::null_mut())
}

#[doc(hidden)]
fn internal_new_from_pointers(
method_def: PyMethodDef,
py: Python,
mod_ptr: *mut ffi::PyObject,
module_name: *mut ffi::PyObject,
) -> PyResult<&Self> {
let def = method_def
.as_method_def()
.map_err(|err| PyValueError::new_err(err.0))?;
unsafe {
py.from_owned_ptr_or_err::<PyCFunction>(ffi::PyCFunction_NewEx(
Box::into_raw(Box::new(def)),
capsule_ptr,
std::ptr::null_mut(),
mod_ptr,
module_name,
))
}
}
Expand All @@ -120,24 +130,14 @@ impl PyCFunction {
py_or_module: PyFunctionArguments,
) -> PyResult<&Self> {
let (py, module) = py_or_module.into_py_and_maybe_module();
let def = method_def
.as_method_def()
.map_err(|err| PyValueError::new_err(err.0))?;
let (mod_ptr, module_name) = if let Some(m) = module {
let mod_ptr = m.as_ptr();
let name = m.name()?.into_py(py);
(mod_ptr, name.as_ptr())
} else {
(std::ptr::null_mut(), std::ptr::null_mut())
};

unsafe {
py.from_owned_ptr_or_err::<PyCFunction>(ffi::PyCFunction_NewEx(
Box::into_raw(Box::new(def)),
mod_ptr,
module_name,
))
}
Self::internal_new_from_pointers(method_def, py, mod_ptr, module_name)
}
}

Expand Down

0 comments on commit 5a02814

Please sign in to comment.