Repository: numpy/numpy
File: numpy/_core/tests/test_multiarray.py (Python)

Code before:
                   "https://foss.heptapod.net/pypy/pypy/-/issues/3724")
    def test_mmap_close(self):
        # The old buffer protocol was not safe for some things that the new
        # one is.  But `frombuffer` always used the old one for a long time.
        # Checks that it is safe with the new one (using memoryviews)
        with tempfile.TemporaryFile(mode='wb') as tmp:
            tmp.write(b"asdf")
            tmp.flush()
            mm = mmap.mmap(tmp.fileno(), 0)
            arr = np.frombuffer(mm, dtype=np.uint8)
            with pytest.raises(BufferError):
                mm.close()  # cannot close while array uses the buffer
            del arr
            mm.close()

class TestFlat:
    def _create_arrays(self):
        a = np.arange(20.0).reshape(4, 5)
        a.flags.writeable = False
        b = a[::2, ::2]
        return a, b

    def _create_arrays0(self):
        a0 = np.arange(20.0).reshape((4, 5))
        b0 = a0[::2, ::2]
        return a0, b0

    def test_contiguous(self):
        testpassed = False
        a, _ = self._create_arrays()
        try:
            a.flat[12] = 100.0
        except ValueError:
            testpassed = True
        assert_(testpassed)
        assert_(a.flat[12] == 12.0)

    def test_discontiguous(self):
        testpassed = False
        _, b = self._create_arrays()
        try:
            b.flat[4] = 100.0
        except ValueError:
            testpassed = True
        assert_(testpassed)
        assert_(b.flat[4] == 12.0)

    def test___array__(self):
        a, b = self._create_arrays()
        a0, b0 = self._create_arrays0()
        c = a.flat.__array__()

Review comment (suggestion):
this is only called once below so you can delete this kinda awkward function and inline its content in `test__array__`. But I'd construct `a0` and `b0` based on `a` and `b` as the `setup_method` used to, it's a little clearer what the intent is.

Code after:
                   "https://foss.heptapod.net/pypy/pypy/-/issues/3724")
    def test_mmap_close(self):
        # The old buffer protocol was not safe for some things that the new
        # one is.  But `frombuffer` always used the old one for a long time.
        # Checks that it is safe with the new one (using memoryviews)
        with tempfile.TemporaryFile(mode='wb') as tmp:
            tmp.write(b"asdf")
            tmp.flush()
            mm = mmap.mmap(tmp.fileno(), 0)
            arr = np.frombuffer(mm, dtype=np.uint8)
            with pytest.raises(BufferError):
                mm.close()  # cannot close while array uses the buffer
            del arr
            mm.close()

class TestFlat:
    def _create_arrays(self):
        a = np.arange(20.0).reshape(4, 5)
        a.flags.writeable = False
        b = a[::2, ::2]
        return a, b

    def test_contiguous(self):
        testpassed = False
        a, _ = self._create_arrays()
        try:
            a.flat[12] = 100.0
        except ValueError:
            testpassed = True
        assert_(testpassed)
        assert_(a.flat[12] == 12.0)

    def test_discontiguous(self):
        testpassed = False
        _, b = self._create_arrays()
        try:
            b.flat[4] = 100.0
        except ValueError:
            testpassed = True
        assert_(testpassed)
        assert_(b.flat[4] == 12.0)

    def test___array__(self):
        a0 = np.arange(20.0)
        a = a0.reshape(4, 5)
        a0 = a0.reshape((4, 5))
        a.flags.writeable = False
        b = a[::2, ::2]
        b0 = a0[::2, ::2]
        c = a.flat.__array__()
        d = b.flat.__array__()