Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 63640cc

Browse files
committed
gh-125331: Allow the parser to activate future imports on the fly
1 parent 033510e commit 63640cc

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

Grammar/python.gram

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ import_name[stmt_ty]: 'import' a=dotted_as_names { _PyAST_Import(a, EXTRA) }
207207
# note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS
208208
import_from[stmt_ty]:
209209
| 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets {
210-
_PyAST_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) }
210+
_PyPegen_check_future_import(p,
211+
_PyAST_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA)
212+
) }
211213
| 'from' a=('.' | '...')+ 'import' b=import_from_targets {
212214
_PyAST_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) }
213215
import_from_targets[asdl_alias_seq*]:

Parser/action_helpers.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,3 +1691,24 @@ _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
16911691
assert(current_pos == n_elements);
16921692
return _PyAST_JoinedStr(values, lineno, col_offset, end_lineno, end_col_offset, p->arena);
16931693
}
1694+
1695+
stmt_ty
1696+
_PyPegen_check_future_import(Parser *p, stmt_ty importfrom) {
1697+
if (importfrom->kind != ImportFrom_kind) {
1698+
goto exit;
1699+
}
1700+
asdl_alias_seq *names = importfrom->v.ImportFrom.names;
1701+
if (asdl_seq_LEN(names) != 1) {
1702+
goto exit;
1703+
}
1704+
identifier mod = importfrom->v.ImportFrom.module;
1705+
if (PyUnicode_CompareWithASCIIString(mod, "__future__") != 0) {
1706+
goto exit;
1707+
}
1708+
alias_ty alias = asdl_seq_GET(names, 0);
1709+
if (PyUnicode_CompareWithASCIIString(alias->name, "barry_as_FLUFL") == 0) {
1710+
p->flags |= PyPARSE_BARRY_AS_BDFL;
1711+
}
1712+
exit:
1713+
return importfrom;
1714+
}

Parser/parser.c

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Parser/pegen.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ asdl_stmt_seq *_PyPegen_interactive_exit(Parser *);
361361
// TODO: move to the correct place in this file
362362
expr_ty _PyPegen_joined_str(Parser *p, Token* a, asdl_expr_seq* expr, Token*b);
363363

364+
stmt_ty _PyPegen_check_future_import(Parser *p, stmt_ty importfrom);
365+
364366
// Generated function in parse.c - function definition in python.gram
365367
void *_PyPegen_parse(Parser *);
366368

0 commit comments

Comments
 (0)