Beispiel #1
0
def test_implicit_conversion_life_support():
    """Ensure the lifetime of temporary objects created for implicit conversions"""
    assert m.implicitly_convert_argument(UserType(5)) == 5
    assert m.implicitly_convert_variable(UserType(5)) == 5

    assert "outside a bound function" in m.implicitly_convert_variable_fail(
        UserType(5))
Beispiel #2
0
def test_reference_wrapper():
    """std::reference_wrapper for builtin and user types"""
    assert m.refwrap_builtin(42) == 420
    assert m.refwrap_usertype(UserType(42)) == 42
    assert m.refwrap_usertype_const(UserType(42)) == 42

    with pytest.raises(TypeError) as excinfo:
        m.refwrap_builtin(None)
    assert "incompatible function arguments" in str(excinfo.value)

    with pytest.raises(TypeError) as excinfo:
        m.refwrap_usertype(None)
    assert "incompatible function arguments" in str(excinfo.value)

    assert m.refwrap_lvalue().value == 1
    assert m.refwrap_lvalue_const().value == 1

    a1 = m.refwrap_list(copy=True)
    a2 = m.refwrap_list(copy=True)
    assert [x.value for x in a1] == [2, 3]
    assert [x.value for x in a2] == [2, 3]
    assert not a1[0] is a2[0] and not a1[1] is a2[1]

    b1 = m.refwrap_list(copy=False)
    b2 = m.refwrap_list(copy=False)
    assert [x.value for x in b1] == [1, 2]
    assert [x.value for x in b2] == [1, 2]
    assert b1[0] is b2[0] and b1[1] is b2[1]

    assert m.refwrap_iiw(IncType(5)) == 5
    assert m.refwrap_call_iiw(IncType(10), m.refwrap_iiw) == [10, 10, 10, 10]
def test_none_deferred():
    """None passed as various argument types should defer to other overloads"""
    assert not m.defer_none_cstring("abc")
    assert m.defer_none_cstring(None)
    assert not m.defer_none_custom(UserType())
    assert m.defer_none_custom(None)
    assert m.nodefer_none_void(None)
def test_pointers(msg):
    living_before = ConstructorStats.get(UserType).alive()
    assert m.get_void_ptr_value(m.return_void_ptr()) == 0x1234
    assert m.get_void_ptr_value(
        UserType())  # Should also work for other C++ types
    assert ConstructorStats.get(UserType).alive() == living_before

    with pytest.raises(TypeError) as excinfo:
        m.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 m.return_null_str() is None
    assert m.get_null_str_value(m.return_null_str()) is not None

    ptr = m.return_unique_ptr()
    assert "StringList" in repr(ptr)
    assert m.print_opaque_list(ptr) == "Opaque list: [some value]"
Beispiel #5
0
def test_keep_alive_single():
    """Issue #1251 - patients are stored multiple times when given to the same nurse"""

    nurse, p1, p2 = UserType(), UserType(), UserType()
    b = m.refcount(nurse)
    assert [m.refcount(nurse), m.refcount(p1), m.refcount(p2)] == [b, b, b]
    m.add_patient(nurse, p1)
    assert m.get_patients(nurse) == [
        p1,
    ]
    assert [m.refcount(nurse), m.refcount(p1), m.refcount(p2)] == [b, b + 1, b]
    m.add_patient(nurse, p1)
    assert m.get_patients(nurse) == [
        p1,
    ]
    assert [m.refcount(nurse), m.refcount(p1), m.refcount(p2)] == [b, b + 1, b]
    m.add_patient(nurse, p1)
    assert m.get_patients(nurse) == [
        p1,
    ]
    assert [m.refcount(nurse), m.refcount(p1), m.refcount(p2)] == [b, b + 1, b]
    m.add_patient(nurse, p2)
    assert m.get_patients(nurse) == [p1, p2]
    assert [m.refcount(nurse),
            m.refcount(p1), m.refcount(p2)] == [b, b + 1, b + 1]
    m.add_patient(nurse, p2)
    assert m.get_patients(nurse) == [p1, p2]
    assert [m.refcount(nurse),
            m.refcount(p1), m.refcount(p2)] == [b, b + 1, b + 1]
    m.add_patient(nurse, p2)
    m.add_patient(nurse, p1)
    assert m.get_patients(nurse) == [p1, p2]
    assert [m.refcount(nurse),
            m.refcount(p1), m.refcount(p2)] == [b, b + 1, b + 1]
    del nurse
    assert [m.refcount(p1), m.refcount(p2)] == [b, b]
Beispiel #6
0
def test_vec_of_reference_wrapper():
    """#171: Can't return reference wrappers (or STL structures containing them)"""
    assert (str(m.return_vec_of_reference_wrapper(
        UserType(4))) == "[UserType(1), UserType(2), UserType(3), UserType(4)]"
            )