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
Bug description:
Since GH-157861, the first write of a
strsubclass instance into an emptyPyUnicodeWriterkeeps that object as the buffer, soPyUnicodeWriter_Finish()returns the subclass instance instead of astr. Inio.StringIO,getvalue()then returns the written object itself, and the next write re-reads it through its__str__(), which changes the contents:When
__str__()returns a shorter string, a laterread()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