File tree 1 file changed +8
-11
lines changed 1 file changed +8
-11
lines changed Original file line number Diff line number Diff line change @@ -467,6 +467,8 @@ fn text_io_base_read(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
467
467
}
468
468
469
469
fn text_io_base_write ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
470
+ use std:: str:: from_utf8;
471
+
470
472
arg_check ! (
471
473
vm,
472
474
args,
@@ -481,24 +483,19 @@ fn text_io_base_write(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
481
483
return Err ( vm. new_value_error ( "not writable" . to_string ( ) ) ) ;
482
484
}
483
485
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) ?;
488
486
let bytes = objstr:: get_value ( obj) . into_bytes ( ) ;
489
487
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( ) ) ] ) ?;
494
489
let len = objint:: get_value ( & len) . to_usize ( ) . ok_or_else ( || {
495
490
vm. new_overflow_error ( "int to large to convert to Rust usize" . to_string ( ) )
496
491
} ) ?;
497
492
498
493
// 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) )
502
499
}
503
500
504
501
fn split_mode_string ( mode_string : String ) -> Result < ( String , String ) , String > {
You can’t perform that action at this time.
0 commit comments