()
| 585 | |
| 586 | |
| 587 | def test_writeable(): |
| 588 | # broadcast_to should return a readonly array |
| 589 | original = np.array([1, 2, 3]) |
| 590 | result = broadcast_to(original, (2, 3)) |
| 591 | assert_equal(result.flags.writeable, False) |
| 592 | assert_raises(ValueError, result.__setitem__, slice(None), 0) |
| 593 | |
| 594 | # but the result of broadcast_arrays needs to be writeable, to |
| 595 | # preserve backwards compatibility |
| 596 | test_cases = [((False,), broadcast_arrays(original,)), |
| 597 | ((True, False), broadcast_arrays(0, original))] |
| 598 | for is_broadcast, results in test_cases: |
| 599 | for array_is_broadcast, result in zip(is_broadcast, results): |
| 600 | # This will change to False in a future version |
| 601 | if array_is_broadcast: |
| 602 | with pytest.warns(FutureWarning): |
| 603 | assert_equal(result.flags.writeable, True) |
| 604 | with pytest.warns(DeprecationWarning): |
| 605 | result[:] = 0 |
| 606 | # Warning not emitted, writing to the array resets it |
| 607 | assert_equal(result.flags.writeable, True) |
| 608 | else: |
| 609 | # No warning: |
| 610 | assert_equal(result.flags.writeable, True) |
| 611 | |
| 612 | for results in [broadcast_arrays(original), |
| 613 | broadcast_arrays(0, original)]: |
| 614 | for result in results: |
| 615 | # resets the warn_on_write DeprecationWarning |
| 616 | result.flags.writeable = True |
| 617 | # check: no warning emitted |
| 618 | assert_equal(result.flags.writeable, True) |
| 619 | result[:] = 0 |
| 620 | |
| 621 | # keep readonly input readonly |
| 622 | original.flags.writeable = False |
| 623 | _, result = broadcast_arrays(0, original) |
| 624 | assert_equal(result.flags.writeable, False) |
| 625 | |
| 626 | # regression test for GH6491 |
| 627 | shape = (2,) |
| 628 | strides = [0] |
| 629 | tricky_array = as_strided(np.array(0), shape, strides) |
| 630 | other = np.zeros((1,)) |
| 631 | first, second = broadcast_arrays(tricky_array, other) |
| 632 | assert_(first.shape == second.shape) |
| 633 | |
| 634 | |
| 635 | def test_writeable_memoryview(): |
nothing calls this directly
no test coverage detected
searching dependent graphs…