Skip to content
New issue

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

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

Already on GitHub? # to your account

No fallback to __rmod__ magic method when __mod__ is not implemented #1933

Closed
lycantropos opened this issue Oct 18, 2021 · 2 comments · Fixed by #1934
Closed

No fallback to __rmod__ magic method when __mod__ is not implemented #1933

lycantropos opened this issue Oct 18, 2021 · 2 comments · Fixed by #1934

Comments

@lycantropos
Copy link
Contributor

🐛 Bug Reports

🌍 Environment

  • Your operating system and version: macOS 11.6 (20G165)
  • Your python version: 3.9
  • How did you install python (brew install python)? Did you use a virtualenv?: yes
  • Your Rust version (rustc --version): rustc 1.53.0 (53cb7b09b 2021-06-17)
  • Your PyO3 version: 0.14.4
  • Have you tried using latest PyO3 main (replace version = "0.x.y" with git = "https://github.com/PyO3/pyo3")?: yes, the same

💥 Reproducing

This repository has MCVE with README.

@lycantropos
Copy link
Contributor Author

lycantropos commented Oct 18, 2021

I've managed to solve this locally by changing

py_binary_num_func!(mod_, PyNumberModProtocol, T::__mod__);

to

py_binary_fallback_num_func!(
    mod_rmod,
    T,
    PyNumberModProtocol::__mod__,
    PyNumberRModProtocol::__rmod__
);
py_binary_num_func!(mod_, PyNumberModProtocol, T::__mod__);
py_binary_reversed_num_func!(rmod, PyNumberRModProtocol, T::__rmod__);

and

SlotDef::new(&["__mod__"], "Py_nb_remainder", "mod_"),

to

        SlotDef::new(&["__mod__", "__rmod__"], "Py_nb_remainder", "mod_rmod"),
        SlotDef::new(&["__mod__"], "Py_nb_remainder", "mod_"),
        SlotDef::new(&["__rmod__"], "Py_nb_remainder", "rmod"),

if this is enough and there are no other potential places to fix -- I can open PR.

@lycantropos lycantropos changed the title No fallback for __rmod__ magic method when __mod__ is not implemented No fallback to __rmod__ magic method when __mod__ is not implemented Oct 18, 2021
@davidhewitt
Copy link
Member

davidhewitt commented Oct 19, 2021

A PR would be very welcome!

Note that in #1884 we're merging #[pyproto] into #[pymethods], so hopefully this already works on main if my implementation was correct:

#[pyclass]
struct A;

#[pymethods]
impl A {
    #[new]
    fn new() -> A {
        A {}
    }

    fn __mod__(&self, other: &PyAny) -> PyResult<PyObject> {
        println!("A.__mod__ is called");
        Ok(other.py().NotImplemented())
    }

    fn __rmod__(&self, other: &PyAny) -> PyResult<PyObject> {
        println!("A.__rmod__ is called");
        Ok(other.py().NotImplemented())
    }
}

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

Successfully merging a pull request may close this issue.

2 participants