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

duplicate definitions with name __pymethod_clear__ #4616

Closed
awolverp opened this issue Oct 13, 2024 · 4 comments · Fixed by #4619
Closed

duplicate definitions with name __pymethod_clear__ #4616

awolverp opened this issue Oct 13, 2024 · 4 comments · Fixed by #4619
Labels

Comments

@awolverp
Copy link

awolverp commented Oct 13, 2024

Bug Description

After updating crate to version 0.22.4, I got this error:

error[E0592]: duplicate definitions with name `__pymethod_clear__`
  --> src/bridge/cache.rs:21:1
   |
21 | #[pyo3::pymethods]
   | ^^^^^^^^^^^^^^^^^^
   | |
   | duplicate definitions for `__pymethod_clear__`
   | other definition for `__pymethod_clear__`
   |
   = note: this error originates in the attribute macro `pyo3::pymethods` (in Nightly builds, run with -Z macro-backtrace for more info)

This is my code:

#[pyo3::pyclass(name="Cache")]
pub struct Cache {
    // fields ...
}

#[pyo3::pymethods]
impl Cache {
    pub fn __traverse__(&self, visit: pyo3::PyVisit<'_>) -> Result<(), pyo3::PyTraverseError> {
        // some code ...
        Ok(())
    }

    pub fn __clear__(&self) {
        // some code ...
    }

    #[pyo3(signature=(*, reuse=false))]
    pub fn clear(&self, reuse: bool) {
        // some code ...
    }
}

I know this is because I have __clear__ and clear methods together, but this was OK before, and now I cannot remove or change them. How can I fix that?

Steps to Reproduce

  1. Initialize a PyO3 project with maturin init
  2. Write this code in src/lib.rs:
use pyo3::prelude::*;

#[pyo3::pyclass(name="Cache")]
pub struct Cache {}

#[pyo3::pymethods]
impl Cache {
    pub fn __traverse__(&self, visit: pyo3::PyVisit<'_>) -> Result<(), pyo3::PyTraverseError> {
        Ok(())
    }

    pub fn __clear__(&self) {
    }

    #[pyo3(signature=(*, reuse=false))]
    pub fn clear(&self, reuse: bool) {
    }
}

/// A Python module implemented in Rust.
#[pymodule]
fn py_test_pyo3(m: &Bound<'_, PyModule>) -> PyResult<()> {
    m.add_class::<Cache>()?;
    Ok(())
}
  1. You will see this error (run maturin develop):
error[E0592]: duplicate definitions with name `__pymethod_clear__`
 --> src/lib.rs:8:1
  |
8 | #[pyo3::pymethods]
  | ^^^^^^^^^^^^^^^^^^
  | |
  | duplicate definitions for `__pymethod_clear__`
  | other definition for `__pymethod_clear__`
  |
  = note: this error originates in the attribute macro `pyo3::pymethods` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
  --> src/lib.rs:9:6
   |
8  |   #[pyo3::pymethods]
   |   ------------------ arguments to this function are incorrect
9  |   impl Cache {
   |  ______^
10 | |     pub fn __traverse__(&self, visit: pyo3::PyVisit<'_>) -> Result<(), pyo3::PyTraverseError> {
11 | |         // some code ...
12 | |         Ok(())
...  |
19 | |     #[pyo3(signature=(*, reuse=false))]
20 | |     pub fn clear(&self, reuse: bool) {
   | |________________^ expected "Rust" fn, found "C" fn
   |
   = note: expected fn pointer `unsafe for<'py> fn(Python<'py>, *mut PyObject, *const *mut PyObject, isize, *mut PyObject) -> Result<*mut PyObject, PyErr>`
                 found fn item `unsafe extern "C" fn(*mut PyObject) -> i32 {Cache::__pymethod_clear__}`
note: function defined here
  --> /home/awolverp/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pyo3-0.22.4/src/impl_/trampoline.rs:56:12
   |
56 |     pub fn fastcall_with_keywords(
   |            ^^^^^^^^^^^^^^^^^^^^^^

Some errors have detailed explanations: E0308, E0592.
For more information about an error, try `rustc --explain E0308`.
warning: `py-test-pyo3` (lib) generated 2 warnings
error: could not compile `py-test-pyo3` (lib) due to 2 previous errors; 2 warnings emitted
💥 maturin failed
  Caused by: Failed to build a native library through cargo
  Caused by: Cargo build finished with "exit status: 101": `env -u CARGO PYO3_ENVIRONMENT_SIGNATURE="cpython-3.12-64bit" PYO3_PYTHON="/home/awolverp/Projects/py-test-pyo3/.venv/bin/python" PYTHON_SYS_EXECUTABLE="/home/awolverp/Projects/py-test-pyo3/.venv/bin/python" "cargo" "rustc" "--features" "pyo3/extension-module" "--message-format" "json-render-diagnostics" "--manifest-path" "/home/awolverp/Projects/py-test-pyo3/Cargo.toml" "--lib"`

Backtrace

No response

Your operating system and version

Linux awolverp-82h8 6.10.13-3-MANJARO #1 SMP PREEMPT_DYNAMIC Tue Oct 8 03:24:49 UTC 2024 x86_64 GNU/Linux

Your Python version (python --version)

Python 3.12.6

Your Rust version (rustc --version)

rustc 1.81.0 (eeb90cda1 2024-09-04)

Your PyO3 version

0.22.4

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

Python is installed by default
Yes, I used virtualenv.

Additional Info

No response

@awolverp awolverp added the bug label Oct 13, 2024
@davidhewitt
Copy link
Member

Ooof, yes this is a regression. Should be easy to fix; I think I will have to ship a 0.22.5 given that we yanked all the rest of 0.22 except for 0.22.4.

@davidhewitt
Copy link
Member

Sorry for the bug and thank you for the report 🙏

@awolverp
Copy link
Author

Thank you for this great crate ❤️

@Icxolu
Copy link
Contributor

Icxolu commented Oct 13, 2024

#4619 should fix this

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.

3 participants