@@ -25,8 +25,8 @@ use crate::obj::objstr::{PyString, PyStringRef};
25
25
use crate :: obj:: objtype:: { self , PyClassRef } ;
26
26
use crate :: pyhash;
27
27
use crate :: pyobject:: {
28
- Either , IdProtocol , IntoPyObject , ItemProtocol , PyIterable , PyObjectRef , PyResult , PyValue ,
29
- TryFromObject , TypeProtocol ,
28
+ Either , IdProtocol , ItemProtocol , PyIterable , PyObjectRef , PyResult , PyValue , TryFromObject ,
29
+ TypeProtocol ,
30
30
} ;
31
31
use crate :: scope:: Scope ;
32
32
use crate :: stdlib:: ast;
@@ -598,13 +598,13 @@ pub struct PrintOptions {
598
598
}
599
599
600
600
trait Printer {
601
- fn write ( & mut self , vm : & VirtualMachine , obj : PyObjectRef ) -> PyResult < ( ) > ;
601
+ fn write ( & mut self , vm : & VirtualMachine , obj : PyStringRef ) -> PyResult < ( ) > ;
602
602
fn flush ( & mut self , vm : & VirtualMachine ) -> PyResult < ( ) > ;
603
603
}
604
604
605
605
impl Printer for & ' _ PyObjectRef {
606
- fn write ( & mut self , vm : & VirtualMachine , obj : PyObjectRef ) -> PyResult < ( ) > {
607
- vm. call_method ( self , "write" , vec ! [ obj] ) ?;
606
+ fn write ( & mut self , vm : & VirtualMachine , obj : PyStringRef ) -> PyResult < ( ) > {
607
+ vm. call_method ( self , "write" , vec ! [ obj. into_object ( ) ] ) ?;
608
608
Ok ( ( ) )
609
609
}
610
610
@@ -615,14 +615,13 @@ impl Printer for &'_ PyObjectRef {
615
615
}
616
616
617
617
impl Printer for std:: io:: StdoutLock < ' _ > {
618
- fn write ( & mut self , vm : & VirtualMachine , obj : PyObjectRef ) -> PyResult < ( ) > {
619
- let s = vm. to_str ( & obj) ?;
620
- write ! ( self , "{}" , s. as_str( ) ) . unwrap ( ) ;
618
+ fn write ( & mut self , _vm : & VirtualMachine , s : PyStringRef ) -> PyResult < ( ) > {
619
+ write ! ( self , "{}" , s) . unwrap ( ) ;
621
620
Ok ( ( ) )
622
621
}
623
622
624
623
fn flush ( & mut self , _vm : & VirtualMachine ) -> PyResult < ( ) > {
625
- <Self as std :: io:: Write >:: flush ( self ) . unwrap ( ) ;
624
+ <Self as io:: Write >:: flush ( self ) . unwrap ( ) ;
626
625
Ok ( ( ) )
627
626
}
628
627
}
@@ -643,10 +642,7 @@ pub fn builtin_print(objects: Args, options: PrintOptions, vm: &VirtualMachine)
643
642
644
643
let sep = options
645
644
. sep
646
- . as_ref ( )
647
- . map_or ( " " , |sep| sep. as_str ( ) )
648
- . into_pyobject ( vm)
649
- . unwrap ( ) ;
645
+ . unwrap_or_else ( || PyString :: from ( " " ) . into_ref ( vm) ) ;
650
646
651
647
let mut first = true ;
652
648
for object in objects {
@@ -656,15 +652,12 @@ pub fn builtin_print(objects: Args, options: PrintOptions, vm: &VirtualMachine)
656
652
printer. write ( vm, sep. clone ( ) ) ?;
657
653
}
658
654
659
- printer. write ( vm, object) ?;
655
+ printer. write ( vm, vm . to_str ( & object) ? ) ?;
660
656
}
661
657
662
658
let end = options
663
659
. end
664
- . as_ref ( )
665
- . map_or ( "\n " , |end| end. as_str ( ) )
666
- . into_pyobject ( vm)
667
- . unwrap ( ) ;
660
+ . unwrap_or_else ( || PyString :: from ( "\n " ) . into_ref ( vm) ) ;
668
661
printer. write ( vm, end) ?;
669
662
670
663
if options. flush . to_bool ( ) {
0 commit comments