Gentoo Archives: gentoo-commits

From: Marek Szuba <marecki@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] repo/gentoo:master commit in: dev-python/numpy/, dev-python/numpy/files/
Date: Fri, 14 May 2021 21:39:15
Message-Id: 1621028341.8901b5e61974c1e6df76de1cd86b72f417ee4221.marecki@gentoo
1 commit: 8901b5e61974c1e6df76de1cd86b72f417ee4221
2 Author: Marek Szuba <marecki <AT> gentoo <DOT> org>
3 AuthorDate: Fri May 14 21:22:11 2021 +0000
4 Commit: Marek Szuba <marecki <AT> gentoo <DOT> org>
5 CommitDate: Fri May 14 21:39:01 2021 +0000
6 URL: https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=8901b5e6
7
8 dev-python/numpy: support python3_10
9
10 Needed one commit to be backported from master. Now it builds, tests and
11 installs fine.
12
13 Signed-off-by: Marek Szuba <marecki <AT> gentoo.org>
14
15 .../files/numpy-1.20.3-float-hashing-py310.patch | 129 +++++++++++++++++++++
16 dev-python/numpy/numpy-1.20.3.ebuild | 3 +-
17 2 files changed, 131 insertions(+), 1 deletion(-)
18
19 diff --git a/dev-python/numpy/files/numpy-1.20.3-float-hashing-py310.patch b/dev-python/numpy/files/numpy-1.20.3-float-hashing-py310.patch
20 new file mode 100644
21 index 00000000000..f3b2ea3ef0c
22 --- /dev/null
23 +++ b/dev-python/numpy/files/numpy-1.20.3-float-hashing-py310.patch
24 @@ -0,0 +1,129 @@
25 +From ad2a73c18dcff95d844c382c94ab7f73b5571cf3 Mon Sep 17 00:00:00 2001
26 +From: Sebastian Berg <sebastian@××××××××××××.net>
27 +Date: Tue, 4 May 2021 17:43:26 -0500
28 +Subject: [PATCH] MAINT: Adjust NumPy float hashing to Python's slightly
29 + changed hash
30 +
31 +This is necessary, since we use the Python double hash and the
32 +semi-private function to calculate it in Python has a new signature
33 +to return the identity-hash when the value is NaN.
34 +
35 +closes gh-18833, gh-18907
36 +---
37 + numpy/core/src/common/npy_pycompat.h | 16 ++++++++++
38 + numpy/core/src/multiarray/scalartypes.c.src | 13 ++++----
39 + numpy/core/tests/test_scalarmath.py | 34 +++++++++++++++++++++
40 + 3 files changed, 57 insertions(+), 6 deletions(-)
41 +
42 +diff --git a/numpy/core/src/common/npy_pycompat.h b/numpy/core/src/common/npy_pycompat.h
43 +index aa0b5c1224d..9e94a971090 100644
44 +--- a/numpy/core/src/common/npy_pycompat.h
45 ++++ b/numpy/core/src/common/npy_pycompat.h
46 +@@ -3,4 +3,20 @@
47 +
48 + #include "numpy/npy_3kcompat.h"
49 +
50 ++
51 ++/*
52 ++ * In Python 3.10a7 (or b1), python started using the identity for the hash
53 ++ * when a value is NaN. See https://bugs.python.org/issue43475
54 ++ */
55 ++#if PY_VERSION_HEX > 0x030a00a6
56 ++#define Npy_HashDouble _Py_HashDouble
57 ++#else
58 ++static NPY_INLINE Py_hash_t
59 ++Npy_HashDouble(PyObject *NPY_UNUSED(identity), double val)
60 ++{
61 ++ return _Py_HashDouble(val);
62 ++}
63 ++#endif
64 ++
65 ++
66 + #endif /* _NPY_COMPAT_H_ */
67 +diff --git a/numpy/core/src/multiarray/scalartypes.c.src b/numpy/core/src/multiarray/scalartypes.c.src
68 +index a001500b0a9..9930f7791d6 100644
69 +--- a/numpy/core/src/multiarray/scalartypes.c.src
70 ++++ b/numpy/core/src/multiarray/scalartypes.c.src
71 +@@ -3172,7 +3172,7 @@ static npy_hash_t
72 + static npy_hash_t
73 + @lname@_arrtype_hash(PyObject *obj)
74 + {
75 +- return _Py_HashDouble((double) PyArrayScalar_VAL(obj, @name@));
76 ++ return Npy_HashDouble(obj, (double)PyArrayScalar_VAL(obj, @name@));
77 + }
78 +
79 + /* borrowed from complex_hash */
80 +@@ -3180,14 +3180,14 @@ static npy_hash_t
81 + c@lname@_arrtype_hash(PyObject *obj)
82 + {
83 + npy_hash_t hashreal, hashimag, combined;
84 +- hashreal = _Py_HashDouble((double)
85 +- PyArrayScalar_VAL(obj, C@name@).real);
86 ++ hashreal = Npy_HashDouble(
87 ++ obj, (double)PyArrayScalar_VAL(obj, C@name@).real);
88 +
89 + if (hashreal == -1) {
90 + return -1;
91 + }
92 +- hashimag = _Py_HashDouble((double)
93 +- PyArrayScalar_VAL(obj, C@name@).imag);
94 ++ hashimag = Npy_HashDouble(
95 ++ obj, (double)PyArrayScalar_VAL(obj, C@name@).imag);
96 + if (hashimag == -1) {
97 + return -1;
98 + }
99 +@@ -3202,7 +3202,8 @@ c@lname@_arrtype_hash(PyObject *obj)
100 + static npy_hash_t
101 + half_arrtype_hash(PyObject *obj)
102 + {
103 +- return _Py_HashDouble(npy_half_to_double(PyArrayScalar_VAL(obj, Half)));
104 ++ return Npy_HashDouble(
105 ++ obj, npy_half_to_double(PyArrayScalar_VAL(obj, Half)));
106 + }
107 +
108 + static npy_hash_t
109 +diff --git a/numpy/core/tests/test_scalarmath.py b/numpy/core/tests/test_scalarmath.py
110 +index d91b4a39146..09a734284a7 100644
111 +--- a/numpy/core/tests/test_scalarmath.py
112 ++++ b/numpy/core/tests/test_scalarmath.py
113 +@@ -712,6 +712,40 @@ def test_shift_all_bits(self, type_code, op):
114 + assert_equal(res_arr, res_scl)
115 +
116 +
117 ++class TestHash:
118 ++ @pytest.mark.parametrize("type_code", np.typecodes['AllInteger'])
119 ++ def test_integer_hashes(self, type_code):
120 ++ scalar = np.dtype(type_code).type
121 ++ for i in range(128):
122 ++ assert hash(i) == hash(scalar(i))
123 ++
124 ++ @pytest.mark.parametrize("type_code", np.typecodes['AllFloat'])
125 ++ def test_float_and_complex_hashes(self, type_code):
126 ++ scalar = np.dtype(type_code).type
127 ++ for val in [np.pi, np.inf, 3, 6.]:
128 ++ numpy_val = scalar(val)
129 ++ # Cast back to Python, in case the NumPy scalar has less precision
130 ++ if numpy_val.dtype.kind == 'c':
131 ++ val = complex(numpy_val)
132 ++ else:
133 ++ val = float(numpy_val)
134 ++ assert val == numpy_val
135 ++ print(repr(numpy_val), repr(val))
136 ++ assert hash(val) == hash(numpy_val)
137 ++
138 ++ if hash(float(np.nan)) != hash(float(np.nan)):
139 ++ # If Python distinguises different NaNs we do so too (gh-18833)
140 ++ assert hash(scalar(np.nan)) != hash(scalar(np.nan))
141 ++
142 ++ @pytest.mark.parametrize("type_code", np.typecodes['Complex'])
143 ++ def test_complex_hashes(self, type_code):
144 ++ # Test some complex valued hashes specifically:
145 ++ scalar = np.dtype(type_code).type
146 ++ for val in [np.pi+1j, np.inf-3j, 3j, 6.+1j]:
147 ++ numpy_val = scalar(val)
148 ++ assert hash(complex(numpy_val)) == hash(numpy_val)
149 ++
150 ++
151 + @contextlib.contextmanager
152 + def recursionlimit(n):
153 + o = sys.getrecursionlimit()
154
155 diff --git a/dev-python/numpy/numpy-1.20.3.ebuild b/dev-python/numpy/numpy-1.20.3.ebuild
156 index 10bbd07b87e..5b772a58a6f 100644
157 --- a/dev-python/numpy/numpy-1.20.3.ebuild
158 +++ b/dev-python/numpy/numpy-1.20.3.ebuild
159 @@ -3,7 +3,7 @@
160
161 EAPI=7
162
163 -PYTHON_COMPAT=( python3_{7..9} )
164 +PYTHON_COMPAT=( python3_{7..10} )
165 PYTHON_REQ_USE="threads(+)"
166
167 FORTRAN_NEEDED=lapack
168 @@ -46,6 +46,7 @@ BDEPEND="
169 PATCHES=(
170 "${FILESDIR}"/numpy-1.20.1-no-hardcode-blasv2.patch
171 "${FILESDIR}"/numpy-1.20.2-fix-ccompiler-tests.patch
172 + "${FILESDIR}"/numpy-1.20.3-float-hashing-py310.patch
173 )
174
175 distutils_enable_tests pytest