Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated __new__ method does not check args to enforce signature #2748

Closed
matthewlloyd opened this issue Nov 19, 2022 · 1 comment · Fixed by #2749
Closed

Generated __new__ method does not check args to enforce signature #2748

matthewlloyd opened this issue Nov 19, 2022 · 1 comment · Fixed by #2749
Labels

Comments

@matthewlloyd
Copy link
Contributor

Bug Description

Firstly, thank you for creating this amazing crate!

I noticed today that when using #[new] to generate a __new__ method with an empty signature, the generated code doesn't enforce that _args and _kwargs must be empty. As a result, it's possible to call the constructor from Python and provide unused arguments without raising an exception. This could be confusing, for example the developer might expect the kwargs to set field values. I think this is a bug.

Here's part of the code generated by the macros, from cargo expand:

    impl Foo {
        unsafe extern "C" fn __pymethod___new____(
            subtype: *mut _pyo3::ffi::PyTypeObject,
            _args: *mut _pyo3::ffi::PyObject,
            _kwargs: *mut _pyo3::ffi::PyObject,
        ) -> *mut _pyo3::ffi::PyObject {
            use _pyo3::{callback::IntoPyCallbackOutput, pyclass_init::PyObjectInit};
            let gil = _pyo3::GILPool::new();
            let _py = gil.python();
            _pyo3::callback::panic_result_into_callback_output(
                _py,
                ::std::panic::catch_unwind(move || -> _pyo3::PyResult<_> {
                    let result = Foo::new();
                    let initializer: _pyo3::PyClassInitializer<Foo> = result.convert(_py)?;
                    initializer.into_new_object(_py, subtype)
                }),
            )
        }
    }

Steps to Reproduce

#![allow(unused_variables)]
use pyo3::prelude::*;
use pyo3::types::IntoPyDict;

#[pyclass]
struct Foo {}

#[pymethods]
impl Foo {
    #[new]
    pub fn new() -> Self {
        Foo {}
    }
}

pub fn main() -> PyResult<()> {
    pyo3::prepare_freethreaded_python();
    Python::with_gil(|py| {
    	let locals = [("Foo", py.get_type::<Foo>())].into_py_dict(py);
    	py.run("print(Foo()); print(Foo(123)); print(Foo(bar=123))", Some(locals), None)
    })
}

This code prints three objects, without raising any exceptions. The expected behavior would be to print the first and raise an exception on the second (and third of course).

Backtrace

No response

Your operating system and version

WSL2

Your Python version (python --version)

3.11.0

Your Rust version (rustc --version)

rustc 1.60.0-nightly (22e491ac7 2022-01-13)

Your PyO3 version

0.17.3

How did you install python? Did you use a virtualenv?

venv

Additional Info

No response

@birkenfeld
Copy link
Member

Indeed, this is due to this check which for empty signatures simply doesn't generate any code, even if it should check for absence of given args.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants