From 1e31fc44512e6e872bb75c89796534715f094b00 Mon Sep 17 00:00:00 2001 From: konstin Date: Tue, 3 Jul 2018 21:11:56 +0200 Subject: [PATCH] Remove even more clutter --- pyo3-derive-backend/src/module.rs | 43 ++++++++++----------- pyo3-derive-backend/src/py_class.rs | 28 +------------- pyo3-derive-backend/src/py_impl.rs | 20 +--------- pyo3-derive-backend/src/py_method.rs | 9 ++--- pyo3-derive-backend/src/py_proto.rs | 19 +--------- pyo3cls/src/lib.rs | 56 ++++++++++++++-------------- 6 files changed, 56 insertions(+), 119 deletions(-) diff --git a/pyo3-derive-backend/src/module.rs b/pyo3-derive-backend/src/module.rs index 6c0a73b8..d878f0ab 100644 --- a/pyo3-derive-backend/src/module.rs +++ b/pyo3-derive-backend/src/module.rs @@ -19,32 +19,31 @@ pub fn py3_init(fnname: &syn::Ident, name: &syn::Ident, doc: syn::Lit) -> TokenS #[no_mangle] #[allow(non_snake_case, unused_imports)] pub unsafe extern "C" fn #cb_name() -> *mut ::pyo3::ffi::PyObject { - use std; - use pyo3::{IntoPyPointer, ObjectProtocol}; + use ::pyo3::{IntoPyPointer, ObjectProtocol}; // initialize pyo3 - pyo3::prepare_pyo3_library(); + ::pyo3::prepare_pyo3_library(); - static mut MODULE_DEF: pyo3::ffi::PyModuleDef = pyo3::ffi::PyModuleDef_INIT; + static mut MODULE_DEF: ::pyo3::ffi::PyModuleDef = ::pyo3::ffi::PyModuleDef_INIT; // We can't convert &'static str to *const c_char within a static initializer, // so we'll do it here in the module initialization: MODULE_DEF.m_name = concat!(stringify!(#name), "\0").as_ptr() as *const _; #[cfg(py_sys_config = "WITH_THREAD")] - pyo3::ffi::PyEval_InitThreads(); + ::pyo3::ffi::PyEval_InitThreads(); - let _module = pyo3::ffi::PyModule_Create(&mut MODULE_DEF); + let _module = ::pyo3::ffi::PyModule_Create(&mut MODULE_DEF); if _module.is_null() { return _module; } - let _pool = pyo3::GILPool::new(); - let _py = pyo3::Python::assume_gil_acquired(); - let _module = match _py.from_owned_ptr_or_err::(_module) { + let _pool = ::pyo3::GILPool::new(); + let _py = ::pyo3::Python::assume_gil_acquired(); + let _module = match _py.from_owned_ptr_or_err::<::pyo3::PyModule>(_module) { Ok(m) => m, Err(e) => { - pyo3::PyErr::from(e).restore(_py); - return std::ptr::null_mut(); + ::pyo3::PyErr::from(e).restore(_py); + return ::std::ptr::null_mut(); } }; _module.add("__doc__", #doc).expect("Failed to add doc for module"); @@ -52,7 +51,7 @@ pub fn py3_init(fnname: &syn::Ident, name: &syn::Ident, doc: syn::Lit) -> TokenS Ok(_) => _module.into_ptr(), Err(e) => { e.restore(_py); - std::ptr::null_mut() + ::std::ptr::null_mut() } } } @@ -67,24 +66,22 @@ pub fn py2_init(fnname: &syn::Ident, name: &syn::Ident, doc: syn::Lit) -> TokenS #[no_mangle] #[allow(non_snake_case, unused_imports)] pub unsafe extern "C" fn #cb_name() { - use std; - // initialize python - pyo3::prepare_pyo3_library(); - pyo3::ffi::PyEval_InitThreads(); + ::pyo3::prepare_pyo3_library(); + ::pyo3::ffi::PyEval_InitThreads(); let _name = concat!(stringify!(#name), "\0").as_ptr() as *const _; - let _pool = pyo3::GILPool::new(); - let _py = pyo3::Python::assume_gil_acquired(); - let _module = pyo3::ffi::Py_InitModule(_name, std::ptr::null_mut()); + let _pool = ::pyo3::GILPool::new(); + let _py = ::pyo3::Python::assume_gil_acquired(); + let _module = ::pyo3::ffi::Py_InitModule(_name, ::std::ptr::null_mut()); if _module.is_null() { return } - let _module = match _py.from_borrowed_ptr_or_err::(_module) { + let _module = match _py.from_borrowed_ptr_or_err::<::pyo3::PyModule>(_module) { Ok(m) => m, Err(e) => { - pyo3::PyErr::from(e).restore(_py); + ::pyo3::PyErr::from(e).restore(_py); return } }; @@ -242,8 +239,6 @@ pub fn add_fn_to_module( let tokens = quote! { fn #function_wrapper_ident(py: ::pyo3::Python) -> ::pyo3::PyObject { - use std; - #wrapper let _def = ::pyo3::class::PyMethodDef { @@ -258,7 +253,7 @@ pub fn add_fn_to_module( py, ::pyo3::ffi::PyCFunction_New( Box::into_raw(Box::new(_def.as_method_def())), - std::ptr::null_mut() + ::std::ptr::null_mut() ) ) }; diff --git a/pyo3-derive-backend/src/py_class.rs b/pyo3-derive-backend/src/py_class.rs index 1b6f4bed..490eaab7 100644 --- a/pyo3-derive-backend/src/py_class.rs +++ b/pyo3-derive-backend/src/py_class.rs @@ -39,17 +39,10 @@ pub fn build_py_class( panic!("#[class] can only be used with structs") } - let dummy_const = syn::Ident::new(&format!("_IMPL_PYO3_CLS_{}", ast.ident), Span::call_site()); let tokens = impl_class(&ast.ident, &base, token, doc, params, flags, descriptors); quote! { - #[allow(non_upper_case_globals, unused_attributes, - unused_qualifications, unused_variables, non_camel_case_types)] - const #dummy_const: () = { - use std; - - #tokens - }; + #tokens } } @@ -360,7 +353,7 @@ fn impl_descriptors( }).collect::>() }).collect(); - let tokens = quote! { + quote! { #(#methods)* impl ::pyo3::class::methods::PyPropMethodsProtocolImpl for #cls { @@ -371,23 +364,6 @@ fn impl_descriptors( METHODS } } - }; - - let n = match cls { - &syn::Type::Path(ref typath) => { - typath.path.segments.last().as_ref().unwrap().value().ident.to_string() - } - _ => "CLS_METHODS".to_string() - }; - - let dummy_const = syn::Ident::new(&format!("_IMPL_pyo3_DESCRIPTORS_{}", n), Span::call_site()); - quote! { - #[feature(specialization)] - #[allow(non_upper_case_globals, unused_attributes, - unused_qualifications, unused_variables, unused_imports)] - const #dummy_const: () = { - #tokens - }; } } diff --git a/pyo3-derive-backend/src/py_impl.rs b/pyo3-derive-backend/src/py_impl.rs index 796acccb..9c3f878c 100644 --- a/pyo3-derive-backend/src/py_impl.rs +++ b/pyo3-derive-backend/src/py_impl.rs @@ -3,7 +3,7 @@ use syn; use py_method; -use proc_macro2::{TokenStream, Span}; +use proc_macro2::TokenStream; pub fn build_py_methods(ast: &mut syn::ItemImpl) -> TokenStream { @@ -25,7 +25,7 @@ pub fn impl_methods(ty: &syn::Type, impls: &mut Vec) -> TokenStre } } - let tokens = quote! { + quote! { impl ::pyo3::class::methods::PyMethodsProtocolImpl for #ty { fn py_methods() -> &'static [::pyo3::class::PyMethodDefType] { static METHODS: &'static [::pyo3::class::PyMethodDefType] = &[ @@ -34,21 +34,5 @@ pub fn impl_methods(ty: &syn::Type, impls: &mut Vec) -> TokenStre METHODS } } - }; - - let n = if let &syn::Type::Path(ref typath) = ty { - typath.path.segments.last().as_ref().unwrap().value().ident.to_string() - } else { - "CLS_METHODS".to_string() - }; - - let dummy_const = syn::Ident::new(&format!("_IMPL_PYO3_METHODS_{}", n), Span::call_site()); - quote! { - #[feature(specialization)] - #[allow(non_upper_case_globals, unused_attributes, - unused_qualifications, unused_variables, unused_imports)] - const #dummy_const: () = { - #tokens - }; } } diff --git a/pyo3-derive-backend/src/py_method.rs b/pyo3-derive-backend/src/py_method.rs index 170f81f3..da5c2cdd 100644 --- a/pyo3-derive-backend/src/py_method.rs +++ b/pyo3-derive-backend/src/py_method.rs @@ -88,7 +88,6 @@ pub fn impl_wrap(cls: &syn::Type, name: &syn::Ident, spec: &FnSpec, noargs: bool _args: *mut ::pyo3::ffi::PyObject, _kwargs: *mut ::pyo3::ffi::PyObject) -> *mut ::pyo3::ffi::PyObject { - use pyo3::ToPyPointer; const _LOCATION: &'static str = concat!( stringify!(#cls), ".", stringify!(#name), "()"); let _pool = ::pyo3::GILPool::new(); @@ -152,7 +151,6 @@ pub fn impl_wrap_new(cls: &syn::Type, name: &syn::Ident, spec: &FnSpec) -> Token _args: *mut ::pyo3::ffi::PyObject, _kwargs: *mut ::pyo3::ffi::PyObject) -> *mut ::pyo3::ffi::PyObject { - use std::ptr; use pyo3::typeob::PyTypeInfo; const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#name),"()"); @@ -169,13 +167,13 @@ pub fn impl_wrap_new(cls: &syn::Type, name: &syn::Ident, spec: &FnSpec) -> Token Ok(_) => _obj.into_ptr(), Err(e) => { e.restore(_py); - ptr::null_mut() + ::std::ptr::null_mut() } } } Err(e) => { e.restore(_py); - ptr::null_mut() + ::std::ptr::null_mut() } } } @@ -292,7 +290,6 @@ pub(crate) fn impl_wrap_getter(cls: &syn::Type, name: &syn::Ident) -> TokenStrea unsafe extern "C" fn __wrap( _slf: *mut ::pyo3::ffi::PyObject, _: *mut ::pyo3::c_void) -> *mut ::pyo3::ffi::PyObject { - use std; const _LOCATION: &'static str = concat!(stringify!(#cls),".",stringify!(#name),"()"); let _pool = ::pyo3::GILPool::new(); @@ -305,7 +302,7 @@ pub(crate) fn impl_wrap_getter(cls: &syn::Type, name: &syn::Ident) -> TokenStrea } Err(e) => { e.restore(_py); - std::ptr::null_mut() + ::std::ptr::null_mut() } } } diff --git a/pyo3-derive-backend/src/py_proto.rs b/pyo3-derive-backend/src/py_proto.rs index 949dd37b..4f5a7278 100644 --- a/pyo3-derive-backend/src/py_proto.rs +++ b/pyo3-derive-backend/src/py_proto.rs @@ -108,24 +108,9 @@ fn impl_proto_impl( } } - // unique mod name - let p = proto.name; - let n = if let syn::Type::Path(ref typath) = ty { - typath.path.segments.last().as_ref().unwrap().value().ident.to_string() - } else { - "PROTO_METHODS".to_string() - }; - - let dummy_const: syn::Path = syn::parse_str(&format!("_IMPL_PYO3_{}_{}", n, p)).unwrap(); quote! { - #[feature(specialization)] - #[allow(non_upper_case_globals, unused_attributes, - unused_qualifications, unused_variables, - unused_imports)] - const #dummy_const: () = { - #tokens + #tokens - #(#py_methods)* - }; + #(#py_methods)* } } diff --git a/pyo3cls/src/lib.rs b/pyo3cls/src/lib.rs index 353124ef..4b54b5f9 100644 --- a/pyo3cls/src/lib.rs +++ b/pyo3cls/src/lib.rs @@ -8,10 +8,10 @@ extern crate proc_macro; extern crate proc_macro2; extern crate pyo3_derive_backend; +#[macro_use] extern crate quote; extern crate syn; -use quote::{ToTokens, TokenStreamExt}; use syn::buffer::TokenBuffer; use syn::punctuated::Punctuated; use syn::token::Comma; @@ -33,12 +33,12 @@ pub fn mod2init(attr: proc_macro::TokenStream, input: proc_macro::TokenStream) - module::process_functions_in_module(&mut ast); // Create the module initialisation function - let init = module::py2_init(&ast.ident, &modname, utils::get_doc(&ast.attrs, false)); + let expanded = module::py2_init(&ast.ident, &modname, utils::get_doc(&ast.attrs, false)); - // Return the generated code as a TokenStream - let mut tokens = ast.into_token_stream(); - tokens.append_all(init); - tokens.into() + quote! ( + #ast + #expanded + ).into() } #[proc_macro_attribute] @@ -55,12 +55,12 @@ pub fn mod3init(attr: proc_macro::TokenStream, input: proc_macro::TokenStream) - module::process_functions_in_module(&mut ast); // Create the module initialisation function - let init = module::py3_init(&ast.ident, &modname, utils::get_doc(&ast.attrs, false)); + let expanded = module::py3_init(&ast.ident, &modname, utils::get_doc(&ast.attrs, false)); - // Return the generated code as a TokenStream - let mut tokens = ast.into_token_stream(); - tokens.append_all(init); - tokens.into() + quote! ( + #ast + #expanded + ).into() } #[proc_macro_attribute] @@ -72,10 +72,10 @@ pub fn proto(_: proc_macro::TokenStream, input: proc_macro::TokenStream) -> proc // Build the output let expanded = py_proto::build_py_proto(&mut ast); - // Return the generated impl as a TokenStream - let mut tokens = ast.into_token_stream(); - tokens.append_all(expanded); - tokens.into() + quote! ( + #ast + #expanded + ).into() } #[proc_macro_attribute] @@ -94,25 +94,25 @@ pub fn class(attr: proc_macro::TokenStream, input: proc_macro::TokenStream) -> p // Build the output let expanded = py_class::build_py_class(&mut ast, &args); - // Return the generated impl as a TokenStream - let mut tokens = ast.into_token_stream(); - tokens.append_all(expanded); - tokens.into() + quote! ( + #ast + #expanded + ).into() } #[proc_macro_attribute] pub fn methods(_: proc_macro::TokenStream, input: proc_macro::TokenStream) -> proc_macro::TokenStream { // Parse the token stream into a syntax tree - let mut ast: syn::ItemImpl = syn::parse(input) + let mut ast: syn::ItemImpl = syn::parse(input.clone()) .expect("#[methods] must be used on an `impl` block"); // Build the output let expanded = py_impl::build_py_methods(&mut ast); - // Return the generated impl as a TokenStream - let mut tokens = ast.into_token_stream(); - tokens.append_all(expanded); - tokens.into() + quote! ( + #ast + #expanded + ).into() } #[proc_macro_attribute] @@ -125,8 +125,8 @@ pub fn function(_: proc_macro::TokenStream, input: proc_macro::TokenStream) -> p let python_name = ast.ident.clone(); let expanded = module::add_fn_to_module(&mut ast, &python_name, Vec::new()); - // Return the generated impl as a TokenStream - let mut tokens = ast.into_token_stream(); - tokens.append_all(expanded); - tokens.into() + quote! ( + #ast + #expanded + ).into() }