Skip to content

Commit ca390dc

Browse files
authored
io.TextIoWrapper.reconfigure to always flush (#7281)
* Always flush on reconfigure (io.TextWrapper) * Add test
1 parent a6b3a5b commit ca390dc

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

crates/vm/src/stdlib/io.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3029,7 +3029,6 @@ mod _io {
30293029
let mut newline_changed = false;
30303030
let mut line_buffering = None;
30313031
let mut write_through = None;
3032-
let mut flush_on_reconfigure = false;
30333032

30343033
if let Some(enc) = args.encoding {
30353034
if enc.as_str().contains('\0') && enc.as_str().starts_with("locale") {
@@ -3056,11 +3055,9 @@ mod _io {
30563055
}
30573056

30583057
if let OptionalArg::Present(Some(value)) = args.line_buffering {
3059-
flush_on_reconfigure = true;
30603058
line_buffering = Some(Self::bool_from_index(value, vm)?);
30613059
}
30623060
if let OptionalArg::Present(Some(value)) = args.write_through {
3063-
flush_on_reconfigure = true;
30643061
write_through = Some(Self::bool_from_index(value, vm)?);
30653062
}
30663063

@@ -3077,12 +3074,10 @@ mod _io {
30773074
));
30783075
}
30793076

3080-
if flush_on_reconfigure {
3081-
if data.pending.num_bytes > 0 {
3082-
data.write_pending(vm)?;
3083-
}
3084-
vm.call_method(&data.buffer, "flush", ())?;
3077+
if data.pending.num_bytes > 0 {
3078+
data.write_pending(vm)?;
30853079
}
3080+
vm.call_method(&data.buffer, "flush", ())?;
30863081

30873082
if encoding_changed || errors_changed || newline_changed {
30883083
if data.pending.num_bytes > 0 {

extra_tests/snippets/stdlib_io.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from io import BufferedReader, BytesIO, FileIO, RawIOBase, StringIO
2+
from io import BufferedReader, BytesIO, FileIO, RawIOBase, StringIO, TextIOWrapper
33

44
from testutils import assert_raises
55

@@ -58,3 +58,29 @@ def readinto(self, b):
5858

5959
with assert_raises(ValueError):
6060
f.isatty()
61+
62+
63+
class Gh6588:
64+
def __init__(self):
65+
self.textio = None
66+
self.closed = False
67+
68+
def writable(self):
69+
return True
70+
71+
def readable(self):
72+
return False
73+
74+
def seekable(self):
75+
return False
76+
77+
def write(self, data):
78+
self.textio.reconfigure(encoding="utf-8")
79+
return len(data)
80+
81+
82+
raw = Gh6588()
83+
textio = TextIOWrapper(raw, encoding="utf-8", write_through=True)
84+
raw.textio = textio
85+
with assert_raises(AttributeError):
86+
textio.writelines(["x"])

0 commit comments

Comments
 (0)