[Python-checkins] cpython (2.7): check by equality for __future__ not identity (closes #14378)
benjamin.peterson
python-checkins at python.org
Thu Mar 22 13:58:06 CET 2012
http://hg.python.org/cpython/rev/1729ec440bb6
changeset: 75872:1729ec440bb6
branch: 2.7
parent: 75838:44a8385a8241
user: Benjamin Peterson <benjamin at python.org>
date: Thu Mar 22 08:19:04 2012 -0400
summary:
check by equality for __future__ not identity (closes #14378)
files:
Lib/test/test_ast.py | 6 ++++++
Misc/NEWS | 3 +++
Python/future.c | 11 +++--------
3 files changed, 12 insertions(+), 8 deletions(-)
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -231,6 +231,12 @@
im = ast.parse("from . import y").body[0]
self.assertIsNone(im.module)
+ def test_non_interned_future_from_ast(self):
+ mod = ast.parse("from __future__ import division")
+ self.assertIsInstance(mod.body[0], ast.ImportFrom)
+ mod.body[0].module = " __future__ ".strip()
+ compile(mod, "<test>", "exec")
+
def test_base_classes(self):
self.assertTrue(issubclass(ast.For, ast.stmt))
self.assertTrue(issubclass(ast.Name, ast.expr))
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,9 @@
Core and Builtins
-----------------
+- Issue #14378: Fix compiling ast.ImportFrom nodes with a "__future__" string as
+ the module name that was not interned.
+
- Issue #14331: Use significantly less stack space when importing modules by
allocating path buffers on the heap instead of the stack.
diff --git a/Python/future.c b/Python/future.c
--- a/Python/future.c
+++ b/Python/future.c
@@ -59,13 +59,6 @@
{
int i, found_docstring = 0, done = 0, prev_line = 0;
- static PyObject *future;
- if (!future) {
- future = PyString_InternFromString("__future__");
- if (!future)
- return 0;
- }
-
if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
return 1;
@@ -92,7 +85,9 @@
*/
if (s->kind == ImportFrom_kind) {
- if (s->v.ImportFrom.module == future) {
+ PyObject *modname = s->v.ImportFrom.module;
+ if (PyString_GET_SIZE(modname) == 10 &&
+ !strcmp(PyString_AS_STRING(modname), "__future__")) {
if (done) {
PyErr_SetString(PyExc_SyntaxError,
ERR_LATE_FUTURE);
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list