test_buffers.py 3.22 KB
Newer Older
1
# -*- coding: utf-8 -*-
2
import io
3
import struct
4

Dean Moldovan's avatar
Dean Moldovan committed
5
import pytest
6

7
8
from pybind11_tests import buffers as m
from pybind11_tests import ConstructorStats
Dean Moldovan's avatar
Dean Moldovan committed
9

10
11
pytestmark = pytest.requires_numpy

Dean Moldovan's avatar
Dean Moldovan committed
12
13
14
15
with pytest.suppress(ImportError):
    import numpy as np


Wenzel Jakob's avatar
Wenzel Jakob committed
16
17
def test_from_python():
    with pytest.raises(RuntimeError) as excinfo:
18
        m.Matrix(np.array([1, 2, 3]))  # trying to assign a 1D array
Wenzel Jakob's avatar
Wenzel Jakob committed
19
20
21
    assert str(excinfo.value) == "Incompatible buffer format!"

    m3 = np.array([[1, 2, 3], [4, 5, 6]]).astype(np.float32)
22
    m4 = m.Matrix(m3)
Wenzel Jakob's avatar
Wenzel Jakob committed
23
24
25
26
27

    for i in range(m4.rows()):
        for j in range(m4.cols()):
            assert m3[i, j] == m4[i, j]

28
    cstats = ConstructorStats.get(m.Matrix)
Wenzel Jakob's avatar
Wenzel Jakob committed
29
30
31
32
33
34
35
36
37
38
39
40
41
    assert cstats.alive() == 1
    del m3, m4
    assert cstats.alive() == 0
    assert cstats.values() == ["2x3 matrix"]
    assert cstats.copy_constructions == 0
    # assert cstats.move_constructions >= 0  # Don't invoke any
    assert cstats.copy_assignments == 0
    assert cstats.move_assignments == 0


# PyPy: Memory leak in the "np.array(m, copy=False)" call
# https://bitbucket.org/pypy/pypy/issues/2444
@pytest.unsupported_on_pypy
Dean Moldovan's avatar
Dean Moldovan committed
42
def test_to_python():
43
44
    mat = m.Matrix(5, 4)
    assert memoryview(mat).shape == (5, 4)
Dean Moldovan's avatar
Dean Moldovan committed
45

46
    assert mat[2, 3] == 0
47
48
    mat[2, 3] = 4.0
    mat[3, 2] = 7.0
49
    assert mat[2, 3] == 4
50
51
52
    assert mat[3, 2] == 7
    assert struct.unpack_from('f', mat, (3 * 4 + 2) * 4) == (7, )
    assert struct.unpack_from('f', mat, (2 * 4 + 3) * 4) == (4, )
Dean Moldovan's avatar
Dean Moldovan committed
53

54
    mat2 = np.array(mat, copy=False)
55
56
57
    assert mat2.shape == (5, 4)
    assert abs(mat2).sum() == 11
    assert mat2[2, 3] == 4 and mat2[3, 2] == 7
58
59
    mat2[2, 3] = 5
    assert mat2[2, 3] == 5
Dean Moldovan's avatar
Dean Moldovan committed
60

61
    cstats = ConstructorStats.get(m.Matrix)
Dean Moldovan's avatar
Dean Moldovan committed
62
    assert cstats.alive() == 1
63
    del mat
Wenzel Jakob's avatar
Wenzel Jakob committed
64
    pytest.gc_collect()
Dean Moldovan's avatar
Dean Moldovan committed
65
    assert cstats.alive() == 1
66
    del mat2  # holds a mat reference
Wenzel Jakob's avatar
Wenzel Jakob committed
67
    pytest.gc_collect()
Dean Moldovan's avatar
Dean Moldovan committed
68
    assert cstats.alive() == 0
69
    assert cstats.values() == ["5x4 matrix"]
Dean Moldovan's avatar
Dean Moldovan committed
70
71
72
73
    assert cstats.copy_constructions == 0
    # assert cstats.move_constructions >= 0  # Don't invoke any
    assert cstats.copy_assignments == 0
    assert cstats.move_assignments == 0
74
75


76
77
78
79
@pytest.unsupported_on_pypy
def test_inherited_protocol():
    """SquareMatrix is derived from Matrix and inherits the buffer protocol"""

80
    matrix = m.SquareMatrix(5)
81
82
83
84
    assert memoryview(matrix).shape == (5, 5)
    assert np.asarray(matrix).shape == (5, 5)


85
@pytest.unsupported_on_pypy
86
87
def test_pointer_to_member_fn():
    for cls in [m.Buffer, m.ConstBuffer, m.DerivedBuffer]:
88
89
90
91
        buf = cls()
        buf.value = 0x12345678
        value = struct.unpack('i', bytearray(buf))[0]
        assert value == 0x12345678
92
93
94
95
96
97


@pytest.unsupported_on_pypy
def test_readonly_buffer():
    buf = m.BufferReadOnly(0x64)
    view = memoryview(buf)
98
    assert view[0] == b'd' if pytest.PY2 else 0x64
99
100
101
102
103
104
105
    assert view.readonly


@pytest.unsupported_on_pypy
def test_selective_readonly_buffer():
    buf = m.BufferReadOnlySelect()

106
    memoryview(buf)[0] = b'd' if pytest.PY2 else 0x64
107
108
109
110
111
112
113
    assert buf.value == 0x64

    io.BytesIO(b'A').readinto(buf)
    assert buf.value == ord(b'A')

    buf.readonly = True
    with pytest.raises(TypeError):
114
        memoryview(buf)[0] = b'\0' if pytest.PY2 else 0
115
116
    with pytest.raises(TypeError):
        io.BytesIO(b'1').readinto(buf)