|
6 | 6 | package builtin |
7 | 7 |
|
8 | 8 | import ( |
9 | | - "bufio" |
| 9 | + "errors" |
10 | 10 | "fmt" |
11 | 11 | "io" |
12 | 12 | "math/big" |
13 | 13 | "strconv" |
| 14 | + "strings" |
14 | 15 | "unicode/utf8" |
15 | 16 |
|
16 | 17 | "github.com/go-python/gpython/compile" |
@@ -1242,24 +1243,23 @@ func builtin_input(self py.Object, args py.Tuple) (py.Object, error) { |
1242 | 1243 | } |
1243 | 1244 | } |
1244 | 1245 |
|
1245 | | - file := stdin.(*py.File) |
1246 | | - reader := bufio.NewReader(file.File) |
1247 | | - line, err := reader.ReadString('\n') |
| 1246 | + readline, err := py.GetAttrString(stdin, "readline") |
1248 | 1247 | if err != nil { |
1249 | | - if errors.Is(err, io.EOF) { |
1250 | | - return nil, py.ExceptionNewf(py.EOFError, "EOF when reading a line") |
1251 | | - } |
1252 | 1248 | return nil, err |
1253 | 1249 | } |
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 |
1260 | 1253 | } |
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 |
1263 | 1263 | } |
1264 | 1264 |
|
1265 | 1265 | const locals_doc = `locals() -> dictionary |
|
0 commit comments