Skip to content

Commit 03c31c5

Browse files
Icxoludavidhewitt
andauthored
fix #[pyfunction] option parsing (#5015)
* fix `#[pyfunction]` option parsing * simplify --------- Co-authored-by: David Hewitt <mail@davidhewitt.dev>
1 parent 0f49eb1 commit 03c31c5

File tree

3 files changed

+11
-22
lines changed

3 files changed

+11
-22
lines changed

newsfragments/5015.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes compile error if more options followed after `crate` for `#[pyfunction]`.

pyo3-macros-backend/src/pyfunction.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ use crate::{
99
};
1010
use proc_macro2::TokenStream;
1111
use quote::{format_ident, quote};
12+
use syn::parse::{Parse, ParseStream};
13+
use syn::punctuated::Punctuated;
1214
use syn::{ext::IdentExt, spanned::Spanned, Result};
13-
use syn::{
14-
parse::{Parse, ParseStream},
15-
token::Comma,
16-
};
1715

1816
mod signature;
1917

@@ -96,24 +94,8 @@ impl Parse for PyFunctionOptions {
9694
fn parse(input: ParseStream<'_>) -> Result<Self> {
9795
let mut options = PyFunctionOptions::default();
9896

99-
while !input.is_empty() {
100-
let lookahead = input.lookahead1();
101-
if lookahead.peek(attributes::kw::name)
102-
|| lookahead.peek(attributes::kw::pass_module)
103-
|| lookahead.peek(attributes::kw::signature)
104-
|| lookahead.peek(attributes::kw::text_signature)
105-
{
106-
options.add_attributes(std::iter::once(input.parse()?))?;
107-
if !input.is_empty() {
108-
let _: Comma = input.parse()?;
109-
}
110-
} else if lookahead.peek(syn::Token![crate]) {
111-
// TODO needs duplicate check?
112-
options.krate = Some(input.parse()?);
113-
} else {
114-
return Err(lookahead.error());
115-
}
116-
}
97+
let attrs = Punctuated::<PyFunctionOption, syn::Token![,]>::parse_terminated(input)?;
98+
options.add_attributes(attrs)?;
11799

118100
Ok(options)
119101
}

src/tests/hygiene/pyfunction.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ fn do_something(x: i32) -> crate::PyResult<i32> {
44
::std::result::Result::Ok(x)
55
}
66

7+
#[crate::pyfunction]
8+
#[pyo3(crate = "crate", name = "check5012")]
9+
fn check_5012(x: i32) -> crate::PyResult<i32> {
10+
::std::result::Result::Ok(x)
11+
}
12+
713
#[test]
814
fn invoke_wrap_pyfunction() {
915
crate::Python::with_gil(|py| {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy