Skip to content

Commit 1e6930d

Browse files
committed
py: add convenience function Println
Signed-off-by: Sebastien Binet <[email protected]>
1 parent 6db2137 commit 1e6930d

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

py/util.go

+30
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package py
77
import (
88
"errors"
99
"strconv"
10+
"strings"
1011
)
1112

1213
var (
@@ -204,3 +205,32 @@ func loadValue(src Object, data interface{}) error {
204205
}
205206
return nil
206207
}
208+
209+
// Println prints the provided strings to gpython's stdout.
210+
func Println(self Object, args ...string) bool {
211+
sysModule, err := self.(*Module).Context.GetModule("sys")
212+
if err != nil {
213+
return false
214+
}
215+
stdout := sysModule.Globals["stdout"]
216+
write, err := GetAttrString(stdout, "write")
217+
if err != nil {
218+
return false
219+
}
220+
call, ok := write.(I__call__)
221+
if !ok {
222+
return false
223+
}
224+
for _, v := range args {
225+
if !strings.Contains(v, "\n") {
226+
v += " "
227+
}
228+
_, err := call.M__call__(Tuple{String(v)}, nil)
229+
if err != nil {
230+
return false
231+
}
232+
233+
}
234+
_, err = call.M__call__(Tuple{String("\n")}, nil) // newline
235+
return err == nil
236+
}

0 commit comments

Comments
 (0)