Skip to content

Add OS module #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 24, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
py: add convenience function Println
Signed-off-by: Sebastien Binet <binet@cern.ch>
  • Loading branch information
sbinet committed May 9, 2022
commit 433aea80b1dce34aaaa6af09ec21d1b3eb535e53
30 changes: 30 additions & 0 deletions py/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package py
import (
"errors"
"strconv"
"strings"
)

var (
Expand Down Expand Up @@ -204,3 +205,32 @@ func loadValue(src Object, data interface{}) error {
}
return nil
}

// Println prints the provided strings to gpython's stdout.
func Println(self Object, args ...string) bool {
sysModule, err := self.(*Module).Context.GetModule("sys")
if err != nil {
return false
}
stdout := sysModule.Globals["stdout"]
write, err := GetAttrString(stdout, "write")
if err != nil {
return false
}
call, ok := write.(I__call__)
if !ok {
return false
}
for _, v := range args {
if !strings.Contains(v, "\n") {
v += " "
}
_, err := call.M__call__(Tuple{String(v)}, nil)
if err != nil {
return false
}

}
_, err = call.M__call__(Tuple{String("\n")}, nil) // newline
return err == nil
}