PHP 8.2.30
Preview: test_scalarbuffer.py Size: 5.45 KB
/proc/self/root/proc/self/root/opt/cloudlinux/venv/lib64/python3.11/site-packages/numpy/core/tests/test_scalarbuffer.py

"""
Test scalar buffer interface adheres to PEP 3118
"""
import numpy as np
from numpy.core._rational_tests import rational
from numpy.core._multiarray_tests import get_buffer_info
import pytest

from numpy.testing import assert_, assert_equal, assert_raises

# 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'),
]
scalars_only, codes_only = zip(*scalars_and_codes)


class TestScalarPEP3118:

    @pytest.mark.parametrize('scalar', scalars_only, ids=codes_only)
    def test_scalar_match_array(self, scalar):
        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)

    @pytest.mark.parametrize('scalar', scalars_only, ids=codes_only)
    def test_scalar_dim(self, scalar):
        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, ())

    @pytest.mark.parametrize('scalar, code', scalars_and_codes, ids=codes_only)
    def test_scalar_code_and_properties(self, scalar, code):
        x = scalar()
        expected = dict(strides=(), itemsize=x.dtype.itemsize, ndim=0,
                        shape=(), format=code, readonly=True)

        mv_x = memoryview(x)
        assert self._as_dict(mv_x) == expected

    @pytest.mark.parametrize('scalar', scalars_only, ids=codes_only)
    def test_scalar_buffers_readonly(self, scalar):
        x = scalar()
        with pytest.raises(BufferError, match="scalar buffer is readonly"):
            get_buffer_info(x, ["WRITABLE"])

    def test_void_scalar_structured_data(self):
        dt = np.dtype([('name', np.str_, 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.str_, 1)).itemsize
        expected_size += 2 * np.dtype(np.float64).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)

        # Check that we do not allow writeable buffer export (technically
        # we could allow it sometimes here...)
        with pytest.raises(BufferError, match="scalar buffer is readonly"):
            get_buffer_info(x, ["WRITABLE"])

    def _as_dict(self, m):
        return dict(strides=m.strides, shape=m.shape, itemsize=m.itemsize,
                    ndim=m.ndim, format=m.format, readonly=m.readonly)

    def test_datetime_memoryview(self):
        # gh-11656
        # Values verified with v1.13.3, shape is not () as in test_scalar_dim

        dt1 = np.datetime64('2016-01-01')
        dt2 = np.datetime64('2017-01-01')
        expected = dict(strides=(1,), itemsize=1, ndim=1, shape=(8,),
                        format='B', readonly=True)
        v = memoryview(dt1)
        assert self._as_dict(v) == expected

        v = memoryview(dt2 - dt1)
        assert self._as_dict(v) == expected

        dt = np.dtype([('a', 'uint16'), ('b', 'M8[s]')])
        a = np.empty(1, dt)
        # Fails to create a PEP 3118 valid buffer
        assert_raises((ValueError, BufferError), memoryview, a[0])

        # Check that we do not allow writeable buffer export
        with pytest.raises(BufferError, match="scalar buffer is readonly"):
            get_buffer_info(dt1, ["WRITABLE"])

    @pytest.mark.parametrize('s', [
        pytest.param("\x32\x32", id="ascii"),
        pytest.param("\uFE0F\uFE0F", id="basic multilingual"),
        pytest.param("\U0001f4bb\U0001f4bb", id="non-BMP"),
    ])
    def test_str_ucs4(self, s):
        s = np.str_(s)  # only our subclass implements the buffer protocol

        # all the same, characters always encode as ucs4
        expected = dict(strides=(), itemsize=8, ndim=0, shape=(), format='2w',
                        readonly=True)

        v = memoryview(s)
        assert self._as_dict(v) == expected

        # integers of the paltform-appropriate endianness
        code_points = np.frombuffer(v, dtype='i4')

        assert_equal(code_points, [ord(c) for c in s])

        # Check that we do not allow writeable buffer export
        with pytest.raises(BufferError, match="scalar buffer is readonly"):
            get_buffer_info(s, ["WRITABLE"])

    def test_user_scalar_fails_buffer(self):
        r = rational(1)
        with assert_raises(TypeError):
            memoryview(r)

        # Check that we do not allow writeable buffer export
        with pytest.raises(BufferError, match="scalar buffer is readonly"):
            get_buffer_info(r, ["WRITABLE"])

Directory Contents

Dirs: 3 × Files: 63

Name Size Perms Modified Actions
data DIR
- drwxr-xr-x 2026-02-06 08:01:08
Edit Download
examples DIR
- drwxr-xr-x 2026-01-20 13:01:48
Edit Download
- drwxr-xr-x 2026-02-06 08:01:08
Edit Download
2.17 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
22.46 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
1.92 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
3.17 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
39.51 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
33.57 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
7.42 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
4.94 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
33.49 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
6.41 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
1.49 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
14.51 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
9.18 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
3.54 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
113.49 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
24.41 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
30.35 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
3.44 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
73.52 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
51.72 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
2.17 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
5.51 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
15.23 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
6.56 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
23.66 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
1011 B lrw-r--r-- 2026-01-20 13:01:48
Edit Download
5.01 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
53.04 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
6.31 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
1.14 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
13.58 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
1.04 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
7.30 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
28.40 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
15.63 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
370.43 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
127.75 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
8.63 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
133.34 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
21.18 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
25.47 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
6.68 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
1.14 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
19.79 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
89.30 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
5.45 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
2.31 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
41.88 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
18.33 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
5.97 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
7.36 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
29.03 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
47.55 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
3.72 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
3.75 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
121.24 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
180.79 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
3.81 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
22.70 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
12.48 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
2.78 KB lrw-r--r-- 2026-01-20 13:01:48
Edit Download
2.15 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
0 B lrw-r--r-- 2026-01-20 13:01:48
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).