Issue #24619: Simplify async/await tokenization. · pythoncapi/cpython@96ec934 · GitHub
Skip to content

Commit 96ec934

Browse files
author
Yury Selivanov
committed
Issue python#24619: Simplify async/await tokenization.
This commit simplifies async/await tokenization in tokenizer.c, tokenize.py & lib2to3/tokenize.py. Previous solution was to keep a stack of async-def & def blocks, whereas the new approach is just to remember position of the outermost async-def block. This change won't bring any parsing performance improvements, but it makes the code much easier to read and validate.
1 parent f315c1c commit 96ec934

7 files changed

Lines changed: 183 additions & 132 deletions

File tree

Lib/lib2to3/pgen2/tokenize.py

Lines changed: 19 additions & 14 deletions

Lib/lib2to3/tests/test_parser.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,32 @@ def test_await_expr(self):
6767
await x
6868
""")
6969

70+
self.validate("""async def foo():
71+
72+
def foo(): pass
73+
74+
def foo(): pass
75+
76+
await x
77+
""")
78+
79+
self.validate("""async def foo(): return await a""")
80+
81+
self.validate("""def foo():
82+
def foo(): pass
83+
async def foo(): await x
84+
""")
85+
7086
self.invalid_syntax("await x")
7187
self.invalid_syntax("""def foo():
7288
await x""")
7389

90+
self.invalid_syntax("""def foo():
91+
def foo(): pass
92+
async def foo(): pass
93+
await x
94+
""")
95+
7496
def test_async_var(self):
7597
self.validate("""async = 1""")
7698
self.validate("""await = 1""")

Lib/test/test_coroutines.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ async def bar(): return await_
330330
async def f():
331331
async def g(): pass
332332
await z
333+
await = 1
333334
self.assertTrue(inspect.iscoroutinefunction(f))
334335

335336

Lib/test/test_tokenize.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,79 @@
840840
OP ')' (1, 19) (1, 20)
841841
OP ':' (1, 20) (1, 21)
842842
AWAIT 'await' (1, 22) (1, 27)
843+
844+
>>> dump_tokens('''def f():
845+
...
846+
... def baz(): pass
847+
... async def bar(): pass
848+
...
849+
... await = 2''')
850+
ENCODING 'utf-8' (0, 0) (0, 0)
851+
NAME 'def' (1, 0) (1, 3)
852+
NAME 'f' (1, 4) (1, 5)
853+
OP '(' (1, 5) (1, 6)
854+
OP ')' (1, 6) (1, 7)
855+
OP ':' (1, 7) (1, 8)
856+
NEWLINE '\\n' (1, 8) (1, 9)
857+
NL '\\n' (2, 0) (2, 1)
858+
INDENT ' ' (3, 0) (3, 2)
859+
NAME 'def' (3, 2) (3, 5)
860+
NAME 'baz' (3, 6) (3, 9)
861+
OP '(' (3, 9) (3, 10)
862+
OP ')' (3, 10) (3, 11)
863+
OP ':' (3, 11) (3, 12)
864+
NAME 'pass' (3, 13) (3, 17)
865+
NEWLINE '\\n' (3, 17) (3, 18)
866+
ASYNC 'async' (4, 2) (4, 7)
867+
NAME 'def' (4, 8) (4, 11)
868+
NAME 'bar' (4, 12) (4, 15)
869+
OP '(' (4, 15) (4, 16)
870+
OP ')' (4, 16) (4, 17)
871+
OP ':' (4, 17) (4, 18)
872+
NAME 'pass' (4, 19) (4, 23)
873+
NEWLINE '\\n' (4, 23) (4, 24)
874+
NL '\\n' (5, 0) (5, 1)
875+
NAME 'await' (6, 2) (6, 7)
876+
OP '=' (6, 8) (6, 9)
877+
NUMBER '2' (6, 10) (6, 11)
878+
DEDENT '' (7, 0) (7, 0)
879+
880+
>>> dump_tokens('''async def f():
881+
...
882+
... def baz(): pass
883+
... async def bar(): pass
884+
...
885+
... await = 2''')
886+
ENCODING 'utf-8' (0, 0) (0, 0)
887+
ASYNC 'async' (1, 0) (1, 5)
888+
NAME 'def' (1, 6) (1, 9)
889+
NAME 'f' (1, 10) (1, 11)
890+
OP '(' (1, 11) (1, 12)
891+
OP ')' (1, 12) (1, 13)
892+
OP ':' (1, 13) (1, 14)
893+
NEWLINE '\\n' (1, 14) (1, 15)
894+
NL '\\n' (2, 0) (2, 1)
895+
INDENT ' ' (3, 0) (3, 2)
896+
NAME 'def' (3, 2) (3, 5)
897+
NAME 'baz' (3, 6) (3, 9)
898+
OP '(' (3, 9) (3, 10)
899+
OP ')' (3, 10) (3, 11)
900+
OP ':' (3, 11) (3, 12)
901+
NAME 'pass' (3, 13) (3, 17)
902+
NEWLINE '\\n' (3, 17) (3, 18)
903+
ASYNC 'async' (4, 2) (4, 7)
904+
NAME 'def' (4, 8) (4, 11)
905+
NAME 'bar' (4, 12) (4, 15)
906+
OP '(' (4, 15) (4, 16)
907+
OP ')' (4, 16) (4, 17)
908+
OP ':' (4, 17) (4, 18)
909+
NAME 'pass' (4, 19) (4, 23)
910+
NEWLINE '\\n' (4, 23) (4, 24)
911+
NL '\\n' (5, 0) (5, 1)
912+
AWAIT 'await' (6, 2) (6, 7)
913+
OP '=' (6, 8) (6, 9)
914+
NUMBER '2' (6, 10) (6, 11)
915+
DEDENT '' (7, 0) (7, 0)
843916
"""
844917

845918
from test import support

Lib/tokenize.py

Lines changed: 23 additions & 16 deletions

0 commit comments

Comments
 (0)