diff --git a/crates/vm/src/stdlib/io.rs b/crates/vm/src/stdlib/io.rs index 26d4a67c87a..df6bca81245 100644 --- a/crates/vm/src/stdlib/io.rs +++ b/crates/vm/src/stdlib/io.rs @@ -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 { diff --git a/extra_tests/snippets/stdlib_io.py b/extra_tests/snippets/stdlib_io.py index 25985e08968..93c083a90c1 100644 --- a/extra_tests/snippets/stdlib_io.py +++ b/extra_tests/snippets/stdlib_io.py @@ -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 @@ def readinto(self, b): 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"])