https://github.com/python/cpython/commit/fcf1f57d91f69b80f5a89b960c06c4180ed9f3f3 commit: fcf1f57d91f69b80f5a89b960c06c4180ed9f3f3 branch: 3.12 author: Miss Islington (bot) <31488909+miss-isling...@users.noreply.github.com> committer: pablogsal <pablog...@gmail.com> date: 2025-03-10T22:13:59Z summary:
[3.12] gh-125331: Allow the parser to activate future imports on the fly (GH-125482) (#131063) gh-125331: Allow the parser to activate future imports on the fly (GH-125482) (cherry picked from commit 3bd3e09588bfde7edba78c55794a0e28e2d21ea5) Co-authored-by: Pablo Galindo Salgado <pablog...@gmail.com> files: A Misc/NEWS.d/next/Core_and_Builtins/2024-10-29-23-30-35.gh-issue-125331.quKQ7V.rst M Grammar/python.gram M Lib/test/test_flufl.py M Parser/action_helpers.c M Parser/parser.c M Parser/pegen.h diff --git a/Grammar/python.gram b/Grammar/python.gram index 546e54ca1de342..b9191656ef850f 100644 --- a/Grammar/python.gram +++ b/Grammar/python.gram @@ -204,7 +204,7 @@ import_name[stmt_ty]: 'import' a=dotted_as_names { _PyAST_Import(a, EXTRA) } # note below: the ('.' | '...') is necessary because '...' is tokenized as ELLIPSIS import_from[stmt_ty]: | 'from' a=('.' | '...')* b=dotted_name 'import' c=import_from_targets { - _PyAST_ImportFrom(b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) } + _PyPegen_checked_future_import(p, b->v.Name.id, c, _PyPegen_seq_count_dots(a), EXTRA) } | 'from' a=('.' | '...')+ 'import' b=import_from_targets { _PyAST_ImportFrom(NULL, b, _PyPegen_seq_count_dots(a), EXTRA) } import_from_targets[asdl_alias_seq*]: diff --git a/Lib/test/test_flufl.py b/Lib/test/test_flufl.py index fd264c926bd575..d77e481c81d598 100644 --- a/Lib/test/test_flufl.py +++ b/Lib/test/test_flufl.py @@ -34,6 +34,32 @@ def test_guido_as_bdfl(self): # parser reports the start of the token self.assertEqual(cm.exception.offset, 3) + def test_barry_as_bdfl_look_ma_with_no_compiler_flags(self): + # Check that the future import is handled by the parser + # even if the compiler flags are not passed. + code = "from __future__ import barry_as_FLUFL;2 {0} 3" + compile(code.format('<>'), '<BDFL test>', 'exec') + with self.assertRaises(SyntaxError) as cm: + compile(code.format('!='), '<FLUFL test>', 'exec') + self.assertRegex(str(cm.exception), "with Barry as BDFL, use '<>' instead of '!='") + self.assertIn('2 != 3', cm.exception.text) + self.assertEqual(cm.exception.filename, '<FLUFL test>') + self.assertEqual(cm.exception.lineno, 1) + self.assertEqual(cm.exception.offset, len(code) - 4) + + def test_barry_as_bdfl_relative_import(self): + code = "from .__future__ import barry_as_FLUFL;2 {0} 3" + compile(code.format('!='), '<FLUFL test>', 'exec') + with self.assertRaises(SyntaxError) as cm: + compile(code.format('<>'), '<BDFL test>', 'exec') + self.assertRegex(str(cm.exception), "<BDFL test>") + self.assertIn('2 <> 3', cm.exception.text) + self.assertEqual(cm.exception.filename, '<BDFL test>') + self.assertEqual(cm.exception.lineno, 1) + self.assertEqual(cm.exception.offset, len(code) - 4) + + + if __name__ == '__main__': unittest.main() diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-10-29-23-30-35.gh-issue-125331.quKQ7V.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-10-29-23-30-35.gh-issue-125331.quKQ7V.rst new file mode 100644 index 00000000000000..a87467a5ba554b --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-10-29-23-30-35.gh-issue-125331.quKQ7V.rst @@ -0,0 +1,5 @@ +``from __future__ import barry_as_FLUFL`` now works in more contexts, +including when it is used in files, with the ``-c`` flag, and in the REPL +when there are multiple statements on the same line. Previously, it worked +only on subsequent lines in the REPL, and when the appropriate flags were +passed directly to :func:`compile`. Patch by Pablo Galindo. diff --git a/Parser/action_helpers.c b/Parser/action_helpers.c index e029ae692f53b8..95dc3c3ffefd94 100644 --- a/Parser/action_helpers.c +++ b/Parser/action_helpers.c @@ -1601,3 +1601,18 @@ expr_ty _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *strings, return _PyAST_JoinedStr(values, lineno, col_offset, end_lineno, end_col_offset, p->arena); } + +stmt_ty +_PyPegen_checked_future_import(Parser *p, identifier module, asdl_alias_seq * names, int level, + int lineno, int col_offset, int end_lineno, int end_col_offset, + PyArena *arena) { + if (level == 0 && PyUnicode_CompareWithASCIIString(module, "__future__") == 0) { + for (Py_ssize_t i = 0; i < asdl_seq_LEN(names); i++) { + alias_ty alias = asdl_seq_GET(names, i); + if (PyUnicode_CompareWithASCIIString(alias->name, "barry_as_FLUFL") == 0) { + p->flags |= PyPARSE_BARRY_AS_BDFL; + } + } + } + return _PyAST_ImportFrom(module, names, level, lineno, col_offset, end_lineno, end_col_offset, arena); +} diff --git a/Parser/parser.c b/Parser/parser.c index a4134867085232..ebdd7fa5a0cafe 100644 --- a/Parser/parser.c +++ b/Parser/parser.c @@ -3562,7 +3562,7 @@ import_from_rule(Parser *p) UNUSED(_end_lineno); // Only used by EXTRA macro int _end_col_offset = _token->end_col_offset; UNUSED(_end_col_offset); // Only used by EXTRA macro - _res = _PyAST_ImportFrom ( b -> v . Name . id , c , _PyPegen_seq_count_dots ( a ) , EXTRA ); + _res = _PyPegen_checked_future_import ( p , b -> v . Name . id , c , _PyPegen_seq_count_dots ( a ) , EXTRA ); if (_res == NULL && PyErr_Occurred()) { p->error_indicator = 1; p->level--; diff --git a/Parser/pegen.h b/Parser/pegen.h index 4617315a163944..f2cf9aec605689 100644 --- a/Parser/pegen.h +++ b/Parser/pegen.h @@ -349,6 +349,8 @@ mod_ty _PyPegen_make_module(Parser *, asdl_stmt_seq *); void *_PyPegen_arguments_parsing_error(Parser *, expr_ty); expr_ty _PyPegen_get_last_comprehension_item(comprehension_ty comprehension); void *_PyPegen_nonparen_genexp_in_call(Parser *p, expr_ty args, asdl_comprehension_seq *comprehensions); +stmt_ty _PyPegen_checked_future_import(Parser *p, identifier module, asdl_alias_seq *, + int , int, int , int , int , PyArena *); // Parser API _______________________________________________ Python-checkins mailing list -- python-checkins@python.org To unsubscribe send an email to python-checkins-le...@python.org https://mail.python.org/mailman3/lists/python-checkins.python.org/ Member address: arch...@mail-archive.com