add macro quotes module for common snippets
This commit is contained in:
parent
e86dbab387
commit
686fe0aac2
|
@ -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};
|
||||
|
|
|
@ -2,6 +2,7 @@ use crate::attributes::{TextSignatureAttribute, TextSignatureAttributeValue};
|
|||
use crate::params::impl_arg_params;
|
||||
use crate::pyfunction::{FunctionSignature, PyFunctionArgPyO3Attributes};
|
||||
use crate::pyfunction::{PyFunctionOptions, SignatureAttribute};
|
||||
use crate::quotes;
|
||||
use crate::utils::{self, PythonDoc};
|
||||
use proc_macro2::{Span, TokenStream};
|
||||
use quote::ToTokens;
|
||||
|
@ -413,11 +414,7 @@ impl<'a> FnSpec<'a> {
|
|||
let func_name = &self.name;
|
||||
|
||||
let rust_call = |args: Vec<TokenStream>| {
|
||||
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 {
|
||||
|
|
|
@ -2,12 +2,12 @@ use std::borrow::Cow;
|
|||
|
||||
use crate::attributes::NameAttribute;
|
||||
use crate::method::{CallingConvention, ExtractErrorMode};
|
||||
use crate::utils;
|
||||
use crate::utils::{ensure_not_async_fn, PythonDoc};
|
||||
use crate::{
|
||||
method::{FnArg, FnSpec, FnType, SelfType},
|
||||
pyfunction::PyFunctionOptions,
|
||||
};
|
||||
use crate::{quotes, utils};
|
||||
use proc_macro2::{Span, TokenStream};
|
||||
use quote::{format_ident, quote, ToTokens};
|
||||
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 python_name = spec.null_terminated_python_name();
|
||||
let body = quotes::ok_wrap(fncall);
|
||||
|
||||
let associated_method = quote! {
|
||||
fn #wrapper_ident(py: _pyo3::Python<'_>) -> _pyo3::PyResult<_pyo3::PyObject> {
|
||||
let function = #cls::#name; // Shadow the method name to avoid #3017
|
||||
_pyo3::impl_::pymethods::OkWrap::wrap(#fncall, py)
|
||||
.map_err(::core::convert::Into::into)
|
||||
#body
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -641,13 +641,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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue