From a979dd85f7d436d7aa4ff5b16618fce3cfd2146d Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Wed, 21 Jun 2017 15:37:32 -0700 Subject: [PATCH 1/2] Don't make `yield-from` a special form on Python 2 --- hy/compiler.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index da7e858..b990332 100755 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -1128,13 +1128,9 @@ class HyASTCompiler(object): return ret - @builds("yield_from") + @builds_if("yield_from", PY3) @checkargs(max=1) def compile_yield_from_expression(self, expr): - if not PY3: - raise HyCompileError( - "yield-from only supported in python 3.3+!") - expr.pop(0) ret = Result(contains_yield=True) From 801836f6c2e0750f613950ac46adc17c069b9609 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Mon, 10 Jul 2017 09:04:24 -0700 Subject: [PATCH 2/2] Remove the Python 2 yield-from macro I moved the yield-from tests from native_macros to py3_only_tests. --- NEWS | 1 + hy/core/macros.hy | 18 ------------------ tests/native_tests/native_macros.hy | 25 ------------------------- tests/native_tests/py3_only_tests.hy | 26 ++++++++++++++++++++++++++ 4 files changed, 27 insertions(+), 43 deletions(-) diff --git a/NEWS b/NEWS index 52aaa5a..fe7ffb9 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,7 @@ Changes from 0.13.0 [ Language Changes ] + * `yield-from` is no longer supported under Python 2 * Single-character "sharp macros" changed to "tag macros", which can have longer names * Periods are no longer allowed in keywords diff --git a/hy/core/macros.hy b/hy/core/macros.hy index f2bd8ea..27eadf6 100644 --- a/hy/core/macros.hy +++ b/hy/core/macros.hy @@ -201,24 +201,6 @@ `(do (setv ~@(interleave ~gs ~os)) ~@~body))) -(if-python2 - (defmacro/g! yield-from [expr] - `(do (import types) - (setv ~g!iter (iter ~expr)) - (setv ~g!return None) - (setv ~g!message None) - (while True - (try (if (isinstance ~g!iter types.GeneratorType) - (setv ~g!message (yield (.send ~g!iter ~g!message))) - (setv ~g!message (yield (next ~g!iter)))) - (except [~g!e StopIteration] - (do (setv ~g!return (if (hasattr ~g!e "value") - (. ~g!e value) - None)) - (break))))) - ~g!return)) - None) - (defmacro defmain [args &rest body] "Write a function named \"main\" and do the if __main__ dance" diff --git a/tests/native_tests/native_macros.hy b/tests/native_tests/native_macros.hy index 6baa7a4..3a0d428 100644 --- a/tests/native_tests/native_macros.hy +++ b/tests/native_tests/native_macros.hy @@ -306,31 +306,6 @@ (assert (= (lif-not 0 "false" "true") "true"))) -(defn test-yield-from [] - "NATIVE: testing yield from" - (defn yield-from-test [] - (for* [i (range 3)] - (yield i)) - (yield-from [1 2 3])) - (assert (= (list (yield-from-test)) [0 1 2 1 2 3]))) - -(defn test-yield-from-exception-handling [] - "NATIVE: Ensure exception handling in yield from works right" - (defn yield-from-subgenerator-test [] - (yield 1) - (yield 2) - (yield 3) - (assert 0)) - (defn yield-from-test [] - (for* [i (range 3)] - (yield i)) - (try - (yield-from (yield-from-subgenerator-test)) - (except [e AssertionError] - (yield 4)))) - (assert (= (list (yield-from-test)) [0 1 2 1 2 3 4]))) - - (defn test-defmain [] "NATIVE: make sure defmain is clean" (global --name--) diff --git a/tests/native_tests/py3_only_tests.hy b/tests/native_tests/py3_only_tests.hy index bfd1d34..b376f6c 100644 --- a/tests/native_tests/py3_only_tests.hy +++ b/tests/native_tests/py3_only_tests.hy @@ -35,3 +35,29 @@ (assert (= (apply function-of-various-args [1 2 3 4] {"foo" 5 "bar" 6 "quux" 7}) (, 1 2 (, 3 4) 5 {"bar" 6 "quux" 7})))) + + +(defn test-yield-from [] + "NATIVE: testing yield from" + (defn yield-from-test [] + (for* [i (range 3)] + (yield i)) + (yield-from [1 2 3])) + (assert (= (list (yield-from-test)) [0 1 2 1 2 3]))) + + +(defn test-yield-from-exception-handling [] + "NATIVE: Ensure exception handling in yield from works right" + (defn yield-from-subgenerator-test [] + (yield 1) + (yield 2) + (yield 3) + (assert 0)) + (defn yield-from-test [] + (for* [i (range 3)] + (yield i)) + (try + (yield-from (yield-from-subgenerator-test)) + (except [e AssertionError] + (yield 4)))) + (assert (= (list (yield-from-test)) [0 1 2 1 2 3 4])))