| 1393 | @pytest.mark.parametrize("loop_dtype", np.typecodes["All"]) |
| 1394 | @pytest.mark.filterwarnings("ignore::numpy.ComplexWarning") |
| 1395 | def test_iter_copy_casts(dtype, loop_dtype): |
| 1396 | # Ensure the dtype is never flexible: |
| 1397 | if loop_dtype.lower() == "m": |
| 1398 | loop_dtype = loop_dtype + "[ms]" |
| 1399 | elif np.dtype(loop_dtype).itemsize == 0: |
| 1400 | loop_dtype = loop_dtype + "50" |
| 1401 | |
| 1402 | # Make things a bit more interesting by requiring a byte-swap as well: |
| 1403 | arr = np.ones(1000, dtype=np.dtype(dtype).newbyteorder()) |
| 1404 | try: |
| 1405 | expected = arr.astype(loop_dtype) |
| 1406 | except Exception: |
| 1407 | # Some casts are not possible, do not worry about them |
| 1408 | return |
| 1409 | |
| 1410 | it = np.nditer((arr,), ["buffered", "external_loop", "refs_ok"], |
| 1411 | op_dtypes=[loop_dtype], casting="unsafe") |
| 1412 | |
| 1413 | if np.issubdtype(np.dtype(loop_dtype), np.number): |
| 1414 | # Casting to strings may be strange, but for simple dtypes do not rely |
| 1415 | # on the cast being correct: |
| 1416 | assert_array_equal(expected, np.ones(1000, dtype=loop_dtype)) |
| 1417 | |
| 1418 | it_copy = it.copy() |
| 1419 | res = next(it) |
| 1420 | del it |
| 1421 | res_copy = next(it_copy) |
| 1422 | del it_copy |
| 1423 | |
| 1424 | assert_array_equal(res, expected) |
| 1425 | assert_array_equal(res_copy, expected) |
| 1426 | |
| 1427 | |
| 1428 | def test_iter_copy_casts_structured(): |