aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/spec.md9
-rw-r--r--starlark/eval_test.go4
-rw-r--r--starlark/library.go25
3 files changed, 17 insertions, 21 deletions
diff --git a/doc/spec.md b/doc/spec.md
index 8acccdb..b8f7379 100644
--- a/doc/spec.md
+++ b/doc/spec.md
@@ -3055,14 +3055,17 @@ See also: `chr`.
### print
-`print(*args, **kwargs)` prints its arguments, followed by a newline.
-Arguments are formatted as if by `str(x)` and separated with a space.
+`print(*args, sep=" ")` prints its arguments, followed by a newline.
+Arguments are formatted as if by `str(x)` and separated with a space,
+unless an alternative separator is specified by a `sep` named argument.
Keyword arguments are preceded by their name.
Example:
```python
-print(1, "hi", x=3) # "1 hi x=3\n"
+print(1, "hi") # "1 hi\n"
+print("hello", "world") # "hello world\n"
+print("hello", "world", sep=", ") # "hello, world\n"
```
Typically the formatted string is printed to the standard error file,
diff --git a/starlark/eval_test.go b/starlark/eval_test.go
index 5c26e56..bd3a1b4 100644
--- a/starlark/eval_test.go
+++ b/starlark/eval_test.go
@@ -327,7 +327,7 @@ def f(a, b=42, *args, **kwargs):
func TestPrint(t *testing.T) {
const src = `
print("hello")
-def f(): print("world")
+def f(): print("hello", "world", sep=", ")
f()
`
buf := new(bytes.Buffer)
@@ -341,7 +341,7 @@ f()
t.Fatal(err)
}
want := "foo.go:2: <toplevel>: hello\n" +
- "foo.go:3: f: world\n"
+ "foo.go:3: f: hello, world\n"
if got := buf.String(); got != want {
t.Errorf("output was %s, want %s", got, want)
}
diff --git a/starlark/library.go b/starlark/library.go
index 906e152..edfdc6b 100644
--- a/starlark/library.go
+++ b/starlark/library.go
@@ -783,28 +783,21 @@ func ord(thread *Thread, _ *Builtin, args Tuple, kwargs []Tuple) (Value, error)
// https://github.com/google/starlark-go/blob/master/doc/spec.md#print
func print(thread *Thread, fn *Builtin, args Tuple, kwargs []Tuple) (Value, error) {
- var buf bytes.Buffer
+ sep := " "
+ if err := UnpackArgs("print", nil, kwargs, "sep?", &sep); err != nil {
+ return nil, err
+ }
path := make([]Value, 0, 4)
- sep := ""
- for _, v := range args {
- buf.WriteString(sep)
+ var buf bytes.Buffer
+ for i, v := range args {
+ if i > 0 {
+ buf.WriteString(sep)
+ }
if s, ok := AsString(v); ok {
buf.WriteString(s)
} else {
writeValue(&buf, v, path)
}
- sep = " "
- }
- for _, pair := range kwargs {
- buf.WriteString(sep)
- buf.WriteString(string(pair[0].(String)))
- buf.WriteString("=")
- if s, ok := AsString(pair[1]); ok {
- buf.WriteString(s)
- } else {
- writeValue(&buf, pair[1], path)
- }
- sep = " "
}
if thread.Print != nil {