Skip to content

Commit c57fe6b

Browse files
committed
Update tests to just run inline, and fold many of the tests into one.
Format blurb correctly, and reduce the chatter (There is a user-visible behaviour change, but I guess it's so minor it's not worth mentioning)
1 parent f5ea967 commit c57fe6b

2 files changed

Lines changed: 36 additions & 74 deletions

File tree

Lib/test/test_bytes.py

Lines changed: 35 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import test.string_tests
2626
import test.list_tests
2727
from test.support import bigaddrspacetest, MAX_Py_ssize_t
28-
from test.support.script_helper import assert_python_failure, assert_python_ok
28+
from test.support.script_helper import assert_python_failure
2929

3030

3131
if sys.flags.bytes_warning:
@@ -2197,84 +2197,49 @@ def __len__(self):
21972197

21982198
self.assertRaises(BufferError, ba.hex, S(b':'))
21992199

2200-
2201-
class ByteArrayInitialization1Test(unittest.TestCase):
2202-
# A bytearray created with __new__ so that __init__ is never called
2203-
# (often as a side-effect of a subclass not calling super().__init__).
2204-
# Is left with ob_bytes_object == NULL. It's easy for implementation
2205-
# code to not realize that ob_bytes_object can be NULL, so these tests
2206-
# verify a set of code paths that have historically crashed or asserted
2207-
# (see gh-153419).
2208-
2209-
def _check(self, stmt, expected):
2210-
code = textwrap.dedent(f"""
2211-
a = bytearray.__new__(bytearray)
2212-
{stmt}
2213-
print(list(a))
2214-
""")
2215-
rc, out, err = assert_python_ok('-c', code)
2216-
self.assertEqual(out.decode().strip(), expected)
2217-
2218-
def test_append(self):
2219-
self._check("a.append(1)", "[1]")
2220-
2221-
def test_insert(self):
2222-
self._check("a.insert(0, 1)", "[1]")
2223-
2224-
def test_extend_bytes(self):
2225-
self._check("a.extend(b'x')", "[120]")
2226-
2227-
def test_extend_iterable(self):
2228-
self._check("a.extend([1, 2, 3])", "[1, 2, 3]")
2229-
2230-
def test_iadd(self):
2231-
self._check("a += b'x'", "[120]")
2232-
2233-
def test_slice_assign(self):
2234-
self._check("a[:] = b'xyz'", "[120, 121, 122]")
2235-
2236-
def test_resize(self):
2237-
self._check("a.resize(4)", "[0, 0, 0, 0]")
2238-
2239-
def test_init_int(self):
2240-
self._check("a.__init__(5)", "[0, 0, 0, 0, 0]")
2241-
2242-
def test_init_bytes(self):
2243-
self._check("a.__init__(b'xyz')", "[120, 121, 122]")
2200+
def test_uninitialized_instance(self):
2201+
# A bytearray created with __new__ so that __init__ is never called
2202+
# (often as a side-effect of a subclass not calling super().__init__)
2203+
# is left with ob_bytes_object == NULL. It's easy for implementation
2204+
# code to not realize that ob_bytes_object can be NULL, so these
2205+
# checks exercise code paths that have historically crashed or
2206+
# asserted (see gh-153419).
2207+
def uninitialized():
2208+
return bytearray.__new__(bytearray)
2209+
2210+
uninitialized().insert(0, 1)
2211+
uninitialized().extend(b"x")
2212+
uninitialized().extend([1, 2, 3])
2213+
uninitialized().resize(4)
2214+
uninitialized().__init__(5)
2215+
uninitialized().__init__(b"xyz")
2216+
uninitialized().take_bytes()
2217+
uninitialized().take_bytes(0)
2218+
2219+
a = uninitialized()
2220+
a.append(1)
2221+
2222+
a = uninitialized()
2223+
a += b"x"
2224+
2225+
a = uninitialized()
2226+
a[:] = b"xyz"
22442227

22452228
def test_reinit_length1(self):
22462229
# There is a shortcut taken when resizing, where alloc/2 < newsize.
22472230
# In this case, the existing buffer is reused, rather than reset.
22482231
# If this happens when newsize == 0 and alloc == 1, then various
22492232
# code assumptions can be violated. This test should catch those
22502233
# in debug builds. (see gh-153419)
2251-
code = textwrap.dedent("""
2252-
a = bytearray(1)
2253-
a.__init__()
2254-
print(list(a))
2255-
""")
2256-
rc, out, err = assert_python_ok('-c', code)
2257-
self.assertEqual(out.decode().strip(), repr([]))
2258-
2259-
def test_take_bytes_all(self):
2260-
self._check("a.take_bytes()", "[]")
2261-
2262-
def test_take_bytes_zero(self):
2263-
self._check("a.take_bytes(0)", "[]")
2234+
a = bytearray(1)
2235+
a.__init__()
2236+
self.assertEqual(a, b"")
22642237

22652238
def test_reinit_with_view(self):
2266-
code = textwrap.dedent("""
2267-
a = bytearray()
2268-
v = memoryview(a)
2269-
try:
2270-
a.__init__("x", "ascii")
2271-
except BufferError:
2272-
print("PASS")
2273-
else:
2274-
print("NO EXCEPTION")
2275-
""")
2276-
rc, out, err = assert_python_ok('-c', code)
2277-
self.assertEqual(out.decode().strip(), "PASS")
2239+
a = bytearray()
2240+
with memoryview(a):
2241+
self.assertRaises(BufferError, a.__init__, "x", "ascii")
2242+
self.assertEqual(a, b"")
22782243

22792244

22802245
class AssortedBytesTest(unittest.TestCase):
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
Fix several ``bytearray`` crashes caused by calling, or not calling,
2-
``__init__`` when expected. As a side-effect, calling ``__init__`` on
3-
an empty ``bytearray`` that has an active buffer view (``memoryview`` or
4-
similar) now raises a ``BufferError``.
1+
Fix several :class:`bytearray` crashes caused by calling :meth:`~object.__init__`/:meth:`~object.__new__` in unusual ways.

0 commit comments

Comments
 (0)