diff --git a/newsfragments/3142.fixed.md b/newsfragments/3142.fixed.md new file mode 100644 index 00000000..45af8b95 --- /dev/null +++ b/newsfragments/3142.fixed.md @@ -0,0 +1 @@ +Extend lifetime of holder variables to avoid "temporary value dropped while borrowed" errors when `#[pyfunction]`s take references into `#[pyclass]`es diff --git a/pyo3-macros-backend/src/method.rs b/pyo3-macros-backend/src/method.rs index 01aedf69..b7f96747 100644 --- a/pyo3-macros-backend/src/method.rs +++ b/pyo3-macros-backend/src/method.rs @@ -425,9 +425,8 @@ impl<'a> FnSpec<'a> { let rust_call = |args: Vec| { quote! { - let mut ret = function(#self_arg #(#args),*); - let owned = _pyo3::impl_::pymethods::OkWrap::wrap(ret, #py); - owned.map(|obj| _pyo3::conversion::IntoPyPointer::into_ptr(obj)) + _pyo3::impl_::pymethods::OkWrap::wrap(function(#self_arg #(#args),*), #py) + .map(|obj| _pyo3::conversion::IntoPyPointer::into_ptr(obj)) .map_err(::core::convert::Into::into) } }; diff --git a/pyo3-macros-backend/src/pymethod.rs b/pyo3-macros-backend/src/pymethod.rs index 686f0798..a0b0b425 100644 --- a/pyo3-macros-backend/src/pymethod.rs +++ b/pyo3-macros-backend/src/pymethod.rs @@ -456,9 +456,8 @@ fn impl_py_class_attribute(cls: &syn::Type, spec: &FnSpec<'_>) -> syn::Result) -> _pyo3::PyResult<_pyo3::PyObject> { let function = #cls::#name; // Shadow the method name to avoid #3017 #deprecations - let mut ret = #fncall; - let owned = _pyo3::impl_::pymethods::OkWrap::wrap(ret, py); - owned.map_err(::core::convert::Into::into) + _pyo3::impl_::pymethods::OkWrap::wrap(#fncall, py) + .map_err(::core::convert::Into::into) } };