Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/test/test_io/test_memoryio.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,10 @@ 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_peek(self):
buf = self.buftype("1234567890")
Expand Down Expand Up @@ -984,6 +988,12 @@ def __index__(self):
finally:
size.result.release()

# Sizes above 2**31 - 1 must work where C long is 32-bit.
if sys.maxsize > 2**31:
stream = self.ioclass(b'abc')
self.assertEqual(stream.truncate(2**31), 2**31)
self.assertEqual(stream.getvalue(), b'abc')

def test_readinto_overlapping_buffer(self):
with self.ioclass(b'abcd') as stream:
with stream.getbuffer()[1:] as target:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :class:`io.BytesIO` raising :exc:`SystemError` instead of
:exc:`OverflowError` when writing at a position close to
:data:`sys.maxsize`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :class:`io.BytesIO` implementation of :meth:`io.IOBase.truncate` rejecting
sizes larger than ``2**31 - 1`` on Windows.
5 changes: 4 additions & 1 deletion Modules/_io/bytesio.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ resize_buffer_lock_held(bytesio *self, size_t size)
/* Major upsize; resize up to exact size */
alloc = size + 1;
}
if (alloc > PY_SSIZE_T_MAX) {
goto overflow;
}

if (SHARED_BUF(self)) {
if (unshare_buffer_lock_held(self, alloc) < 0)
Expand Down Expand Up @@ -755,7 +758,7 @@ _io_BytesIO_truncate_impl(bytesio *self, PyObject *size)
new_size = self->pos;
}
else {
new_size = PyLong_AsLong(size);
new_size = PyNumber_AsSsize_t(size, PyExc_OverflowError);
if (new_size == -1 && PyErr_Occurred()) {
return NULL;
}
Expand Down
Loading