Implement yield-from in Python 2.x as a macro

And who said you can't teach an old dog new tricks.

  ... but at the same time, drop Python 3.2 for not knowing this new
  trick.
This commit is contained in:
Paul Tagliamonte 2014-11-15 08:33:04 -05:00 committed by Christopher Allan Webber
parent 703005f9d9
commit 602f392fe7
6 changed files with 29 additions and 18 deletions

View File

@ -3,7 +3,6 @@ python:
- "pypy"
- "2.6"
- "2.7"
- "3.2"
- "3.3"
- "3.4"
cache:

View File

@ -179,6 +179,25 @@
~@body))))
(if-python2
(defmacro/g! yield-from [expr]
`(do (import types)
(setv ~g!iter (iter ~expr))
(setv ~g!return nil)
(setv ~g!message nil)
(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))))
(catch [~g!e StopIteration]
(do (setv ~g!return (if (hasattr ~g!e "value")
(. ~g!e value)
nil))
(break)))))
~g!return))
nil)
(defmacro defmain [args &rest body]
"Write a function named \"main\" and do the if __main__ dance"
(let [[retval (gensym)]]

View File

@ -87,7 +87,7 @@ goto :EOF
if "%1" == "tox" (
:tox
call :venv
tox -e "py26,py27,py32,py33,flake8"
tox -e "py26,py27,py33,py34,flake8"
goto :EOF
)

View File

@ -233,5 +233,13 @@
(assert (= (tda-a2) :bazinga))
(assert (= tda-main tda-a1 tda-a2)))
(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-botsbuildbots []
(assert (> (len (first (Botsbuildbots))) 50)))

View File

@ -4,21 +4,6 @@
(import [hy._compat [PY33]])
(import [hy.errors [HyCompileError]])
(defn test-yield-from []
"NATIVE: testing yield from"
(try
(eval
'(do (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]))))
(catch [e HyCompileError]
;; Yield-from is supported in py3.3+ only
(assert (not PY33)))
(else (assert PY33))))
(defn test-exception-cause []

View File

@ -1,5 +1,5 @@
[tox]
envlist = py26,py27,pypy,py32,py33,flake8
envlist = py26,py27,pypy,py33,flake8
skipsdist = True
[testenv]