Skip to content

[3.15] gh-160324: Keep io.StringIO state after MemoryError (GH-160325) - #161041

Merged
gpshead merged 3 commits into
python:3.15from
miss-islington:backport-83b047e-3.15
Nov 9, 2026
Merged

gpshead merged 3 commits into
python:3.15from
miss-islington:backport-83b047e-3.15

Conversation

@miss-islington

@miss-islington miss-islington commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Allocate before consuming the accumulated writes in realize() and
make_intermediate(), and reset the size when the contents are lost,
so that later calls do not index outside the internal buffer.

Shrink the buffer of the accumulating writer before finishing it, so
that getvalue(), read() and the other methods leave the contents
unchanged when they fail with MemoryError instead of dropping them.

ANT-2026-MPHGX2FL
(cherry picked from commit 83b047e)

Co-authored-by: Gregory P. Smith 68491+gpshead@users.noreply.github.com

…GH-160325)

Allocate before consuming the accumulated writes in realize() and
make_intermediate(), and reset the size when the contents are lost,
so that later calls do not index outside the internal buffer.

Shrink the buffer of the accumulating writer before finishing it, so
that getvalue(), read() and the other methods leave the contents
unchanged when they fail with MemoryError instead of dropping them.

ANT-2026-MPHGX2FL
(cherry picked from commit 83b047ef80836c24672cf3e2e807e68efed7d495)

Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
@hugovk

hugovk commented Nov 1, 2026

Copy link
Copy Markdown
Member
./Modules/_io/stringio.c:150:5: error: implicit declaration of function ‘_PyUnicodeWriter_SetBuffer’; did you mean ‘_PyUnicodeWriter_InitWithBuffer’? [-Wimplicit-function-declaration]

@gpshead _PyUnicodeWriter_SetBuffer was added in #158452 and hasn't been backported.

Same thing for the 3.14 backport: #161042.

@gpshead gpshead self-assigned this Nov 1, 2026
@gpshead
gpshead disabled auto-merge November 9, 2026 00:28
Update the writer fields directly instead.
In 3.15, writing a string to a new PyUnicodeWriter copies it, so a
MemoryError there lost the contents of the StringIO.  Disable
overallocation for this write so the writer shares the string
(copy-on-write) instead, as it does in main.
@github-project-automation github-project-automation Bot moved this from Todo to In Progress in Sprint Nov 9, 2026
@gpshead
gpshead enabled auto-merge (squash) November 9, 2026 03:20
@gpshead
gpshead merged commit ff1c6a1 into python:3.15 Nov 9, 2026
58 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Sprint Nov 9, 2026
@miss-islington
miss-islington deleted the backport-83b047e-3.15 branch November 9, 2026 03:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants