|
6 | 6 | package builtin |
7 | 7 |
|
8 | 8 | import ( |
| 9 | + "bufio" |
9 | 10 | "fmt" |
| 11 | + "io" |
10 | 12 | "math/big" |
11 | 13 | "strconv" |
12 | 14 | "unicode/utf8" |
@@ -44,7 +46,7 @@ func init() { |
44 | 46 | // py.MustNewMethod("hash", builtin_hash, 0, hash_doc), |
45 | 47 | py.MustNewMethod("hex", builtin_hex, 0, hex_doc), |
46 | 48 | // py.MustNewMethod("id", builtin_id, 0, id_doc), |
47 | | - // py.MustNewMethod("input", builtin_input, 0, input_doc), |
| 49 | + py.MustNewMethod("input", builtin_input, 0, input_doc), |
48 | 50 | py.MustNewMethod("isinstance", builtin_isinstance, 0, isinstance_doc), |
49 | 51 | // py.MustNewMethod("issubclass", builtin_issubclass, 0, issubclass_doc), |
50 | 52 | py.MustNewMethod("iter", builtin_iter, 0, iter_doc), |
@@ -1181,6 +1183,82 @@ func builtin_chr(self py.Object, args py.Tuple) (py.Object, error) { |
1181 | 1183 | return py.String(buf[:n]), nil |
1182 | 1184 | } |
1183 | 1185 |
|
| 1186 | +const input_doc = `input([prompt]) -> string |
| 1187 | +
|
| 1188 | +Read a string from standard input. The trailing newline is stripped. |
| 1189 | +The prompt string, if given, is printed to standard output without a |
| 1190 | +trailing newline before reading input. |
| 1191 | +If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError. |
| 1192 | +On *nix systems, GNU readline is used if enabled.` |
| 1193 | + |
| 1194 | +func builtin_input(self py.Object, args py.Tuple) (py.Object, error) { |
| 1195 | + var prompt py.Object = py.None |
| 1196 | + |
| 1197 | + err := py.UnpackTuple(args, nil, "input", 0, 1, &prompt) |
| 1198 | + if err != nil { |
| 1199 | + return nil, err |
| 1200 | + } |
| 1201 | + |
| 1202 | + // Use InputHook if available (e.g., in REPL mode) |
| 1203 | + if py.InputHook != nil { |
| 1204 | + promptStr := "" |
| 1205 | + if prompt != py.None { |
| 1206 | + promptStr = string(prompt.(py.String)) |
| 1207 | + } |
| 1208 | + line, err := py.InputHook(promptStr) |
| 1209 | + if err != nil { |
| 1210 | + if err == io.EOF { |
| 1211 | + return nil, py.ExceptionNewf(py.EOFError, "EOF when reading a line") |
| 1212 | + } |
| 1213 | + return nil, err |
| 1214 | + } |
| 1215 | + return py.String(line), nil |
| 1216 | + } |
| 1217 | + |
| 1218 | + sysModule, err := self.(*py.Module).Context.GetModule("sys") |
| 1219 | + if err != nil { |
| 1220 | + return nil, err |
| 1221 | + } |
| 1222 | + |
| 1223 | + stdin := sysModule.Globals["stdin"] |
| 1224 | + stdout := sysModule.Globals["stdout"] |
| 1225 | + |
| 1226 | + if prompt != py.None { |
| 1227 | + write, err := py.GetAttrString(stdout, "write") |
| 1228 | + if err != nil { |
| 1229 | + return nil, err |
| 1230 | + } |
| 1231 | + _, err = py.Call(write, py.Tuple{prompt}, nil) |
| 1232 | + if err != nil { |
| 1233 | + return nil, err |
| 1234 | + } |
| 1235 | + |
| 1236 | + flush, err := py.GetAttrString(stdout, "flush") |
| 1237 | + if err == nil { |
| 1238 | + py.Call(flush, nil, nil) |
| 1239 | + } |
| 1240 | + } |
| 1241 | + |
| 1242 | + file := stdin.(*py.File) |
| 1243 | + reader := bufio.NewReader(file.File) |
| 1244 | + line, err := reader.ReadString('\n') |
| 1245 | + if err != nil { |
| 1246 | + if err.Error() == "EOF" { |
| 1247 | + return nil, py.ExceptionNewf(py.EOFError, "EOF when reading a line") |
| 1248 | + } |
| 1249 | + return nil, err |
| 1250 | + } |
| 1251 | + |
| 1252 | + if len(line) > 0 && line[len(line)-1] == '\n' { |
| 1253 | + line = line[:len(line)-1] |
| 1254 | + if len(line) > 0 && line[len(line)-1] == '\r' { |
| 1255 | + line = line[:len(line)-1] |
| 1256 | + } |
| 1257 | + } |
| 1258 | + |
| 1259 | + return py.String(line), nil |
| 1260 | +} |
| 1261 | + |
1184 | 1262 | const locals_doc = `locals() -> dictionary |
1185 | 1263 |
|
1186 | 1264 | Update and return a dictionary containing the current scope's local variables.` |
|
0 commit comments