From 5810b6e8694128bf6f04bd5fe3e7549d502a9580 Mon Sep 17 00:00:00 2001 From: Chris Rink Date: Fri, 27 Dec 2024 13:10:00 -0500 Subject: [PATCH] Fix a bug where `condp` could throw an exception in some cases --- CHANGELOG.md | 1 + src/basilisp/core.lpy | 26 ++++++++++++++++---------- tests/basilisp/test_core_macros.lpy | 5 +++++ 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38e35bcc..11ad63da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed * Fix a bug in `defn` where the `attr-map?` and function metdata were merged into a seq instead of a map, causing `macroexpand` to fail in some cases (#1186) * Fix a bug where `basilisp.process/exec` threw an exception when inheriting the stdout stream from the current process (#1190) + * Fix a bug where `condp` threw an exception in certain cases (#1194) ## [v0.3.5] ### Changed diff --git a/src/basilisp/core.lpy b/src/basilisp/core.lpy index 303880db..09171512 100644 --- a/src/basilisp/core.lpy +++ b/src/basilisp/core.lpy @@ -3573,16 +3573,22 @@ (let [result (first remaining) remaining (rest remaining)] (cond - (not (seq remaining)) `(throw - (python/ValueError - (str "Expected result expression for condp " {:test ~test-expr}))) - (= result :>>) `(let [res# ~(list pred test-expr expr)] - (if res# - (~(first remaining) res#) - (condp ~pred ~expr ~@(rest remaining)))) - :else `(if ~(list pred test-expr expr) - ~result - (condp ~pred ~expr ~@remaining)))) + (= result :>>) `(let [res# ~(list pred test-expr expr)] + (if res# + (~(first remaining) res#) + (condp ~pred ~expr ~@(rest remaining)))) + (seq remaining) `(if ~(list pred test-expr expr) + ~result + (condp ~pred ~expr ~@remaining)) + (and result + (not (seq remaining))) `(if ~(list pred test-expr expr) + ~result + (throw + (python/ValueError + (str "Expected result expression for condp " {:test ~test-expr})))) + :else `(throw + (python/ValueError + (str "Expected result expression for condp " {:test ~test-expr}))))) test-expr)))) (defmacro declare diff --git a/tests/basilisp/test_core_macros.lpy b/tests/basilisp/test_core_macros.lpy index a80ad352..de26274f 100644 --- a/tests/basilisp/test_core_macros.lpy +++ b/tests/basilisp/test_core_macros.lpy @@ -416,6 +416,11 @@ (deftest condp-test (testing "condp result value" (is (= :a (condp = "a" :a))) + (is (= :a (condp = "a" + "a" :a))) + (is (= :a (condp = "a" + "a" :a + :b))) (is (= :a (condp = "a" "b" :b :a)))