Skip to content

Commit

Permalink
macros: fix the check for applying METH_NOARGS
Browse files Browse the repository at this point in the history
to only consider the Python argument list.

Fixes #2750
  • Loading branch information
birkenfeld committed Nov 20, 2022
1 parent 3408cc4 commit 8b22de2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pyo3-macros-backend/src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl CallingConvention {
/// Different other slots (tp_call, tp_new) can have other requirements
/// and are set manually (see `parse_fn_type` below).
pub fn from_signature(signature: &FunctionSignature<'_>) -> Self {
if signature.arguments.is_empty() {
if signature.python_signature.has_no_args() {
Self::Noargs
} else if signature.python_signature.accepts_kwargs {
// for functions that accept **kwargs, always prefer varargs
Expand Down Expand Up @@ -457,7 +457,12 @@ impl<'a> FnSpec<'a> {

Ok(match self.convention {
CallingConvention::Noargs => {
let call = rust_call(vec![]);
let call = if !self.signature.arguments.is_empty() {
// Only `py` arg can be here
rust_call(vec![quote!(#py)])
} else {
rust_call(vec![])
};
quote! {
unsafe fn #ident<'py>(
#py: _pyo3::Python<'py>,
Expand Down
9 changes: 9 additions & 0 deletions pyo3-macros-backend/src/pyfunction/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,15 @@ pub struct PythonSignature {
pub accepts_kwargs: bool,
}

impl PythonSignature {
pub fn has_no_args(&self) -> bool {
self.positional_parameters.is_empty()
&& self.keyword_only_parameters.is_empty()
&& !self.accepts_varargs
&& !self.accepts_kwargs
}
}

pub struct FunctionSignature<'a> {
pub arguments: Vec<FnArg<'a>>,
pub python_signature: PythonSignature,
Expand Down

0 comments on commit 8b22de2

Please sign in to comment.