@@ -2959,8 +2959,7 @@ class TextIOWrapper : public TextIOBase
29592959
29602960 std::optional<Bytes> m_buffer_bytes;
29612961 size_t m_position{ 0 };
2962- std::optional<Bytes> m_pending_bytes;// bytes to be written
2963- size_t m_pending_bytes_count{ 0 };
2962+ Bytes m_pending_bytes;
29642963
29652964 size_t m_chunk_size{ 8192 };
29662965
@@ -3230,18 +3229,17 @@ class TextIOWrapper : public TextIOBase
32303229 return Ok (result);
32313230 }
32323231
3233- PyResult<size_t > writeflush ()
3232+ PyResult<std::monostate > writeflush ()
32343233 {
3235- if (! m_pending_bytes.has_value ()) { return Ok (0 ); }
3234+ if (m_pending_bytes.b . empty ()) { return Ok (std::monostate{} ); }
32363235 auto written =
32373236 m_buffer->get_method (PyString::create (" write" ).unwrap ())
32383237 .and_then ([this ](PyObject *write) {
3239- return write->call (PyTuple::create (m_pending_bytes. value () ).unwrap (), nullptr );
3238+ return write->call (PyTuple::create (m_pending_bytes).unwrap (), nullptr );
32403239 });
32413240 if (written.is_err ()) { return Err (written.unwrap_err ()); }
32423241 m_pending_bytes = {};
3243- m_pending_bytes_count = 0 ;
3244- return Ok (0 );
3242+ return Ok (std::monostate{});
32453243 }
32463244
32473245
@@ -3252,26 +3250,25 @@ class TextIOWrapper : public TextIOBase
32523250 auto *text = PyObject::from (args->elements ()[0 ]).unwrap ();
32533251 ASSERT (as<PyString>(text));
32543252 const auto text_len = as<PyString>(text)->size ();
3253+
32553254 // TODO: Should use encoder
3256- m_pending_bytes = Bytes::from_unescaped_string (as<PyString>(text)->to_string ());
3257- m_pending_bytes_count = m_pending_bytes->b .size ();
3255+ auto new_bytes = Bytes::from_unescaped_string (as<PyString>(text)->to_string ());
3256+ m_pending_bytes.b .insert (m_pending_bytes.b .end (), new_bytes.b .begin (), new_bytes.b .end ());
3257+
32583258 const auto should_flush = [this ]() -> bool {
32593259 if (!m_line_buffering) { return false ; }
3260- if (std::ranges::find (m_pending_bytes-> b , std::byte{ ' \n ' })
3261- != m_pending_bytes-> b .end ()) {
3260+ if (std::ranges::find (m_pending_bytes. b , std::byte{ ' \n ' })
3261+ != m_pending_bytes. b .end ()) {
32623262 return true ;
32633263 }
3264- return std::ranges::find (m_pending_bytes-> b , std::byte{ ' \r ' })
3265- != m_pending_bytes-> b .end ();
3264+ return std::ranges::find (m_pending_bytes. b , std::byte{ ' \r ' })
3265+ != m_pending_bytes. b .end ();
32663266 }();
3267- auto result = writeflush ();
3268- while (result.is_ok ()) {
3269- ASSERT (m_pending_bytes_count >= result.unwrap ());
3270- m_pending_bytes_count -= result.unwrap ();
3271- if (m_pending_bytes_count == 0 ) { break ; }
3272- result = writeflush ();
3267+
3268+ if (m_pending_bytes.b .size () >= m_chunk_size || should_flush || m_write_through) {
3269+ auto result = writeflush ();
3270+ if (result.is_err ()) { return Err (result.unwrap_err ()); }
32733271 }
3274- if (result.is_err ()) { return Err (result.unwrap_err ()); }
32753272
32763273 if (should_flush) {
32773274 if (auto r = m_buffer->get_method (PyString::create (" flush" ).unwrap ())
@@ -3337,8 +3334,7 @@ class TextIOWrapper : public TextIOBase
33373334
33383335 m_line_buffering = line_buffering;
33393336 m_write_through = write_through;
3340- m_newline = newline.value_or (" '\n " );
3341- m_pending_bytes_count = 0 ;
3337+ m_newline = newline.value_or (" \n " );
33423338
33433339 return Ok (1 );
33443340 }
0 commit comments