Exemple #1
0
def test_numpy_ref_mutators():
    """Tests numpy mutating Eigen matrices (for returned Eigen::Ref<...>s)"""
    from pybind11_tests import (
        get_cm_ref, get_cm_const_ref, get_rm_ref, get_rm_const_ref, reset_refs)
    reset_refs()  # In case another test already changed it

    zc = get_cm_ref()
    zcro = get_cm_const_ref()
    zr = get_rm_ref()
    zrro = get_rm_const_ref()

    assert [zc[1, 2], zcro[1, 2], zr[1, 2], zrro[1, 2]] == [23] * 4

    assert not zc.flags.owndata and zc.flags.writeable
    assert not zr.flags.owndata and zr.flags.writeable
    assert not zcro.flags.owndata and not zcro.flags.writeable
    assert not zrro.flags.owndata and not zrro.flags.writeable

    zc[1, 2] = 99
    expect = np.array([[11., 12, 13], [21, 22, 99], [31, 32, 33]])
    # We should have just changed zc, of course, but also zcro and the original eigen matrix
    assert np.all(zc == expect)
    assert np.all(zcro == expect)
    assert np.all(get_cm_ref() == expect)

    zr[1, 2] = 99
    assert np.all(zr == expect)
    assert np.all(zrro == expect)
    assert np.all(get_rm_ref() == expect)

    # Make sure the readonly ones are numpy-readonly:
    with pytest.raises(ValueError):
        zcro[1, 2] = 6
    with pytest.raises(ValueError):
        zrro[1, 2] = 6

    # We should be able to explicitly copy like this (and since we're copying,
    # the const should drop away)
    y1 = np.array(get_cm_const_ref())

    assert y1.flags.owndata and y1.flags.writeable
    # We should get copies of the eigen data, which was modified above:
    assert y1[1, 2] == 99
    y1[1, 2] += 12
    assert y1[1, 2] == 111
    assert zc[1, 2] == 99  # Make sure we aren't referencing the original
Exemple #2
0
def test_numpy_ref_mutators():
    """Tests numpy mutating Eigen matrices (for returned Eigen::Ref<...>s)"""
    from pybind11_tests import (
        get_cm_ref, get_cm_const_ref, get_rm_ref, get_rm_const_ref, reset_refs)
    reset_refs()  # In case another test already changed it

    zc = get_cm_ref()
    zcro = get_cm_const_ref()
    zr = get_rm_ref()
    zrro = get_rm_const_ref()

    assert [zc[1, 2], zcro[1, 2], zr[1, 2], zrro[1, 2]] == [23] * 4

    assert not zc.flags.owndata and zc.flags.writeable
    assert not zr.flags.owndata and zr.flags.writeable
    assert not zcro.flags.owndata and not zcro.flags.writeable
    assert not zrro.flags.owndata and not zrro.flags.writeable

    zc[1, 2] = 99
    expect = np.array([[11., 12, 13], [21, 22, 99], [31, 32, 33]])
    # We should have just changed zc, of course, but also zcro and the original eigen matrix
    assert np.all(zc == expect)
    assert np.all(zcro == expect)
    assert np.all(get_cm_ref() == expect)

    zr[1, 2] = 99
    assert np.all(zr == expect)
    assert np.all(zrro == expect)
    assert np.all(get_rm_ref() == expect)

    # Make sure the readonly ones are numpy-readonly:
    with pytest.raises(ValueError):
        zcro[1, 2] = 6
    with pytest.raises(ValueError):
        zrro[1, 2] = 6

    # We should be able to explicitly copy like this (and since we're copying,
    # the const should drop away)
    y1 = np.array(get_cm_const_ref())

    assert y1.flags.owndata and y1.flags.writeable
    # We should get copies of the eigen data, which was modified above:
    assert y1[1, 2] == 99
    y1[1, 2] += 12
    assert y1[1, 2] == 111
    assert zc[1, 2] == 99  # Make sure we aren't referencing the original
Exemple #3
0
def test_cpp_casting():
    from pybind11_tests import (cpp_copy, cpp_ref_c, cpp_ref_r, cpp_ref_any,
                                fixed_r, fixed_c, get_cm_ref, get_rm_ref, ReturnTester)
    assert cpp_copy(fixed_r()) == 22.
    assert cpp_copy(fixed_c()) == 22.
    z = np.array([[5., 6], [7, 8]])
    assert cpp_copy(z) == 7.
    assert cpp_copy(get_cm_ref()) == 21.
    assert cpp_copy(get_rm_ref()) == 21.
    assert cpp_ref_c(get_cm_ref()) == 21.
    assert cpp_ref_r(get_rm_ref()) == 21.
    with pytest.raises(RuntimeError) as excinfo:
        # Can't reference fixed_c: it contains floats, cpp_ref_any wants doubles
        cpp_ref_any(fixed_c())
    assert 'Unable to cast Python instance' in str(excinfo.value)
    with pytest.raises(RuntimeError) as excinfo:
        # Can't reference fixed_r: it contains floats, cpp_ref_any wants doubles
        cpp_ref_any(fixed_r())
    assert 'Unable to cast Python instance' in str(excinfo.value)
    assert cpp_ref_any(ReturnTester.create()) == 1.

    assert cpp_ref_any(get_cm_ref()) == 21.
    assert cpp_ref_any(get_cm_ref()) == 21.
Exemple #4
0
def test_cpp_casting():
    from pybind11_tests import (cpp_copy, cpp_ref_c, cpp_ref_r, cpp_ref_any,
                                fixed_r, fixed_c, get_cm_ref, get_rm_ref, ReturnTester)
    assert cpp_copy(fixed_r()) == 22.
    assert cpp_copy(fixed_c()) == 22.
    z = np.array([[5., 6], [7, 8]])
    assert cpp_copy(z) == 7.
    assert cpp_copy(get_cm_ref()) == 21.
    assert cpp_copy(get_rm_ref()) == 21.
    assert cpp_ref_c(get_cm_ref()) == 21.
    assert cpp_ref_r(get_rm_ref()) == 21.
    with pytest.raises(RuntimeError) as excinfo:
        # Can't reference fixed_c: it contains floats, cpp_ref_any wants doubles
        cpp_ref_any(fixed_c())
    assert 'Unable to cast Python instance' in str(excinfo.value)
    with pytest.raises(RuntimeError) as excinfo:
        # Can't reference fixed_r: it contains floats, cpp_ref_any wants doubles
        cpp_ref_any(fixed_r())
    assert 'Unable to cast Python instance' in str(excinfo.value)
    assert cpp_ref_any(ReturnTester.create()) == 1.

    assert cpp_ref_any(get_cm_ref()) == 21.
    assert cpp_ref_any(get_cm_ref()) == 21.