Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cython
Commits
d930a704
Commit
d930a704
authored
Jul 04, 2015
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
disable async/await keywords in nested normal 'def' functions (as CPython does)
parent
471d145c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
42 additions
and
22 deletions
+42
-22
Cython/Compiler/Parsing.py
Cython/Compiler/Parsing.py
+21
-7
Cython/Compiler/Scanning.pxd
Cython/Compiler/Scanning.pxd
+2
-3
Cython/Compiler/Scanning.py
Cython/Compiler/Scanning.py
+8
-12
tests/run/test_coroutines_pep492.pyx
tests/run/test_coroutines_pep492.pyx
+11
-0
No files found.
Cython/Compiler/Parsing.py
View file @
d930a704
...
...
@@ -2039,18 +2039,14 @@ def p_statement(s, ctx, first_statement = 0):
return
p_async_statement
(
s
,
ctx
,
decorators
)
else
:
if
s
.
sy
==
'IDENT'
and
s
.
systring
==
'async'
:
ident_name
=
s
.
systring
# PEP 492 enables the async/await keywords when it spots "async def ..."
s
.
next
()
if
s
.
sy
==
'def'
:
s
.
enable_keyword
(
'async'
)
s
.
enable_keyword
(
'await'
)
result
=
p_async_statement
(
s
,
ctx
,
decorators
)
s
.
disable_keyword
(
'await'
)
s
.
disable_keyword
(
'async'
)
return
result
return
p_async_statement
(
s
,
ctx
,
decorators
)
elif
decorators
:
s
.
error
(
"Decorators can only be followed by functions or classes"
)
s
.
put_back
(
'IDENT'
,
'async'
)
s
.
put_back
(
'IDENT'
,
ident_name
)
# re-insert original token
return
p_simple_statement_list
(
s
,
ctx
,
first_statement
=
first_statement
)
...
...
@@ -3072,7 +3068,25 @@ def p_def_statement(s, decorators=None, is_async_def=False):
if
s
.
sy
==
'->'
:
s
.
next
()
return_type_annotation
=
p_test
(
s
)
# PEP 492 switches the async/await keywords off in simple "def" functions
# and on in "async def" functions
await_was_enabled
=
s
.
enable_keyword
(
'await'
)
if
is_async_def
else
s
.
disable_keyword
(
'await'
)
async_was_enabled
=
s
.
enable_keyword
(
'async'
)
if
is_async_def
else
s
.
disable_keyword
(
'async'
)
doc
,
body
=
p_suite_with_docstring
(
s
,
Ctx
(
level
=
'function'
))
if
is_async_def
:
if
not
async_was_enabled
:
s
.
disable_keyword
(
'async'
)
if
not
await_was_enabled
:
s
.
disable_keyword
(
'await'
)
else
:
if
async_was_enabled
:
s
.
enable_keyword
(
'async'
)
if
await_was_enabled
:
s
.
enable_keyword
(
'await'
)
return
Nodes
.
DefNode
(
pos
,
name
=
name
,
args
=
args
,
star_arg
=
star_arg
,
starstar_arg
=
starstar_arg
,
doc
=
doc
,
body
=
body
,
decorators
=
decorators
,
is_async_def
=
is_async_def
,
...
...
Cython/Compiler/Scanning.pxd
View file @
d930a704
...
...
@@ -30,7 +30,6 @@ cdef class PyrexScanner(Scanner):
cdef
public
bint
in_python_file
cdef
public
source_encoding
cdef
set
keywords
cdef
public
dict
keywords_stack
cdef
public
list
indentation_stack
cdef
public
indentation_char
cdef
public
int
bracket_nesting_level
...
...
@@ -58,5 +57,5 @@ cdef class PyrexScanner(Scanner):
cdef
expect_indent
(
self
)
cdef
expect_dedent
(
self
)
cdef
expect_newline
(
self
,
message
=*
,
bint
ignore_semicolon
=*
)
cdef
enable_keyword
(
self
,
name
)
cdef
disable_keyword
(
self
,
name
)
cdef
bint
enable_keyword
(
self
,
name
)
except
-
1
cdef
bint
disable_keyword
(
self
,
name
)
except
-
1
Cython/Compiler/Scanning.py
View file @
d930a704
...
...
@@ -314,7 +314,6 @@ class PyrexScanner(Scanner):
self
.
in_python_file
=
False
self
.
keywords
=
set
(
pyx_reserved_words
)
self
.
trace
=
trace_scanner
self
.
keywords_stack
=
{}
self
.
indentation_stack
=
[
0
]
self
.
indentation_char
=
None
self
.
bracket_nesting_level
=
0
...
...
@@ -495,16 +494,13 @@ class PyrexScanner(Scanner):
warning
(
useless_trailing_semicolon
,
"useless trailing semicolon"
)
def
enable_keyword
(
self
,
name
):
if
name
in
self
.
keywords_stack
:
self
.
keywords_stack
[
name
]
+=
1
else
:
self
.
keywords_stack
[
name
]
=
1
self
.
keywords
.
add
(
name
)
if
name
in
self
.
keywords
:
return
True
# was enabled before
self
.
keywords
.
add
(
name
)
return
False
# was not enabled before
def
disable_keyword
(
self
,
name
):
count
=
self
.
keywords_stack
.
get
(
name
,
1
)
if
count
==
1
:
self
.
keywords
.
discard
(
name
)
del
self
.
keywords_stack
[
name
]
else
:
self
.
keywords_stack
[
name
]
=
count
-
1
if
name
not
in
self
.
keywords
:
return
False
# was not enabled before
self
.
keywords
.
remove
(
name
)
return
True
# was enabled before
tests/run/test_coroutines_pep492.pyx
View file @
d930a704
...
...
@@ -138,6 +138,17 @@ class TokenizerRegrTest(unittest.TestCase):
self
.
assertEqual
(
type
(
ns
[
'foo'
]()).
__name__
,
'coroutine'
)
#self.assertTrue(inspect.iscoroutinefunction(ns['foo']))
def
test_syntax_async_await_as_names
(
self
):
async
def
enable
():
await
123
def
disable
():
await
=
123
async
=
'abc'
async
def
reenable
():
await
432
class
CoroutineTest
(
unittest
.
TestCase
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment