Skip to content

Commit f4be2e5

Browse files
authored
[3.15] gh-160781: Fix StringIO.truncate() rejecting sizes above 2**31 - 1 on Windows (GH-160784) (#160918)
(cherry picked from commit 0bfee22)
1 parent 16b6719 commit f4be2e5

3 files changed

Lines changed: 10 additions & 1 deletion

File tree

‎Lib/test/test_io/test_memoryio.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,13 @@ class CStringIOTest(PyStringIOTest):
10041004
ioclass = io.StringIO
10051005
UnsupportedOperation = io.UnsupportedOperation
10061006

1007+
def test_truncate_large_size(self):
1008+
# Sizes above 2**31 - 1 must work where C long is 32-bit.
1009+
if sys.maxsize > 2**31:
1010+
stream = self.ioclass("abc")
1011+
self.assertEqual(stream.truncate(2**31), 2**31)
1012+
self.assertEqual(stream.getvalue(), "abc")
1013+
10071014
# XXX: For the Python version of io.StringIO, this is highly
10081015
# dependent on the encoding used for the underlying buffer.
10091016
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
@@ -469,7 +469,7 @@ _io_StringIO_truncate_impl(stringio *self, PyObject *pos)
469469
size = self->pos;
470470
}
471471
else {
472-
size = PyLong_AsLong(pos);
472+
size = PyNumber_AsSsize_t(pos, PyExc_OverflowError);
473473
if (size == -1 && PyErr_Occurred()) {
474474
return NULL;
475475
}

0 commit comments

Comments
 (0)