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:
Wenzel Jakob
2018-09-01 01:09:16 +02:00
parent 5c8746ff13
commit 067100201f
3 changed files with 110 additions and 0 deletions

View File

@@ -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;
});
}