aboutsummaryrefslogtreecommitdiffstats
path: root/3rdparty/pybind11/tests/test_buffers.py
diff options
context:
space:
mode:
authorMiodrag Milanović <mmicko@gmail.com>2021-01-02 11:16:49 +0100
committerGitHub <noreply@github.com>2021-01-02 11:16:49 +0100
commit9b9628047c01a970cfe20f83f2b7129ed109440d (patch)
tree1db418e9a889dc6fbe6199c5259aac9bd8cbb32f /3rdparty/pybind11/tests/test_buffers.py
parentc6cdf30501dcb2da01361229dd66a05dad73a132 (diff)
parent61b07bc9a664d6a88b85aae99f9756d7569688a9 (diff)
downloadnextpnr-9b9628047c01a970cfe20f83f2b7129ed109440d.tar.gz
nextpnr-9b9628047c01a970cfe20f83f2b7129ed109440d.tar.bz2
nextpnr-9b9628047c01a970cfe20f83f2b7129ed109440d.zip
Merge pull request #549 from YosysHQ/update
Update pybind11 version and fix for future python versions
Diffstat (limited to '3rdparty/pybind11/tests/test_buffers.py')
-rw-r--r--3rdparty/pybind11/tests/test_buffers.py90
1 files changed, 67 insertions, 23 deletions
diff --git a/3rdparty/pybind11/tests/test_buffers.py b/3rdparty/pybind11/tests/test_buffers.py
index bf7aaed7..f0f37084 100644
--- a/3rdparty/pybind11/tests/test_buffers.py
+++ b/3rdparty/pybind11/tests/test_buffers.py
@@ -1,18 +1,16 @@
+# -*- coding: utf-8 -*-
import io
import struct
-import sys
+import ctypes
import pytest
+import env # noqa: F401
+
from pybind11_tests import buffers as m
from pybind11_tests import ConstructorStats
-PY3 = sys.version_info[0] >= 3
-
-pytestmark = pytest.requires_numpy
-
-with pytest.suppress(ImportError):
- import numpy as np
+np = pytest.importorskip("numpy")
def test_from_python():
@@ -38,9 +36,7 @@ def test_from_python():
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
+# https://foss.heptapod.net/pypy/pypy/-/issues/2444
def test_to_python():
mat = m.Matrix(5, 4)
assert memoryview(mat).shape == (5, 4)
@@ -50,8 +46,8 @@ def test_to_python():
mat[3, 2] = 7.0
assert mat[2, 3] == 4
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, )
+ assert struct.unpack_from("f", mat, (3 * 4 + 2) * 4) == (7,)
+ assert struct.unpack_from("f", mat, (2 * 4 + 3) * 4) == (4,)
mat2 = np.array(mat, copy=False)
assert mat2.shape == (5, 4)
@@ -75,7 +71,6 @@ def test_to_python():
assert cstats.move_assignments == 0
-@pytest.unsupported_on_pypy
def test_inherited_protocol():
"""SquareMatrix is derived from Matrix and inherits the buffer protocol"""
@@ -84,35 +79,84 @@ def test_inherited_protocol():
assert np.asarray(matrix).shape == (5, 5)
-@pytest.unsupported_on_pypy
def test_pointer_to_member_fn():
for cls in [m.Buffer, m.ConstBuffer, m.DerivedBuffer]:
buf = cls()
buf.value = 0x12345678
- value = struct.unpack('i', bytearray(buf))[0]
+ value = struct.unpack("i", bytearray(buf))[0]
assert value == 0x12345678
-@pytest.unsupported_on_pypy
def test_readonly_buffer():
buf = m.BufferReadOnly(0x64)
view = memoryview(buf)
- assert view[0] == 0x64 if PY3 else b'd'
+ assert view[0] == b"d" if env.PY2 else 0x64
assert view.readonly
-@pytest.unsupported_on_pypy
def test_selective_readonly_buffer():
buf = m.BufferReadOnlySelect()
- memoryview(buf)[0] = 0x64 if PY3 else b'd'
+ memoryview(buf)[0] = b"d" if env.PY2 else 0x64
assert buf.value == 0x64
- io.BytesIO(b'A').readinto(buf)
- assert buf.value == ord(b'A')
+ io.BytesIO(b"A").readinto(buf)
+ assert buf.value == ord(b"A")
buf.readonly = True
with pytest.raises(TypeError):
- memoryview(buf)[0] = 0 if PY3 else b'\0'
+ memoryview(buf)[0] = b"\0" if env.PY2 else 0
with pytest.raises(TypeError):
- io.BytesIO(b'1').readinto(buf)
+ io.BytesIO(b"1").readinto(buf)
+
+
+def test_ctypes_array_1d():
+ char1d = (ctypes.c_char * 10)()
+ int1d = (ctypes.c_int * 15)()
+ long1d = (ctypes.c_long * 7)()
+
+ for carray in (char1d, int1d, long1d):
+ info = m.get_buffer_info(carray)
+ assert info.itemsize == ctypes.sizeof(carray._type_)
+ assert info.size == len(carray)
+ assert info.ndim == 1
+ assert info.shape == [info.size]
+ assert info.strides == [info.itemsize]
+ assert not info.readonly
+
+
+def test_ctypes_array_2d():
+ char2d = ((ctypes.c_char * 10) * 4)()
+ int2d = ((ctypes.c_int * 15) * 3)()
+ long2d = ((ctypes.c_long * 7) * 2)()
+
+ for carray in (char2d, int2d, long2d):
+ info = m.get_buffer_info(carray)
+ assert info.itemsize == ctypes.sizeof(carray[0]._type_)
+ assert info.size == len(carray) * len(carray[0])
+ assert info.ndim == 2
+ assert info.shape == [len(carray), len(carray[0])]
+ assert info.strides == [info.itemsize * len(carray[0]), info.itemsize]
+ assert not info.readonly
+
+
+@pytest.mark.skipif(
+ "env.PYPY and env.PY2", reason="PyPy2 bytes buffer not reported as readonly"
+)
+def test_ctypes_from_buffer():
+ test_pystr = b"0123456789"
+ for pyarray in (test_pystr, bytearray(test_pystr)):
+ pyinfo = m.get_buffer_info(pyarray)
+
+ if pyinfo.readonly:
+ cbytes = (ctypes.c_char * len(pyarray)).from_buffer_copy(pyarray)
+ cinfo = m.get_buffer_info(cbytes)
+ else:
+ cbytes = (ctypes.c_char * len(pyarray)).from_buffer(pyarray)
+ cinfo = m.get_buffer_info(cbytes)
+
+ assert cinfo.size == pyinfo.size
+ assert cinfo.ndim == pyinfo.ndim
+ assert cinfo.shape == pyinfo.shape
+ assert cinfo.strides == pyinfo.strides
+ assert not cinfo.readonly