PHP 8.2.30
Preview: test_conversion_utils.py Size: 6.41 KB
//opt/cloudlinux/venv/lib/python3.11/site-packages/numpy/core/tests/test_conversion_utils.py

"""
Tests for numpy/core/src/multiarray/conversion_utils.c
"""
import re
import sys

import pytest

import numpy as np
import numpy.core._multiarray_tests as mt
from numpy.testing import assert_warns, IS_PYPY


class StringConverterTestCase:
    allow_bytes = True
    case_insensitive = True
    exact_match = False
    warn = True

    def _check_value_error(self, val):
        pattern = r'\(got {}\)'.format(re.escape(repr(val)))
        with pytest.raises(ValueError, match=pattern) as exc:
            self.conv(val)

    def _check_conv_assert_warn(self, val, expected):
        if self.warn:
            with assert_warns(DeprecationWarning) as exc:
                assert self.conv(val) == expected
        else:
            assert self.conv(val) == expected

    def _check(self, val, expected):
        """Takes valid non-deprecated inputs for converters,
        runs converters on inputs, checks correctness of outputs,
        warnings and errors"""
        assert self.conv(val) == expected

        if self.allow_bytes:
            assert self.conv(val.encode('ascii')) == expected
        else:
            with pytest.raises(TypeError):
                self.conv(val.encode('ascii'))

        if len(val) != 1:
            if self.exact_match:
                self._check_value_error(val[:1])
                self._check_value_error(val + '\0')
            else:
                self._check_conv_assert_warn(val[:1], expected)

        if self.case_insensitive:
            if val != val.lower():
                self._check_conv_assert_warn(val.lower(), expected)
            if val != val.upper():
                self._check_conv_assert_warn(val.upper(), expected)
        else:
            if val != val.lower():
                self._check_value_error(val.lower())
            if val != val.upper():
                self._check_value_error(val.upper())

    def test_wrong_type(self):
        # common cases which apply to all the below
        with pytest.raises(TypeError):
            self.conv({})
        with pytest.raises(TypeError):
            self.conv([])

    def test_wrong_value(self):
        # nonsense strings
        self._check_value_error('')
        self._check_value_error('\N{greek small letter pi}')

        if self.allow_bytes:
            self._check_value_error(b'')
            # bytes which can't be converted to strings via utf8
            self._check_value_error(b"\xFF")
        if self.exact_match:
            self._check_value_error("there's no way this is supported")


class TestByteorderConverter(StringConverterTestCase):
    """ Tests of PyArray_ByteorderConverter """
    conv = mt.run_byteorder_converter
    warn = False

    def test_valid(self):
        for s in ['big', '>']:
            self._check(s, 'NPY_BIG')
        for s in ['little', '<']:
            self._check(s, 'NPY_LITTLE')
        for s in ['native', '=']:
            self._check(s, 'NPY_NATIVE')
        for s in ['ignore', '|']:
            self._check(s, 'NPY_IGNORE')
        for s in ['swap']:
            self._check(s, 'NPY_SWAP')


class TestSortkindConverter(StringConverterTestCase):
    """ Tests of PyArray_SortkindConverter """
    conv = mt.run_sortkind_converter
    warn = False

    def test_valid(self):
        self._check('quicksort', 'NPY_QUICKSORT')
        self._check('heapsort', 'NPY_HEAPSORT')
        self._check('mergesort', 'NPY_STABLESORT')  # alias
        self._check('stable', 'NPY_STABLESORT')


class TestSelectkindConverter(StringConverterTestCase):
    """ Tests of PyArray_SelectkindConverter """
    conv = mt.run_selectkind_converter
    case_insensitive = False
    exact_match = True

    def test_valid(self):
        self._check('introselect', 'NPY_INTROSELECT')


class TestSearchsideConverter(StringConverterTestCase):
    """ Tests of PyArray_SearchsideConverter """
    conv = mt.run_searchside_converter
    def test_valid(self):
        self._check('left', 'NPY_SEARCHLEFT')
        self._check('right', 'NPY_SEARCHRIGHT')


class TestOrderConverter(StringConverterTestCase):
    """ Tests of PyArray_OrderConverter """
    conv = mt.run_order_converter
    warn = False

    def test_valid(self):
        self._check('c', 'NPY_CORDER')
        self._check('f', 'NPY_FORTRANORDER')
        self._check('a', 'NPY_ANYORDER')
        self._check('k', 'NPY_KEEPORDER')

    def test_flatten_invalid_order(self):
        # invalid after gh-14596
        with pytest.raises(ValueError):
            self.conv('Z')
        for order in [False, True, 0, 8]:
            with pytest.raises(TypeError):
                self.conv(order)


class TestClipmodeConverter(StringConverterTestCase):
    """ Tests of PyArray_ClipmodeConverter """
    conv = mt.run_clipmode_converter
    def test_valid(self):
        self._check('clip', 'NPY_CLIP')
        self._check('wrap', 'NPY_WRAP')
        self._check('raise', 'NPY_RAISE')

        # integer values allowed here
        assert self.conv(np.CLIP) == 'NPY_CLIP'
        assert self.conv(np.WRAP) == 'NPY_WRAP'
        assert self.conv(np.RAISE) == 'NPY_RAISE'


class TestCastingConverter(StringConverterTestCase):
    """ Tests of PyArray_CastingConverter """
    conv = mt.run_casting_converter
    case_insensitive = False
    exact_match = True

    def test_valid(self):
        self._check("no", "NPY_NO_CASTING")
        self._check("equiv", "NPY_EQUIV_CASTING")
        self._check("safe", "NPY_SAFE_CASTING")
        self._check("same_kind", "NPY_SAME_KIND_CASTING")
        self._check("unsafe", "NPY_UNSAFE_CASTING")


class TestIntpConverter:
    """ Tests of PyArray_IntpConverter """
    conv = mt.run_intp_converter

    def test_basic(self):
        assert self.conv(1) == (1,)
        assert self.conv((1, 2)) == (1, 2)
        assert self.conv([1, 2]) == (1, 2)
        assert self.conv(()) == ()

    def test_none(self):
        # once the warning expires, this will raise TypeError
        with pytest.warns(DeprecationWarning):
            assert self.conv(None) == ()

    @pytest.mark.skipif(IS_PYPY and sys.implementation.version <= (7, 3, 8),
            reason="PyPy bug in error formatting")
    def test_float(self):
        with pytest.raises(TypeError):
            self.conv(1.0)
        with pytest.raises(TypeError):
            self.conv([1, 1.0])

    def test_too_large(self):
        with pytest.raises(ValueError):
            self.conv(2**64)

    def test_too_many_dims(self):
        assert self.conv([1]*32) == (1,)*32
        with pytest.raises(ValueError):
            self.conv([1]*33)

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).