add macro quotes module for common snippets

This commit is contained in:
David Hewitt 2023-07-28 22:39:23 +01:00
parent e86dbab387
commit 686fe0aac2
4 changed files with 24 additions and 15 deletions

View File

@ -19,6 +19,7 @@ mod pyclass;
mod pyfunction; mod pyfunction;
mod pyimpl; mod pyimpl;
mod pymethod; mod pymethod;
mod quotes;
pub use frompyobject::build_derive_from_pyobject; pub use frompyobject::build_derive_from_pyobject;
pub use module::{process_functions_in_module, pymodule_impl, PyModuleOptions}; pub use module::{process_functions_in_module, pymodule_impl, PyModuleOptions};

View File

@ -2,6 +2,7 @@ use crate::attributes::{TextSignatureAttribute, TextSignatureAttributeValue};
use crate::params::impl_arg_params; use crate::params::impl_arg_params;
use crate::pyfunction::{FunctionSignature, PyFunctionArgPyO3Attributes}; use crate::pyfunction::{FunctionSignature, PyFunctionArgPyO3Attributes};
use crate::pyfunction::{PyFunctionOptions, SignatureAttribute}; use crate::pyfunction::{PyFunctionOptions, SignatureAttribute};
use crate::quotes;
use crate::utils::{self, PythonDoc}; use crate::utils::{self, PythonDoc};
use proc_macro2::{Span, TokenStream}; use proc_macro2::{Span, TokenStream};
use quote::ToTokens; use quote::ToTokens;
@ -413,11 +414,7 @@ impl<'a> FnSpec<'a> {
let func_name = &self.name; let func_name = &self.name;
let rust_call = |args: Vec<TokenStream>| { let rust_call = |args: Vec<TokenStream>| {
quote! { quotes::map_result_into_ptr(quotes::ok_wrap(quote! { function(#self_arg #(#args),*) }))
_pyo3::impl_::pymethods::OkWrap::wrap(function(#self_arg #(#args),*), #py)
.map(|obj| _pyo3::conversion::IntoPyPointer::into_ptr(obj))
.map_err(::core::convert::Into::into)
}
}; };
let rust_name = if let Some(cls) = cls { let rust_name = if let Some(cls) = cls {

View File

@ -2,12 +2,12 @@ use std::borrow::Cow;
use crate::attributes::NameAttribute; use crate::attributes::NameAttribute;
use crate::method::{CallingConvention, ExtractErrorMode}; use crate::method::{CallingConvention, ExtractErrorMode};
use crate::utils;
use crate::utils::{ensure_not_async_fn, PythonDoc}; use crate::utils::{ensure_not_async_fn, PythonDoc};
use crate::{ use crate::{
method::{FnArg, FnSpec, FnType, SelfType}, method::{FnArg, FnSpec, FnType, SelfType},
pyfunction::PyFunctionOptions, pyfunction::PyFunctionOptions,
}; };
use crate::{quotes, utils};
use proc_macro2::{Span, TokenStream}; use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote, ToTokens}; use quote::{format_ident, quote, ToTokens};
use syn::{ext::IdentExt, spanned::Spanned, Result}; use syn::{ext::IdentExt, spanned::Spanned, Result};
@ -451,12 +451,12 @@ fn impl_py_class_attribute(cls: &syn::Type, spec: &FnSpec<'_>) -> syn::Result<Me
let wrapper_ident = format_ident!("__pymethod_{}__", name); let wrapper_ident = format_ident!("__pymethod_{}__", name);
let python_name = spec.null_terminated_python_name(); let python_name = spec.null_terminated_python_name();
let body = quotes::ok_wrap(fncall);
let associated_method = quote! { let associated_method = quote! {
fn #wrapper_ident(py: _pyo3::Python<'_>) -> _pyo3::PyResult<_pyo3::PyObject> { fn #wrapper_ident(py: _pyo3::Python<'_>) -> _pyo3::PyResult<_pyo3::PyObject> {
let function = #cls::#name; // Shadow the method name to avoid #3017 let function = #cls::#name; // Shadow the method name to avoid #3017
_pyo3::impl_::pymethods::OkWrap::wrap(#fncall, py) #body
.map_err(::core::convert::Into::into)
} }
}; };
@ -641,13 +641,9 @@ pub fn impl_py_getter_def(
// tuple struct field // tuple struct field
syn::Index::from(field_index).to_token_stream() syn::Index::from(field_index).to_token_stream()
}; };
quote! { quotes::map_result_into_ptr(quotes::ok_wrap(quote! {
::std::result::Result::Ok( ::std::clone::Clone::clone(&(#slf.#field_token))
_pyo3::conversion::IntoPyPointer::into_ptr( }))
_pyo3::IntoPy::<_pyo3::Py<_pyo3::PyAny>>::into_py(::std::clone::Clone::clone(&(#slf.#field_token)), _py)
)
)
}
} }
// Forward to `IntoPyCallbackOutput`, to handle `#[getter]`s returning results. // Forward to `IntoPyCallbackOutput`, to handle `#[getter]`s returning results.
PropertyType::Function { PropertyType::Function {

View File

@ -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)
}
}