diff options
Diffstat (limited to 'ext/pybind11/tests/test_numpy_array.py')
-rw-r--r-- | ext/pybind11/tests/test_numpy_array.py | 55 |
1 files changed, 50 insertions, 5 deletions
diff --git a/ext/pybind11/tests/test_numpy_array.py b/ext/pybind11/tests/test_numpy_array.py index 27433934f..d0a6324df 100644 --- a/ext/pybind11/tests/test_numpy_array.py +++ b/ext/pybind11/tests/test_numpy_array.py @@ -7,6 +7,21 @@ with pytest.suppress(ImportError): import numpy as np +def test_dtypes(): + # See issue #1328. + # - Platform-dependent sizes. + for size_check in m.get_platform_dtype_size_checks(): + print(size_check) + assert size_check.size_cpp == size_check.size_numpy, size_check + # - Concrete sizes. + for check in m.get_concrete_dtype_checks(): + print(check) + assert check.numpy == check.pybind11, check + if check.numpy.num != check.pybind11.num: + print("NOTE: typenum mismatch for {}: {} != {}".format( + check, check.numpy.num, check.pybind11.num)) + + @pytest.fixture(scope='function') def arr(): return np.array([[1, 2, 3], [4, 5, 6]], '=u2') @@ -135,8 +150,18 @@ def test_make_c_f_array(): assert not m.make_f_array().flags.c_contiguous +def test_make_empty_shaped_array(): + m.make_empty_shaped_array() + + # empty shape means numpy scalar, PEP 3118 + assert m.scalar_int().ndim == 0 + assert m.scalar_int().shape == () + assert m.scalar_int() == 42 + + def test_wrap(): def assert_references(a, b, base=None): + from distutils.version import LooseVersion if base is None: base = a assert a is not b @@ -147,7 +172,10 @@ def test_wrap(): assert a.flags.f_contiguous == b.flags.f_contiguous assert a.flags.writeable == b.flags.writeable assert a.flags.aligned == b.flags.aligned - assert a.flags.updateifcopy == b.flags.updateifcopy + if LooseVersion(np.__version__) >= LooseVersion("1.14.0"): + assert a.flags.writebackifcopy == b.flags.writebackifcopy + else: + assert a.flags.updateifcopy == b.flags.updateifcopy assert np.all(a == b) assert not b.flags.owndata assert b.base is base @@ -282,17 +310,17 @@ def test_overload_resolution(msg): 1. (arg0: numpy.ndarray[int32]) -> str 2. (arg0: numpy.ndarray[float64]) -> str - Invoked with:""" + Invoked with: """ with pytest.raises(TypeError) as excinfo: m.overloaded3(np.array([1], dtype='uintc')) - assert msg(excinfo.value) == expected_exc + " array([1], dtype=uint32)" + assert msg(excinfo.value) == expected_exc + repr(np.array([1], dtype='uint32')) with pytest.raises(TypeError) as excinfo: m.overloaded3(np.array([1], dtype='float32')) - assert msg(excinfo.value) == expected_exc + " array([ 1.], dtype=float32)" + assert msg(excinfo.value) == expected_exc + repr(np.array([1.], dtype='float32')) with pytest.raises(TypeError) as excinfo: m.overloaded3(np.array([1], dtype='complex')) - assert msg(excinfo.value) == expected_exc + " array([ 1.+0.j])" + assert msg(excinfo.value) == expected_exc + repr(np.array([1. + 0.j])) # Exact matches: assert m.overloaded4(np.array([1], dtype='double')) == 'double' @@ -400,3 +428,20 @@ def test_array_create_and_resize(msg): a = m.create_and_resize(2) assert(a.size == 4) assert(np.all(a == 42.)) + + +@pytest.unsupported_on_py2 +def test_index_using_ellipsis(): + a = m.index_using_ellipsis(np.zeros((5, 6, 7))) + assert a.shape == (6,) + + +@pytest.unsupported_on_pypy +def test_dtype_refcount_leak(): + from sys import getrefcount + dtype = np.dtype(np.float_) + a = np.array([1], dtype=dtype) + before = getrefcount(dtype) + m.ndim(a) + after = getrefcount(dtype) + assert after == before |