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

@@ -1,3 +1,4 @@
from __future__ import division
import pytest
import sys
@@ -238,3 +239,10 @@ def test_hash():
assert m.hash_function(Hashable(42)) == 42
with pytest.raises(TypeError):
m.hash_function(Unhashable())
def test_number_protocol():
for a, b in [(1, 1), (3, 5)]:
li = [a == b, a != b, a < b, a <= b, a > b, a >= b, a + b,
a - b, a * b, a / b, a | b, a & b, a ^ b, a >> b, a << b]
assert m.test_number_protocol(a, b) == li