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

Skip to content

Commit cd167c2

Browse files
pablogsallysnikolaounineteendo
committed
Apply suggestions from code review
Co-authored-by: Lysandros Nikolaou <[email protected]> Co-authored-by: Nice Zombies <[email protected]> Signed-off-by: Pablo Galindo <[email protected]>
1 parent 63640cc commit cd167c2

File tree

5 files changed

+21
-23
lines changed

5 files changed

+21
-23
lines changed

Grammar/python.gram

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,7 @@ 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-
_PyPegen_check_future_import(p,
211-
_PyAST_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA)
212-
) }
210+
_PyPegen_checked_future_import(p, b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) }
213211
| 'from' a=('.' | '...')+ 'import' b=import_from_targets {
214212
_PyAST_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) }
215213
import_from_targets[asdl_alias_seq*]:

Lib/test/test_grammar.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,6 +1972,12 @@ async def foo():
19721972
with self.assertRaises(Done):
19731973
foo().send(None)
19741974

1975+
def test_inline_future_imports(self):
1976+
# Check that future imports that change the behavior of the parser are
1977+
# correctly handled by the parser.
1978+
compile("from __future__ import barry_as_FLUFL; 1<>2", "<string>", "exec")
1979+
compile("from __future__ import annotations, barry_as_FLUFL; 1<>2", "<string>", "exec")
1980+
19751981

19761982
if __name__ == '__main__':
19771983
unittest.main()

Parser/action_helpers.c

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,22 +1693,16 @@ _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings,
16931693
}
16941694

16951695
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;
1696+
_PyPegen_checked_future_import(Parser *p, identifier module, asdl_alias_seq * names, int level,
1697+
int lineno, int col_offset, int end_lineno, int end_col_offset,
1698+
PyArena *arena) {
1699+
if (PyUnicode_CompareWithASCIIString(module, "__future__") == 0) {
1700+
for (Py_ssize_t i = 0; i < asdl_seq_LEN(names); i++) {
1701+
alias_ty alias = asdl_seq_GET(names, i);
1702+
if (PyUnicode_CompareWithASCIIString(alias->name, "barry_as_FLUFL") == 0) {
1703+
p->flags |= PyPARSE_BARRY_AS_BDFL;
1704+
}
1705+
}
17111706
}
1712-
exit:
1713-
return importfrom;
1707+
return _PyAST_ImportFrom(module, names, level, lineno, col_offset, end_lineno, end_col_offset, arena);
17141708
}

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ mod_ty _PyPegen_make_module(Parser *, asdl_stmt_seq *);
346346
void *_PyPegen_arguments_parsing_error(Parser *, expr_ty);
347347
expr_ty _PyPegen_get_last_comprehension_item(comprehension_ty comprehension);
348348
void *_PyPegen_nonparen_genexp_in_call(Parser *p, expr_ty args, asdl_comprehension_seq *comprehensions);
349+
stmt_ty _PyPegen_checked_future_import(Parser *p, identifier module, asdl_alias_seq *,
350+
int , int, int , int , int , PyArena *);
349351

350352
// Parser API
351353

@@ -361,8 +363,6 @@ asdl_stmt_seq *_PyPegen_interactive_exit(Parser *);
361363
// TODO: move to the correct place in this file
362364
expr_ty _PyPegen_joined_str(Parser *p, Token* a, asdl_expr_seq* expr, Token*b);
363365

364-
stmt_ty _PyPegen_check_future_import(Parser *p, stmt_ty importfrom);
365-
366366
// Generated function in parse.c - function definition in python.gram
367367
void *_PyPegen_parse(Parser *);
368368

0 commit comments

Comments
 (0)