Skip to content

Commit

Permalink
Speficy METH_COEXIST for protocol methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kngwyu committed Mar 29, 2020
1 parent a76bd7c commit b2ebd08
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pyo3-derive-backend/src/pyproto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ fn impl_proto_impl(
Some(pyo3::class::PyMethodDef {
ml_name: stringify!(#name),
ml_meth: pyo3::class::PyMethodType::PyCFunctionWithKeywords(__wrap),
ml_flags: pyo3::ffi::METH_VARARGS | pyo3::ffi::METH_KEYWORDS,
// We need METH_COEXIST here to prevent __add__ from overriding __radd__
ml_flags: pyo3::ffi::METH_VARARGS | pyo3::ffi::METH_KEYWORDS | pyo3::ffi::METH_COEXIST,
ml_doc: ""
})
}
Expand Down
6 changes: 4 additions & 2 deletions tests/test_arithmetics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,11 @@ fn lhs_override_rhs() {
let py = gil.python();

let c = PyCell::new(py, LhsAndRhsArithmetic {}).unwrap();
py_run!(py, c, "assert c.__radd__(1) == '1 + BA'");
// Not overrided
py_run!(py, c, "assert c.__radd__(1) == '1 + RA'");
py_run!(py, c, "assert c.__rsub__(1) == '1 - RA'");
// Overrided
py_run!(py, c, "assert 1 + c == '1 + BA'");
py_run!(py, c, "assert c.__rsub__(1) == '1 - BA'");
py_run!(py, c, "assert 1 - c == '1 - BA'");
}

Expand Down

0 comments on commit b2ebd08

Please sign in to comment.