Skip to content

Commit

Permalink
Python3.5: Detect yield in coroutines as an error.
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhayen committed Dec 18, 2015
1 parent b00c736 commit f4a27c5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
11 changes: 11 additions & 0 deletions nuitka/tree/ReformulationYieldExpressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"""

import ast

from nuitka.nodes.BuiltinIteratorNodes import ExpressionBuiltinIter1
from nuitka.nodes.ConstantRefNodes import ExpressionConstantRef
from nuitka.nodes.YieldNodes import ExpressionYield, ExpressionYieldFrom
Expand All @@ -39,6 +41,15 @@ def _markAsGenerator(provider, node, source_ref):
None if Utils.python_version < 300 else node.col_offset
)

if provider.isExpressionCoroutineBody():
SyntaxErrors.raiseSyntaxError(
"'%s' inside async function" % (
"yield" if node.__class__ is ast.Yield else "yield from",
),
source_ref,
node.col_offset+3
)

assert provider.isExpressionGeneratorObjectBody(), provider


Expand Down
20 changes: 20 additions & 0 deletions tests/syntax/YieldFromInModule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2015, Kay Hayen, mailto:[email protected]
#
# Python tests originally created or extracted from other peoples work. The
# parts were too small to be protected.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

x = (i for i in
(yield from range(7)) if (yield from range(2)))
20 changes: 20 additions & 0 deletions tests/syntax/YieldInAsync35.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2015, Kay Hayen, mailto:[email protected]
#
# Python tests originally created or extracted from other peoples work. The
# parts were too small to be protected.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
async def g():
yield from None

0 comments on commit f4a27c5

Please sign in to comment.