File indexing completed on 2025-01-18 10:17:55
0001 import pytest
0002
0003 import env
0004 from pybind11_tests import numpy_array as m
0005
0006 np = pytest.importorskip("numpy")
0007
0008
0009 def test_dtypes():
0010
0011
0012 for size_check in m.get_platform_dtype_size_checks():
0013 print(size_check)
0014 assert size_check.size_cpp == size_check.size_numpy, size_check
0015
0016 for check in m.get_concrete_dtype_checks():
0017 print(check)
0018 assert check.numpy == check.pybind11, check
0019 if check.numpy.num != check.pybind11.num:
0020 print(
0021 f"NOTE: typenum mismatch for {check}: {check.numpy.num} != {check.pybind11.num}"
0022 )
0023
0024
0025 @pytest.fixture(scope="function")
0026 def arr():
0027 return np.array([[1, 2, 3], [4, 5, 6]], "=u2")
0028
0029
0030 def test_array_attributes():
0031 a = np.array(0, "f8")
0032 assert m.ndim(a) == 0
0033 assert all(m.shape(a) == [])
0034 assert all(m.strides(a) == [])
0035 with pytest.raises(IndexError) as excinfo:
0036 m.shape(a, 0)
0037 assert str(excinfo.value) == "invalid axis: 0 (ndim = 0)"
0038 with pytest.raises(IndexError) as excinfo:
0039 m.strides(a, 0)
0040 assert str(excinfo.value) == "invalid axis: 0 (ndim = 0)"
0041 assert m.writeable(a)
0042 assert m.size(a) == 1
0043 assert m.itemsize(a) == 8
0044 assert m.nbytes(a) == 8
0045 assert m.owndata(a)
0046
0047 a = np.array([[1, 2, 3], [4, 5, 6]], "u2").view()
0048 a.flags.writeable = False
0049 assert m.ndim(a) == 2
0050 assert all(m.shape(a) == [2, 3])
0051 assert m.shape(a, 0) == 2
0052 assert m.shape(a, 1) == 3
0053 assert all(m.strides(a) == [6, 2])
0054 assert m.strides(a, 0) == 6
0055 assert m.strides(a, 1) == 2
0056 with pytest.raises(IndexError) as excinfo:
0057 m.shape(a, 2)
0058 assert str(excinfo.value) == "invalid axis: 2 (ndim = 2)"
0059 with pytest.raises(IndexError) as excinfo:
0060 m.strides(a, 2)
0061 assert str(excinfo.value) == "invalid axis: 2 (ndim = 2)"
0062 assert not m.writeable(a)
0063 assert m.size(a) == 6
0064 assert m.itemsize(a) == 2
0065 assert m.nbytes(a) == 12
0066 assert not m.owndata(a)
0067
0068
0069 @pytest.mark.parametrize(
0070 "args, ret", [([], 0), ([0], 0), ([1], 3), ([0, 1], 1), ([1, 2], 5)]
0071 )
0072 def test_index_offset(arr, args, ret):
0073 assert m.index_at(arr, *args) == ret
0074 assert m.index_at_t(arr, *args) == ret
0075 assert m.offset_at(arr, *args) == ret * arr.dtype.itemsize
0076 assert m.offset_at_t(arr, *args) == ret * arr.dtype.itemsize
0077
0078
0079 def test_dim_check_fail(arr):
0080 for func in (
0081 m.index_at,
0082 m.index_at_t,
0083 m.offset_at,
0084 m.offset_at_t,
0085 m.data,
0086 m.data_t,
0087 m.mutate_data,
0088 m.mutate_data_t,
0089 ):
0090 with pytest.raises(IndexError) as excinfo:
0091 func(arr, 1, 2, 3)
0092 assert str(excinfo.value) == "too many indices for an array: 3 (ndim = 2)"
0093
0094
0095 @pytest.mark.parametrize(
0096 "args, ret",
0097 [
0098 ([], [1, 2, 3, 4, 5, 6]),
0099 ([1], [4, 5, 6]),
0100 ([0, 1], [2, 3, 4, 5, 6]),
0101 ([1, 2], [6]),
0102 ],
0103 )
0104 def test_data(arr, args, ret):
0105 from sys import byteorder
0106
0107 assert all(m.data_t(arr, *args) == ret)
0108 assert all(m.data(arr, *args)[(0 if byteorder == "little" else 1) :: 2] == ret)
0109 assert all(m.data(arr, *args)[(1 if byteorder == "little" else 0) :: 2] == 0)
0110
0111
0112 @pytest.mark.parametrize("dim", [0, 1, 3])
0113 def test_at_fail(arr, dim):
0114 for func in m.at_t, m.mutate_at_t:
0115 with pytest.raises(IndexError) as excinfo:
0116 func(arr, *([0] * dim))
0117 assert str(excinfo.value) == f"index dimension mismatch: {dim} (ndim = 2)"
0118
0119
0120 def test_at(arr):
0121 assert m.at_t(arr, 0, 2) == 3
0122 assert m.at_t(arr, 1, 0) == 4
0123
0124 assert all(m.mutate_at_t(arr, 0, 2).ravel() == [1, 2, 4, 4, 5, 6])
0125 assert all(m.mutate_at_t(arr, 1, 0).ravel() == [1, 2, 4, 5, 5, 6])
0126
0127
0128 def test_mutate_readonly(arr):
0129 arr.flags.writeable = False
0130 for func, args in (
0131 (m.mutate_data, ()),
0132 (m.mutate_data_t, ()),
0133 (m.mutate_at_t, (0, 0)),
0134 ):
0135 with pytest.raises(ValueError) as excinfo:
0136 func(arr, *args)
0137 assert str(excinfo.value) == "array is not writeable"
0138
0139
0140 def test_mutate_data(arr):
0141 assert all(m.mutate_data(arr).ravel() == [2, 4, 6, 8, 10, 12])
0142 assert all(m.mutate_data(arr).ravel() == [4, 8, 12, 16, 20, 24])
0143 assert all(m.mutate_data(arr, 1).ravel() == [4, 8, 12, 32, 40, 48])
0144 assert all(m.mutate_data(arr, 0, 1).ravel() == [4, 16, 24, 64, 80, 96])
0145 assert all(m.mutate_data(arr, 1, 2).ravel() == [4, 16, 24, 64, 80, 192])
0146
0147 assert all(m.mutate_data_t(arr).ravel() == [5, 17, 25, 65, 81, 193])
0148 assert all(m.mutate_data_t(arr).ravel() == [6, 18, 26, 66, 82, 194])
0149 assert all(m.mutate_data_t(arr, 1).ravel() == [6, 18, 26, 67, 83, 195])
0150 assert all(m.mutate_data_t(arr, 0, 1).ravel() == [6, 19, 27, 68, 84, 196])
0151 assert all(m.mutate_data_t(arr, 1, 2).ravel() == [6, 19, 27, 68, 84, 197])
0152
0153
0154 def test_bounds_check(arr):
0155 for func in (
0156 m.index_at,
0157 m.index_at_t,
0158 m.data,
0159 m.data_t,
0160 m.mutate_data,
0161 m.mutate_data_t,
0162 m.at_t,
0163 m.mutate_at_t,
0164 ):
0165 with pytest.raises(IndexError) as excinfo:
0166 func(arr, 2, 0)
0167 assert str(excinfo.value) == "index 2 is out of bounds for axis 0 with size 2"
0168 with pytest.raises(IndexError) as excinfo:
0169 func(arr, 0, 4)
0170 assert str(excinfo.value) == "index 4 is out of bounds for axis 1 with size 3"
0171
0172
0173 def test_make_c_f_array():
0174 assert m.make_c_array().flags.c_contiguous
0175 assert not m.make_c_array().flags.f_contiguous
0176 assert m.make_f_array().flags.f_contiguous
0177 assert not m.make_f_array().flags.c_contiguous
0178
0179
0180 def test_make_empty_shaped_array():
0181 m.make_empty_shaped_array()
0182
0183
0184 assert m.scalar_int().ndim == 0
0185 assert m.scalar_int().shape == ()
0186 assert m.scalar_int() == 42
0187
0188
0189 def test_wrap():
0190 def assert_references(a, b, base=None):
0191 if base is None:
0192 base = a
0193 assert a is not b
0194 assert a.__array_interface__["data"][0] == b.__array_interface__["data"][0]
0195 assert a.shape == b.shape
0196 assert a.strides == b.strides
0197 assert a.flags.c_contiguous == b.flags.c_contiguous
0198 assert a.flags.f_contiguous == b.flags.f_contiguous
0199 assert a.flags.writeable == b.flags.writeable
0200 assert a.flags.aligned == b.flags.aligned
0201
0202 if tuple(int(x) for x in np.__version__.split(".")[:2]) >= (1, 14):
0203 assert a.flags.writebackifcopy == b.flags.writebackifcopy
0204 else:
0205 assert a.flags.updateifcopy == b.flags.updateifcopy
0206 assert np.all(a == b)
0207 assert not b.flags.owndata
0208 assert b.base is base
0209 if a.flags.writeable and a.ndim == 2:
0210 a[0, 0] = 1234
0211 assert b[0, 0] == 1234
0212
0213 a1 = np.array([1, 2], dtype=np.int16)
0214 assert a1.flags.owndata and a1.base is None
0215 a2 = m.wrap(a1)
0216 assert_references(a1, a2)
0217
0218 a1 = np.array([[1, 2], [3, 4]], dtype=np.float32, order="F")
0219 assert a1.flags.owndata and a1.base is None
0220 a2 = m.wrap(a1)
0221 assert_references(a1, a2)
0222
0223 a1 = np.array([[1, 2], [3, 4]], dtype=np.float32, order="C")
0224 a1.flags.writeable = False
0225 a2 = m.wrap(a1)
0226 assert_references(a1, a2)
0227
0228 a1 = np.random.random((4, 4, 4))
0229 a2 = m.wrap(a1)
0230 assert_references(a1, a2)
0231
0232 a1t = a1.transpose()
0233 a2 = m.wrap(a1t)
0234 assert_references(a1t, a2, a1)
0235
0236 a1d = a1.diagonal()
0237 a2 = m.wrap(a1d)
0238 assert_references(a1d, a2, a1)
0239
0240 a1m = a1[::-1, ::-1, ::-1]
0241 a2 = m.wrap(a1m)
0242 assert_references(a1m, a2, a1)
0243
0244
0245 def test_numpy_view(capture):
0246 with capture:
0247 ac = m.ArrayClass()
0248 ac_view_1 = ac.numpy_view()
0249 ac_view_2 = ac.numpy_view()
0250 assert np.all(ac_view_1 == np.array([1, 2], dtype=np.int32))
0251 del ac
0252 pytest.gc_collect()
0253 assert (
0254 capture
0255 == """
0256 ArrayClass()
0257 ArrayClass::numpy_view()
0258 ArrayClass::numpy_view()
0259 """
0260 )
0261 ac_view_1[0] = 4
0262 ac_view_1[1] = 3
0263 assert ac_view_2[0] == 4
0264 assert ac_view_2[1] == 3
0265 with capture:
0266 del ac_view_1
0267 del ac_view_2
0268 pytest.gc_collect()
0269 pytest.gc_collect()
0270 assert (
0271 capture
0272 == """
0273 ~ArrayClass()
0274 """
0275 )
0276
0277
0278 def test_cast_numpy_int64_to_uint64():
0279 m.function_taking_uint64(123)
0280 m.function_taking_uint64(np.uint64(123))
0281
0282
0283 def test_isinstance():
0284 assert m.isinstance_untyped(np.array([1, 2, 3]), "not an array")
0285 assert m.isinstance_typed(np.array([1.0, 2.0, 3.0]))
0286
0287
0288 def test_constructors():
0289 defaults = m.default_constructors()
0290 for a in defaults.values():
0291 assert a.size == 0
0292 assert defaults["array"].dtype == np.array([]).dtype
0293 assert defaults["array_t<int32>"].dtype == np.int32
0294 assert defaults["array_t<double>"].dtype == np.float64
0295
0296 results = m.converting_constructors([1, 2, 3])
0297 for a in results.values():
0298 np.testing.assert_array_equal(a, [1, 2, 3])
0299 assert results["array"].dtype == np.int_
0300 assert results["array_t<int32>"].dtype == np.int32
0301 assert results["array_t<double>"].dtype == np.float64
0302
0303
0304 def test_overload_resolution(msg):
0305
0306 assert m.overloaded(np.array([1], dtype="float64")) == "double"
0307 assert m.overloaded(np.array([1], dtype="float32")) == "float"
0308 assert m.overloaded(np.array([1], dtype="ushort")) == "unsigned short"
0309 assert m.overloaded(np.array([1], dtype="intc")) == "int"
0310 assert m.overloaded(np.array([1], dtype="longlong")) == "long long"
0311 assert m.overloaded(np.array([1], dtype="complex")) == "double complex"
0312 assert m.overloaded(np.array([1], dtype="csingle")) == "float complex"
0313
0314
0315 assert m.overloaded(np.array([1], dtype="uint8")) == "double"
0316
0317 with pytest.raises(TypeError) as excinfo:
0318 m.overloaded("not an array")
0319 assert (
0320 msg(excinfo.value)
0321 == """
0322 overloaded(): incompatible function arguments. The following argument types are supported:
0323 1. (arg0: numpy.ndarray[numpy.float64]) -> str
0324 2. (arg0: numpy.ndarray[numpy.float32]) -> str
0325 3. (arg0: numpy.ndarray[numpy.int32]) -> str
0326 4. (arg0: numpy.ndarray[numpy.uint16]) -> str
0327 5. (arg0: numpy.ndarray[numpy.int64]) -> str
0328 6. (arg0: numpy.ndarray[numpy.complex128]) -> str
0329 7. (arg0: numpy.ndarray[numpy.complex64]) -> str
0330
0331 Invoked with: 'not an array'
0332 """
0333 )
0334
0335 assert m.overloaded2(np.array([1], dtype="float64")) == "double"
0336 assert m.overloaded2(np.array([1], dtype="float32")) == "float"
0337 assert m.overloaded2(np.array([1], dtype="complex64")) == "float complex"
0338 assert m.overloaded2(np.array([1], dtype="complex128")) == "double complex"
0339 assert m.overloaded2(np.array([1], dtype="float32")) == "float"
0340
0341 assert m.overloaded3(np.array([1], dtype="float64")) == "double"
0342 assert m.overloaded3(np.array([1], dtype="intc")) == "int"
0343 expected_exc = """
0344 overloaded3(): incompatible function arguments. The following argument types are supported:
0345 1. (arg0: numpy.ndarray[numpy.int32]) -> str
0346 2. (arg0: numpy.ndarray[numpy.float64]) -> str
0347
0348 Invoked with: """
0349
0350 with pytest.raises(TypeError) as excinfo:
0351 m.overloaded3(np.array([1], dtype="uintc"))
0352 assert msg(excinfo.value) == expected_exc + repr(np.array([1], dtype="uint32"))
0353 with pytest.raises(TypeError) as excinfo:
0354 m.overloaded3(np.array([1], dtype="float32"))
0355 assert msg(excinfo.value) == expected_exc + repr(np.array([1.0], dtype="float32"))
0356 with pytest.raises(TypeError) as excinfo:
0357 m.overloaded3(np.array([1], dtype="complex"))
0358 assert msg(excinfo.value) == expected_exc + repr(np.array([1.0 + 0.0j]))
0359
0360
0361 assert m.overloaded4(np.array([1], dtype="double")) == "double"
0362 assert m.overloaded4(np.array([1], dtype="longlong")) == "long long"
0363
0364
0365
0366 assert m.overloaded4(np.array([1], dtype="float32")) == "double"
0367 assert m.overloaded4(np.array([1], dtype="short")) == "long long"
0368
0369 assert m.overloaded5(np.array([1], dtype="double")) == "double"
0370 assert m.overloaded5(np.array([1], dtype="uintc")) == "unsigned int"
0371 assert m.overloaded5(np.array([1], dtype="float32")) == "unsigned int"
0372
0373
0374 def test_greedy_string_overload():
0375 """Tests fix for #685 - ndarray shouldn't go to std::string overload"""
0376
0377 assert m.issue685("abc") == "string"
0378 assert m.issue685(np.array([97, 98, 99], dtype="b")) == "array"
0379 assert m.issue685(123) == "other"
0380
0381
0382 def test_array_unchecked_fixed_dims(msg):
0383 z1 = np.array([[1, 2], [3, 4]], dtype="float64")
0384 m.proxy_add2(z1, 10)
0385 assert np.all(z1 == [[11, 12], [13, 14]])
0386
0387 with pytest.raises(ValueError) as excinfo:
0388 m.proxy_add2(np.array([1.0, 2, 3]), 5.0)
0389 assert (
0390 msg(excinfo.value) == "array has incorrect number of dimensions: 1; expected 2"
0391 )
0392
0393 expect_c = np.ndarray(shape=(3, 3, 3), buffer=np.array(range(3, 30)), dtype="int")
0394 assert np.all(m.proxy_init3(3.0) == expect_c)
0395 expect_f = np.transpose(expect_c)
0396 assert np.all(m.proxy_init3F(3.0) == expect_f)
0397
0398 assert m.proxy_squared_L2_norm(np.array(range(6))) == 55
0399 assert m.proxy_squared_L2_norm(np.array(range(6), dtype="float64")) == 55
0400
0401 assert m.proxy_auxiliaries2(z1) == [11, 11, True, 2, 8, 2, 2, 4, 32]
0402 assert m.proxy_auxiliaries2(z1) == m.array_auxiliaries2(z1)
0403
0404 assert m.proxy_auxiliaries1_const_ref(z1[0, :])
0405 assert m.proxy_auxiliaries2_const_ref(z1)
0406
0407
0408 def test_array_unchecked_dyn_dims():
0409 z1 = np.array([[1, 2], [3, 4]], dtype="float64")
0410 m.proxy_add2_dyn(z1, 10)
0411 assert np.all(z1 == [[11, 12], [13, 14]])
0412
0413 expect_c = np.ndarray(shape=(3, 3, 3), buffer=np.array(range(3, 30)), dtype="int")
0414 assert np.all(m.proxy_init3_dyn(3.0) == expect_c)
0415
0416 assert m.proxy_auxiliaries2_dyn(z1) == [11, 11, True, 2, 8, 2, 2, 4, 32]
0417 assert m.proxy_auxiliaries2_dyn(z1) == m.array_auxiliaries2(z1)
0418
0419
0420 def test_array_failure():
0421 with pytest.raises(ValueError) as excinfo:
0422 m.array_fail_test()
0423 assert str(excinfo.value) == "cannot create a pybind11::array from a nullptr"
0424
0425 with pytest.raises(ValueError) as excinfo:
0426 m.array_t_fail_test()
0427 assert str(excinfo.value) == "cannot create a pybind11::array_t from a nullptr"
0428
0429 with pytest.raises(ValueError) as excinfo:
0430 m.array_fail_test_negative_size()
0431 assert str(excinfo.value) == "negative dimensions are not allowed"
0432
0433
0434 def test_initializer_list():
0435 assert m.array_initializer_list1().shape == (1,)
0436 assert m.array_initializer_list2().shape == (1, 2)
0437 assert m.array_initializer_list3().shape == (1, 2, 3)
0438 assert m.array_initializer_list4().shape == (1, 2, 3, 4)
0439
0440
0441 def test_array_resize():
0442 a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype="float64")
0443 m.array_reshape2(a)
0444 assert a.size == 9
0445 assert np.all(a == [[1, 2, 3], [4, 5, 6], [7, 8, 9]])
0446
0447
0448 m.array_resize3(a, 4, False)
0449 assert a.size == 64
0450
0451 try:
0452 m.array_resize3(a, 3, True)
0453 except ValueError as e:
0454 assert str(e).startswith("cannot resize an array")
0455
0456 b = a.transpose()
0457 try:
0458 m.array_resize3(b, 3, False)
0459 except ValueError as e:
0460 assert str(e).startswith("cannot resize this array: it does not own its data")
0461
0462 m.array_reshape2(b)
0463 assert b.shape == (8, 8)
0464
0465
0466 @pytest.mark.xfail("env.PYPY")
0467 def test_array_create_and_resize():
0468 a = m.create_and_resize(2)
0469 assert a.size == 4
0470 assert np.all(a == 42.0)
0471
0472
0473 def test_array_view():
0474 a = np.ones(100 * 4).astype("uint8")
0475 a_float_view = m.array_view(a, "float32")
0476 assert a_float_view.shape == (100 * 1,)
0477
0478 a_int16_view = m.array_view(a, "int16")
0479 assert a_int16_view.shape == (100 * 2,)
0480
0481
0482 def test_array_view_invalid():
0483 a = np.ones(100 * 4).astype("uint8")
0484 with pytest.raises(TypeError):
0485 m.array_view(a, "deadly_dtype")
0486
0487
0488 def test_reshape_initializer_list():
0489 a = np.arange(2 * 7 * 3) + 1
0490 x = m.reshape_initializer_list(a, 2, 7, 3)
0491 assert x.shape == (2, 7, 3)
0492 assert list(x[1][4]) == [34, 35, 36]
0493 with pytest.raises(ValueError) as excinfo:
0494 m.reshape_initializer_list(a, 1, 7, 3)
0495 assert str(excinfo.value) == "cannot reshape array of size 42 into shape (1,7,3)"
0496
0497
0498 def test_reshape_tuple():
0499 a = np.arange(3 * 7 * 2) + 1
0500 x = m.reshape_tuple(a, (3, 7, 2))
0501 assert x.shape == (3, 7, 2)
0502 assert list(x[1][4]) == [23, 24]
0503 y = m.reshape_tuple(x, (x.size,))
0504 assert y.shape == (42,)
0505 with pytest.raises(ValueError) as excinfo:
0506 m.reshape_tuple(a, (3, 7, 1))
0507 assert str(excinfo.value) == "cannot reshape array of size 42 into shape (3,7,1)"
0508 with pytest.raises(ValueError) as excinfo:
0509 m.reshape_tuple(a, ())
0510 assert str(excinfo.value) == "cannot reshape array of size 42 into shape ()"
0511
0512
0513 def test_index_using_ellipsis():
0514 a = m.index_using_ellipsis(np.zeros((5, 6, 7)))
0515 assert a.shape == (6,)
0516
0517
0518 @pytest.mark.parametrize(
0519 "test_func",
0520 [
0521 m.test_fmt_desc_float,
0522 m.test_fmt_desc_double,
0523 m.test_fmt_desc_const_float,
0524 m.test_fmt_desc_const_double,
0525 ],
0526 )
0527 def test_format_descriptors_for_floating_point_types(test_func):
0528 assert "numpy.ndarray[numpy.float" in test_func.__doc__
0529
0530
0531 @pytest.mark.parametrize("forcecast", [False, True])
0532 @pytest.mark.parametrize("contiguity", [None, "C", "F"])
0533 @pytest.mark.parametrize("noconvert", [False, True])
0534 @pytest.mark.filterwarnings(
0535 "ignore:Casting complex values to real discards the imaginary part:numpy.ComplexWarning"
0536 )
0537 def test_argument_conversions(forcecast, contiguity, noconvert):
0538 function_name = "accept_double"
0539 if contiguity == "C":
0540 function_name += "_c_style"
0541 elif contiguity == "F":
0542 function_name += "_f_style"
0543 if forcecast:
0544 function_name += "_forcecast"
0545 if noconvert:
0546 function_name += "_noconvert"
0547 function = getattr(m, function_name)
0548
0549 for dtype in [np.dtype("float32"), np.dtype("float64"), np.dtype("complex128")]:
0550 for order in ["C", "F"]:
0551 for shape in [(2, 2), (1, 3, 1, 1), (1, 1, 1), (0,)]:
0552 if not noconvert:
0553
0554
0555
0556 should_raise = dtype.name == "complex128" and not forcecast
0557 else:
0558
0559
0560
0561 trivially_contiguous = sum(1 for d in shape if d > 1) <= 1
0562 should_raise = dtype.name != "float64" or (
0563 contiguity is not None
0564 and contiguity != order
0565 and not trivially_contiguous
0566 )
0567
0568 array = np.zeros(shape, dtype=dtype, order=order)
0569 if not should_raise:
0570 function(array)
0571 else:
0572 with pytest.raises(
0573 TypeError, match="incompatible function arguments"
0574 ):
0575 function(array)
0576
0577
0578 @pytest.mark.xfail("env.PYPY")
0579 def test_dtype_refcount_leak():
0580 from sys import getrefcount
0581
0582 dtype = np.dtype(np.float_)
0583 a = np.array([1], dtype=dtype)
0584 before = getrefcount(dtype)
0585 m.ndim(a)
0586 after = getrefcount(dtype)
0587 assert after == before
0588
0589
0590 def test_round_trip_float():
0591 arr = np.zeros((), np.float64)
0592 arr[()] = 37.2
0593 assert m.round_trip_float(arr) == 37.2