-
Notifications
You must be signed in to change notification settings - Fork 100
py: implement input #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
py: implement input #252
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,9 @@ | |
| package builtin | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "io" | ||
| "math/big" | ||
| "strconv" | ||
| "unicode/utf8" | ||
|
|
@@ -44,7 +46,7 @@ func init() { | |
| // py.MustNewMethod("hash", builtin_hash, 0, hash_doc), | ||
| py.MustNewMethod("hex", builtin_hex, 0, hex_doc), | ||
| // py.MustNewMethod("id", builtin_id, 0, id_doc), | ||
| // py.MustNewMethod("input", builtin_input, 0, input_doc), | ||
| py.MustNewMethod("input", builtin_input, 0, input_doc), | ||
| py.MustNewMethod("isinstance", builtin_isinstance, 0, isinstance_doc), | ||
| // py.MustNewMethod("issubclass", builtin_issubclass, 0, issubclass_doc), | ||
| py.MustNewMethod("iter", builtin_iter, 0, iter_doc), | ||
|
|
@@ -1181,6 +1183,82 @@ func builtin_chr(self py.Object, args py.Tuple) (py.Object, error) { | |
| return py.String(buf[:n]), nil | ||
| } | ||
|
|
||
| const input_doc = `input([prompt]) -> string | ||
|
|
||
| Read a string from standard input. The trailing newline is stripped. | ||
| The prompt string, if given, is printed to standard output without a | ||
| trailing newline before reading input. | ||
| If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError. | ||
| On *nix systems, GNU readline is used if enabled.` | ||
|
|
||
| func builtin_input(self py.Object, args py.Tuple) (py.Object, error) { | ||
| var prompt py.Object = py.None | ||
|
|
||
| err := py.UnpackTuple(args, nil, "input", 0, 1, &prompt) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Use InputHook if available (e.g., in REPL mode) | ||
| if py.InputHook != nil { | ||
| promptStr := "" | ||
| if prompt != py.None { | ||
| promptStr = string(prompt.(py.String)) | ||
| } | ||
|
Comment on lines
+1205
to
+1207
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't we use the |
||
| line, err := py.InputHook(promptStr) | ||
| if err != nil { | ||
| if err == io.EOF { | ||
| return nil, py.ExceptionNewf(py.EOFError, "EOF when reading a line") | ||
| } | ||
|
Comment on lines
+1210
to
+1212
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perhaps we should prefer the |
||
| return nil, err | ||
| } | ||
| return py.String(line), nil | ||
| } | ||
|
|
||
| sysModule, err := self.(*py.Module).Context.GetModule("sys") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| stdin := sysModule.Globals["stdin"] | ||
| stdout := sysModule.Globals["stdout"] | ||
|
|
||
| if prompt != py.None { | ||
| write, err := py.GetAttrString(stdout, "write") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| _, err = py.Call(write, py.Tuple{prompt}, nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| flush, err := py.GetAttrString(stdout, "flush") | ||
| if err == nil { | ||
| py.Call(flush, nil, nil) | ||
| } | ||
| } | ||
|
Comment on lines
+1226
to
+1240
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do you go through the |
||
|
|
||
| file := stdin.(*py.File) | ||
| reader := bufio.NewReader(file.File) | ||
| line, err := reader.ReadString('\n') | ||
| if err != nil { | ||
| if err.Error() == "EOF" { | ||
| return nil, py.ExceptionNewf(py.EOFError, "EOF when reading a line") | ||
| } | ||
|
Comment on lines
+1246
to
+1248
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't we use the |
||
| return nil, err | ||
| } | ||
|
|
||
| if len(line) > 0 && line[len(line)-1] == '\n' { | ||
| line = line[:len(line)-1] | ||
| if len(line) > 0 && line[len(line)-1] == '\r' { | ||
| line = line[:len(line)-1] | ||
| } | ||
| } | ||
|
|
||
| return py.String(line), nil | ||
| } | ||
|
|
||
| const locals_doc = `locals() -> dictionary | ||
|
|
||
| Update and return a dictionary containing the current scope's local variables.` | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that probably isn't true for gpython, is it ?