Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit da8f87b

Browse files
authoredJul 26, 2023
pythongh-107015: Remove async_hacks from the tokenizer (python#107018)
1 parent b0202a4 commit da8f87b

File tree

20 files changed

+404
-499
lines changed

20 files changed

+404
-499
lines changed
 

‎Doc/library/ast.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2146,7 +2146,7 @@ and classes for traversing abstract syntax trees:
21462146
Currently ``major`` must equal to ``3``. For example, setting
21472147
``feature_version=(3, 4)`` will allow the use of ``async`` and
21482148
``await`` as variable names. The lowest supported version is
2149-
``(3, 4)``; the highest is ``sys.version_info[0:2]``.
2149+
``(3, 7)``; the highest is ``sys.version_info[0:2]``.
21502150

21512151
If source contains a null character ('\0'), :exc:`ValueError` is raised.
21522152

@@ -2169,6 +2169,9 @@ and classes for traversing abstract syntax trees:
21692169
.. versionchanged:: 3.8
21702170
Added ``type_comments``, ``mode='func_type'`` and ``feature_version``.
21712171

2172+
.. versionchanged:: 3.13
2173+
The minimum supported version for feature_version is now (3,7)
2174+
21722175

21732176
.. function:: unparse(ast_obj)
21742177

‎Doc/library/token-list.inc

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Doc/library/token.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,21 @@ the :mod:`tokenize` module.
8080

8181

8282
.. versionchanged:: 3.5
83-
Added :data:`AWAIT` and :data:`ASYNC` tokens.
83+
Added :data:`!AWAIT` and :data:`!ASYNC` tokens.
8484

8585
.. versionchanged:: 3.7
8686
Added :data:`COMMENT`, :data:`NL` and :data:`ENCODING` tokens.
8787

8888
.. versionchanged:: 3.7
89-
Removed :data:`AWAIT` and :data:`ASYNC` tokens. "async" and "await" are
89+
Removed :data:`!AWAIT` and :data:`!ASYNC` tokens. "async" and "await" are
9090
now tokenized as :data:`NAME` tokens.
9191

9292
.. versionchanged:: 3.8
9393
Added :data:`TYPE_COMMENT`, :data:`TYPE_IGNORE`, :data:`COLONEQUAL`.
94-
Added :data:`AWAIT` and :data:`ASYNC` tokens back (they're needed
94+
Added :data:`!AWAIT` and :data:`!ASYNC` tokens back (they're needed
9595
to support parsing older Python versions for :func:`ast.parse` with
9696
``feature_version`` set to 6 or lower).
97+
98+
.. versionchanged:: 3.13
99+
Removed :data:`!AWAIT` and :data:`!ASYNC` tokens again.
100+

‎Grammar/Tokens

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ COLONEQUAL ':='
5656
EXCLAMATION '!'
5757

5858
OP
59-
AWAIT
60-
ASYNC
6159
TYPE_IGNORE
6260
TYPE_COMMENT
6361
SOFT_KEYWORD

‎Grammar/python.gram

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ simple_stmt[stmt_ty] (memo):
127127
| &'nonlocal' nonlocal_stmt
128128

129129
compound_stmt[stmt_ty]:
130-
| &('def' | '@' | ASYNC) function_def
130+
| &('def' | '@' | 'async') function_def
131131
| &'if' if_stmt
132132
| &('class' | '@') class_def
133-
| &('with' | ASYNC) with_stmt
134-
| &('for' | ASYNC) for_stmt
133+
| &('with' | 'async') with_stmt
134+
| &('for' | 'async') for_stmt
135135
| &'try' try_stmt
136136
| &'while' while_stmt
137137
| match_stmt
@@ -272,7 +272,7 @@ function_def_raw[stmt_ty]:
272272
_PyAST_FunctionDef(n->v.Name.id,
273273
(params) ? params : CHECK(arguments_ty, _PyPegen_empty_arguments(p)),
274274
b, NULL, a, NEW_TYPE_COMMENT(p, tc), t, EXTRA) }
275-
| ASYNC 'def' n=NAME t=[type_params] &&'(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
275+
| 'async' 'def' n=NAME t=[type_params] &&'(' params=[params] ')' a=['->' z=expression { z }] &&':' tc=[func_type_comment] b=block {
276276
CHECK_VERSION(
277277
stmt_ty,
278278
5,
@@ -385,7 +385,7 @@ for_stmt[stmt_ty]:
385385
| invalid_for_stmt
386386
| 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] {
387387
_PyAST_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) }
388-
| ASYNC 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] {
388+
| 'async' 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] {
389389
CHECK_VERSION(stmt_ty, 5, "Async for loops are", _PyAST_AsyncFor(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
390390
| invalid_for_target
391391

@@ -398,9 +398,9 @@ with_stmt[stmt_ty]:
398398
CHECK_VERSION(stmt_ty, 9, "Parenthesized context managers are", _PyAST_With(a, b, NULL, EXTRA)) }
399399
| 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
400400
_PyAST_With(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA) }
401-
| ASYNC 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
401+
| 'async' 'with' '(' a[asdl_withitem_seq*]=','.with_item+ ','? ')' ':' b=block {
402402
CHECK_VERSION(stmt_ty, 5, "Async with statements are", _PyAST_AsyncWith(a, b, NULL, EXTRA)) }
403-
| ASYNC 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
403+
| 'async' 'with' a[asdl_withitem_seq*]=','.with_item+ ':' tc=[TYPE_COMMENT] b=block {
404404
CHECK_VERSION(stmt_ty, 5, "Async with statements are", _PyAST_AsyncWith(a, b, NEW_TYPE_COMMENT(p, tc), EXTRA)) }
405405
| invalid_with_stmt
406406

@@ -814,7 +814,7 @@ power[expr_ty]:
814814
# Primary elements are things like "obj.something.something", "obj[something]", "obj(something)", "obj" ...
815815

816816
await_primary[expr_ty] (memo):
817-
| AWAIT a=primary { CHECK_VERSION(expr_ty, 5, "Await expressions are", _PyAST_Await(a, EXTRA)) }
817+
| 'await' a=primary { CHECK_VERSION(expr_ty, 5, "Await expressions are", _PyAST_Await(a, EXTRA)) }
818818
| primary
819819

820820
primary[expr_ty]:
@@ -966,7 +966,7 @@ for_if_clauses[asdl_comprehension_seq*]:
966966
| a[asdl_comprehension_seq*]=for_if_clause+ { a }
967967

968968
for_if_clause[comprehension_ty]:
969-
| ASYNC 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
969+
| 'async' 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
970970
CHECK_VERSION(comprehension_ty, 6, "Async comprehensions are", _PyAST_comprehension(a, b, c, 1, p->arena)) }
971971
| 'for' a=star_targets 'in' ~ b=disjunction c[asdl_expr_seq*]=('if' z=disjunction { z })* {
972972
_PyAST_comprehension(a, b, c, 0, p->arena) }
@@ -1284,7 +1284,7 @@ invalid_with_item:
12841284
RAISE_SYNTAX_ERROR_INVALID_TARGET(STAR_TARGETS, a) }
12851285

12861286
invalid_for_target:
1287-
| ASYNC? 'for' a=star_expressions {
1287+
| 'async'? 'for' a=star_expressions {
12881288
RAISE_SYNTAX_ERROR_INVALID_TARGET(FOR_TARGETS, a) }
12891289

12901290
invalid_group:
@@ -1301,12 +1301,12 @@ invalid_import_from_targets:
13011301
RAISE_SYNTAX_ERROR("trailing comma not allowed without surrounding parentheses") }
13021302

13031303
invalid_with_stmt:
1304-
| [ASYNC] 'with' ','.(expression ['as' star_target])+ NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
1305-
| [ASYNC] 'with' '(' ','.(expressions ['as' star_target])+ ','? ')' NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
1304+
| ['async'] 'with' ','.(expression ['as' star_target])+ NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
1305+
| ['async'] 'with' '(' ','.(expressions ['as' star_target])+ ','? ')' NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
13061306
invalid_with_stmt_indent:
1307-
| [ASYNC] a='with' ','.(expression ['as' star_target])+ ':' NEWLINE !INDENT {
1307+
| ['async'] a='with' ','.(expression ['as' star_target])+ ':' NEWLINE !INDENT {
13081308
RAISE_INDENTATION_ERROR("expected an indented block after 'with' statement on line %d", a->lineno) }
1309-
| [ASYNC] a='with' '(' ','.(expressions ['as' star_target])+ ','? ')' ':' NEWLINE !INDENT {
1309+
| ['async'] a='with' '(' ','.(expressions ['as' star_target])+ ','? ')' ':' NEWLINE !INDENT {
13101310
RAISE_INDENTATION_ERROR("expected an indented block after 'with' statement on line %d", a->lineno) }
13111311

13121312
invalid_try_stmt:
@@ -1367,11 +1367,11 @@ invalid_while_stmt:
13671367
| a='while' named_expression ':' NEWLINE !INDENT {
13681368
RAISE_INDENTATION_ERROR("expected an indented block after 'while' statement on line %d", a->lineno) }
13691369
invalid_for_stmt:
1370-
| [ASYNC] 'for' star_targets 'in' star_expressions NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
1371-
| [ASYNC] a='for' star_targets 'in' star_expressions ':' NEWLINE !INDENT {
1370+
| ['async'] 'for' star_targets 'in' star_expressions NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
1371+
| ['async'] a='for' star_targets 'in' star_expressions ':' NEWLINE !INDENT {
13721372
RAISE_INDENTATION_ERROR("expected an indented block after 'for' statement on line %d", a->lineno) }
13731373
invalid_def_raw:
1374-
| [ASYNC] a='def' NAME '(' [params] ')' ['->' expression] ':' NEWLINE !INDENT {
1374+
| ['async'] a='def' NAME '(' [params] ')' ['->' expression] ':' NEWLINE !INDENT {
13751375
RAISE_INDENTATION_ERROR("expected an indented block after function definition on line %d", a->lineno) }
13761376
invalid_class_def_raw:
13771377
| 'class' NAME ['(' [arguments] ')'] NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }

‎Include/internal/pycore_token.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,16 @@ extern "C" {
6969
#define COLONEQUAL 53
7070
#define EXCLAMATION 54
7171
#define OP 55
72-
#define AWAIT 56
73-
#define ASYNC 57
74-
#define TYPE_IGNORE 58
75-
#define TYPE_COMMENT 59
76-
#define SOFT_KEYWORD 60
77-
#define FSTRING_START 61
78-
#define FSTRING_MIDDLE 62
79-
#define FSTRING_END 63
80-
#define COMMENT 64
81-
#define NL 65
82-
#define ERRORTOKEN 66
83-
#define N_TOKENS 68
72+
#define TYPE_IGNORE 56
73+
#define TYPE_COMMENT 57
74+
#define SOFT_KEYWORD 58
75+
#define FSTRING_START 59
76+
#define FSTRING_MIDDLE 60
77+
#define FSTRING_END 61
78+
#define COMMENT 62
79+
#define NL 63
80+
#define ERRORTOKEN 64
81+
#define N_TOKENS 66
8482
#define NT_OFFSET 256
8583

8684
/* Special definitions for cooperation with parser */

‎Lib/test/test_peg_generator/test_c_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ def test_ternary_operator(self) -> None:
404404
a='[' b=NAME c=for_if_clauses d=']' { _PyAST_ListComp(b, c, EXTRA) }
405405
)
406406
for_if_clauses[asdl_comprehension_seq*]: (
407-
a[asdl_comprehension_seq*]=(y=[ASYNC] 'for' a=NAME 'in' b=NAME c[asdl_expr_seq*]=('if' z=NAME { z })*
407+
a[asdl_comprehension_seq*]=(y=['async'] 'for' a=NAME 'in' b=NAME c[asdl_expr_seq*]=('if' z=NAME { z })*
408408
{ _PyAST_comprehension(_PyAST_Name(((expr_ty) a)->v.Name.id, Store, EXTRA), b, c, (y == NULL) ? 0 : 1, p->arena) })+ { a }
409409
)
410410
"""

0 commit comments

Comments
 (0)
Please sign in to comment.