Fix being able to call arg-less `#[new]` with any args from Python

Fixes #2748
This commit is contained in:
Georg Brandl 2022-11-19 07:33:43 +01:00
parent 740cfa0057
commit 2a630a2a52
4 changed files with 10 additions and 5 deletions

View File

@ -0,0 +1,2 @@
Fix a bug that allowed `#[new]` pymethods with no arguments to be called from
Python with any argument list.

View File

@ -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) {

View File

@ -2,6 +2,7 @@
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::IntoPyDict;
#[pyclass]
struct EmptyClassWithNew {}
@ -23,6 +24,12 @@ 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());
});
}

View File

@ -259,7 +259,7 @@ mod inheriting_native_type {
#[pymethods]
impl CustomException {
#[new]
fn new() -> Self {
fn new(_exc_arg: &PyAny) -> Self {
CustomException {
context: "Hello :)",
}