Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a4e4e35

Browse files
committed
check by equality for __future__ not identity (closes python#14378)
1 parent e112153 commit a4e4e35

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

Lib/test/test_ast.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,12 @@ def test_from_import(self):
218218
im = ast.parse("from . import y").body[0]
219219
self.assertIsNone(im.module)
220220

221+
def test_non_interned_future_from_ast(self):
222+
mod = ast.parse("from __future__ import division")
223+
self.assertIsInstance(mod.body[0], ast.ImportFrom)
224+
mod.body[0].module = " __future__ ".strip()
225+
compile(mod, "<test>", "exec")
226+
221227
def test_base_classes(self):
222228
self.assertTrue(issubclass(ast.For, ast.stmt))
223229
self.assertTrue(issubclass(ast.Name, ast.expr))

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.2.4
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #14378: Fix compiling ast.ImportFrom nodes with a "__future__" string as
14+
the module name that was not interned.
15+
1316
- Issue #14331: Use significantly less stack space when importing modules by
1417
allocating path buffers on the heap instead of the stack.
1518

Python/future.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,6 @@ future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename)
6060
{
6161
int i, found_docstring = 0, done = 0, prev_line = 0;
6262

63-
static PyObject *future;
64-
if (!future) {
65-
future = PyUnicode_InternFromString("__future__");
66-
if (!future)
67-
return 0;
68-
}
69-
7063
if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
7164
return 1;
7265

@@ -93,7 +86,8 @@ future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename)
9386
*/
9487

9588
if (s->kind == ImportFrom_kind) {
96-
if (s->v.ImportFrom.module == future) {
89+
PyObject *modname = s->v.ImportFrom.module;
90+
if (!PyUnicode_CompareWithASCIIString(modname, "__future__")) {
9791
if (done) {
9892
PyErr_SetString(PyExc_SyntaxError,
9993
ERR_LATE_FUTURE);

0 commit comments

Comments
 (0)