Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit b35a243

Browse files
committed
pyPrint prints to any filelike having .write()
1 parent c09c642 commit b35a243

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

runtime/builtin_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ func builtinPrint(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException)
562562
// to the file descriptor probably
563563
}
564564
}
565-
return nil, pyPrint(f, args, sep, end, file)
565+
return nil, pyPrint(f, args, sep, end, file.ToObject())
566566
}
567567

568568
func builtinRange(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
@@ -590,7 +590,7 @@ func builtinRawInput(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseExceptio
590590
}
591591

592592
if len(args) == 1 {
593-
err := pyPrint(f, args, "", "", Stdout)
593+
err := pyPrint(f, args, "", "", Stdout.ToObject())
594594
if err != nil {
595595
return nil, err
596596
}

runtime/core.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)