You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
unsynchronized write pointer in io.TextIOWrapper in 'r+' mode #82891
Previously bpo-12215 and a couple of other duplicates were opened about this. Writing after reading with TextIOWrapper doesn't work as people expect. The report was closed apparently because Victor thought there wasn't enough interest in it.
FWIW the seek-then-write workaround will probably work for most common codecs, but would suffer the same problems discussed in bpo-26158 for codecs like UTF-7 and ISO-2022 that would need the encoder state constructed from the decoder state.
Activity
ManuelIgnacioPrezAlcolea commentedon Nov 6, 2019
There seems to be a bug in the
io.TextIOWrapperclass while working in 'r+' mode, although I can't say the source of the problem is right there.The write pointer doesn't match
file.tell()after performing a read operation.For example, this file, consisting of 3 lines:
line one
line two
line three
Doesn't result in the expected modification running the following program:
Instad of
line one
Hello
wo
line three
It results in
line one
line two
line threeHello
But it works just fine if
f.seek(f.tell())is added the program, right before the write operation.There are several possible explanations on StackOverflow, involving the buffering for IO in text files:
https://stackoverflow.com/a/58722058/11601118
vadmium commentedon Nov 17, 2019
Previously bpo-12215 and a couple of other duplicates were opened about this. Writing after reading with TextIOWrapper doesn't work as people expect. The report was closed apparently because Victor thought there wasn't enough interest in it.
FWIW the seek-then-write workaround will probably work for most common codecs, but would suffer the same problems discussed in bpo-26158 for codecs like UTF-7 and ISO-2022 that would need the encoder state constructed from the decoder state.