Repository navigation
gh-160781: Fix BytesIO overflow handling for large sizes - #160783
Conversation
BytesIO.write() at a position near sys.maxsize raised SystemError
("Negative size passed to PyBytes_FromStringAndSize") because the
overallocated buffer size could exceed PY_SSIZE_T_MAX. Check the
computed allocation, not only the requested size.
BytesIO.truncate() converted its argument with PyLong_AsLong(), which
rejects sizes above 2**31 - 1 on Windows where C long is 32-bit. Use
PyNumber_AsSsize_t() like the Py_ssize_t Argument Clinic converter it
used before.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
| @@ -0,0 +1,2 @@ | |||
| Fix :class:`io.BytesIO` implementation of :meth:`io.IOBase.truncate` rejecting | |||
There was a problem hiding this comment.
In the future, it's fine to handle this with StringIO and put them in the same news entry.
|
Thanks @cmaloney for the PR, and @StanFromIreland for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15. |
|
GH-160891 is a backport of this pull request to the 3.15 branch. |
|
GH-160892 is a backport of this pull request to the 3.14 branch. |
|
|
|
|
Fix for the test in #160903. |
BytesIO.write() at a position near sys.maxsize raised SystemError ("Negative size passed to PyBytes_FromStringAndSize") because the overallocated buffer size could exceed PY_SSIZE_T_MAX. Check the computed allocation, not only the requested size.
BytesIO.truncate() converted its argument with PyLong_AsLong(), which rejects sizes above 2**31 - 1 on Windows where C long is 32-bit. Use PyNumber_AsSsize_t() like the Py_ssize_t Argument Clinic converter it used before.