@@ -1073,42 +1073,89 @@ PyDoc_STRVAR(hex_doc,
10731073Return the hexadecimal representation of an integer or long integer." );
10741074
10751075
1076- static PyObject * builtin_raw_input (PyObject * , PyObject * );
1077-
10781076static PyObject *
10791077builtin_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
11081151PyDoc_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
11141161static PyObject *
@@ -1686,90 +1733,6 @@ For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\
16861733These 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-
17731736static PyObject *
17741737builtin_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