Fix a bug in the parser's future statement handling that led to "with"
not being recognized as a keyword after, e.g., this statement:
from __future__ import division, with_statement
(backport from rev. 51993)
diff --git a/Lib/test/test_future.py b/Lib/test/test_future.py
index f5462e2..f67c692 100644
--- a/Lib/test/test_future.py
+++ b/Lib/test/test_future.py
@@ -82,6 +82,23 @@
else:
self.fail("expected exception didn't occur")
+ def test_parserhack(self):
+ # test that the parser.c::future_hack function works as expected
+ try:
+ exec "from __future__ import division, with_statement; with = 0"
+ except SyntaxError:
+ pass
+ else:
+ self.fail("syntax error didn't occur")
+
+ try:
+ exec "from __future__ import (with_statement, division); with = 0"
+ except SyntaxError:
+ pass
+ else:
+ self.fail("syntax error didn't occur")
+
+
def test_main():
test_support.run_unittest(FutureTest)
diff --git a/Misc/NEWS b/Misc/NEWS
index b6087a9..e930453 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,10 @@
Core and builtins
-----------------
+- Fix a bug in the parser's future statement handling that led to "with"
+ not being recognized as a keyword after, e.g., this statement:
+ from __future__ import division, with_statement
+
- Allow exception instances to be directly sliced again.
diff --git a/Parser/parser.c b/Parser/parser.c
index 04d5817..2ce84cd 100644
--- a/Parser/parser.c
+++ b/Parser/parser.c
@@ -181,7 +181,7 @@
future_hack(parser_state *ps)
{
node *n = ps->p_stack.s_top->s_parent;
- node *ch;
+ node *ch, *cch;
int i;
/* from __future__ import ..., must have at least 4 children */
@@ -195,15 +195,17 @@
if (NCH(ch) == 1 && STR(CHILD(ch, 0)) &&
strcmp(STR(CHILD(ch, 0)), "__future__") != 0)
return;
- for (i = 3; i < NCH(n); i += 2) {
- /* XXX: assume we don't have parentheses in import:
- from __future__ import (x, y, z)
- */
- ch = CHILD(n, i);
- if (NCH(ch) == 1)
- ch = CHILD(ch, 0);
- if (NCH(ch) >= 1 && TYPE(CHILD(ch, 0)) == NAME &&
- strcmp(STR(CHILD(ch, 0)), "with_statement") == 0) {
+ ch = CHILD(n, 3);
+ /* ch can be a star, a parenthesis or import_as_names */
+ if (TYPE(ch) == STAR)
+ return;
+ if (TYPE(ch) == LPAR)
+ ch = CHILD(n, 4);
+
+ for (i = 0; i < NCH(ch); i += 2) {
+ cch = CHILD(ch, i);
+ if (NCH(cch) >= 1 && TYPE(CHILD(cch, 0)) == NAME &&
+ strcmp(STR(CHILD(cch, 0)), "with_statement") == 0) {
ps->p_flags |= CO_FUTURE_WITH_STATEMENT;
break;
}