|
10 | 10 | from itertools import product
|
11 | 11 | from numpy.testing import (
|
12 | 12 | TestCase, run_module_suite, assert_, assert_equal, assert_raises,
|
13 |
| - assert_array_equal, assert_warns, HAS_REFCOUNT |
| 13 | + assert_array_equal, assert_warns, dec, HAS_REFCOUNT, suppress_warnings |
14 | 14 | )
|
15 | 15 |
|
16 | 16 |
|
@@ -617,6 +617,54 @@ def __array_finalize__(self, old):
|
617 | 617 | assert_array_equal(new_s.old, s)
|
618 | 618 |
|
619 | 619 | class TestFancyIndexingCast(TestCase):
|
| 620 | + @dec.skipif(not HAS_REFCOUNT) |
| 621 | + def test_slice_decref_getsetslice(self): |
| 622 | + # See gh-10066, a temporary slice object should be discarted. |
| 623 | + # This test is only really interesting on Python 2 since |
| 624 | + # it goes through `__set/getslice__` here and can probably be |
| 625 | + # removed. Use 0:7 to make sure it is never None:7. |
| 626 | + class KeepIndexObject(np.ndarray): |
| 627 | + def __getitem__(self, indx): |
| 628 | + self.indx = indx |
| 629 | + if indx == slice(0, 7): |
| 630 | + raise ValueError |
| 631 | + |
| 632 | + def __setitem__(self, indx, val): |
| 633 | + self.indx = indx |
| 634 | + if indx == slice(0, 4): |
| 635 | + raise ValueError |
| 636 | + |
| 637 | + k = np.array([1]).view(KeepIndexObject) |
| 638 | + k[0:5] |
| 639 | + assert_equal(k.indx, slice(0, 5)) |
| 640 | + assert_equal(sys.getrefcount(k.indx), 2) |
| 641 | + try: |
| 642 | + k[0:7] |
| 643 | + raise AssertionError |
| 644 | + except ValueError: |
| 645 | + # The exception holds a reference to the slice so clear on Py2 |
| 646 | + if hasattr(sys, 'exc_clear'): |
| 647 | + with suppress_warnings() as sup: |
| 648 | + sup.filter(DeprecationWarning) |
| 649 | + sys.exc_clear() |
| 650 | + assert_equal(k.indx, slice(0, 7)) |
| 651 | + assert_equal(sys.getrefcount(k.indx), 2) |
| 652 | + |
| 653 | + k[0:3] = 6 |
| 654 | + assert_equal(k.indx, slice(0, 3)) |
| 655 | + assert_equal(sys.getrefcount(k.indx), 2) |
| 656 | + try: |
| 657 | + k[0:4] = 2 |
| 658 | + raise AssertionError |
| 659 | + except ValueError: |
| 660 | + # The exception holds a reference to the slice so clear on Py2 |
| 661 | + if hasattr(sys, 'exc_clear'): |
| 662 | + with suppress_warnings() as sup: |
| 663 | + sup.filter(DeprecationWarning) |
| 664 | + sys.exc_clear() |
| 665 | + assert_equal(k.indx, slice(0, 4)) |
| 666 | + assert_equal(sys.getrefcount(k.indx), 2) |
| 667 | + |
620 | 668 | def test_boolean_index_cast_assign(self):
|
621 | 669 | # Setup the boolean index and float arrays.
|
622 | 670 | shape = (8, 63)
|
@@ -1103,6 +1151,7 @@ def test_1d(self):
|
1103 | 1151 | for index in self.complex_indices:
|
1104 | 1152 | self._check_single_index(a, index)
|
1105 | 1153 |
|
| 1154 | + |
1106 | 1155 | class TestFloatNonIntegerArgument(TestCase):
|
1107 | 1156 | """
|
1108 | 1157 | These test that ``TypeError`` is raised when you try to use
|
|
0 commit comments