gh-160303: Fix bytearray state after a failed del b[:n] (GH-160304)

* gh-160303: Fix bytearray state after a failed del b[:n]

When shrinking the storage of a bytearray which has a logical offset,
allocate the new buffer before modifying the object, so that
bytearray_setslice_linear() can restore ob_start if the allocation fails.

* gh-160303: Keep ob_start instead of allocating in del b[:n]

Move ob_start and the size before the shrinking resize, so a failed
shrink leaves the bytes removed and the larger block in place, as the
non-front path already does.  Split the checks out of
bytearray_resize_lock_held() so the slice code can resize directly.
Move the tests into test_resize_error.

* gh-160303: Test failed shrinks not at the front; narrow the unchecked resize

Only the front path of bytearray_setslice_linear() calls
bytearray_resize_unchecked(): it has already advanced ob_start and reduced
the size, so bytearray_resize_lock_held() would return early without
releasing memory. Other slices go through bytearray_resize_lock_held()
again and keep the failure fixup to themselves.

test_resize_error now also covers slices in the middle and at the end,
and checks the null byte after the data with float().
3 files changed