Skip to content

Commit fee2c03

Browse files
committed
runtime: flush on newline
1 parent 07db39d commit fee2c03

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

src/runtime/modules/IOModule.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3255,6 +3255,15 @@ class TextIOWrapper : public TextIOBase
32553255
// TODO: Should use encoder
32563256
m_pending_bytes = Bytes::from_unescaped_string(as<PyString>(text)->to_string());
32573257
m_pending_bytes_count = m_pending_bytes->b.size();
3258+
const auto should_flush = [this]() -> bool {
3259+
if (!m_line_buffering) { return false; }
3260+
if (std::ranges::find(m_pending_bytes->b, std::byte{ '\n' })
3261+
!= m_pending_bytes->b.end()) {
3262+
return true;
3263+
}
3264+
return std::ranges::find(m_pending_bytes->b, std::byte{ '\r' })
3265+
!= m_pending_bytes->b.end();
3266+
}();
32583267
auto result = writeflush();
32593268
while (result.is_ok()) {
32603269
ASSERT(m_pending_bytes_count >= result.unwrap());
@@ -3264,6 +3273,13 @@ class TextIOWrapper : public TextIOBase
32643273
}
32653274
if (result.is_err()) { return Err(result.unwrap_err()); }
32663275

3276+
if (should_flush) {
3277+
if (auto r = m_buffer->get_method(PyString::create("flush").unwrap())
3278+
.and_then([](PyObject *f) { return f->call(nullptr, nullptr); });
3279+
r.is_err()) {
3280+
return Err(r.unwrap_err());
3281+
}
3282+
}
32673283
return PyInteger::create(text_len);
32683284
}
32693285

0 commit comments

Comments
 (0)