From a4e4e35783d26151be19253ff6dd1c3ec2ca7efa Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Thu, 22 Mar 2012 08:19:04 -0400 Subject: [PATCH] check by equality for __future__ not identity (closes #14378) --- Lib/test/test_ast.py | 6 ++++++ Misc/NEWS | 3 +++ Python/future.c | 10 ++-------- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py index f4ce615ad8f..e22d4de88ba 100644 --- a/Lib/test/test_ast.py +++ b/Lib/test/test_ast.py @@ -218,6 +218,12 @@ class AST_Tests(unittest.TestCase): 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, "", "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 index 606898f6d2c..1c1bf164667 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 3.2.4 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 index d6b653f3159..978dc253065 100644 --- a/Python/future.c +++ b/Python/future.c @@ -60,13 +60,6 @@ future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename) { int i, found_docstring = 0, done = 0, prev_line = 0; - static PyObject *future; - if (!future) { - future = PyUnicode_InternFromString("__future__"); - if (!future) - return 0; - } - if (!(mod->kind == Module_kind || mod->kind == Interactive_kind)) return 1; @@ -93,7 +86,8 @@ future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename) */ if (s->kind == ImportFrom_kind) { - if (s->v.ImportFrom.module == future) { + PyObject *modname = s->v.ImportFrom.module; + if (!PyUnicode_CompareWithASCIIString(modname, "__future__")) { if (done) { PyErr_SetString(PyExc_SyntaxError, ERR_LATE_FUTURE);