macros: fix the check for applying METH_NOARGS

to only consider the Python argument list.

Fixes #2750
This commit is contained in:
Georg Brandl 2022-11-20 08:48:47 +01:00
parent 3408cc4200
commit ea9da80ab1
3 changed files with 18 additions and 2 deletions

View file

@ -0,0 +1,2 @@
Also apply the `NOARGS` argument convention to methods that have a single
`py: Python` argument.

View file

@ -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
@ -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>,

View file

@ -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,