Skip to content

Commit

Permalink
Fix being able to call arg-less #[new] with any args from Python
Browse files Browse the repository at this point in the history
Fixes #2748
  • Loading branch information
birkenfeld committed Nov 19, 2022
1 parent 249c020 commit 2acd431
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
2 changes: 2 additions & 0 deletions newsfragments/2749.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a bug that allowed `#[new]` pymethods with no arguments to be called from
Python with any argument list.
4 changes: 0 additions & 4 deletions pyo3-macros-backend/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ pub fn impl_arg_params(
py: &syn::Ident,
fastcall: bool,
) -> Result<(TokenStream, Vec<TokenStream>)> {
if spec.signature.arguments.is_empty() {
return Ok((TokenStream::new(), vec![]));
}

let args_array = syn::Ident::new("output", Span::call_site());

if !fastcall && is_forwarded_args(&spec.signature) {
Expand Down
9 changes: 9 additions & 0 deletions tests/test_class_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::IntoPyDict;

#[pyclass]
struct EmptyClassWithNew {}
Expand All @@ -23,6 +24,14 @@ fn empty_class_with_new() {
.unwrap()
.downcast::<PyCell<EmptyClassWithNew>>()
.is_ok());

// Calling with arbitrary args or kwargs is not ok
assert!(typeobj
.call(("some", "args"), None)
.is_err());
assert!(typeobj
.call((), Some([("some", "kwarg")].into_py_dict(py)))
.is_err());
});
}

Expand Down

0 comments on commit 2acd431

Please sign in to comment.