import re import pytest with pytest.suppress(ImportError): import numpy as np @pytest.fixture(scope='module') def simple_dtype(): return np.dtype({'names': ['x', 'y', 'z'], 'formats': ['?', 'u4', 'f4'], 'offsets': [0, 4, 8]}) @pytest.fixture(scope='module') def packed_dtype(): return np.dtype([('x', '?'), ('y', 'u4'), ('z', 'f4')]) def assert_equal(actual, expected_data, expected_dtype): np.testing.assert_equal(actual, np.array(expected_data, dtype=expected_dtype)) @pytest.requires_numpy def test_format_descriptors(): from pybind11_tests import get_format_unbound, print_format_descriptors with pytest.raises(RuntimeError) as excinfo: get_format_unbound() assert re.match('^NumPy type info missing for .*UnboundStruct.*$', str(excinfo.value)) assert print_format_descriptors() == [ "T{?:x:3xI:y:f:z:}", "T{?:x:=I:y:=f:z:}", "T{T{?:x:3xI:y:f:z:}:a:T{?:x:=I:y:=f:z:}:b:}", "T{?:x:3xI:y:f:z:12x}", "T{8xT{?:x:3xI:y:f:z:12x}:a:8x}", "T{3s:a:3s:b:}", 'T{q:e1:B:e2:}' ] @pytest.requires_numpy def test_dtype(simple_dtype): from pybind11_tests import (print_dtypes, test_dtype_ctors, test_dtype_methods, trailing_padding_dtype, buffer_to_dtype) assert print_dtypes() == [ "{'names':['x','y','z'], 'formats':['?',' simple_dtype.itemsize assert_equal(arr, elements, simple_dtype) assert_equal(arr, elements, packed_dtype) arr = create_rec_partial_nested(3) assert str(arr.dtype) == \ "{'names':['a'], 'formats':[{'names':['x','y','z'], 'formats':['?',' partial_dtype.itemsize np.testing.assert_equal(arr['a'], create_rec_partial(3)) @pytest.requires_numpy def test_array_constructors(): from pybind11_tests import test_array_ctors data = np.arange(1, 7, dtype='int32') for i in range(8): np.testing.assert_array_equal(test_array_ctors(10 + i), data.reshape((3, 2))) np.testing.assert_array_equal(test_array_ctors(20 + i), data.reshape((3, 2))) for i in range(5): np.testing.assert_array_equal(test_array_ctors(30 + i), data) np.testing.assert_array_equal(test_array_ctors(40 + i), data) @pytest.requires_numpy def test_string_array(): from pybind11_tests import create_string_array, print_string_array arr = create_string_array(True) assert str(arr.dtype) == "[('a', 'S3'), ('b', 'S3')]" assert print_string_array(arr) == [ "a='',b=''", "a='a',b='a'", "a='ab',b='ab'", "a='abc',b='abc'" ] dtype = arr.dtype assert arr['a'].tolist() == [b'', b'a', b'ab', b'abc'] assert arr['b'].tolist() == [b'', b'a', b'ab', b'abc'] arr = create_string_array(False) assert dtype == arr.dtype @pytest.requires_numpy def test_enum_array(): from pybind11_tests import create_enum_array, print_enum_array arr = create_enum_array(3) dtype = arr.dtype assert dtype == np.dtype([('e1', ' numpy.ndarray[NestedStruct]" @pytest.requires_numpy def test_scalar_conversion(): from pybind11_tests import (create_rec_simple, f_simple, create_rec_packed, f_packed, create_rec_nested, f_nested, create_enum_array) n = 3 arrays = [create_rec_simple(n), create_rec_packed(n), create_rec_nested(n), create_enum_array(n)] funcs = [f_simple, f_packed, f_nested] for i, func in enumerate(funcs): for j, arr in enumerate(arrays): if i == j and i < 2: assert [func(arr[k]) for k in range(n)] == [k * 10 for k in range(n)] else: with pytest.raises(TypeError) as excinfo: func(arr[0]) assert 'incompatible function arguments' in str(excinfo.value) @pytest.requires_numpy def test_register_dtype(): from pybind11_tests import register_dtype with pytest.raises(RuntimeError) as excinfo: register_dtype() assert 'dtype is already registered' in str(excinfo.value)