File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -467,6 +467,8 @@ fn text_io_base_read(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
467467}
468468
469469fn text_io_base_write ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
470+ use std:: str:: from_utf8;
471+
470472 arg_check ! (
471473 vm,
472474 args,
@@ -481,24 +483,19 @@ fn text_io_base_write(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
481483 return Err ( vm. new_value_error ( "not writable" . to_string ( ) ) ) ;
482484 }
483485
484- let write = vm
485- . get_method ( raw. clone ( ) , "write" )
486- . ok_or_else ( || vm. new_attribute_error ( "BufferedWriter has no write method" . to_owned ( ) ) )
487- . and_then ( |it| it) ?;
488486 let bytes = objstr:: get_value ( obj) . into_bytes ( ) ;
489487
490- let len = vm. invoke (
491- write,
492- PyFuncArgs :: new ( vec ! [ vm. ctx. new_bytes( bytes. clone( ) ) ] , vec ! [ ] ) ,
493- ) ?;
488+ let len = vm. call_method ( & raw , "write" , vec ! [ vm. ctx. new_bytes( bytes. clone( ) ) ] ) ?;
494489 let len = objint:: get_value ( & len) . to_usize ( ) . ok_or_else ( || {
495490 vm. new_overflow_error ( "int to large to convert to Rust usize" . to_string ( ) )
496491 } ) ?;
497492
498493 // returns the count of unicode code points written
499- Ok ( vm
500- . ctx
501- . new_int ( String :: from_utf8_lossy ( & bytes[ 0 ..len] ) . chars ( ) . count ( ) ) )
494+ let len = from_utf8 ( & bytes[ ..len] )
495+ . unwrap_or_else ( |e| from_utf8 ( & bytes[ ..e. valid_up_to ( ) ] ) . unwrap ( ) )
496+ . chars ( )
497+ . count ( ) ;
498+ Ok ( vm. ctx . new_int ( len) )
502499}
503500
504501fn split_mode_string ( mode_string : String ) -> Result < ( String , String ) , String > {
You can’t perform that action at this time.
0 commit comments