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

Commit b2da45f

Browse files
corona10trotterdylan
authored andcommitted
Switch pyPrint() into based on *grumpy.File (#247)
1 parent 332739d commit b2da45f

6 files changed

Lines changed: 49 additions & 25 deletions

File tree

lib/sys.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
"""System-specific parameters and functions."""
1616

17-
from __go__.os import Args, Stdin, Stdout, Stderr
18-
from __go__.grumpy import SysModules, MaxInt, NewFileFromFD # pylint: disable=g-multiple-import
17+
from __go__.os import Args
18+
from __go__.grumpy import SysModules, MaxInt, Stdin as stdin, Stdout as stdout, Stderr as stderr # pylint: disable=g-multiple-import
1919
from __go__.runtime import Version
2020
from __go__.unicode import MaxRune
2121

@@ -33,11 +33,6 @@
3333
# TODO: Support actual byteorder
3434
byteorder = 'little'
3535

36-
stdin = NewFileFromFD(Stdin.Fd())
37-
stdout = NewFileFromFD(Stdout.Fd())
38-
stderr = NewFileFromFD(Stderr.Fd())
39-
40-
4136
class _Flags(object):
4237
"""Container class for sys.flags."""
4338
debug = 0

runtime/builtin_types.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"fmt"
1919
"math"
2020
"math/big"
21-
"os"
2221
"unicode"
2322
)
2423

@@ -528,7 +527,7 @@ func builtinOrd(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
528527
func builtinPrint(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
529528
sep := " "
530529
end := "\n"
531-
file := os.Stdout
530+
file := Stdout
532531
for _, kwarg := range kwargs {
533532
switch kwarg.Name {
534533
case "sep":

runtime/builtin_types_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,10 @@ func captureStdout(f *Frame, fn func() *BaseException) (string, *BaseException)
350350
if err != nil {
351351
return "", f.RaiseType(RuntimeErrorType, fmt.Sprintf("failed to open pipe: %v", err))
352352
}
353-
oldStdout := os.Stdout
354-
os.Stdout = w
353+
oldStdout := Stdout
354+
Stdout = NewFileFromFD(w.Fd())
355355
defer func() {
356-
os.Stdout = oldStdout
356+
Stdout = oldStdout
357357
}()
358358
done := make(chan struct{})
359359
var raised *BaseException

runtime/core.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package grumpy
1616

1717
import (
1818
"fmt"
19-
"io"
2019
"log"
2120
"os"
2221
"reflect"
@@ -641,7 +640,7 @@ func Print(f *Frame, args Args, nl bool) *BaseException {
641640
} else if len(args) > 0 {
642641
end = " "
643642
}
644-
return pyPrint(f, args, " ", end, os.Stdout)
643+
return pyPrint(f, args, " ", end, Stdout)
645644
}
646645

647646
// Repr returns a string containing a printable representation of o. This is
@@ -1216,17 +1215,30 @@ func hashNotImplemented(f *Frame, o *Object) (*Object, *BaseException) {
12161215
}
12171216

12181217
// pyPrint encapsulates the logic of the Python print function.
1219-
func pyPrint(f *Frame, args Args, sep, end string, file io.Writer) *BaseException {
1218+
func pyPrint(f *Frame, args Args, sep, end string, file *File) *BaseException {
12201219
for i, arg := range args {
12211220
if i > 0 {
1222-
fmt.Fprint(file, sep)
1221+
err := file.writeString(sep)
1222+
if err != nil {
1223+
return f.RaiseType(IOErrorType, err.Error())
1224+
}
12231225
}
1226+
12241227
s, raised := ToStr(f, arg)
12251228
if raised != nil {
12261229
return raised
12271230
}
1228-
fmt.Fprint(file, s.Value())
1231+
1232+
err := file.writeString(s.Value())
1233+
if err != nil {
1234+
return f.RaiseType(IOErrorType, err.Error())
1235+
}
12291236
}
1230-
fmt.Fprint(file, end)
1237+
1238+
err := file.writeString(end)
1239+
if err != nil {
1240+
return f.RaiseType(IOErrorType, err.Error())
1241+
}
1242+
12311243
return nil
12321244
}

runtime/core_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package grumpy
1616

1717
import (
18-
"bytes"
1918
"fmt"
2019
"math/big"
2120
"reflect"
@@ -790,12 +789,9 @@ func TestOct(t *testing.T) {
790789

791790
func TestPyPrint(t *testing.T) {
792791
fun := wrapFuncForTest(func(f *Frame, args *Tuple, sep, end string) (string, *BaseException) {
793-
var buf bytes.Buffer
794-
raised := pyPrint(NewRootFrame(), args.elems, sep, end, &buf)
795-
if raised != nil {
796-
return "", raised
797-
}
798-
return buf.String(), nil
792+
return captureStdout(f, func() *BaseException {
793+
return pyPrint(NewRootFrame(), args.elems, sep, end, Stdout)
794+
})
799795
})
800796
cases := []invokeTestCase{
801797
{args: wrapArgs(NewTuple(), "", "\n"), want: NewStr("\n").ToObject()},

runtime/file.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ func (f *File) readLine(maxBytes int) (string, error) {
8989
return buf.String(), nil
9090
}
9191

92+
func (f *File) writeString(s string) error {
93+
f.mutex.Lock()
94+
defer f.mutex.Unlock()
95+
if !f.open {
96+
return io.ErrClosedPipe
97+
}
98+
if _, err := f.file.Write([]byte(s)); err != nil {
99+
return err
100+
}
101+
102+
return nil
103+
}
104+
92105
// FileType is the object representing the Python 'file' type.
93106
var FileType = newBasisType("file", reflect.TypeOf(File{}), toFileUnsafe, ObjectType)
94107

@@ -342,3 +355,12 @@ func fileParseReadArgs(f *Frame, method string, args Args) (*File, int, *BaseExc
342355
}
343356
return toFileUnsafe(args[0]), size, nil
344357
}
358+
359+
var (
360+
// Stdin is an alias for sys.stdin.
361+
Stdin = NewFileFromFD(os.Stdin.Fd())
362+
// Stdout is an alias for sys.stdout.
363+
Stdout = NewFileFromFD(os.Stdout.Fd())
364+
// Stderr is an aliaas for sys.stderr.
365+
Stderr = NewFileFromFD(os.Stderr.Fd())
366+
)

0 commit comments

Comments
 (0)