mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
[3.11] gh-96587: Raise SyntaxError
for PEP654 on older feature_version
(GH-96588) (#96591)
(cherry picked from commit 2c7d2e8d46
)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
This commit is contained in:
parent
a8d50aeada
commit
8c6ced36ab
4 changed files with 15 additions and 2 deletions
|
@ -412,7 +412,9 @@ try_stmt[stmt_ty]:
|
||||||
| invalid_try_stmt
|
| invalid_try_stmt
|
||||||
| 'try' &&':' b=block f=finally_block { _PyAST_Try(b, NULL, NULL, f, EXTRA) }
|
| 'try' &&':' b=block f=finally_block { _PyAST_Try(b, NULL, NULL, f, EXTRA) }
|
||||||
| 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_block+ el=[else_block] f=[finally_block] { _PyAST_Try(b, ex, el, f, EXTRA) }
|
| 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_block+ el=[else_block] f=[finally_block] { _PyAST_Try(b, ex, el, f, EXTRA) }
|
||||||
| 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_star_block+ el=[else_block] f=[finally_block] { _PyAST_TryStar(b, ex, el, f, EXTRA) }
|
| 'try' &&':' b=block ex[asdl_excepthandler_seq*]=except_star_block+ el=[else_block] f=[finally_block] {
|
||||||
|
CHECK_VERSION(stmt_ty, 11, "Exception groups are",
|
||||||
|
_PyAST_TryStar(b, ex, el, f, EXTRA)) }
|
||||||
|
|
||||||
|
|
||||||
# Except statement
|
# Except statement
|
||||||
|
|
|
@ -771,6 +771,15 @@ class AST_Tests(unittest.TestCase):
|
||||||
with self.assertRaises(SyntaxError):
|
with self.assertRaises(SyntaxError):
|
||||||
ast.parse('(x := 0)', feature_version=(3, 7))
|
ast.parse('(x := 0)', feature_version=(3, 7))
|
||||||
|
|
||||||
|
def test_exception_groups_feature_version(self):
|
||||||
|
code = dedent('''
|
||||||
|
try: ...
|
||||||
|
except* Exception: ...
|
||||||
|
''')
|
||||||
|
ast.parse(code)
|
||||||
|
with self.assertRaises(SyntaxError):
|
||||||
|
ast.parse(code, feature_version=(3, 10))
|
||||||
|
|
||||||
def test_constant_as_name(self):
|
def test_constant_as_name(self):
|
||||||
for constant in "True", "False", "None":
|
for constant in "True", "False", "None":
|
||||||
expr = ast.Expression(ast.Name(constant, ast.Load()))
|
expr = ast.Expression(ast.Name(constant, ast.Load()))
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Correctly raise ``SyntaxError`` on exception groups (:pep:`654`) on python
|
||||||
|
versions prior to 3.11
|
2
Parser/parser.c
generated
2
Parser/parser.c
generated
|
@ -6970,7 +6970,7 @@ try_stmt_rule(Parser *p)
|
||||||
UNUSED(_end_lineno); // Only used by EXTRA macro
|
UNUSED(_end_lineno); // Only used by EXTRA macro
|
||||||
int _end_col_offset = _token->end_col_offset;
|
int _end_col_offset = _token->end_col_offset;
|
||||||
UNUSED(_end_col_offset); // Only used by EXTRA macro
|
UNUSED(_end_col_offset); // Only used by EXTRA macro
|
||||||
_res = _PyAST_TryStar ( b , ex , el , f , EXTRA );
|
_res = CHECK_VERSION ( stmt_ty , 11 , "Exception groups are" , _PyAST_TryStar ( b , ex , el , f , EXTRA ) );
|
||||||
if (_res == NULL && PyErr_Occurred()) {
|
if (_res == NULL && PyErr_Occurred()) {
|
||||||
p->error_indicator = 1;
|
p->error_indicator = 1;
|
||||||
p->level--;
|
p->level--;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue