codekingpro/portable-devtools
114k
1import builtins
2import collections.abc
3import ctypes
4import functools
5import gc
6import importlib
7import inspect
8import io
9import itertools
10import mmap
11import operator
12import os
13import pathlib
14import pickle
15import re
16import subprocess
17import sys
18import tempfile
19import textwrap
20import warnings
21import weakref
22from contextlib import contextmanager
23
24# Need to test an object that does not fully implement math interface
25from datetime import datetime, timedelta
26from decimal import Decimal
27
28import pytest
29
30import numpy as np
31import numpy._core._multiarray_tests as _multiarray_tests
32from numpy._core._rational_tests import rational
33from numpy._core.multiarray import _get_ndarray_c_version, dot
34from numpy._core.tests._locales import CommaDecimalPointLocale
35from numpy.exceptions import AxisError, ComplexWarning
36from numpy.lib import stride_tricks
37from numpy.lib.recfunctions import repack_fields
38from numpy.testing import (
39 BLAS_SUPPORTS_FPE,
40 HAS_REFCOUNT,
41 IS_64BIT,
42 IS_PYPY,
43 IS_PYSTON,
44 IS_WASM,
45 assert_,
46 assert_allclose,
47 assert_almost_equal,
48 assert_array_almost_equal,
49 assert_array_compare,
50 assert_array_equal,
51 assert_array_less,
52 assert_equal,
53 assert_raises,
54 assert_raises_regex,
55 break_cycles,
56 check_support_sve,
57 runstring,
58 temppath,
59)
60from numpy.testing._private.utils import _no_tracing, requires_memory
61
62
63def assert_arg_sorted(arr, arg):
64 # resulting array should be sorted and arg values should be unique
65 assert_equal(arr[arg], np.sort(arr))
66 assert_equal(np.sort(arg), np.arange(len(arg)))
67
68
69def assert_arr_partitioned(kth, k, arr_part):
70 assert_equal(arr_part[k], kth)
71 assert_array_compare(operator.__le__, arr_part[:k], kth)
72 assert_array_compare(operator.__ge__, arr_part[k:], kth)
73
74
75def _aligned_zeros(shape, dtype=float, order="C", align=None):
76 """
77 Allocate a new ndarray with aligned memory.
78
79 The ndarray is guaranteed *not* aligned to twice the requested alignment.
80 Eg, if align=4, guarantees it is not aligned to 8. If align=None uses
81 dtype.alignment."""
82 dtype = np.dtype(dtype)
83 if dtype == np.dtype(object):
84 # Can't do this, fall back to standard allocation (which
85 # should always be sufficiently aligned)
86 if align is not None:
87 raise ValueError("object array alignment not supported")
88 return np.zeros(shape, dtype=dtype, order=order)
89 if align is None:
90 align = dtype.alignment
91 if not hasattr(shape, '__len__'):
92 shape = (shape,)
93 size = functools.reduce(operator.mul, shape) * dtype.itemsize
94 buf = np.empty(size + 2 * align + 1, np.uint8)
95
96 ptr = buf.__array_interface__['data'][0]
97 offset = ptr % align
98 if offset != 0:
99 offset = align - offset
100 if (ptr % (2 * align)) == 0:
101 offset += align
102
103 # Note: slices producing 0-size arrays do not necessarily change
104 # data pointer --- so we use and allocate size+1
105 buf = buf[offset:offset + size + 1][:-1]
106 buf.fill(0)
107 data = np.ndarray(shape, dtype, buf, order=order)
108 return data
109
110
111class TestFlags:
112 def test_writeable(self):
113 arr = np.arange(10)
114 mydict = locals()
115 arr.flags.writeable = False
116 assert_raises(ValueError, runstring, 'arr[0] = 3', mydict)
117 arr.flags.writeable = True
118 arr[0] = 5
119 arr[0] = 0
120
121 def test_writeable_any_base(self):
122 # Ensure that any base being writeable is sufficient to change flag;
123 # this is especially interesting for arrays from an array interface.
124 arr = np.arange(10)
125
126 class subclass(np.ndarray):
127 pass
128
129 # Create subclass so base will not be collapsed, this is OK to change
130 view1 = arr.view(subclass)
131 view2 = view1[...]
132 arr.flags.writeable = False
133 view2.flags.writeable = False
134 view2.flags.writeable = True # Can be set to True again.
135
136 arr = np.arange(10)
137
138 class frominterface:
139 def __init__(self, arr):
140 self.arr = arr
141 self.__array_interface__ = arr.__array_interface__
142
143 view1 = np.asarray(frominterface)
144 view2 = view1[...]
145 view2.flags.writeable = False
146 view2.flags.writeable = True
147
148 view1.flags.writeable = False
149 view2.flags.writeable = False
150 with assert_raises(ValueError):
151 # Must assume not writeable, since only base is not:
152 view2.flags.writeable = True
153
154 def test_writeable_from_readonly(self):
155 # gh-9440 - make sure fromstring, from buffer on readonly buffers
156 # set writeable False
157 data = b'\x00' * 100
158 vals = np.frombuffer(data, 'B')
159 assert_raises(ValueError, vals.setflags, write=True)
160 types = np.dtype([('vals', 'u1'), ('res3', 'S4')])
161 values = np._core.records.fromstring(data, types)
162 vals = values['vals']
163 assert_raises(ValueError, vals.setflags, write=True)
164
165 def test_writeable_from_buffer(self):
166 data = bytearray(b'\x00' * 100)
167 vals = np.frombuffer(data, 'B')
168 assert_(vals.flags.writeable)
169 vals.setflags(write=False)
170 assert_(vals.flags.writeable is False)
171 vals.setflags(write=True)
172 assert_(vals.flags.writeable)
173 types = np.dtype([('vals', 'u1'), ('res3', 'S4')])
174 values = np._core.records.fromstring(data, types)
175 vals = values['vals']
176 assert_(vals.flags.writeable)
177 vals.setflags(write=False)
178 assert_(vals.flags.writeable is False)
179 vals.setflags(write=True)
180 assert_(vals.flags.writeable)
181
182 @pytest.mark.skipif(IS_PYPY, reason="PyPy always copies")
183 def test_writeable_pickle(self):
184 import pickle
185 # Small arrays will be copied without setting base.
186 # See condition for using PyArray_SetBaseObject in
187 # array_setstate.
188 a = np.arange(1000)
189 for v in range(pickle.HIGHEST_PROTOCOL):
190 vals = pickle.loads(pickle.dumps(a, v))
191 assert_(vals.flags.writeable)
192 assert_(isinstance(vals.base, bytes))
193
194 def test_writeable_from_c_data(self):
195 # Test that the writeable flag can be changed for an array wrapping
196 # low level C-data, but not owning its data.
197 # Also see that this is deprecated to change from python.
198 from numpy._core._multiarray_tests import get_c_wrapping_array
199
200 arr_writeable = get_c_wrapping_array(True)
201 assert not arr_writeable.flags.owndata
202 assert arr_writeable.flags.writeable
203 view = arr_writeable[...]
204
205 # Toggling the writeable flag works on the view:
206 view.flags.writeable = False
207 assert not view.flags.writeable
208 view.flags.writeable = True
209 assert view.flags.writeable
210 # Flag can be unset on the arr_writeable:
211 arr_writeable.flags.writeable = False
212
213 arr_readonly = get_c_wrapping_array(False)
214 assert not arr_readonly.flags.owndata
215 assert not arr_readonly.flags.writeable
216
217 for arr in [arr_writeable, arr_readonly]:
218 view = arr[...]
219 view.flags.writeable = False # make sure it is readonly
220 arr.flags.writeable = False
221 assert not arr.flags.writeable
222
223 with assert_raises(ValueError):
224 view.flags.writeable = True
225
226 with assert_raises(ValueError):
227 arr.flags.writeable = True
228
229 def test_warnonwrite(self):
230 a = np.arange(10)
231 a.flags._warn_on_write = True
232 with warnings.catch_warnings(record=True) as w:
233 warnings.filterwarnings('always')
234 a[1] = 10
235 a[2] = 10
236 # only warn once
237 assert_(len(w) == 1)
238
239 @pytest.mark.parametrize(["flag", "flag_value", "writeable"],
240 [("writeable", True, True),
241 # Delete _warn_on_write after deprecation and simplify
242 # the parameterization:
243 ("_warn_on_write", True, False),
244 ("writeable", False, False)])
245 def test_readonly_flag_protocols(self, flag, flag_value, writeable):
246 a = np.arange(10)
247 setattr(a.flags, flag, flag_value)
248
249 class MyArr:
250 __array_struct__ = a.__array_struct__
251
252 assert memoryview(a).readonly is not writeable
253 assert a.__array_interface__['data'][1] is not writeable
254 assert np.asarray(MyArr()).flags.writeable is writeable
255
256 def test_otherflags(self):
257 arr = np.arange(10)
258 assert_equal(arr.flags.carray, True)
259 assert_equal(arr.flags['C'], True)
260 assert_equal(arr.flags.farray, False)
261 assert_equal(arr.flags.behaved, True)
262 assert_equal(arr.flags.fnc, False)
263 assert_equal(arr.flags.forc, True)
264 assert_equal(arr.flags.owndata, True)
265 assert_equal(arr.flags.writeable, True)
266 assert_equal(arr.flags.aligned, True)
267 assert_equal(arr.flags.writebackifcopy, False)
268 assert_equal(arr.flags['X'], False)
269 assert_equal(arr.flags['WRITEBACKIFCOPY'], False)
270
271 def test_string_align(self):
272 a = np.zeros(4, dtype=np.dtype('|S4'))
273 assert_(a.flags.aligned)
274 # not power of two are accessed byte-wise and thus considered aligned
275 a = np.zeros(5, dtype=np.dtype('|S4'))
276 assert_(a.flags.aligned)
277
278 def test_void_align(self):
279 a = np.zeros(4, dtype=np.dtype([("a", "i4"), ("b", "i4")]))
280 assert_(a.flags.aligned)
281
282 @pytest.mark.parametrize("row_size", [5, 1 << 16])
283 @pytest.mark.parametrize("row_count", [1, 5])
284 @pytest.mark.parametrize("ndmin", [0, 1, 2])
285 def test_xcontiguous_load_txt(self, row_size, row_count, ndmin):
286 s = io.StringIO('\n'.join(['1.0 ' * row_size] * row_count))
287 a = np.loadtxt(s, ndmin=ndmin)
288
289 assert a.flags.c_contiguous
290 x = [i for i in a.shape if i != 1]
291 assert a.flags.f_contiguous == (len(x) <= 1)
292
293
294class TestHash:
295 # see #3793
296 def test_int(self):
297 for st, ut, s in [(np.int8, np.uint8, 8),
298 (np.int16, np.uint16, 16),
299 (np.int32, np.uint32, 32),
300 (np.int64, np.uint64, 64)]:
301 for i in range(1, s):
302 assert_equal(hash(st(-2**i)), hash(-2**i),
303 err_msg="%r: -2**%d" % (st, i))
304 assert_equal(hash(st(2**(i - 1))), hash(2**(i - 1)),
305 err_msg="%r: 2**%d" % (st, i - 1))
306 assert_equal(hash(st(2**i - 1)), hash(2**i - 1),
307 err_msg="%r: 2**%d - 1" % (st, i))
308
309 i = max(i - 1, 1)
310 assert_equal(hash(ut(2**(i - 1))), hash(2**(i - 1)),
311 err_msg="%r: 2**%d" % (ut, i - 1))
312 assert_equal(hash(ut(2**i - 1)), hash(2**i - 1),
313 err_msg="%r: 2**%d - 1" % (ut, i))
314
315
316class TestAttributes:
317 def _create_arrays(self):
318 one = np.arange(10)
319 two = np.arange(20).reshape(4, 5)
320 three = np.arange(60, dtype=np.float64).reshape(2, 5, 6)
321 return one, two, three
322
323 def test_attributes(self):
324 one, two, three = self._create_arrays()
325 assert_equal(one.shape, (10,))
326 assert_equal(two.shape, (4, 5))
327 assert_equal(three.shape, (2, 5, 6))
328 three.shape = (10, 3, 2)
329 assert_equal(three.shape, (10, 3, 2))
330 three.shape = (2, 5, 6)
331 assert_equal(one.strides, (one.itemsize,))
332 num = two.itemsize
333 assert_equal(two.strides, (5 * num, num))
334 num = three.itemsize
335 assert_equal(three.strides, (30 * num, 6 * num, num))
336 assert_equal(one.ndim, 1)
337 assert_equal(two.ndim, 2)
338 assert_equal(three.ndim, 3)
339 num = two.itemsize
340 assert_equal(two.size, 20)
341 assert_equal(two.nbytes, 20 * num)
342 assert_equal(two.itemsize, two.dtype.itemsize)
343 assert_equal(two.base, np.arange(20))
344
345 def test_dtypeattr(self):
346 one, _, three = self._create_arrays()
347 assert_equal(one.dtype, np.dtype(np.int_))
348 assert_equal(three.dtype, np.dtype(np.float64))
349 assert_equal(one.dtype.char, np.dtype(int).char)
350 assert one.dtype.char in "lq"
351 assert_equal(three.dtype.char, 'd')
352 assert_(three.dtype.str[0] in '<>')
353 assert_equal(one.dtype.str[1], 'i')
354 assert_equal(three.dtype.str[1], 'f')
355
356 def test_int_subclassing(self):
357 # Regression test for https://github.com/numpy/numpy/pull/3526
358
359 numpy_int = np.int_(0)
360
361 # int_ doesn't inherit from Python int, because it's not fixed-width
362 assert_(not isinstance(numpy_int, int))
363
364 def test_stridesattr(self):
365 x, _, _ = self._create_arrays()
366
367 def make_array(size, offset, strides):
368 return np.ndarray(size, buffer=x, dtype=int,
369 offset=offset * x.itemsize,
370 strides=strides * x.itemsize)
371
372 assert_equal(make_array(4, 4, -1), np.array([4, 3, 2, 1]))
373 assert_raises(ValueError, make_array, 4, 4, -2)
374 assert_raises(ValueError, make_array, 4, 2, -1)
375 assert_raises(ValueError, make_array, 8, 3, 1)
376 assert_equal(make_array(8, 3, 0), np.array([3] * 8))
377 # Check behavior reported in gh-2503:
378 assert_raises(ValueError, make_array, (2, 3), 5, np.array([-2, -3]))
379 make_array(0, 0, 10)
380
381 def test_set_stridesattr(self):
382 x, _, _ = self._create_arrays()
383
384 def make_array(size, offset, strides):
385 try:
386 r = np.ndarray([size], dtype=int, buffer=x,
387 offset=offset * x.itemsize)
388 except Exception as e:
389 raise RuntimeError(e)
390 with pytest.warns(DeprecationWarning):
391 r.strides = strides * x.itemsize
392 return r
393
394 assert_equal(make_array(4, 4, -1), np.array([4, 3, 2, 1]))
395 assert_equal(make_array(7, 3, 1), np.array([3, 4, 5, 6, 7, 8, 9]))
396 assert_raises(ValueError, make_array, 4, 4, -2)
397 assert_raises(ValueError, make_array, 4, 2, -1)
398 assert_raises(RuntimeError, make_array, 8, 3, 1)
399 # Check that the true extent of the array is used.
400 # Test relies on as_strided base not exposing a buffer.
401 x = stride_tricks.as_strided(np.arange(1), (10, 10), (0, 0))
402
403 def set_strides(arr, strides):
404 with pytest.warns(DeprecationWarning):
405 arr.strides = strides
406
407 assert_raises(ValueError, set_strides, x, (10 * x.itemsize, x.itemsize))
408
409 # Test for offset calculations:
410 x = stride_tricks.as_strided(np.arange(10, dtype=np.int8)[-1],
411 shape=(10,), strides=(-1,))
412 assert_raises(ValueError, set_strides, x[::-1], -1)
413 a = x[::-1]
414 with pytest.warns(DeprecationWarning):
415 a.strides = 1
416 with pytest.warns(DeprecationWarning):
417 a[::2].strides = 2
418
419 # test 0d
420 arr_0d = np.array(0)
421 with pytest.warns(DeprecationWarning):
422 arr_0d.strides = ()
423 assert_raises(TypeError, set_strides, arr_0d, None)
424
425 def test_fill(self):
426 for t in "?bhilqpBHILQPfdgFDGO":
427 x = np.empty((3, 2, 1), t)
428 y = np.empty((3, 2, 1), t)
429 x.fill(1)
430 y[...] = 1
431 assert_equal(x, y)
432
433 def test_fill_max_uint64(self):
434 x = np.empty((3, 2, 1), dtype=np.uint64)
435 y = np.empty((3, 2, 1), dtype=np.uint64)
436 value = 2**64 - 1
437 y[...] = value
438 x.fill(value)
439 assert_array_equal(x, y)
440
441 def test_fill_struct_array(self):
442 # Filling from a scalar
443 x = np.array([(0, 0.0), (1, 1.0)], dtype='i4,f8')
444 x.fill(x[0])
445 assert_equal(x['f1'][1], x['f1'][0])
446 # Filling from a tuple that can be converted
447 # to a scalar
448 x = np.zeros(2, dtype=[('a', 'f8'), ('b', 'i4')])
449 x.fill((3.5, -2))
450 assert_array_equal(x['a'], [3.5, 3.5])
451 assert_array_equal(x['b'], [-2, -2])
452
453 def test_fill_readonly(self):
454 # gh-22922
455 a = np.zeros(11)
456 a.setflags(write=False)
457 with pytest.raises(ValueError, match=".*read-only"):
458 a.fill(0)
459
460 def test_fill_subarrays(self):
461 # NOTE:
462 # This is also a regression test for a crash with PYTHONMALLOC=debug
463
464 dtype = np.dtype("2<i8, 2<i8, 2<i8")
465 data = ([1, 2], [3, 4], [5, 6])
466
467 arr = np.empty(1, dtype=dtype)
468 arr.fill(data)
469
470 assert_equal(arr, np.array(data, dtype=dtype))
471
472
473class TestArrayConstruction:
474 def test_array(self):
475 d = np.ones(6)
476 r = np.array([d, d])
477 assert_equal(r, np.ones((2, 6)))
478
479 d = np.ones(6)
480 tgt = np.ones((2, 6))
481 r = np.array([d, d])
482 assert_equal(r, tgt)
483 tgt[1] = 2
484 r = np.array([d, d + 1])
485 assert_equal(r, tgt)
486
487 d = np.ones(6)
488 r = np.array([[d, d]])
489 assert_equal(r, np.ones((1, 2, 6)))
490
491 d = np.ones(6)
492 r = np.array([[d, d], [d, d]])
493 assert_equal(r, np.ones((2, 2, 6)))
494
495 d = np.ones((6, 6))
496 r = np.array([d, d])
497 assert_equal(r, np.ones((2, 6, 6)))
498
499 d = np.ones((6, ))
500 r = np.array([[d, d + 1], d + 2], dtype=object)
501 assert_equal(len(r), 2)
502 assert_equal(r[0], [d, d + 1])
503 assert_equal(r[1], d + 2)
504
505 tgt = np.ones((2, 3), dtype=bool)
506 tgt[0, 2] = False
507 tgt[1, 0:2] = False
508 r = np.array([[True, True, False], [False, False, True]])
509 assert_equal(r, tgt)
510 r = np.array([[True, False], [True, False], [False, True]])
511 assert_equal(r, tgt.T)
512
513 def test_array_empty(self):
514 assert_raises(TypeError, np.array)
515
516 def test_0d_array_shape(self):
517 assert np.ones(np.array(3)).shape == (3,)
518
519 def test_array_copy_false(self):
520 d = np.array([1, 2, 3])
521 e = np.array(d, copy=False)
522 d[1] = 3
523 assert_array_equal(e, [1, 3, 3])
524 np.array(d, copy=False, order='F')
525
526 def test_array_copy_if_needed(self):
527 d = np.array([1, 2, 3])
528 e = np.array(d, copy=None)
529 d[1] = 3
530 assert_array_equal(e, [1, 3, 3])
531 e = np.array(d, copy=None, order='F')
532 d[1] = 4
533 assert_array_equal(e, [1, 4, 3])
534 e[2] = 7
535 assert_array_equal(d, [1, 4, 7])
536
537 def test_array_copy_true(self):
538 d = np.array([[1, 2, 3], [1, 2, 3]])
539 e = np.array(d, copy=True)
540 d[0, 1] = 3
541 e[0, 2] = -7
542 assert_array_equal(e, [[1, 2, -7], [1, 2, 3]])
543 assert_array_equal(d, [[1, 3, 3], [1, 2, 3]])
544 e = np.array(d, copy=True, order='F')
545 d[0, 1] = 5
546 e[0, 2] = 7
547 assert_array_equal(e, [[1, 3, 7], [1, 2, 3]])
548 assert_array_equal(d, [[1, 5, 3], [1, 2, 3]])
549
550 def test_array_copy_str(self):
551 with pytest.raises(
552 ValueError,
553 match="strings are not allowed for 'copy' keyword. "
554 "Use True/False/None instead."
555 ):
556 np.array([1, 2, 3], copy="always")
557
558 def test_array_cont(self):
559 d = np.ones(10)[::2]
560 assert_(np.ascontiguousarray(d).flags.c_contiguous)
561 assert_(np.ascontiguousarray(d).flags.f_contiguous)
562 assert_(np.asfortranarray(d).flags.c_contiguous)
563 assert_(np.asfortranarray(d).flags.f_contiguous)
564 d = np.ones((10, 10))[::2, ::2]
565 assert_(np.ascontiguousarray(d).flags.c_contiguous)
566 assert_(np.asfortranarray(d).flags.f_contiguous)
567
568 @pytest.mark.parametrize("func",
569 [np.array,
570 np.asarray,
571 np.asanyarray,
572 np.ascontiguousarray,
573 np.asfortranarray])
574 def test_bad_arguments_error(self, func):
575 with pytest.raises(TypeError):
576 func(3, dtype="bad dtype")
577 with pytest.raises(TypeError):
578 func() # missing arguments
579 with pytest.raises(TypeError):
580 func(1, 2, 3, 4, 5, 6, 7, 8) # too many arguments
581
582 @pytest.mark.parametrize("func",
583 [np.array,
584 np.asarray,
585 np.asanyarray,
586 np.ascontiguousarray,
587 np.asfortranarray])
588 def test_array_as_keyword(self, func):
589 # This should likely be made positional only, but do not change
590 # the name accidentally.
591 if func is np.array:
592 func(object=3)
593 else:
594 func(a=3)
595
596 @pytest.mark.skipif(sys.flags.optimize == 2, reason="Python running -OO")
597 @pytest.mark.xfail(IS_PYPY, reason="PyPy does not modify tp_doc")
598 @pytest.mark.parametrize("func",
599 [np.array,
600 np.asarray,
601 np.asanyarray,
602 np.ascontiguousarray,
603 np.asfortranarray])
604 def test_array_signature(self, func):
605 sig = inspect.signature(func)
606
607 assert len(sig.parameters) >= 3
608
609 arg0 = "object" if func is np.array else "a"
610 assert arg0 in sig.parameters
611 assert sig.parameters[arg0].default is inspect.Parameter.empty
612 assert sig.parameters[arg0].kind is inspect.Parameter.POSITIONAL_OR_KEYWORD
613
614 assert "dtype" in sig.parameters
615 assert sig.parameters["dtype"].default is None
616 assert sig.parameters["dtype"].kind is inspect.Parameter.POSITIONAL_OR_KEYWORD
617
618 assert "like" in sig.parameters
619 assert sig.parameters["like"].default is None
620 assert sig.parameters["like"].kind is inspect.Parameter.KEYWORD_ONLY
621
622
623class TestAssignment:
624 def test_assignment_broadcasting(self):
625 a = np.arange(6).reshape(2, 3)
626
627 # Broadcasting the input to the output
628 a[...] = np.arange(3)
629 assert_equal(a, [[0, 1, 2], [0, 1, 2]])
630 a[...] = np.arange(2).reshape(2, 1)
631 assert_equal(a, [[0, 0, 0], [1, 1, 1]])
632
633 # For compatibility with <= 1.5, a limited version of broadcasting
634 # the output to the input.
635 #
636 # This behavior is inconsistent with NumPy broadcasting
637 # in general, because it only uses one of the two broadcasting
638 # rules (adding a new "1" dimension to the left of the shape),
639 # applied to the output instead of an input. In NumPy 2.0, this kind
640 # of broadcasting assignment will likely be disallowed.
641 a[...] = np.arange(6)[::-1].reshape(1, 2, 3)
642 assert_equal(a, [[5, 4, 3], [2, 1, 0]])
643 # The other type of broadcasting would require a reduction operation.
644
645 def assign(a, b):
646 a[...] = b
647
648 assert_raises(ValueError, assign, a, np.arange(12).reshape(2, 2, 3))
649
650 def test_assignment_errors(self):
651 # Address issue #2276
652 class C:
653 pass
654 a = np.zeros(1)
655
656 def assign(v):
657 a[0] = v
658
659 assert_raises((AttributeError, TypeError), assign, C())
660 assert_raises(ValueError, assign, [1])
661
662 @pytest.mark.filterwarnings(
663 "ignore:.*set_string_function.*:DeprecationWarning"
664 )
665 def test_unicode_assignment(self):
666 # gh-5049
667 from numpy._core.arrayprint import set_printoptions
668
669 @contextmanager
670 def inject_str(s):
671 """ replace ndarray.__str__ temporarily """
672 set_printoptions(formatter={"all": lambda x: s})
673 try:
674 yield
675 finally:
676 set_printoptions()
677
678 a1d = np.array(['test'])
679 a0d = np.array('done')
680 with inject_str('bad'):
681 a1d[0] = a0d # previously this would invoke __str__
682 assert_equal(a1d[0], 'done')
683
684 # this would crash for the same reason
685 np.array([np.array('\xe5\xe4\xf6')])
686
687 def test_stringlike_empty_list(self):
688 # gh-8902
689 u = np.array(['done'])
690 b = np.array([b'done'])
691
692 class bad_sequence:
693 def __getitem__(self, _, /): pass
694 def __len__(self): raise RuntimeError
695
696 assert_raises(ValueError, operator.setitem, u, 0, [])
697 assert_raises(ValueError, operator.setitem, b, 0, [])
698
699 assert_raises(ValueError, operator.setitem, u, 0, bad_sequence())
700 assert_raises(ValueError, operator.setitem, b, 0, bad_sequence())
701
702 def test_longdouble_assignment(self):
703 # only relevant if longdouble is larger than float
704 # we're looking for loss of precision
705
706 for dtype in (np.longdouble, np.clongdouble):
707 # gh-8902
708 tinyb = np.nextafter(np.longdouble(0), 1).astype(dtype)
709 tinya = np.nextafter(np.longdouble(0), -1).astype(dtype)
710
711 # construction
712 tiny1d = np.array([tinya])
713 assert_equal(tiny1d[0], tinya)
714
715 # scalar = scalar
716 tiny1d[0] = tinyb
717 assert_equal(tiny1d[0], tinyb)
718
719 # 0d = scalar
720 tiny1d[0, ...] = tinya
721 assert_equal(tiny1d[0], tinya)
722
723 # 0d = 0d
724 tiny1d[0, ...] = tinyb[...]
725 assert_equal(tiny1d[0], tinyb)
726
727 # scalar = 0d
728 tiny1d[0] = tinyb[...]
729 assert_equal(tiny1d[0], tinyb)
730
731 arr = np.array([np.array(tinya)])
732 assert_equal(arr[0], tinya)
733
734 def test_cast_to_string(self):
735 # cast to str should do "str(scalar)", not "str(scalar.item())"
736 # When converting a float to a string via array assignment, we
737 # want to ensure that the conversion uses str(scalar) to preserve
738 # the expected precision.
739 a = np.zeros(1, dtype='S20')
740 a[:] = np.array(['1.12345678901234567890'], dtype='f8')
741 assert_equal(a[0], b"1.1234567890123457")
742
743
744class TestDtypedescr:
745 def test_construction(self):
746 d1 = np.dtype('i4')
747 assert_equal(d1, np.dtype(np.int32))
748 d2 = np.dtype('f8')
749 assert_equal(d2, np.dtype(np.float64))
750
751 def test_byteorders(self):
752 assert_(np.dtype('<i4') != np.dtype('>i4'))
753 assert_(np.dtype([('a', '<i4')]) != np.dtype([('a', '>i4')]))
754
755 def test_structured_non_void(self):
756 fields = [('a', '<i2'), ('b', '<i2')]
757 dt_int = np.dtype(('i4', fields))
758 assert_equal(str(dt_int), "(numpy.int32, [('a', '<i2'), ('b', '<i2')])")
759
760 # gh-9821
761 arr_int = np.zeros(4, dt_int)
762 assert_equal(repr(arr_int),
763 "array([0, 0, 0, 0], dtype=(numpy.int32, [('a', '<i2'), ('b', '<i2')]))")
764
765
766class TestZeroRank:
767 def _create_arrays(self):
768 return np.array(0), np.array('x', object)
769
770 def test_ellipsis_subscript(self):
771 a, b = self._create_arrays()
772 assert_equal(a[...], 0)
773 assert_equal(b[...], 'x')
774 assert_(a[...].base is a) # `a[...] is a` in numpy <1.9.
775 assert_(b[...].base is b) # `b[...] is b` in numpy <1.9.
776
777 def test_empty_subscript(self):
778 a, b = self._create_arrays()
779 assert_equal(a[()], 0)
780 assert_equal(b[()], 'x')
781 assert_(type(a[()]) is a.dtype.type)
782 assert_(type(b[()]) is str)
783
784 def test_invalid_subscript(self):
785 a, b = self._create_arrays()
786 assert_raises(IndexError, lambda x: x[0], a)
787 assert_raises(IndexError, lambda x: x[0], b)
788 assert_raises(IndexError, lambda x: x[np.array([], int)], a)
789 assert_raises(IndexError, lambda x: x[np.array([], int)], b)
790
791 def test_ellipsis_subscript_assignment(self):
792 a, b = self._create_arrays()
793 a[...] = 42
794 assert_equal(a, 42)
795 b[...] = ''
796 assert_equal(b.item(), '')
797
798 def test_empty_subscript_assignment(self):
799 a, b = self._create_arrays()
800 a[()] = 42
801 assert_equal(a, 42)
802 b[()] = ''
803 assert_equal(b.item(), '')
804
805 def test_invalid_subscript_assignment(self):
806 a, b = self._create_arrays()
807
808 def assign(x, i, v):
809 x[i] = v
810
811 assert_raises(IndexError, assign, a, 0, 42)
812 assert_raises(IndexError, assign, b, 0, '')
813 assert_raises(ValueError, assign, a, (), '')
814
815 def test_newaxis(self):
816 a, _ = self._create_arrays()
817 assert_equal(a[np.newaxis].shape, (1,))
818 assert_equal(a[..., np.newaxis].shape, (1,))
819 assert_equal(a[np.newaxis, ...].shape, (1,))
820 assert_equal(a[..., np.newaxis].shape, (1,))
821 assert_equal(a[np.newaxis, ..., np.newaxis].shape, (1, 1))
822 assert_equal(a[..., np.newaxis, np.newaxis].shape, (1, 1))
823 assert_equal(a[np.newaxis, np.newaxis, ...].shape, (1, 1))
824 assert_equal(a[(np.newaxis,) * 10].shape, (1,) * 10)
825
826 def test_invalid_newaxis(self):
827 a, _ = self._create_arrays()
828
829 def subscript(x, i):
830 x[i]
831
832 assert_raises(IndexError, subscript, a, (np.newaxis, 0))
833 assert_raises(IndexError, subscript, a, (np.newaxis,) * 70)
834
835 def test_constructor(self):
836 x = np.ndarray(())
837 x[()] = 5
838 assert_equal(x[()], 5)
839 y = np.ndarray((), buffer=x)
840 y[()] = 6
841 assert_equal(x[()], 6)
842
843 # strides and shape must be the same length
844 with pytest.raises(ValueError):
845 np.ndarray((2,), strides=())
846 with pytest.raises(ValueError):
847 np.ndarray((), strides=(2,))
848
849 def test_output(self):
850 x = np.array(2)
851 assert_raises(ValueError, np.add, x, [1], x)
852
853 def test_real_imag(self):
854 # contiguity checks are for gh-11245
855 x = np.array(1j)
856 xr = x.real
857 xi = x.imag
858
859 assert_equal(xr, np.array(0))
860 assert_(type(xr) is np.ndarray)
861 assert_equal(xr.flags.contiguous, True)
862 assert_equal(xr.flags.f_contiguous, True)
863
864 assert_equal(xi, np.array(1))
865 assert_(type(xi) is np.ndarray)
866 assert_equal(xi.flags.contiguous, True)
867 assert_equal(xi.flags.f_contiguous, True)
868
869
870class TestScalarIndexing:
871 def _create_array(self):
872 return np.array([0, 1])[0]
873
874 def test_ellipsis_subscript(self):
875 a = self._create_array()
876 assert_equal(a[...], 0)
877 assert_equal(a[...].shape, ())
878
879 def test_empty_subscript(self):
880 a = self._create_array()
881 assert_equal(a[()], 0)
882 assert_equal(a[()].shape, ())
883
884 def test_invalid_subscript(self):
885 a = self._create_array()
886 assert_raises(IndexError, lambda x: x[0], a)
887 assert_raises(IndexError, lambda x: x[np.array([], int)], a)
888
889 def test_invalid_subscript_assignment(self):
890 a = self._create_array()
891
892 def assign(x, i, v):
893 x[i] = v
894
895 assert_raises(TypeError, assign, a, 0, 42)
896
897 def test_newaxis(self):
898 a = self._create_array()
899 assert_equal(a[np.newaxis].shape, (1,))
900 assert_equal(a[..., np.newaxis].shape, (1,))
901 assert_equal(a[np.newaxis, ...].shape, (1,))
902 assert_equal(a[..., np.newaxis].shape, (1,))
903 assert_equal(a[np.newaxis, ..., np.newaxis].shape, (1, 1))
904 assert_equal(a[..., np.newaxis, np.newaxis].shape, (1, 1))
905 assert_equal(a[np.newaxis, np.newaxis, ...].shape, (1, 1))
906 assert_equal(a[(np.newaxis,) * 10].shape, (1,) * 10)
907
908 def test_invalid_newaxis(self):
909 a = self._create_array()
910
911 def subscript(x, i):
912 x[i]
913
914 assert_raises(IndexError, subscript, a, (np.newaxis, 0))
915 assert_raises(IndexError, subscript, a, (np.newaxis,) * 70)
916
917 def test_overlapping_assignment(self):
918 # With positive strides
919 a = np.arange(4)
920 a[:-1] = a[1:]
921 assert_equal(a, [1, 2, 3, 3])
922
923 a = np.arange(4)
924 a[1:] = a[:-1]
925 assert_equal(a, [0, 0, 1, 2])
926
927 # With positive and negative strides
928 a = np.arange(4)
929 a[:] = a[::-1]
930 assert_equal(a, [3, 2, 1, 0])
931
932 a = np.arange(6).reshape(2, 3)
933 a[::-1, :] = a[:, ::-1]
934 assert_equal(a, [[5, 4, 3], [2, 1, 0]])
935
936 a = np.arange(6).reshape(2, 3)
937 a[::-1, ::-1] = a[:, ::-1]
938 assert_equal(a, [[3, 4, 5], [0, 1, 2]])
939
940 # With just one element overlapping
941 a = np.arange(5)
942 a[:3] = a[2:]
943 assert_equal(a, [2, 3, 4, 3, 4])
944
945 a = np.arange(5)
946 a[2:] = a[:3]
947 assert_equal(a, [0, 1, 0, 1, 2])
948
949 a = np.arange(5)
950 a[2::-1] = a[2:]
951 assert_equal(a, [4, 3, 2, 3, 4])
952
953 a = np.arange(5)
954 a[2:] = a[2::-1]
955 assert_equal(a, [0, 1, 2, 1, 0])
956
957 a = np.arange(5)
958 a[2::-1] = a[:1:-1]
959 assert_equal(a, [2, 3, 4, 3, 4])
960
961 a = np.arange(5)
962 a[:1:-1] = a[2::-1]
963 assert_equal(a, [0, 1, 0, 1, 2])
964
965
966class TestCreation:
967 """
968 Test the np.array constructor
969 """
970 def test_from_attribute(self):
971 class x:
972 def __array__(self, dtype=None, copy=None):
973 pass
974
975 assert_raises(ValueError, np.array, x())
976
977 def test_from_string(self):
978 types = np.typecodes['AllInteger'] + np.typecodes['Float']
979 nstr = ['123', '123']
980 result = np.array([123, 123], dtype=int)
981 for type in types:
982 msg = f'String conversion for {type}'
983 assert_equal(np.array(nstr, dtype=type), result, err_msg=msg)
984
985 def test_void(self):
986 arr = np.array([], dtype='V')
987 assert arr.dtype == 'V8' # current default
988 # Same length scalars (those that go to the same void) work:
989 arr = np.array([b"1234", b"1234"], dtype="V")
990 assert arr.dtype == "V4"
991
992 # Promoting different lengths will fail (pre 1.20 this worked)
993 # by going via S5 and casting to V5.
994 with pytest.raises(TypeError):
995 np.array([b"1234", b"12345"], dtype="V")
996 with pytest.raises(TypeError):
997 np.array([b"12345", b"1234"], dtype="V")
998
999 # Check the same for the casting path:
1000 arr = np.array([b"1234", b"1234"], dtype="O").astype("V")
1001 assert arr.dtype == "V4"
1002 with pytest.raises(TypeError):
1003 np.array([b"1234", b"12345"], dtype="O").astype("V")
1004
1005 @pytest.mark.parametrize("idx",
1006 [pytest.param(Ellipsis, id="arr"), pytest.param((), id="scalar")])
1007 def test_structured_void_promotion(self, idx):
1008 arr = np.array(
1009 [np.array(1, dtype="i,i")[idx], np.array(2, dtype='i,i')[idx]],
1010 dtype="V")
1011 assert_array_equal(arr, np.array([(1, 1), (2, 2)], dtype="i,i"))
1012 # The following fails to promote the two dtypes, resulting in an error
1013 with pytest.raises(TypeError):
1014 np.array(
1015 [np.array(1, dtype="i,i")[idx], np.array(2, dtype='i,i,i')[idx]],
1016 dtype="V")
1017
1018 def test_too_big_error(self):
1019 # 45341 is the smallest integer greater than sqrt(2**31 - 1).
1020 # 3037000500 is the smallest integer greater than sqrt(2**63 - 1).
1021 # We want to make sure that the square byte array with those dimensions
1022 # is too big on 32 or 64 bit systems respectively.
1023 if np.iinfo('intp').max == 2**31 - 1:
1024 shape = (46341, 46341)
1025 elif np.iinfo('intp').max == 2**63 - 1:
1026 shape = (3037000500, 3037000500)
1027 else:
1028 return
1029 assert_raises(ValueError, np.empty, shape, dtype=np.int8)
1030 assert_raises(ValueError, np.zeros, shape, dtype=np.int8)
1031 assert_raises(ValueError, np.ones, shape, dtype=np.int8)
1032
1033 @pytest.mark.skipif(not IS_64BIT,
1034 reason="malloc may not fail on 32 bit systems")
1035 @pytest.mark.thread_unsafe(reason="large slow test in parallel")
1036 def test_malloc_fails(self):
1037 # This test is guaranteed to fail due to a too large allocation
1038 with assert_raises(np._core._exceptions._ArrayMemoryError):
1039 np.empty(np.iinfo(np.intp).max, dtype=np.uint8)
1040
1041 def test_zeros(self):
1042 types = np.typecodes['AllInteger'] + np.typecodes['AllFloat']
1043 for dt in types:
1044 d = np.zeros((13,), dtype=dt)
1045 assert_equal(np.count_nonzero(d), 0)
1046 # true for ieee floats
1047 assert_equal(d.sum(), 0)
1048 assert_(not d.any())
1049
1050 d = np.zeros(2, dtype='(2,4)i4')
1051 assert_equal(np.count_nonzero(d), 0)
1052 assert_equal(d.sum(), 0)
1053 assert_(not d.any())
1054
1055 d = np.zeros(2, dtype='4i4')
1056 assert_equal(np.count_nonzero(d), 0)
1057 assert_equal(d.sum(), 0)
1058 assert_(not d.any())
1059
1060 d = np.zeros(2, dtype='(2,4)i4, (2,4)i4')
1061 assert_equal(np.count_nonzero(d), 0)
1062
1063 @pytest.mark.slow
1064 def test_zeros_big(self):
1065 # test big array as they might be allocated different by the system
1066 types = np.typecodes['AllInteger'] + np.typecodes['AllFloat']
1067 for dt in types:
1068 d = np.zeros((30 * 1024**2,), dtype=dt)
1069 assert_(not d.any())
1070 # This test can fail on 32-bit systems due to insufficient
1071 # contiguous memory. Deallocating the previous array increases the
1072 # chance of success.
1073 del d
1074
1075 def test_zeros_obj(self):
1076 # test initialization from PyLong(0)
1077 d = np.zeros((13,), dtype=object)
1078 assert_array_equal(d, [0] * 13)
1079 assert_equal(np.count_nonzero(d), 0)
1080
1081 def test_zeros_obj_obj(self):
1082 d = np.zeros(10, dtype=[('k', object, 2)])
1083 assert_array_equal(d['k'], 0)
1084
1085 def test_zeros_like_like_zeros(self):
1086 # test zeros_like returns the same as zeros
1087 for c in np.typecodes['All']:
1088 if c == 'V':
1089 continue
1090 d = np.zeros((3, 3), dtype=c)
1091 assert_array_equal(np.zeros_like(d), d)
1092 assert_equal(np.zeros_like(d).dtype, d.dtype)
1093 # explicitly check some special cases
1094 d = np.zeros((3, 3), dtype='S5')
1095 assert_array_equal(np.zeros_like(d), d)
1096 assert_equal(np.zeros_like(d).dtype, d.dtype)
1097 d = np.zeros((3, 3), dtype='U5')
1098 assert_array_equal(np.zeros_like(d), d)
1099 assert_equal(np.zeros_like(d).dtype, d.dtype)
1100
1101 d = np.zeros((3, 3), dtype='<i4')
1102 assert_array_equal(np.zeros_like(d), d)
1103 assert_equal(np.zeros_like(d).dtype, d.dtype)
1104 d = np.zeros((3, 3), dtype='>i4')
1105 assert_array_equal(np.zeros_like(d), d)
1106 assert_equal(np.zeros_like(d).dtype, d.dtype)
1107
1108 d = np.zeros((3, 3), dtype='<M8[s]')
1109 assert_array_equal(np.zeros_like(d), d)
1110 assert_equal(np.zeros_like(d).dtype, d.dtype)
1111 d = np.zeros((3, 3), dtype='>M8[s]')
1112 assert_array_equal(np.zeros_like(d), d)
1113 assert_equal(np.zeros_like(d).dtype, d.dtype)
1114
1115 d = np.zeros((3, 3), dtype='f4,f4')
1116 assert_array_equal(np.zeros_like(d), d)
1117 assert_equal(np.zeros_like(d).dtype, d.dtype)
1118
1119 def test_empty_unicode(self):
1120 # don't throw decode errors on garbage memory
1121 for i in range(5, 100, 5):
1122 d = np.empty(i, dtype='U')
1123 str(d)
1124
1125 def test_sequence_non_homogeneous(self):
1126 assert_equal(np.array([4, 2**80]).dtype, object)
1127 assert_equal(np.array([4, 2**80, 4]).dtype, object)
1128 assert_equal(np.array([2**80, 4]).dtype, object)
1129 assert_equal(np.array([2**80] * 3).dtype, object)
1130 assert_equal(np.array([[1, 1], [1j, 1j]]).dtype, complex)
1131 assert_equal(np.array([[1j, 1j], [1, 1]]).dtype, complex)
1132 assert_equal(np.array([[1, 1, 1], [1, 1j, 1.], [1, 1, 1]]).dtype, complex)
1133
1134 def test_non_sequence_sequence(self):
1135 """Should not segfault.
1136
1137 Class Fail breaks the sequence protocol for new style classes, i.e.,
1138 those derived from object. Class Map is a mapping type indicated by
1139 raising a ValueError. At some point we may raise a warning instead
1140 of an error in the Fail case.
1141
1142 """
1143 class Fail:
1144 def __len__(self):
1145 return 1
1146
1147 def __getitem__(self, index):
1148 raise ValueError
1149
1150 class Map:
1151 def __len__(self):
1152 return 1
1153
1154 def __getitem__(self, index):
1155 raise KeyError
1156
1157 a = np.array([Map()])
1158 assert_(a.shape == (1,))
1159 assert_(a.dtype == np.dtype(object))
1160 assert_raises(ValueError, np.array, [Fail()])
1161
1162 def test_no_len_object_type(self):
1163 # gh-5100, want object array from iterable object without len()
1164 class Point2:
1165 def __init__(self):
1166 pass
1167
1168 def __getitem__(self, ind):
1169 if ind in [0, 1]:
1170 return ind
1171 else:
1172 raise IndexError
1173 d = np.array([Point2(), Point2(), Point2()])
1174 assert_equal(d.dtype, np.dtype(object))
1175
1176 def test_false_len_sequence(self):
1177 # gh-7264, segfault for this example
1178 class C:
1179 def __getitem__(self, i):
1180 raise IndexError
1181
1182 def __len__(self):
1183 return 42
1184
1185 a = np.array(C()) # segfault?
1186 assert_equal(len(a), 0)
1187
1188 def test_false_len_iterable(self):
1189 # Special case where a bad __getitem__ makes us fall back on __iter__:
1190 class C:
1191 def __getitem__(self, x):
1192 raise Exception
1193
1194 def __iter__(self):
1195 return iter(())
1196
1197 def __len__(self):
1198 return 2
1199
1200 a = np.empty(2)
