]> granicus.if.org Git - python/commitdiff
check by equality for __future__ not identity (closes #14378)
authorBenjamin Peterson <benjamin@python.org>
Thu, 22 Mar 2012 12:19:04 +0000 (08:19 -0400)
committerBenjamin Peterson <benjamin@python.org>
Thu, 22 Mar 2012 12:19:04 +0000 (08:19 -0400)
Lib/test/test_ast.py
Misc/NEWS
Python/future.c

index e3aa5b170a87b4a55790232c259ad1e4960e7f59..4f80197f8d790e4abcb8297fa27978b3b51e2c4d 100644 (file)
@@ -231,6 +231,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, "<test>", "exec")
+
     def test_base_classes(self):
         self.assertTrue(issubclass(ast.For, ast.stmt))
         self.assertTrue(issubclass(ast.Name, ast.expr))
index 0e12494e24adbd1515bb358aa8937a368a8f23cd..310d0fdb95d55a4c6b3c843e9a5abddcf134baff 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,9 @@ What's New in Python 2.7.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.
 
index 96be757b2ed616363fe9b7a8df727efc7766a41f..1ab7a1a104a5e05ad4c3d2722ab911a752f727ab 100644 (file)
@@ -59,13 +59,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 = PyString_InternFromString("__future__");
-        if (!future)
-            return 0;
-    }
-
     if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
         return 1;
 
@@ -92,7 +85,9 @@ 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 (PyString_GET_SIZE(modname) == 10 &&
+                !strcmp(PyString_AS_STRING(modname), "__future__")) {
                 if (done) {
                     PyErr_SetString(PyExc_SyntaxError,
                                     ERR_LATE_FUTURE);