From 1fa46d0726b5f72845cdd1bf7d342f249d1ca617 Mon Sep 17 00:00:00 2001 From: David Hewitt <1939362+davidhewitt@users.noreply.github.com> Date: Fri, 28 Jul 2023 22:39:23 +0100 Subject: [PATCH] add macro quotes module for common snippets --- pyo3-macros-backend/src/lib.rs | 1 + pyo3-macros-backend/src/method.rs | 7 ++----- pyo3-macros-backend/src/pymethod.rs | 16 ++++++---------- pyo3-macros-backend/src/quotes.rs | 15 +++++++++++++++ 4 files changed, 24 insertions(+), 15 deletions(-) create mode 100644 pyo3-macros-backend/src/quotes.rs diff --git a/pyo3-macros-backend/src/lib.rs b/pyo3-macros-backend/src/lib.rs index 61cdbb630c0..745a8471c2b 100644 --- a/pyo3-macros-backend/src/lib.rs +++ b/pyo3-macros-backend/src/lib.rs @@ -19,6 +19,7 @@ mod pyclass; mod pyfunction; mod pyimpl; mod pymethod; +mod quotes; pub use frompyobject::build_derive_from_pyobject; pub use module::{process_functions_in_module, pymodule_impl, PyModuleOptions}; diff --git a/pyo3-macros-backend/src/method.rs b/pyo3-macros-backend/src/method.rs index 3d97e70ad56..b290fd55c13 100644 --- a/pyo3-macros-backend/src/method.rs +++ b/pyo3-macros-backend/src/method.rs @@ -3,6 +3,7 @@ use crate::deprecations::{Deprecation, Deprecations}; use crate::params::impl_arg_params; use crate::pyfunction::{DeprecatedArgs, FunctionSignature, PyFunctionArgPyO3Attributes}; use crate::pyfunction::{PyFunctionOptions, SignatureAttribute}; +use crate::quotes; use crate::utils::{self, PythonDoc}; use proc_macro2::{Span, TokenStream}; use quote::ToTokens; @@ -425,11 +426,7 @@ impl<'a> FnSpec<'a> { let func_name = &self.name; let rust_call = |args: Vec| { - quote! { - _pyo3::impl_::pymethods::OkWrap::wrap(function(#self_arg #(#args),*), #py) - .map(|obj| _pyo3::conversion::IntoPyPointer::into_ptr(obj)) - .map_err(::core::convert::Into::into) - } + quotes::map_result_into_ptr(quotes::ok_wrap(quote! { function(#self_arg #(#args),*) })) }; let rust_name = if let Some(cls) = cls { diff --git a/pyo3-macros-backend/src/pymethod.rs b/pyo3-macros-backend/src/pymethod.rs index 87d745dcbab..0658e28af7a 100644 --- a/pyo3-macros-backend/src/pymethod.rs +++ b/pyo3-macros-backend/src/pymethod.rs @@ -3,7 +3,7 @@ use std::borrow::Cow; use crate::attributes::NameAttribute; use crate::method::{CallingConvention, ExtractErrorMode}; use crate::utils::{ensure_not_async_fn, PythonDoc}; -use crate::{deprecations::Deprecations, utils}; +use crate::{deprecations::Deprecations, quotes, utils}; use crate::{ method::{FnArg, FnSpec, FnType, SelfType}, pyfunction::PyFunctionOptions, @@ -446,13 +446,13 @@ 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 - _pyo3::impl_::pymethods::OkWrap::wrap(#fncall, py) - .map_err(::core::convert::Into::into) + #body } }; @@ -636,13 +636,9 @@ pub fn impl_py_getter_def( // tuple struct field syn::Index::from(field_index).to_token_stream() }; - quote! { - ::std::result::Result::Ok( - _pyo3::conversion::IntoPyPointer::into_ptr( - _pyo3::IntoPy::<_pyo3::Py<_pyo3::PyAny>>::into_py(::std::clone::Clone::clone(&(#slf.#field_token)), _py) - ) - ) - } + quotes::map_result_into_ptr(quotes::ok_wrap(quote! { + ::std::clone::Clone::clone(&(#slf.#field_token)) + })) } // Forward to `IntoPyCallbackOutput`, to handle `#[getter]`s returning results. PropertyType::Function { diff --git a/pyo3-macros-backend/src/quotes.rs b/pyo3-macros-backend/src/quotes.rs new file mode 100644 index 00000000000..b3b51404cf6 --- /dev/null +++ b/pyo3-macros-backend/src/quotes.rs @@ -0,0 +1,15 @@ +use proc_macro2::TokenStream; +use quote::quote; + +pub(crate) fn ok_wrap(obj: TokenStream) -> TokenStream { + quote! { + _pyo3::impl_::pymethods::OkWrap::wrap(#obj, py) + .map_err(::core::convert::Into::into) + } +} + +pub(crate) fn map_result_into_ptr(result: TokenStream) -> TokenStream { + quote! { + #result.map(_pyo3::IntoPyPointer::into_ptr) + } +}