|
25 | 25 | import test.string_tests |
26 | 26 | import test.list_tests |
27 | 27 | 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 |
29 | 29 |
|
30 | 30 |
|
31 | 31 | if sys.flags.bytes_warning: |
@@ -2197,84 +2197,49 @@ def __len__(self): |
2197 | 2197 |
|
2198 | 2198 | self.assertRaises(BufferError, ba.hex, S(b':')) |
2199 | 2199 |
|
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" |
2244 | 2227 |
|
2245 | 2228 | def test_reinit_length1(self): |
2246 | 2229 | # There is a shortcut taken when resizing, where alloc/2 < newsize. |
2247 | 2230 | # In this case, the existing buffer is reused, rather than reset. |
2248 | 2231 | # If this happens when newsize == 0 and alloc == 1, then various |
2249 | 2232 | # code assumptions can be violated. This test should catch those |
2250 | 2233 | # 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"") |
2264 | 2237 |
|
2265 | 2238 | 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"") |
2278 | 2243 |
|
2279 | 2244 |
|
2280 | 2245 | class AssortedBytesTest(unittest.TestCase): |
|
0 commit comments