io.TextIoWrapper.reconfigure to always flush (#7281)

* Always flush on reconfigure (io.TextWrapper)

* Add test
This commit is contained in:
Shahar Naveh
2026-03-01 17:03:44 +01:00
committed by GitHub
parent a6b3a5b6bc
commit ca390dc5fb
2 changed files with 30 additions and 9 deletions

View File

@@ -3029,7 +3029,6 @@ mod _io {
let mut newline_changed = false;
let mut line_buffering = None;
let mut write_through = None;
let mut flush_on_reconfigure = false;
if let Some(enc) = args.encoding {
if enc.as_str().contains('\0') && enc.as_str().starts_with("locale") {
@@ -3056,11 +3055,9 @@ mod _io {
}
if let OptionalArg::Present(Some(value)) = args.line_buffering {
flush_on_reconfigure = true;
line_buffering = Some(Self::bool_from_index(value, vm)?);
}
if let OptionalArg::Present(Some(value)) = args.write_through {
flush_on_reconfigure = true;
write_through = Some(Self::bool_from_index(value, vm)?);
}
@@ -3077,12 +3074,10 @@ mod _io {
));
}
if flush_on_reconfigure {
if data.pending.num_bytes > 0 {
data.write_pending(vm)?;
}
vm.call_method(&data.buffer, "flush", ())?;
if data.pending.num_bytes > 0 {
data.write_pending(vm)?;
}
vm.call_method(&data.buffer, "flush", ())?;
if encoding_changed || errors_changed || newline_changed {
if data.pending.num_bytes > 0 {

View File

@@ -1,5 +1,5 @@
import os
from io import BufferedReader, BytesIO, FileIO, RawIOBase, StringIO
from io import BufferedReader, BytesIO, FileIO, RawIOBase, StringIO, TextIOWrapper
from testutils import assert_raises
@@ -58,3 +58,29 @@ assert f.closed
with assert_raises(ValueError):
f.isatty()
class Gh6588:
def __init__(self):
self.textio = None
self.closed = False
def writable(self):
return True
def readable(self):
return False
def seekable(self):
return False
def write(self, data):
self.textio.reconfigure(encoding="utf-8")
return len(data)
raw = Gh6588()
textio = TextIOWrapper(raw, encoding="utf-8", write_through=True)
raw.textio = textio
with assert_raises(AttributeError):
textio.writelines(["x"])