def test_pointers(msg): from pybind11_tests import (return_void_ptr, get_void_ptr_value, ExampleMandA, print_opaque_list, return_null_str, get_null_str_value, return_unique_ptr, ConstructorStats) living_before = ConstructorStats.get(ExampleMandA).alive() if get_void_ptr_value(return_void_ptr()) != 0x1234: raise AssertionError if not get_void_ptr_value(ExampleMandA()): raise AssertionError if ConstructorStats.get(ExampleMandA).alive() != living_before: raise AssertionError with pytest.raises(TypeError) as excinfo: get_void_ptr_value([1, 2, 3]) # This should not work if msg(excinfo.value) != """ get_void_ptr_value(): incompatible function arguments. The following argument types are supported: 1. (arg0: capsule) -> int Invoked with: [1, 2, 3] """: raise AssertionError if return_null_str() is not None: raise AssertionError if get_null_str_value(return_null_str()) is None: raise AssertionError ptr = return_unique_ptr() if "StringList" not in repr(ptr): raise AssertionError if print_opaque_list(ptr) != "Opaque list: [some value]": raise AssertionError
def test_pointers(msg): from pybind11_tests import (return_void_ptr, get_void_ptr_value, ExampleMandA, print_opaque_list, return_null_str, get_null_str_value, return_unique_ptr, ConstructorStats) assert get_void_ptr_value(return_void_ptr()) == 0x1234 assert get_void_ptr_value( ExampleMandA()) # Should also work for other C++ types assert ConstructorStats.get(ExampleMandA).alive() == 0 with pytest.raises(TypeError) as excinfo: get_void_ptr_value([1, 2, 3]) # This should not work assert msg(excinfo.value) == """ get_void_ptr_value(): incompatible function arguments. The following argument types are supported: 1. (arg0: capsule) -> int Invoked with: [1, 2, 3] """ assert return_null_str() is None assert get_null_str_value(return_null_str()) is not None ptr = return_unique_ptr() assert "StringList" in repr(ptr) assert print_opaque_list(ptr) == "Opaque list: [some value]"
def test_copy_method(): """Issue #443: calling copied methods fails in Python 3""" from pybind11_tests import ExampleMandA ExampleMandA.add2c = ExampleMandA.add2 ExampleMandA.add2d = ExampleMandA.add2b a = ExampleMandA(123) assert a.value == 123 a.add2(ExampleMandA(-100)) assert a.value == 23 a.add2b(ExampleMandA(20)) assert a.value == 43 a.add2c(ExampleMandA(6)) assert a.value == 49 a.add2d(ExampleMandA(-7)) assert a.value == 42
def test_no_mixed_overloads(): from pybind11_tests import debug_enabled with pytest.raises(RuntimeError) as excinfo: ExampleMandA.add_mixed_overloads1() assert (str(excinfo.value) == "overloading a method with both static and instance methods is not supported; " + ("compile in debug mode for more details" if not debug_enabled else "error while attempting to bind static method ExampleMandA.overload_mixed1" "() -> str") ) with pytest.raises(RuntimeError) as excinfo: ExampleMandA.add_mixed_overloads2() assert (str(excinfo.value) == "overloading a method with both static and instance methods is not supported; " + ("compile in debug mode for more details" if not debug_enabled else "error while attempting to bind instance method ExampleMandA.overload_mixed2" "(self: pybind11_tests.ExampleMandA, arg0: int, arg1: int) -> str") )
def test_methods_and_attributes(): instance1 = ExampleMandA() instance2 = ExampleMandA(32) instance1.add1(instance2) instance1.add2(instance2) instance1.add3(instance2) instance1.add4(instance2) instance1.add5(instance2) instance1.add6(32) instance1.add7(32) instance1.add8(32) instance1.add9(32) instance1.add10(32) assert str(instance1) == "ExampleMandA[value=320]" assert str(instance2) == "ExampleMandA[value=32]" assert str(instance1.self1()) == "ExampleMandA[value=320]" assert str(instance1.self2()) == "ExampleMandA[value=320]" assert str(instance1.self3()) == "ExampleMandA[value=320]" assert str(instance1.self4()) == "ExampleMandA[value=320]" assert str(instance1.self5()) == "ExampleMandA[value=320]" assert instance1.internal1() == 320 assert instance1.internal2() == 320 assert instance1.internal3() == 320 assert instance1.internal4() == 320 assert instance1.internal5() == 320 assert instance1.overloaded(1, 1.0) == "(int, float)" assert instance1.overloaded(2.0, 2) == "(float, int)" assert instance1.overloaded_const(3, 3.0) == "(int, float) const" assert instance1.overloaded_const(4.0, 4) == "(float, int) const" assert instance1.value == 320 instance1.value = 100 assert str(instance1) == "ExampleMandA[value=100]" cstats = ConstructorStats.get(ExampleMandA) assert cstats.alive() == 2 del instance1, instance2 assert cstats.alive() == 0 assert cstats.values() == ["32"] assert cstats.default_constructions == 1 assert cstats.copy_constructions == 3 assert cstats.move_constructions >= 1 assert cstats.copy_assignments == 0 assert cstats.move_assignments == 0
def test_methods_and_attributes(): instance1 = ExampleMandA() instance2 = ExampleMandA(32) instance1.add1(instance2) instance1.add2(instance2) instance1.add3(instance2) instance1.add4(instance2) instance1.add5(instance2) instance1.add6(32) instance1.add7(32) instance1.add8(32) instance1.add9(32) instance1.add10(32) assert str(instance1) == "ExampleMandA[value=320]" assert str(instance2) == "ExampleMandA[value=32]" assert str(instance1.self1()) == "ExampleMandA[value=320]" assert str(instance1.self2()) == "ExampleMandA[value=320]" assert str(instance1.self3()) == "ExampleMandA[value=320]" assert str(instance1.self4()) == "ExampleMandA[value=320]" assert str(instance1.self5()) == "ExampleMandA[value=320]" assert instance1.internal1() == 320 assert instance1.internal2() == 320 assert instance1.internal3() == 320 assert instance1.internal4() == 320 assert instance1.internal5() == 320 assert instance1.overloaded(1, 1.0) == "(int, float)" assert instance1.overloaded(2.0, 2) == "(float, int)" assert instance1.overloaded(3, 3) == "(int, int)" assert instance1.overloaded(4., 4.) == "(float, float)" assert instance1.overloaded_const(5, 5.0) == "(int, float) const" assert instance1.overloaded_const(6.0, 6) == "(float, int) const" assert instance1.overloaded_const(7, 7) == "(int, int) const" assert instance1.overloaded_const(8., 8.) == "(float, float) const" assert instance1.overloaded_float(1, 1) == "(float, float)" assert instance1.overloaded_float(1, 1.) == "(float, float)" assert instance1.overloaded_float(1., 1) == "(float, float)" assert instance1.overloaded_float(1., 1.) == "(float, float)" assert instance1.value == 320 instance1.value = 100 assert str(instance1) == "ExampleMandA[value=100]" cstats = ConstructorStats.get(ExampleMandA) assert cstats.alive() == 2 del instance1, instance2 assert cstats.alive() == 0 assert cstats.values() == ["32"] assert cstats.default_constructions == 1 assert cstats.copy_constructions == 3 assert cstats.move_constructions >= 1 assert cstats.copy_assignments == 0 assert cstats.move_assignments == 0
def test_methods_and_attributes(): instance1 = ExampleMandA() instance2 = ExampleMandA(32) instance1.add1(instance2) instance1.add2(instance2) instance1.add3(instance2) instance1.add4(instance2) instance1.add5(instance2) instance1.add6(32) instance1.add7(32) instance1.add8(32) instance1.add9(32) instance1.add10(32) assert str(instance1) == "ExampleMandA[value=320]" assert str(instance2) == "ExampleMandA[value=32]" assert str(instance1.self1()) == "ExampleMandA[value=320]" assert str(instance1.self2()) == "ExampleMandA[value=320]" assert str(instance1.self3()) == "ExampleMandA[value=320]" assert str(instance1.self4()) == "ExampleMandA[value=320]" assert str(instance1.self5()) == "ExampleMandA[value=320]" assert instance1.internal1() == 320 assert instance1.internal2() == 320 assert instance1.internal3() == 320 assert instance1.internal4() == 320 assert instance1.internal5() == 320 assert instance1.value == 320 instance1.value = 100 assert str(instance1) == "ExampleMandA[value=100]" cstats = ConstructorStats.get(ExampleMandA) assert cstats.alive() == 2 del instance1, instance2 assert cstats.alive() == 0 assert cstats.values() == ["32"] assert cstats.default_constructions == 1 assert cstats.copy_constructions == 3 assert cstats.move_constructions >= 1 assert cstats.copy_assignments == 0 assert cstats.move_assignments == 0
def test_methods_and_attributes(): instance1 = ExampleMandA() instance2 = ExampleMandA(32) instance1.add1(instance2) instance1.add2(instance2) instance1.add3(instance2) instance1.add4(instance2) instance1.add5(instance2) instance1.add6(32) instance1.add7(32) instance1.add8(32) instance1.add9(32) instance1.add10(32) if str(instance1) != "ExampleMandA[value=320]": raise AssertionError if str(instance2) != "ExampleMandA[value=32]": raise AssertionError if str(instance1.self1()) != "ExampleMandA[value=320]": raise AssertionError if str(instance1.self2()) != "ExampleMandA[value=320]": raise AssertionError if str(instance1.self3()) != "ExampleMandA[value=320]": raise AssertionError if str(instance1.self4()) != "ExampleMandA[value=320]": raise AssertionError if str(instance1.self5()) != "ExampleMandA[value=320]": raise AssertionError if instance1.internal1() != 320: raise AssertionError if instance1.internal2() != 320: raise AssertionError if instance1.internal3() != 320: raise AssertionError if instance1.internal4() != 320: raise AssertionError if instance1.internal5() != 320: raise AssertionError if instance1.overloaded(1, 1.0) != "(int, float)": raise AssertionError if instance1.overloaded(2.0, 2) != "(float, int)": raise AssertionError if instance1.overloaded(3, 3) != "(int, int)": raise AssertionError if instance1.overloaded(4., 4.) != "(float, float)": raise AssertionError if instance1.overloaded_const(5, 5.0) != "(int, float) const": raise AssertionError if instance1.overloaded_const(6.0, 6) != "(float, int) const": raise AssertionError if instance1.overloaded_const(7, 7) != "(int, int) const": raise AssertionError if instance1.overloaded_const(8., 8.) != "(float, float) const": raise AssertionError if instance1.overloaded_float(1, 1) != "(float, float)": raise AssertionError if instance1.overloaded_float(1, 1.) != "(float, float)": raise AssertionError if instance1.overloaded_float(1., 1) != "(float, float)": raise AssertionError if instance1.overloaded_float(1., 1.) != "(float, float)": raise AssertionError if instance1.value != 320: raise AssertionError instance1.value = 100 if str(instance1) != "ExampleMandA[value=100]": raise AssertionError cstats = ConstructorStats.get(ExampleMandA) if cstats.alive() != 2: raise AssertionError del instance1, instance2 if cstats.alive() != 0: raise AssertionError if cstats.values() != ["32"]: raise AssertionError if cstats.default_constructions != 1: raise AssertionError if cstats.copy_constructions != 3: raise AssertionError if cstats.move_constructions < 1: raise AssertionError if cstats.copy_assignments != 0: raise AssertionError if cstats.move_assignments != 0: raise AssertionError