def test05_pythonizer(self): """Implement and test a pythonizor""" import cppyy def pythonizor(klass, name): if name == 'Integer2': klass.__repr__ = lambda self: repr(self.m_data) cppyy.py.add_pythonization(pythonizor, 'Math') Integer2 = cppyy.gbl.Math.Integer2 # first time a new namespace is used, it can not be imported from v = [Integer2(j) for j in range(10)] assert str(v) == '[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]' i2 = Integer2(13) assert int(i2) == 13
def test06_add_operator(self): """Add operator+""" import cppyy cppyy.cppdef(""" namespace Math { Integer2 operator+(const Integer2& left, const Integer1& right) { return left.m_data + right.m_data; } }""") from cppyy.gbl import Integer1 from cppyy.gbl.Math import Integer2 i = Integer1(42) i2 = Integer2(13) k = i2 + i assert int(k) == i2.m_data + i.m_data