From 01716d3d116a592a33ca906e7f6b3187b21558f8 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Thu, 29 Oct 2026 16:58:18 +0000 Subject: [PATCH] gh-160781: Only test BytesIO write overflow on the C implementation (GH-160903) (cherry picked from commit 8dbc313ff9e36d38062532646c6c7faeab560115) Co-authored-by: Stan Ulbrych --- Lib/test/test_io/test_memoryio.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_io/test_memoryio.py b/Lib/test/test_io/test_memoryio.py index b0c150342061b84..c82acceffb09c8b 100644 --- a/Lib/test/test_io/test_memoryio.py +++ b/Lib/test/test_io/test_memoryio.py @@ -565,10 +565,6 @@ def test_issue141311(self): self.assertEqual(sys.maxsize, memio.seek(sys.maxsize)) buf = bytearray(2) self.assertEqual(0, memio.readinto(buf)) - # Writing there must not overflow the buffer size. - memio.seek(sys.maxsize - 1) - self.assertRaises((MemoryError, OverflowError), memio.write, b"x") - self.assertEqual(memio.getvalue(), b"") def test_unicode(self): memio = self.ioclass() @@ -891,6 +887,15 @@ def __index__(self): self.assertEqual(stream.truncate(2**31), 2**31) self.assertEqual(stream.getvalue(), b'abc') + def test_write_overflow(self): + # gh-160781: overallocation at a position near sys.maxsize + # must raise OverflowError, not SystemError. + memio = self.ioclass() + memio.seek(sys.maxsize - 1) + self.assertRaises(OverflowError, memio.write, b"x") + self.assertEqual(memio.getvalue(), b"") + self.assertEqual(memio.tell(), sys.maxsize - 1) + def test_readinto_overlapping_buffer(self): with self.ioclass(b'abcd') as stream: with stream.getbuffer()[1:] as target: