@@ -683,7 +683,7 @@ func Print(f *Frame, args Args, nl bool) *BaseException {
683683 } else if len (args ) > 0 {
684684 end = " "
685685 }
686- return pyPrint (f , args , " " , end , Stdout )
686+ return pyPrint (f , args , " " , end , Stdout . ToObject () )
687687}
688688
689689// Repr returns a string containing a printable representation of o. This is
@@ -1258,29 +1258,38 @@ func hashNotImplemented(f *Frame, o *Object) (*Object, *BaseException) {
12581258}
12591259
12601260// pyPrint encapsulates the logic of the Python print function.
1261- func pyPrint (f * Frame , args Args , sep , end string , file * File ) * BaseException {
1261+ func pyPrint (f * Frame , args Args , sep , end string , filelike * Object ) * BaseException {
1262+ writeFunc , raised := GetAttr (f , filelike , NewStr ("write" ), nil )
1263+ if raised != nil {
1264+ return raised
1265+ }
1266+
1267+ pySep := NewStr (sep )
1268+ pyEnd := NewStr (end )
1269+ callArgs := f .MakeArgs (1 )
12621270 for i , arg := range args {
12631271 if i > 0 {
1264- err := file .writeString (sep )
1265- if err != nil {
1266- return f .RaiseType (IOErrorType , err .Error ())
1272+ callArgs [0 ] = pySep .ToObject ()
1273+ _ , raised := writeFunc .Call (f , callArgs , nil )
1274+ if raised != nil {
1275+ return raised
12671276 }
12681277 }
12691278
1270- s , raised := ToStr (f , arg )
1271- if raised != nil {
1272- return raised
1279+ s , raised_ := ToStr (f , arg )
1280+ if raised_ != nil {
1281+ return raised_
12731282 }
12741283
1275- err := file . writeString ( s . Value () )
1276- if err != nil {
1277- return f . RaiseType ( IOErrorType , err . Error ())
1284+ callArgs [ 0 ] = s . ToObject ( )
1285+ if _ , raised := writeFunc . Call ( f , callArgs , nil ); raised != nil {
1286+ return raised
12781287 }
12791288 }
12801289
1281- err := file . writeString ( end )
1282- if err != nil {
1283- return f . RaiseType ( IOErrorType , err . Error ())
1290+ callArgs [ 0 ] = pyEnd . ToObject ( )
1291+ if _ , raised := writeFunc . Call ( f , callArgs , nil ); raised != nil {
1292+ return raised
12841293 }
12851294
12861295 return nil
0 commit comments