-
Notifications
You must be signed in to change notification settings - Fork 679
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
Allow inline struct, enum, fn and mod inside of declarative modules #3902
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Allow to add `#[pymodule_export]` to elements annotated with `#[pyclass]`, `#[pyfunction]` and `#[pymodule]`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,19 +114,10 @@ pub fn pymodule_module_impl(mut module: syn::ItemMod) -> Result<TokenStream> { | |
for item in &mut *items { | ||
match item { | ||
Item::Use(item_use) => { | ||
let mut is_pyo3 = false; | ||
item_use.attrs.retain(|attr| { | ||
let found = attr.path().is_ident("pymodule_export"); | ||
is_pyo3 |= found; | ||
!found | ||
}); | ||
if is_pyo3 { | ||
let cfg_attrs = item_use | ||
.attrs | ||
.iter() | ||
.filter(|attr| attr.path().is_ident("cfg")) | ||
.cloned() | ||
.collect::<Vec<_>>(); | ||
let is_pymodule_export = | ||
find_and_remove_attribute(&mut item_use.attrs, "pymodule_export"); | ||
if is_pymodule_export { | ||
let cfg_attrs = get_cfg_attributes(&item_use.attrs); | ||
extract_use_items( | ||
&item_use.tree, | ||
&cfg_attrs, | ||
|
@@ -136,23 +127,116 @@ pub fn pymodule_module_impl(mut module: syn::ItemMod) -> Result<TokenStream> { | |
} | ||
} | ||
Item::Fn(item_fn) => { | ||
let mut is_module_init = false; | ||
item_fn.attrs.retain(|attr| { | ||
let found = attr.path().is_ident("pymodule_init"); | ||
is_module_init |= found; | ||
!found | ||
}); | ||
if is_module_init { | ||
ensure_spanned!(pymodule_init.is_none(), item_fn.span() => "only one pymodule_init may be specified"); | ||
let ident = &item_fn.sig.ident; | ||
ensure_spanned!( | ||
!has_attribute(&item_fn.attrs, "pymodule_export"), | ||
item.span() => "`#[pymodule_export]` may only be used on `use` statements" | ||
); | ||
let is_pymodule_init = | ||
find_and_remove_attribute(&mut item_fn.attrs, "pymodule_init"); | ||
let ident = &item_fn.sig.ident; | ||
if is_pymodule_init { | ||
ensure_spanned!( | ||
!has_attribute(&item_fn.attrs, "pyfunction"), | ||
item_fn.span() => "`#[pyfunction]` cannot be used alongside `#[pymodule_init]`" | ||
); | ||
ensure_spanned!(pymodule_init.is_none(), item_fn.span() => "only one `#[pymodule_init]` may be specified"); | ||
pymodule_init = Some(quote! { #ident(module)?; }); | ||
} else { | ||
bail_spanned!(item.span() => "only 'use' statements and and pymodule_init functions are allowed in #[pymodule]") | ||
} else if has_attribute(&item_fn.attrs, "pyfunction") { | ||
module_items.push(ident.clone()); | ||
module_items_cfg_attrs.push(get_cfg_attributes(&item_fn.attrs)); | ||
} | ||
} | ||
item => { | ||
bail_spanned!(item.span() => "only 'use' statements and and pymodule_init functions are allowed in #[pymodule]") | ||
Item::Struct(item_struct) => { | ||
ensure_spanned!( | ||
!has_attribute(&item_struct.attrs, "pymodule_export"), | ||
item.span() => "`#[pymodule_export]` may only be used on `use` statements" | ||
); | ||
if has_attribute(&item_struct.attrs, "pyclass") { | ||
module_items.push(item_struct.ident.clone()); | ||
module_items_cfg_attrs.push(get_cfg_attributes(&item_struct.attrs)); | ||
} | ||
} | ||
Item::Enum(item_enum) => { | ||
ensure_spanned!( | ||
!has_attribute(&item_enum.attrs, "pymodule_export"), | ||
item.span() => "`#[pymodule_export]` may only be used on `use` statements" | ||
); | ||
if has_attribute(&item_enum.attrs, "pyclass") { | ||
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. Very nice way to reuse the existing machinery! |
||
module_items.push(item_enum.ident.clone()); | ||
module_items_cfg_attrs.push(get_cfg_attributes(&item_enum.attrs)); | ||
} | ||
} | ||
Item::Mod(item_mod) => { | ||
ensure_spanned!( | ||
!has_attribute(&item_mod.attrs, "pymodule_export"), | ||
item.span() => "`#[pymodule_export]` may only be used on `use` statements" | ||
); | ||
if has_attribute(&item_mod.attrs, "pymodule") { | ||
module_items.push(item_mod.ident.clone()); | ||
module_items_cfg_attrs.push(get_cfg_attributes(&item_mod.attrs)); | ||
} | ||
} | ||
Item::ForeignMod(item) => { | ||
ensure_spanned!( | ||
!has_attribute(&item.attrs, "pymodule_export"), | ||
item.span() => "`#[pymodule_export]` may only be used on `use` statements" | ||
); | ||
} | ||
Item::Trait(item) => { | ||
ensure_spanned!( | ||
!has_attribute(&item.attrs, "pymodule_export"), | ||
item.span() => "`#[pymodule_export]` may only be used on `use` statements" | ||
); | ||
} | ||
Item::Const(item) => { | ||
ensure_spanned!( | ||
!has_attribute(&item.attrs, "pymodule_export"), | ||
item.span() => "`#[pymodule_export]` may only be used on `use` statements" | ||
); | ||
} | ||
Item::Static(item) => { | ||
ensure_spanned!( | ||
!has_attribute(&item.attrs, "pymodule_export"), | ||
item.span() => "`#[pymodule_export]` may only be used on `use` statements" | ||
); | ||
} | ||
Item::Macro(item) => { | ||
ensure_spanned!( | ||
!has_attribute(&item.attrs, "pymodule_export"), | ||
item.span() => "`#[pymodule_export]` may only be used on `use` statements" | ||
); | ||
} | ||
Item::ExternCrate(item) => { | ||
ensure_spanned!( | ||
!has_attribute(&item.attrs, "pymodule_export"), | ||
item.span() => "`#[pymodule_export]` may only be used on `use` statements" | ||
); | ||
} | ||
Item::Impl(item) => { | ||
ensure_spanned!( | ||
!has_attribute(&item.attrs, "pymodule_export"), | ||
item.span() => "`#[pymodule_export]` may only be used on `use` statements" | ||
); | ||
} | ||
Item::TraitAlias(item) => { | ||
ensure_spanned!( | ||
!has_attribute(&item.attrs, "pymodule_export"), | ||
item.span() => "`#[pymodule_export]` may only be used on `use` statements" | ||
); | ||
} | ||
Item::Type(item) => { | ||
ensure_spanned!( | ||
!has_attribute(&item.attrs, "pymodule_export"), | ||
item.span() => "`#[pymodule_export]` may only be used on `use` statements" | ||
); | ||
} | ||
Item::Union(item) => { | ||
ensure_spanned!( | ||
!has_attribute(&item.attrs, "pymodule_export"), | ||
item.span() => "`#[pymodule_export]` may only be used on `use` statements" | ||
); | ||
} | ||
_ => (), | ||
} | ||
} | ||
|
||
|
@@ -352,6 +436,31 @@ fn get_pyfn_attr(attrs: &mut Vec<syn::Attribute>) -> syn::Result<Option<PyFnArgs | |
Ok(pyfn_args) | ||
} | ||
|
||
fn get_cfg_attributes(attrs: &[syn::Attribute]) -> Vec<syn::Attribute> { | ||
attrs | ||
.iter() | ||
.filter(|attr| attr.path().is_ident("cfg")) | ||
.cloned() | ||
.collect() | ||
} | ||
|
||
fn find_and_remove_attribute(attrs: &mut Vec<syn::Attribute>, ident: &str) -> bool { | ||
let mut found = false; | ||
attrs.retain(|attr| { | ||
if attr.path().is_ident(ident) { | ||
found = true; | ||
false | ||
} else { | ||
true | ||
} | ||
}); | ||
found | ||
} | ||
|
||
fn has_attribute(attrs: &[syn::Attribute], ident: &str) -> bool { | ||
attrs.iter().any(|attr| attr.path().is_ident(ident)) | ||
} | ||
|
||
enum PyModulePyO3Option { | ||
Crate(CrateAttribute), | ||
Name(NameAttribute), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thing about the changelog entry not being needed here, I'll remove.