]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix input() builtin function to respect compiler flags.
authorHye-Shik Chang <hyeshik@gmail.com>
Mon, 2 Feb 2004 13:39:01 +0000 (13:39 +0000)
committerHye-Shik Chang <hyeshik@gmail.com>
Mon, 2 Feb 2004 13:39:01 +0000 (13:39 +0000)
(SF patch 876178, patch by mwh, unittest by perky)

Lib/test/test_builtin.py
Misc/NEWS
Python/bltinmodule.c

index db823ff9c07558cf063fb116b88ab4b4acf5f0e4..46f3d68c5f5d97c397dc6a9989ddda309005f7c7 100644 (file)
@@ -931,6 +931,19 @@ class BuiltinTest(unittest.TestCase):
             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 e4332d8b0bfeb8e396223e4c054eec2692723465..9d2e3548756bad7eb351b811b2cf4f903ced68e1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.4 alpha 1?
 Core and builtins
 -----------------
 
+- input() builtin function now respects compiler flags such as
+  __future__ statements.  SF patch 876178.
+
 - Removed PendingDeprecationWarning from apply().  apply() remains
   deprecated, but the nuisance warning will not be issued.
 
index a17c6d9c1f1edcec7f30309b5a94e00cac903e5e..7321b74c02be29d458b65fd1681670cdf79a796c 100644 (file)
@@ -979,6 +979,7 @@ builtin_input(PyObject *self, PyObject *args)
        char *str;
        PyObject *res;
        PyObject *globals, *locals;
+       PyCompilerFlags cf;
 
        line = builtin_raw_input(self, args);
        if (line == NULL)
@@ -994,7 +995,9 @@ builtin_input(PyObject *self, PyObject *args)
                                         PyEval_GetBuiltins()) != 0)
                        return NULL;
        }
-       res = PyRun_String(str, Py_eval_input, globals, locals);
+       cf.cf_flags = 0;
+       PyEval_MergeCompilerFlags(&cf);
+       res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
        Py_DECREF(line);
        return res;
 }