MCPcopy Index your code
hub / github.com/numpy/numpy / test_iter_copy_casts

Function test_iter_copy_casts

numpy/_core/tests/test_nditer.py:1536–1566  ·  view source on GitHub ↗
(dtype, loop_dtype)

Source from the content-addressed store, hash-verified

1534 "ignore::DeprecationWarning",
1535)
1536def test_iter_copy_casts(dtype, loop_dtype):
1537 # Ensure the dtype is never flexible:
1538 if loop_dtype.lower() == "m":
1539 loop_dtype = loop_dtype + "[ms]"
1540 elif np.dtype(loop_dtype).itemsize == 0:
1541 loop_dtype = loop_dtype + "50"
1542
1543 # Make things a bit more interesting by requiring a byte-swap as well:
1544 arr = np.ones(1000, dtype=np.dtype(dtype).newbyteorder())
1545 try:
1546 expected = arr.astype(loop_dtype)
1547 except Exception:
1548 # Some casts are not possible, do not worry about them
1549 return
1550
1551 it = np.nditer((arr,), ["buffered", "external_loop", "refs_ok"],
1552 op_dtypes=[loop_dtype], casting="unsafe")
1553
1554 if np.issubdtype(np.dtype(loop_dtype), np.number):
1555 # Casting to strings may be strange, but for simple dtypes do not rely
1556 # on the cast being correct:
1557 assert_array_equal(expected, np.ones(1000, dtype=loop_dtype))
1558
1559 it_copy = it.copy()
1560 res = next(it)
1561 del it
1562 res_copy = next(it_copy)
1563 del it_copy
1564
1565 assert_array_equal(res, expected)
1566 assert_array_equal(res_copy, expected)
1567
1568
1569def test_iter_copy_casts_structured():

Callers

nothing calls this directly

Calls 5

assert_array_equalFunction · 0.90
lowerMethod · 0.80
astypeMethod · 0.80
dtypeMethod · 0.45
copyMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…