Exemplo n.º 1
0
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()
    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() == living_before

    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]
    """  # noqa: E501 line too long

    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]"
Exemplo n.º 2
0
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_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