diff options
author | Jeff VanOss <vanossj@users.noreply.github.com> | 2018-03-25 01:29:20 -0400 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2018-03-24 22:29:20 -0700 |
commit | e4d678a2f5859d29a853d617e9e5bbd4b6241898 (patch) | |
tree | 8173f444ce205578313f8ffb4c82faa108544612 /numpy/core/tests | |
parent | b296ad34652a4ee15e7c372e00bed5bcae34a4ff (diff) | |
download | python-numpy-e4d678a2f5859d29a853d617e9e5bbd4b6241898.tar.gz python-numpy-e4d678a2f5859d29a853d617e9e5bbd4b6241898.tar.bz2 python-numpy-e4d678a2f5859d29a853d617e9e5bbd4b6241898.zip |
BUG: Provide correct format in Py_buffer for scalars (#10564)
Unifies scalar and ndarray pep3118 format string generation
Diffstat (limited to 'numpy/core/tests')
-rw-r--r-- | numpy/core/tests/test_scalarbuffer.py | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/numpy/core/tests/test_scalarbuffer.py b/numpy/core/tests/test_scalarbuffer.py new file mode 100644 index 000000000..cd887f2fb --- /dev/null +++ b/numpy/core/tests/test_scalarbuffer.py @@ -0,0 +1,84 @@ +""" +Test scalar buffer interface adheres to PEP 3118 +""" +import sys +import numpy as np +from numpy.testing import run_module_suite, assert_, assert_equal, dec + +# PEP3118 format strings for native (standard alignment and byteorder) types +scalars_and_codes = [ + (np.bool_, '?'), + (np.byte, 'b'), + (np.short, 'h'), + (np.intc, 'i'), + (np.int_, 'l'), + (np.longlong, 'q'), + (np.ubyte, 'B'), + (np.ushort, 'H'), + (np.uintc, 'I'), + (np.uint, 'L'), + (np.ulonglong, 'Q'), + (np.half, 'e'), + (np.single, 'f'), + (np.double, 'd'), + (np.longdouble, 'g'), + (np.csingle, 'Zf'), + (np.cdouble, 'Zd'), + (np.clongdouble, 'Zg'), +] + + +class TestScalarPEP3118(object): + skip_if_no_buffer_interface = dec.skipif(sys.version_info.major < 3, + "scalars do not implement buffer interface in Python 2") + + @skip_if_no_buffer_interface + def test_scalar_match_array(self): + for scalar, _ in scalars_and_codes: + x = scalar() + a = np.array([], dtype=np.dtype(scalar)) + mv_x = memoryview(x) + mv_a = memoryview(a) + assert_equal(mv_x.format, mv_a.format) + + @skip_if_no_buffer_interface + def test_scalar_dim(self): + for scalar, _ in scalars_and_codes: + x = scalar() + mv_x = memoryview(x) + assert_equal(mv_x.itemsize, np.dtype(scalar).itemsize) + assert_equal(mv_x.ndim, 0) + assert_equal(mv_x.shape, ()) + assert_equal(mv_x.strides, ()) + assert_equal(mv_x.suboffsets, ()) + + @skip_if_no_buffer_interface + def test_scalar_known_code(self): + for scalar, code in scalars_and_codes: + x = scalar() + mv_x = memoryview(x) + assert_equal(mv_x.format, code) + + @skip_if_no_buffer_interface + def test_void_scalar_structured_data(self): + dt = np.dtype([('name', np.unicode_, 16), ('grades', np.float64, (2,))]) + x = np.array(('ndarray_scalar', (1.2, 3.0)), dtype=dt)[()] + assert_(isinstance(x, np.void)) + mv_x = memoryview(x) + expected_size = 16 * np.dtype((np.unicode_, 1)).itemsize + expected_size += 2 * np.dtype((np.float64, 1)).itemsize + assert_equal(mv_x.itemsize, expected_size) + assert_equal(mv_x.ndim, 0) + assert_equal(mv_x.shape, ()) + assert_equal(mv_x.strides, ()) + assert_equal(mv_x.suboffsets, ()) + + # check scalar format string against ndarray format string + a = np.array([('Sarah', (8.0, 7.0)), ('John', (6.0, 7.0))], dtype=dt) + assert_(isinstance(a, np.ndarray)) + mv_a = memoryview(a) + assert_equal(mv_x.itemsize, mv_a.itemsize) + assert_equal(mv_x.format, mv_a.format) + +if __name__ == "__main__": + run_module_suite() |