test_scalarbuffer.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. """
  2. Test scalar buffer interface adheres to PEP 3118
  3. """
  4. import numpy as np
  5. from numpy.core._rational_tests import rational
  6. from numpy.core._multiarray_tests import get_buffer_info
  7. import pytest
  8. from numpy.testing import assert_, assert_equal, assert_raises
  9. # PEP3118 format strings for native (standard alignment and byteorder) types
  10. scalars_and_codes = [
  11. (np.bool_, '?'),
  12. (np.byte, 'b'),
  13. (np.short, 'h'),
  14. (np.intc, 'i'),
  15. (np.int_, 'l'),
  16. (np.longlong, 'q'),
  17. (np.ubyte, 'B'),
  18. (np.ushort, 'H'),
  19. (np.uintc, 'I'),
  20. (np.uint, 'L'),
  21. (np.ulonglong, 'Q'),
  22. (np.half, 'e'),
  23. (np.single, 'f'),
  24. (np.double, 'd'),
  25. (np.longdouble, 'g'),
  26. (np.csingle, 'Zf'),
  27. (np.cdouble, 'Zd'),
  28. (np.clongdouble, 'Zg'),
  29. ]
  30. scalars_only, codes_only = zip(*scalars_and_codes)
  31. class TestScalarPEP3118:
  32. @pytest.mark.parametrize('scalar', scalars_only, ids=codes_only)
  33. def test_scalar_match_array(self, scalar):
  34. x = scalar()
  35. a = np.array([], dtype=np.dtype(scalar))
  36. mv_x = memoryview(x)
  37. mv_a = memoryview(a)
  38. assert_equal(mv_x.format, mv_a.format)
  39. @pytest.mark.parametrize('scalar', scalars_only, ids=codes_only)
  40. def test_scalar_dim(self, scalar):
  41. x = scalar()
  42. mv_x = memoryview(x)
  43. assert_equal(mv_x.itemsize, np.dtype(scalar).itemsize)
  44. assert_equal(mv_x.ndim, 0)
  45. assert_equal(mv_x.shape, ())
  46. assert_equal(mv_x.strides, ())
  47. assert_equal(mv_x.suboffsets, ())
  48. @pytest.mark.parametrize('scalar, code', scalars_and_codes, ids=codes_only)
  49. def test_scalar_code_and_properties(self, scalar, code):
  50. x = scalar()
  51. expected = dict(strides=(), itemsize=x.dtype.itemsize, ndim=0,
  52. shape=(), format=code, readonly=True)
  53. mv_x = memoryview(x)
  54. assert self._as_dict(mv_x) == expected
  55. @pytest.mark.parametrize('scalar', scalars_only, ids=codes_only)
  56. def test_scalar_buffers_readonly(self, scalar):
  57. x = scalar()
  58. with pytest.raises(BufferError, match="scalar buffer is readonly"):
  59. get_buffer_info(x, ["WRITABLE"])
  60. def test_void_scalar_structured_data(self):
  61. dt = np.dtype([('name', np.unicode_, 16), ('grades', np.float64, (2,))])
  62. x = np.array(('ndarray_scalar', (1.2, 3.0)), dtype=dt)[()]
  63. assert_(isinstance(x, np.void))
  64. mv_x = memoryview(x)
  65. expected_size = 16 * np.dtype((np.unicode_, 1)).itemsize
  66. expected_size += 2 * np.dtype(np.float64).itemsize
  67. assert_equal(mv_x.itemsize, expected_size)
  68. assert_equal(mv_x.ndim, 0)
  69. assert_equal(mv_x.shape, ())
  70. assert_equal(mv_x.strides, ())
  71. assert_equal(mv_x.suboffsets, ())
  72. # check scalar format string against ndarray format string
  73. a = np.array([('Sarah', (8.0, 7.0)), ('John', (6.0, 7.0))], dtype=dt)
  74. assert_(isinstance(a, np.ndarray))
  75. mv_a = memoryview(a)
  76. assert_equal(mv_x.itemsize, mv_a.itemsize)
  77. assert_equal(mv_x.format, mv_a.format)
  78. # Check that we do not allow writeable buffer export (technically
  79. # we could allow it sometimes here...)
  80. with pytest.raises(BufferError, match="scalar buffer is readonly"):
  81. get_buffer_info(x, ["WRITABLE"])
  82. def _as_dict(self, m):
  83. return dict(strides=m.strides, shape=m.shape, itemsize=m.itemsize,
  84. ndim=m.ndim, format=m.format, readonly=m.readonly)
  85. def test_datetime_memoryview(self):
  86. # gh-11656
  87. # Values verified with v1.13.3, shape is not () as in test_scalar_dim
  88. dt1 = np.datetime64('2016-01-01')
  89. dt2 = np.datetime64('2017-01-01')
  90. expected = dict(strides=(1,), itemsize=1, ndim=1, shape=(8,),
  91. format='B', readonly=True)
  92. v = memoryview(dt1)
  93. assert self._as_dict(v) == expected
  94. v = memoryview(dt2 - dt1)
  95. assert self._as_dict(v) == expected
  96. dt = np.dtype([('a', 'uint16'), ('b', 'M8[s]')])
  97. a = np.empty(1, dt)
  98. # Fails to create a PEP 3118 valid buffer
  99. assert_raises((ValueError, BufferError), memoryview, a[0])
  100. # Check that we do not allow writeable buffer export
  101. with pytest.raises(BufferError, match="scalar buffer is readonly"):
  102. get_buffer_info(dt1, ["WRITABLE"])
  103. @pytest.mark.parametrize('s', [
  104. pytest.param("\x32\x32", id="ascii"),
  105. pytest.param("\uFE0F\uFE0F", id="basic multilingual"),
  106. pytest.param("\U0001f4bb\U0001f4bb", id="non-BMP"),
  107. ])
  108. def test_str_ucs4(self, s):
  109. s = np.str_(s) # only our subclass implements the buffer protocol
  110. # all the same, characters always encode as ucs4
  111. expected = dict(strides=(), itemsize=8, ndim=0, shape=(), format='2w',
  112. readonly=True)
  113. v = memoryview(s)
  114. assert self._as_dict(v) == expected
  115. # integers of the paltform-appropriate endianness
  116. code_points = np.frombuffer(v, dtype='i4')
  117. assert_equal(code_points, [ord(c) for c in s])
  118. # Check that we do not allow writeable buffer export
  119. with pytest.raises(BufferError, match="scalar buffer is readonly"):
  120. get_buffer_info(s, ["WRITABLE"])
  121. def test_user_scalar_fails_buffer(self):
  122. r = rational(1)
  123. with assert_raises(TypeError):
  124. memoryview(r)
  125. # Check that we do not allow writeable buffer export
  126. with pytest.raises(BufferError, match="scalar buffer is readonly"):
  127. get_buffer_info(r, ["WRITABLE"])