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:
parent
703005f9d9
commit
602f392fe7
@ -3,7 +3,6 @@ python:
|
||||
- "pypy"
|
||||
- "2.6"
|
||||
- "2.7"
|
||||
- "3.2"
|
||||
- "3.3"
|
||||
- "3.4"
|
||||
cache:
|
||||
|
@ -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)]]
|
||||
|
2
make.bat
2
make.bat
@ -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
|
||||
)
|
||||
|
||||
|
@ -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)))
|
||||
|
@ -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 []
|
||||
|
Loading…
Reference in New Issue
Block a user