mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-04 13:41:36 +00:00
object_api: support the number protocol
This commit revamps the object_api class so that it maps most C++ operators to their Python analogs. This makes it possible to, e.g. perform arithmetic using a py::int_ or py::array.
This commit is contained in:
@@ -269,4 +269,24 @@ TEST_SUBMODULE(pytypes, m) {
|
||||
m.def("print_failure", []() { py::print(42, UnregisteredType()); });
|
||||
|
||||
m.def("hash_function", [](py::object obj) { return py::hash(obj); });
|
||||
|
||||
m.def("test_number_protocol", [](py::object a, py::object b) {
|
||||
py::list l;
|
||||
l.append(a.equal(b));
|
||||
l.append(a.not_equal(b));
|
||||
l.append(a < b);
|
||||
l.append(a <= b);
|
||||
l.append(a > b);
|
||||
l.append(a >= b);
|
||||
l.append(a + b);
|
||||
l.append(a - b);
|
||||
l.append(a * b);
|
||||
l.append(a / b);
|
||||
l.append(a | b);
|
||||
l.append(a & b);
|
||||
l.append(a ^ b);
|
||||
l.append(a >> b);
|
||||
l.append(a << b);
|
||||
return l;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user