Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd65e3f

Browse files
committed
raw_input() -> input(). old input behavior is history (and test_builtin passes again). It was failing due to future division.
1 parent ac3625f commit cd65e3f

2 files changed

Lines changed: 81 additions & 132 deletions

File tree

Lib/test/test_builtin.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -658,8 +658,6 @@ def test_id(self):
658658
id([0,1,2,3])
659659
id({'spam': 1, 'eggs': 2, 'ham': 3})
660660

661-
# Test input() later, together with raw_input
662-
663661
def test_int(self):
664662
self.assertEqual(int(314), 314)
665663
self.assertEqual(int(3.14), 3)
@@ -1108,7 +1106,7 @@ def test_oct(self):
11081106
self.assertRaises(TypeError, oct, ())
11091107

11101108
def write_testfile(self):
1111-
# NB the first 4 lines are also used to test input and raw_input, below
1109+
# NB the first 4 lines are also used to test input, below
11121110
fp = open(TESTFN, 'w')
11131111
try:
11141112
fp.write('1+1\n')
@@ -1267,37 +1265,26 @@ def __cmp__(self, other):
12671265
self.assertRaises(OverflowError, range, -sys.maxint, sys.maxint)
12681266
self.assertRaises(OverflowError, range, 0, 2*sys.maxint)
12691267

1270-
def test_input_and_raw_input(self):
1268+
def test_input(self):
12711269
self.write_testfile()
12721270
fp = open(TESTFN, 'r')
12731271
savestdin = sys.stdin
12741272
savestdout = sys.stdout # Eats the echo
12751273
try:
12761274
sys.stdin = fp
12771275
sys.stdout = BitBucket()
1278-
self.assertEqual(input(), 2)
1279-
self.assertEqual(input('testing\n'), 2)
1280-
self.assertEqual(raw_input(), 'The quick brown fox jumps over the lazy dog.')
1281-
self.assertEqual(raw_input('testing\n'), 'Dear John')
1276+
self.assertEqual(input(), '1+1')
1277+
self.assertEqual(input('testing\n'), '1+1')
1278+
self.assertEqual(input(), 'The quick brown fox jumps over the lazy dog.')
1279+
self.assertEqual(input('testing\n'), 'Dear John')
12821280
sys.stdin = cStringIO.StringIO("NULL\0")
12831281
self.assertRaises(TypeError, input, 42, 42)
1284-
sys.stdin = cStringIO.StringIO(" 'whitespace'")
1285-
self.assertEqual(input(), 'whitespace')
1282+
whitespace = " 'whitespace'"
1283+
sys.stdin = cStringIO.StringIO(whitespace)
1284+
self.assertEqual(input(), whitespace)
12861285
sys.stdin = cStringIO.StringIO()
12871286
self.assertRaises(EOFError, input)
12881287

1289-
# SF 876178: make sure input() respect future options.
1290-
sys.stdin = cStringIO.StringIO('1/2')
1291-
sys.stdout = cStringIO.StringIO()
1292-
exec compile('print input()', 'test_builtin_tmp', 'exec')
1293-
sys.stdin.seek(0, 0)
1294-
exec compile('from __future__ import division;print input()',
1295-
'test_builtin_tmp', 'exec')
1296-
sys.stdin.seek(0, 0)
1297-
exec compile('print input()', 'test_builtin_tmp', 'exec')
1298-
self.assertEqual(sys.stdout.getvalue().splitlines(),
1299-
['0', '0.5', '0'])
1300-
13011288
del sys.stdout
13021289
self.assertRaises(RuntimeError, input, 'prompt')
13031290
del sys.stdin

Python/bltinmodule.c

Lines changed: 72 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,42 +1073,89 @@ PyDoc_STRVAR(hex_doc,
10731073
Return the hexadecimal representation of an integer or long integer.");
10741074

10751075

1076-
static PyObject *builtin_raw_input(PyObject *, PyObject *);
1077-
10781076
static PyObject *
10791077
builtin_input(PyObject *self, PyObject *args)
10801078
{
1081-
PyObject *line;
1082-
char *str;
1083-
PyObject *res;
1084-
PyObject *globals, *locals;
1085-
PyCompilerFlags cf;
1079+
PyObject *v = NULL;
1080+
PyObject *fin = PySys_GetObject("stdin");
1081+
PyObject *fout = PySys_GetObject("stdout");
10861082

1087-
line = builtin_raw_input(self, args);
1088-
if (line == NULL)
1089-
return line;
1090-
if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
1083+
if (!PyArg_UnpackTuple(args, "input", 0, 1, &v))
10911084
return NULL;
1092-
while (*str == ' ' || *str == '\t')
1093-
str++;
1094-
globals = PyEval_GetGlobals();
1095-
locals = PyEval_GetLocals();
1096-
if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
1097-
if (PyDict_SetItemString(globals, "__builtins__",
1098-
PyEval_GetBuiltins()) != 0)
1085+
1086+
if (fin == NULL) {
1087+
PyErr_SetString(PyExc_RuntimeError, "input: lost sys.stdin");
1088+
return NULL;
1089+
}
1090+
if (fout == NULL) {
1091+
PyErr_SetString(PyExc_RuntimeError, "input: lost sys.stdout");
1092+
return NULL;
1093+
}
1094+
if (PyFile_SoftSpace(fout, 0)) {
1095+
if (PyFile_WriteString(" ", fout) != 0)
10991096
return NULL;
11001097
}
1101-
cf.cf_flags = 0;
1102-
PyEval_MergeCompilerFlags(&cf);
1103-
res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
1104-
Py_DECREF(line);
1105-
return res;
1098+
if (PyFile_Check(fin) && PyFile_Check(fout)
1099+
&& isatty(fileno(PyFile_AsFile(fin)))
1100+
&& isatty(fileno(PyFile_AsFile(fout)))) {
1101+
PyObject *po;
1102+
char *prompt;
1103+
char *s;
1104+
PyObject *result;
1105+
if (v != NULL) {
1106+
po = PyObject_Str(v);
1107+
if (po == NULL)
1108+
return NULL;
1109+
prompt = PyString_AsString(po);
1110+
if (prompt == NULL)
1111+
return NULL;
1112+
}
1113+
else {
1114+
po = NULL;
1115+
prompt = "";
1116+
}
1117+
s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
1118+
prompt);
1119+
Py_XDECREF(po);
1120+
if (s == NULL) {
1121+
if (!PyErr_Occurred())
1122+
PyErr_SetNone(PyExc_KeyboardInterrupt);
1123+
return NULL;
1124+
}
1125+
if (*s == '\0') {
1126+
PyErr_SetNone(PyExc_EOFError);
1127+
result = NULL;
1128+
}
1129+
else { /* strip trailing '\n' */
1130+
size_t len = strlen(s);
1131+
if (len > INT_MAX) {
1132+
PyErr_SetString(PyExc_OverflowError,
1133+
"[raw_]input: input too long");
1134+
result = NULL;
1135+
}
1136+
else {
1137+
result = PyString_FromStringAndSize(s,
1138+
(int)(len-1));
1139+
}
1140+
}
1141+
PyMem_FREE(s);
1142+
return result;
1143+
}
1144+
if (v != NULL) {
1145+
if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
1146+
return NULL;
1147+
}
1148+
return PyFile_GetLine(fin, -1);
11061149
}
11071150

11081151
PyDoc_STRVAR(input_doc,
1109-
"input([prompt]) -> value\n\
1152+
"input([prompt]) -> string\n\
11101153
\n\
1111-
Equivalent to eval(raw_input(prompt)).");
1154+
Read a string from standard input. The trailing newline is stripped.\n\
1155+
If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1156+
On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1157+
is printed without a trailing newline before reading.");
1158+
11121159

11131160

11141161
static PyObject *
@@ -1686,90 +1733,6 @@ For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
16861733
These are exactly the valid indices for a list of 4 elements.");
16871734

16881735

1689-
static PyObject *
1690-
builtin_raw_input(PyObject *self, PyObject *args)
1691-
{
1692-
PyObject *v = NULL;
1693-
PyObject *fin = PySys_GetObject("stdin");
1694-
PyObject *fout = PySys_GetObject("stdout");
1695-
1696-
if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
1697-
return NULL;
1698-
1699-
if (fin == NULL) {
1700-
PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
1701-
return NULL;
1702-
}
1703-
if (fout == NULL) {
1704-
PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
1705-
return NULL;
1706-
}
1707-
if (PyFile_SoftSpace(fout, 0)) {
1708-
if (PyFile_WriteString(" ", fout) != 0)
1709-
return NULL;
1710-
}
1711-
if (PyFile_Check(fin) && PyFile_Check(fout)
1712-
&& isatty(fileno(PyFile_AsFile(fin)))
1713-
&& isatty(fileno(PyFile_AsFile(fout)))) {
1714-
PyObject *po;
1715-
char *prompt;
1716-
char *s;
1717-
PyObject *result;
1718-
if (v != NULL) {
1719-
po = PyObject_Str(v);
1720-
if (po == NULL)
1721-
return NULL;
1722-
prompt = PyString_AsString(po);
1723-
if (prompt == NULL)
1724-
return NULL;
1725-
}
1726-
else {
1727-
po = NULL;
1728-
prompt = "";
1729-
}
1730-
s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
1731-
prompt);
1732-
Py_XDECREF(po);
1733-
if (s == NULL) {
1734-
if (!PyErr_Occurred())
1735-
PyErr_SetNone(PyExc_KeyboardInterrupt);
1736-
return NULL;
1737-
}
1738-
if (*s == '\0') {
1739-
PyErr_SetNone(PyExc_EOFError);
1740-
result = NULL;
1741-
}
1742-
else { /* strip trailing '\n' */
1743-
size_t len = strlen(s);
1744-
if (len > INT_MAX) {
1745-
PyErr_SetString(PyExc_OverflowError,
1746-
"[raw_]input: input too long");
1747-
result = NULL;
1748-
}
1749-
else {
1750-
result = PyString_FromStringAndSize(s,
1751-
(int)(len-1));
1752-
}
1753-
}
1754-
PyMem_FREE(s);
1755-
return result;
1756-
}
1757-
if (v != NULL) {
1758-
if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
1759-
return NULL;
1760-
}
1761-
return PyFile_GetLine(fin, -1);
1762-
}
1763-
1764-
PyDoc_STRVAR(raw_input_doc,
1765-
"raw_input([prompt]) -> string\n\
1766-
\n\
1767-
Read a string from standard input. The trailing newline is stripped.\n\
1768-
If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
1769-
On Unix, GNU readline is used if enabled. The prompt string, if given,\n\
1770-
is printed without a trailing newline before reading.");
1771-
1772-
17731736
static PyObject *
17741737
builtin_reduce(PyObject *self, PyObject *args)
17751738
{
@@ -2244,7 +2207,6 @@ static PyMethodDef builtin_methods[] = {
22442207
{"ord", builtin_ord, METH_O, ord_doc},
22452208
{"pow", builtin_pow, METH_VARARGS, pow_doc},
22462209
{"range", builtin_range, METH_VARARGS, range_doc},
2247-
{"raw_input", builtin_raw_input, METH_VARARGS, raw_input_doc},
22482210
{"reduce", builtin_reduce, METH_VARARGS, reduce_doc},
22492211
{"reload", builtin_reload, METH_O, reload_doc},
22502212
{"repr", builtin_repr, METH_O, repr_doc},

0 commit comments

Comments
 (0)