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

Skip to content

Commit 8fb86b3

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 8fb86b3

File tree

7 files changed

+34
-24
lines changed

7 files changed

+34
-24
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_flufl.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ def test_guido_as_bdfl(self):
3434
# parser reports the start of the token
3535
self.assertEqual(cm.exception.offset, 3)
3636

37+
def test_barry_as_bdfl_look_ma_with_no_compiler_flags(self):
38+
# Check that the future import is handled by the parser
39+
# even if the compiler flags are not passed.
40+
code = "from __future__ import barry_as_FLUFL;2 {0} 3"
41+
compile(code.format('<>'), '<BDFL test>', 'exec')
42+
with self.assertRaises(SyntaxError) as cm:
43+
compile(code.format('!='), '<FLUFL test>', 'exec')
44+
self.assertRegex(str(cm.exception), "with Barry as BDFL, use '<>' instead of '!='")
45+
self.assertIn('2 != 3', cm.exception.text)
46+
self.assertEqual(cm.exception.filename, '<FLUFL test>')
47+
self.assertEqual(cm.exception.lineno, 1)
48+
self.assertEqual(cm.exception.offset, len(code) - 4)
49+
50+
51+
3752

3853
if __name__ == '__main__':
3954
unittest.main()

Lib/test/test_grammar.py

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

1975-
19761975
if __name__ == '__main__':
19771976
unittest.main()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Using ``from __future__ import barry_as_FLUFL`` works now when using
2+
multiple statements on the same line. Additionally the effect is respected
3+
even if the import is used even if the flags are not passed to
4+
:func:`compile`. Patch by Pablo Galindo

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)