Commit 6ab26e7f authored by Chris Toshok's avatar Chris Toshok

Merge pull request #500 from rudi-c/builtin_input

Implement built-in input() function.
parents e9090a32 9d4bf009
......@@ -1012,8 +1012,31 @@ Box* rawInput(Box* prompt) {
}
Box* input(Box* prompt) {
fatalOrError(PyExc_NotImplementedError, "unimplemented");
throwCAPIException();
char* str;
PyObject* line = raw_input(prompt);
if (line == NULL)
throwCAPIException();
if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
throwCAPIException();
// CPython trims the string first, but our eval function takes care of that.
// while (*str == ' ' || *str == '\t')
// str++;
Box* gbls = globals();
Box* lcls = locals();
// CPython has these safety checks that the builtin functions exist
// in the current global scope.
// e.g. eval('input()', {})
if (PyDict_GetItemString(gbls, "__builtins__") == NULL) {
if (PyDict_SetItemString(gbls, "__builtins__", builtins_module) != 0)
throwCAPIException();
}
return eval(line, gbls, lcls);
}
Box* builtinRound(Box* _number, Box* _ndigits) {
......
import StringIO
import sys
success_tests = ["1234", # just a number
" 123", # whitespaces get trimmed
"str(5) + \"6\"" # test for builtin function
]
special_tests = ["str(10)"]
failure_tests = ["abcd"]
orig_stdin = sys.stdin
sio = StringIO.StringIO("\n".join(success_tests + special_tests + failure_tests))
sys.stdin = sio
for _ in success_tests:
print repr(input())
# Special test: if the globals is empty, __builtin__ should be added to it
# in the call to input().
print repr(eval("input()", {}))
try:
print repr(input())
except NameError:
print "caught expected syntax error"
sys.stdin = orig_stdin
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment