]> granicus.if.org Git - python/commitdiff
raw_input() -> input(). old input behavior is history (and test_builtin passes again...
authorNeal Norwitz <nnorwitz@gmail.com>
Fri, 17 Mar 2006 05:59:16 +0000 (05:59 +0000)
committerNeal Norwitz <nnorwitz@gmail.com>
Fri, 17 Mar 2006 05:59:16 +0000 (05:59 +0000)
Lib/test/test_builtin.py
Python/bltinmodule.c

index 7fdc063f1f975839027790d0722ca67897488060..c97a7f1e2dcafc32567064477d2bdab045b8a458 100644 (file)
@@ -658,8 +658,6 @@ class BuiltinTest(unittest.TestCase):
         id([0,1,2,3])
         id({'spam': 1, 'eggs': 2, 'ham': 3})
 
-    # Test input() later, together with raw_input
-
     def test_int(self):
         self.assertEqual(int(314), 314)
         self.assertEqual(int(3.14), 3)
@@ -1108,7 +1106,7 @@ class BuiltinTest(unittest.TestCase):
         self.assertRaises(TypeError, oct, ())
 
     def write_testfile(self):
-        # NB the first 4 lines are also used to test input and raw_input, below
+        # NB the first 4 lines are also used to test input, below
         fp = open(TESTFN, 'w')
         try:
             fp.write('1+1\n')
@@ -1267,7 +1265,7 @@ class BuiltinTest(unittest.TestCase):
         self.assertRaises(OverflowError, range, -sys.maxint, sys.maxint)
         self.assertRaises(OverflowError, range, 0, 2*sys.maxint)
 
-    def test_input_and_raw_input(self):
+    def test_input(self):
         self.write_testfile()
         fp = open(TESTFN, 'r')
         savestdin = sys.stdin
@@ -1275,29 +1273,18 @@ class BuiltinTest(unittest.TestCase):
         try:
             sys.stdin = fp
             sys.stdout = BitBucket()
-            self.assertEqual(input(), 2)
-            self.assertEqual(input('testing\n'), 2)
-            self.assertEqual(raw_input(), 'The quick brown fox jumps over the lazy dog.')
-            self.assertEqual(raw_input('testing\n'), 'Dear John')
+            self.assertEqual(input(), '1+1')
+            self.assertEqual(input('testing\n'), '1+1')
+            self.assertEqual(input(), 'The quick brown fox jumps over the lazy dog.')
+            self.assertEqual(input('testing\n'), 'Dear John')
             sys.stdin = cStringIO.StringIO("NULL\0")
             self.assertRaises(TypeError, input, 42, 42)
-            sys.stdin = cStringIO.StringIO("    'whitespace'")
-            self.assertEqual(input(), 'whitespace')
+            whitespace = "    'whitespace'"
+            sys.stdin = cStringIO.StringIO(whitespace)
+            self.assertEqual(input(), whitespace)
             sys.stdin = cStringIO.StringIO()
             self.assertRaises(EOFError, input)
 
-            # SF 876178: make sure input() respect future options.
-            sys.stdin = cStringIO.StringIO('1/2')
-            sys.stdout = cStringIO.StringIO()
-            exec compile('print input()', 'test_builtin_tmp', 'exec')
-            sys.stdin.seek(0, 0)
-            exec compile('from __future__ import division;print input()',
-                         'test_builtin_tmp', 'exec')
-            sys.stdin.seek(0, 0)
-            exec compile('print input()', 'test_builtin_tmp', 'exec')
-            self.assertEqual(sys.stdout.getvalue().splitlines(),
-                             ['0', '0.5', '0'])
-
             del sys.stdout
             self.assertRaises(RuntimeError, input, 'prompt')
             del sys.stdin
index c9da78c6f6f1fe2cfb8f1bef7c31c65391d8eedc..a5dc0e34aac09ac339cc203020e53a8f315e6f5e 100644 (file)
@@ -1073,42 +1073,89 @@ PyDoc_STRVAR(hex_doc,
 Return the hexadecimal representation of an integer or long integer.");
 
 
-static PyObject *builtin_raw_input(PyObject *, PyObject *);
-
 static PyObject *
 builtin_input(PyObject *self, PyObject *args)
 {
-       PyObject *line;
-       char *str;
-       PyObject *res;
-       PyObject *globals, *locals;
-       PyCompilerFlags cf;
+       PyObject *v = NULL;
+       PyObject *fin = PySys_GetObject("stdin");
+       PyObject *fout = PySys_GetObject("stdout");
 
-       line = builtin_raw_input(self, args);
-       if (line == NULL)
-               return line;
-       if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str))
+       if (!PyArg_UnpackTuple(args, "input", 0, 1, &v))
                return NULL;
-       while (*str == ' ' || *str == '\t')
-                       str++;
-       globals = PyEval_GetGlobals();
-       locals = PyEval_GetLocals();
-       if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
-               if (PyDict_SetItemString(globals, "__builtins__",
-                                        PyEval_GetBuiltins()) != 0)
+
+       if (fin == NULL) {
+               PyErr_SetString(PyExc_RuntimeError, "input: lost sys.stdin");
+               return NULL;
+       }
+       if (fout == NULL) {
+               PyErr_SetString(PyExc_RuntimeError, "input: lost sys.stdout");
+               return NULL;
+       }
+       if (PyFile_SoftSpace(fout, 0)) {
+               if (PyFile_WriteString(" ", fout) != 0)
                        return NULL;
        }
-       cf.cf_flags = 0;
-       PyEval_MergeCompilerFlags(&cf);
-       res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
-       Py_DECREF(line);
-       return res;
+       if (PyFile_Check(fin) && PyFile_Check(fout)
+            && isatty(fileno(PyFile_AsFile(fin)))
+            && isatty(fileno(PyFile_AsFile(fout)))) {
+               PyObject *po;
+               char *prompt;
+               char *s;
+               PyObject *result;
+               if (v != NULL) {
+                       po = PyObject_Str(v);
+                       if (po == NULL)
+                               return NULL;
+                       prompt = PyString_AsString(po);
+                       if (prompt == NULL)
+                               return NULL;
+               }
+               else {
+                       po = NULL;
+                       prompt = "";
+               }
+               s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
+                                  prompt);
+               Py_XDECREF(po);
+               if (s == NULL) {
+                       if (!PyErr_Occurred())
+                               PyErr_SetNone(PyExc_KeyboardInterrupt);
+                       return NULL;
+               }
+               if (*s == '\0') {
+                       PyErr_SetNone(PyExc_EOFError);
+                       result = NULL;
+               }
+               else { /* strip trailing '\n' */
+                       size_t len = strlen(s);
+                       if (len > INT_MAX) {
+                               PyErr_SetString(PyExc_OverflowError,
+                                               "[raw_]input: input too long");
+                               result = NULL;
+                       }
+                       else {
+                               result = PyString_FromStringAndSize(s,
+                                                               (int)(len-1));
+                       }
+               }
+               PyMem_FREE(s);
+               return result;
+       }
+       if (v != NULL) {
+               if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
+                       return NULL;
+       }
+       return PyFile_GetLine(fin, -1);
 }
 
 PyDoc_STRVAR(input_doc,
-"input([prompt]) -> value\n\
+"input([prompt]) -> string\n\
 \n\
-Equivalent to eval(raw_input(prompt)).");
+Read a string from standard input.  The trailing newline is stripped.\n\
+If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
+On Unix, GNU readline is used if enabled.  The prompt string, if given,\n\
+is printed without a trailing newline before reading.");
+
 
 
 static PyObject *
@@ -1686,90 +1733,6 @@ For example, range(4) returns [0, 1, 2, 3].  The end point is omitted!\n\
 These are exactly the valid indices for a list of 4 elements.");
 
 
-static PyObject *
-builtin_raw_input(PyObject *self, PyObject *args)
-{
-       PyObject *v = NULL;
-       PyObject *fin = PySys_GetObject("stdin");
-       PyObject *fout = PySys_GetObject("stdout");
-
-       if (!PyArg_UnpackTuple(args, "[raw_]input", 0, 1, &v))
-               return NULL;
-
-       if (fin == NULL) {
-               PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdin");
-               return NULL;
-       }
-       if (fout == NULL) {
-               PyErr_SetString(PyExc_RuntimeError, "[raw_]input: lost sys.stdout");
-               return NULL;
-       }
-       if (PyFile_SoftSpace(fout, 0)) {
-               if (PyFile_WriteString(" ", fout) != 0)
-                       return NULL;
-       }
-       if (PyFile_Check(fin) && PyFile_Check(fout)
-            && isatty(fileno(PyFile_AsFile(fin)))
-            && isatty(fileno(PyFile_AsFile(fout)))) {
-               PyObject *po;
-               char *prompt;
-               char *s;
-               PyObject *result;
-               if (v != NULL) {
-                       po = PyObject_Str(v);
-                       if (po == NULL)
-                               return NULL;
-                       prompt = PyString_AsString(po);
-                       if (prompt == NULL)
-                               return NULL;
-               }
-               else {
-                       po = NULL;
-                       prompt = "";
-               }
-               s = PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout),
-                                  prompt);
-               Py_XDECREF(po);
-               if (s == NULL) {
-                       if (!PyErr_Occurred())
-                               PyErr_SetNone(PyExc_KeyboardInterrupt);
-                       return NULL;
-               }
-               if (*s == '\0') {
-                       PyErr_SetNone(PyExc_EOFError);
-                       result = NULL;
-               }
-               else { /* strip trailing '\n' */
-                       size_t len = strlen(s);
-                       if (len > INT_MAX) {
-                               PyErr_SetString(PyExc_OverflowError,
-                                               "[raw_]input: input too long");
-                               result = NULL;
-                       }
-                       else {
-                               result = PyString_FromStringAndSize(s,
-                                                               (int)(len-1));
-                       }
-               }
-               PyMem_FREE(s);
-               return result;
-       }
-       if (v != NULL) {
-               if (PyFile_WriteObject(v, fout, Py_PRINT_RAW) != 0)
-                       return NULL;
-       }
-       return PyFile_GetLine(fin, -1);
-}
-
-PyDoc_STRVAR(raw_input_doc,
-"raw_input([prompt]) -> string\n\
-\n\
-Read a string from standard input.  The trailing newline is stripped.\n\
-If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.\n\
-On Unix, GNU readline is used if enabled.  The prompt string, if given,\n\
-is printed without a trailing newline before reading.");
-
-
 static PyObject *
 builtin_reduce(PyObject *self, PyObject *args)
 {
@@ -2244,7 +2207,6 @@ static PyMethodDef builtin_methods[] = {
        {"ord",         builtin_ord,        METH_O, ord_doc},
        {"pow",         builtin_pow,        METH_VARARGS, pow_doc},
        {"range",       builtin_range,      METH_VARARGS, range_doc},
-       {"raw_input",   builtin_raw_input,  METH_VARARGS, raw_input_doc},
        {"reduce",      builtin_reduce,     METH_VARARGS, reduce_doc},
        {"reload",      builtin_reload,     METH_O, reload_doc},
        {"repr",        builtin_repr,       METH_O, repr_doc},