From b11f2fcf497b09ee56596c392db0e7eb7fd273de Mon Sep 17 00:00:00 2001 From: Konrad Hinsen Date: Mon, 2 Sep 2013 09:58:35 +0200 Subject: [PATCH] Macro if-python2 for compile-time choice between Python 2 and Python 3 code branches --- hy/core/macros.hy | 6 ++++++ tests/native_tests/native_macros.hy | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/hy/core/macros.hy b/hy/core/macros.hy index 28bb36e..d25dd25 100644 --- a/hy/core/macros.hy +++ b/hy/core/macros.hy @@ -1,5 +1,11 @@ ;;; hy core macros +(defmacro if-python2 [python2-form python3-form] + (import sys) + (if (< (get sys.version_info 0) 3) + python2-form + python3-form)) + (defmacro yield-from [_hy_yield_from_els] (quasiquote (for [_hy_yield_from_x (unquote _hy_yield_from_els)] diff --git a/tests/native_tests/native_macros.hy b/tests/native_tests/native_macros.hy index d890d70..f5b1b74 100644 --- a/tests/native_tests/native_macros.hy +++ b/tests/native_tests/native_macros.hy @@ -99,3 +99,8 @@ (yield i)) (yield-from [1 2 3])) (assert (= (list (yield-from-test)) [0 1 2 1 2 3]))) + +(defn test-if-python2 [] + (import sys) + (assert (= (get sys.version_info 0) + (if-python2 2 3))))