FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

`io.StringIO` corrupts its contents after writing a `str` subclass · Issue #158213 · python/cpython · GitHub

Repository navigation

io.StringIO corrupts its contents after writing a str subclass #158213

Description

Bug description:

Since GH-157861, the first write of a str subclass instance into an empty PyUnicodeWriter keeps that object as the buffer, so PyUnicodeWriter_Finish() returns the subclass instance instead of a str. In io.StringIO, getvalue() then returns the written object itself, and the next write re-reads it through its __str__(), which changes the contents:

import enum, io

class Color(str, enum.Enum):
    RED = "red"

f = io.StringIO()
f.write(Color.RED)
print(repr(f.getvalue()))
f.write("!")
print(repr(f.getvalue()))
<Color.RED: 'red'>
'Color.RED!'

When __str__() returns a shorter string, a later read() reads past the end of the internal buffer.

Expected: 'red' and then 'red!', as on 3.15.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added a commit that references this issue on Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)topic-IOtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL