Skip to content

Commit ebe6cab

Browse files
committed
Using monkey type to get input
1 parent de4b483 commit ebe6cab

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

stdlib/builtin/builtin.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
package builtin
77

88
import (
9-
"bufio"
9+
"errors"
1010
"fmt"
1111
"io"
1212
"math/big"
1313
"strconv"
14+
"strings"
1415
"unicode/utf8"
1516

1617
"github.com/go-python/gpython/compile"
@@ -1242,24 +1243,23 @@ func builtin_input(self py.Object, args py.Tuple) (py.Object, error) {
12421243
}
12431244
}
12441245

1245-
file := stdin.(*py.File)
1246-
reader := bufio.NewReader(file.File)
1247-
line, err := reader.ReadString('\n')
1246+
readline, err := py.GetAttrString(stdin, "readline")
12481247
if err != nil {
1249-
if errors.Is(err, io.EOF) {
1250-
return nil, py.ExceptionNewf(py.EOFError, "EOF when reading a line")
1251-
}
12521248
return nil, err
12531249
}
1254-
1255-
if len(line) > 0 && line[len(line)-1] == '\n' {
1256-
line = line[:len(line)-1]
1257-
if len(line) > 0 && line[len(line)-1] == '\r' {
1258-
line = line[:len(line)-1]
1259-
}
1250+
result, err := py.Call(readline, nil, nil)
1251+
if err != nil {
1252+
return nil, err
12601253
}
1261-
1262-
return py.String(line), nil
1254+
line, ok := result.(py.String)
1255+
if !ok {
1256+
return nil, py.ExceptionNewf(py.TypeError, "object.readline() should return a str object, got %s", result.Type().Name)
1257+
}
1258+
if line == "" {
1259+
return nil, py.ExceptionNewf(py.EOFError, "EOF when reading a line")
1260+
}
1261+
line = py.String(strings.TrimRight(string(line), "\r\n"))
1262+
return line, nil
12631263
}
12641264

12651265
const locals_doc = `locals() -> dictionary

0 commit comments

Comments
 (0)