From 316220b7426bad2b45a098d3b5bf1000fd3f9b4d Mon Sep 17 00:00:00 2001 From: Simon Gomizelj Date: Thu, 8 Feb 2018 15:08:09 -0500 Subject: [PATCH] Fix AST generation of a naked return --- NEWS.rst | 7 +++++++ hy/compiler.py | 6 ++++-- tests/compilers/test_ast.py | 11 ++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/NEWS.rst b/NEWS.rst index 6896949..e5967d8 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -1,5 +1,12 @@ .. default-role:: code +Unreleased +============================== + +Bug Fixes +------------------------------ +* Fix `(return)` so it works correctly to exit a Python 2 generator + 0.14.0 ============================== diff --git a/hy/compiler.py b/hy/compiler.py index ccacdce..5d8f915 100755 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -2040,8 +2040,10 @@ class HyASTCompiler(object): @checkargs(max=1) def compile_return(self, expr): ret = Result() - if len(expr) > 1: - ret += self.compile(expr[1]) + if len(expr) == 1: + return asty.Return(expr, value=None) + + ret += self.compile(expr[1]) return ret + asty.Return(expr, value=ret.force_expr) @builds("defclass") diff --git a/tests/compilers/test_ast.py b/tests/compilers/test_ast.py index 4836ad2..0ee88cc 100644 --- a/tests/compilers/test_ast.py +++ b/tests/compilers/test_ast.py @@ -8,7 +8,7 @@ from __future__ import unicode_literals from hy import HyString from hy.models import HyObject from hy.compiler import hy_compile -from hy.importer import import_buffer_to_hst +from hy.importer import hy_eval, import_buffer_to_hst from hy.errors import HyCompileError, HyTypeError from hy.lex.exceptions import LexException from hy._compat import PY3 @@ -30,6 +30,10 @@ def can_compile(expr): return hy_compile(import_buffer_to_hst(expr), "__main__") +def can_eval(expr): + return hy_eval(import_buffer_to_hst(expr)) + + def cant_compile(expr): try: hy_compile(import_buffer_to_hst(expr), "__main__") @@ -664,3 +668,8 @@ def test_ast_good_yield_from(): def test_ast_bad_yield_from(): "Make sure AST can't compile invalid yield-from" cant_compile("(yield-from)") + + +def test_eval_generator_with_return(): + """Ensure generators with a return statement works.""" + can_eval("(fn [] (yield 1) (yield 2) (return))")