-
Notifications
You must be signed in to change notification settings - Fork 855
refs #4286 -- allow setting submodule on declarative pymodules #4301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
allow setting `submodule` on declarative `#[pymodule]`s |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
|
||
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] | ||
use proc_macro::TokenStream; | ||
use proc_macro2::TokenStream as TokenStream2; | ||
use proc_macro2::{Span, TokenStream as TokenStream2}; | ||
use pyo3_macros_backend::{ | ||
build_derive_from_pyobject, build_py_class, build_py_enum, build_py_function, build_py_methods, | ||
pymodule_function_impl, pymodule_module_impl, PyClassArgs, PyClassMethodsType, | ||
|
@@ -35,10 +35,26 @@ use syn::{parse::Nothing, parse_macro_input, Item}; | |
/// [1]: https://pyo3.rs/latest/module.html | ||
#[proc_macro_attribute] | ||
pub fn pymodule(args: TokenStream, input: TokenStream) -> TokenStream { | ||
parse_macro_input!(args as Nothing); | ||
match parse_macro_input!(input as Item) { | ||
Item::Mod(module) => pymodule_module_impl(module), | ||
Item::Fn(function) => pymodule_function_impl(function), | ||
Item::Mod(module) => { | ||
let is_submodule = match parse_macro_input!(args as Option<syn::Ident>) { | ||
Some(i) if i == "submodule" => true, | ||
Some(_) => { | ||
return syn::Error::new( | ||
Span::call_site(), | ||
"#[pymodule] only accepts submodule as an argument", | ||
) | ||
.into_compile_error() | ||
.into(); | ||
} | ||
None => false, | ||
}; | ||
pymodule_module_impl(module, is_submodule) | ||
} | ||
Item::Fn(function) => { | ||
parse_macro_input!(args as Nothing); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it seems reasonable to me for function modules to also support There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure it's relevant for function modules -- there's never been a need to use |
||
pymodule_function_impl(function) | ||
} | ||
unsupported => Err(syn::Error::new_spanned( | ||
unsupported, | ||
"#[pymodule] only supports modules and functions.", | ||
|
Uh oh!
There was an error while loading. Please reload this page.