Skip to content

Commit 0bfee22

Browse files
authored
gh-160781: Fix StringIO.truncate() rejecting sizes above 2**31 - 1 on Windows (#160784)
1 parent 2b6f543 commit 0bfee22

3 files changed

Lines changed: 9 additions & 1 deletion

File tree

‎Lib/test/test_io/test_memoryio.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,12 @@ def __index__(self):
11501150
with self.assertRaises(ValueError):
11511151
stream.truncate(Size())
11521152

1153+
# Sizes above 2**31 - 1 must work where C long is 32-bit.
1154+
if sys.maxsize > 2**31:
1155+
stream = self.ioclass("abc")
1156+
self.assertEqual(stream.truncate(2**31), 2**31)
1157+
self.assertEqual(stream.getvalue(), "abc")
1158+
11531159
# XXX: For the Python version of io.StringIO, this is highly
11541160
# dependent on the encoding used for the underlying buffer.
11551161
def test_widechar(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :class:`io.StringIO` implementation of :meth:`io.IOBase.truncate`
2+
rejecting sizes larger than ``2**31 - 1`` on Windows.

‎Modules/_io/stringio.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ _io_StringIO_truncate_impl(stringio *self, PyObject *pos)
490490
size = self->pos;
491491
}
492492
else {
493-
size = PyLong_AsLong(pos);
493+
size = PyNumber_AsSsize_t(pos, PyExc_OverflowError);
494494
if (size == -1 && PyErr_Occurred()) {
495495
return NULL;
496496
}

0 commit comments

Comments
 (0)