Skip to content

Commit ff83c2b

Browse files
committed
Fix input() builtin function to respect compiler flags.
(SF patch 876178, patch by mwh, unittest by perky)
1 parent 96c4465 commit ff83c2b

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

Lib/test/test_builtin.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,19 @@ def test_input_and_raw_input(self):
931931
self.assertEqual(input(), 'whitespace')
932932
sys.stdin = cStringIO.StringIO()
933933
self.assertRaises(EOFError, input)
934+
935+
# SF 876178: make sure input() respect future options.
936+
sys.stdin = cStringIO.StringIO('1/2')
937+
sys.stdout = cStringIO.StringIO()
938+
exec compile('print input()', 'test_builtin_tmp', 'exec')
939+
sys.stdin.seek(0, 0)
940+
exec compile('from __future__ import division;print input()',
941+
'test_builtin_tmp', 'exec')
942+
sys.stdin.seek(0, 0)
943+
exec compile('print input()', 'test_builtin_tmp', 'exec')
944+
self.assertEqual(sys.stdout.getvalue().splitlines(),
945+
['0', '0.5', '0'])
946+
934947
del sys.stdout
935948
self.assertRaises(RuntimeError, input, 'prompt')
936949
del sys.stdin

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 2.4 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- input() builtin function now respects compiler flags such as
16+
__future__ statements. SF patch 876178.
17+
1518
- Removed PendingDeprecationWarning from apply(). apply() remains
1619
deprecated, but the nuisance warning will not be issued.
1720

Python/bltinmodule.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,7 @@ builtin_input(PyObject *self, PyObject *args)
979979
char *str;
980980
PyObject *res;
981981
PyObject *globals, *locals;
982+
PyCompilerFlags cf;
982983

983984
line = builtin_raw_input(self, args);
984985
if (line == NULL)
@@ -994,7 +995,9 @@ builtin_input(PyObject *self, PyObject *args)
994995
PyEval_GetBuiltins()) != 0)
995996
return NULL;
996997
}
997-
res = PyRun_String(str, Py_eval_input, globals, locals);
998+
cf.cf_flags = 0;
999+
PyEval_MergeCompilerFlags(&cf);
1000+
res = PyRun_StringFlags(str, Py_eval_input, globals, locals, &cf);
9981001
Py_DECREF(line);
9991002
return res;
10001003
}

0 commit comments

Comments
 (0)