From 6ec499b2bf266b207e16ccacbc6dc8ab4929bd54 Mon Sep 17 00:00:00 2001 From: Icxolu <10486322+Icxolu@users.noreply.github.com> Date: Mon, 2 Dec 2024 15:42:40 +0100 Subject: [PATCH] fix another `unsafe_op_in_unsafe_fn` trigger (#4753) --- pyo3-macros-backend/src/params.rs | 3 ++- tests/ui/forbid_unsafe.rs | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pyo3-macros-backend/src/params.rs b/pyo3-macros-backend/src/params.rs index 9517d35b25c..f967149c725 100644 --- a/pyo3-macros-backend/src/params.rs +++ b/pyo3-macros-backend/src/params.rs @@ -262,9 +262,10 @@ pub(crate) fn impl_regular_arg_param( )? } } else { + let unwrap = quote! {unsafe { #pyo3_path::impl_::extract_argument::unwrap_required_argument(#arg_value) }}; quote_arg_span! { #pyo3_path::impl_::extract_argument::from_py_with( - #pyo3_path::impl_::extract_argument::unwrap_required_argument(#arg_value), + #unwrap, #name_str, #from_py_with as fn(_) -> _, )? diff --git a/tests/ui/forbid_unsafe.rs b/tests/ui/forbid_unsafe.rs index 660f5fa36c0..4ab7639d658 100644 --- a/tests/ui/forbid_unsafe.rs +++ b/tests/ui/forbid_unsafe.rs @@ -28,4 +28,16 @@ mod gh_4394 { pub struct Version; } +mod from_py_with { + use pyo3::prelude::*; + use pyo3::types::PyBytes; + + fn bytes_from_py(bytes: &Bound<'_, PyAny>) -> PyResult> { + Ok(bytes.downcast::()?.as_bytes().to_vec()) + } + + #[pyfunction] + fn f(#[pyo3(from_py_with = "bytes_from_py")] _bytes: Vec) {} +} + fn main() {}