From 15ad6cb998c423b000caa0256e395b405519e5e4 Mon Sep 17 00:00:00 2001 From: John MacKenzie Date: Tue, 6 May 2014 18:48:17 +0000 Subject: [PATCH] defmacro/g! and HyObject#startswith Currently, defmacro/g! doesn't respond well when it comes across a HyObject that doesn't respond to the instance method startswith (e.g. HyInteger, HyFloat, etc.). This updates defmacro/g! to be a little safer when searching for the gensyms it needs to create. --- hy/core/macros.hy | 2 +- tests/native_tests/native_macros.hy | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/hy/core/macros.hy b/hy/core/macros.hy index 8b46743..c6071ba 100644 --- a/hy/core/macros.hy +++ b/hy/core/macros.hy @@ -169,7 +169,7 @@ ~@body)) (defmacro defmacro/g! [name args &rest body] - (let [[syms (list (distinct (filter (fn [x] (.startswith x "g!")) (flatten body))))]] + (let [[syms (list (distinct (filter (fn [x] (and (hasattr x "startswith") (.startswith x "g!"))) (flatten body))))]] `(defmacro ~name [~@args] (let ~(HyList (map (fn [x] `[~x (gensym (slice '~x 2))]) syms)) ~@body)))) diff --git a/tests/native_tests/native_macros.hy b/tests/native_tests/native_macros.hy index 571e824..848cd8f 100644 --- a/tests/native_tests/native_macros.hy +++ b/tests/native_tests/native_macros.hy @@ -188,7 +188,12 @@ (setv s2 (to_source _ast2)) (assert (in ":res_" s1)) (assert (in ":res_" s2)) - (assert (not (= s1 s2)))) + (assert (not (= s1 s2))) + + ;; defmacro/g! didn't like numbers initially because they + ;; don't have a startswith method and blew up during expansion + (setv macro2 "(defmacro/g! two-point-zero [] `(+ (float 1) 1.0))") + (assert (import_buffer_to_ast macro2 "foo"))) (defn test-if-not []