From 5a1e6a7c6a4fd5345a6de629f1efc199f51bc95c Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Sun, 14 May 2017 11:08:27 -0400 Subject: [PATCH] hy-repr: Support NaN and Inf --- hy/contrib/hy_repr.hy | 16 ++++++++++++---- tests/native_tests/contrib/hy_repr.hy | 14 +++++++++++--- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/hy/contrib/hy_repr.hy b/hy/contrib/hy_repr.hy index b528974..2053504 100644 --- a/hy/contrib/hy_repr.hy +++ b/hy/contrib/hy_repr.hy @@ -2,8 +2,10 @@ ;; This file is part of Hy, which is free software licensed under the Expat ;; license. See the LICENSE. -(import [hy._compat [PY3 str-type bytes-type long-type]]) -(import [hy.models [HyObject HyExpression HySymbol HyKeyword HyInteger HyList HyDict HySet HyString HyBytes]]) +(import + [math [isnan]] + [hy._compat [PY3 str-type bytes-type long-type]] + [hy.models [HyObject HyExpression HySymbol HyKeyword HyInteger HyFloat HyComplex HyList HyDict HySet HyString HyBytes]]) (defn hy-repr [obj] (setv seen (set)) @@ -72,8 +74,14 @@ (.format "(int {})" (repr x)) (and (not PY3) (in t [long_type HyInteger])) (.rstrip (repr x) "L") - (is t complex) - (.strip (repr x) "()") + (and (in t [float HyFloat]) (isnan x)) + "NaN" + (= x Inf) + "Inf" + (= x -Inf) + "-Inf" + (in t [complex HyComplex]) + (.replace (.replace (.strip (repr x) "()") "inf" "Inf") "nan" "NaN") (is t fraction) (.format "{}/{}" (f x.numerator q) (f x.denominator q)) ; else diff --git a/tests/native_tests/contrib/hy_repr.hy b/tests/native_tests/contrib/hy_repr.hy index 0a678b4..9bbdd09 100644 --- a/tests/native_tests/contrib/hy_repr.hy +++ b/tests/native_tests/contrib/hy_repr.hy @@ -3,16 +3,17 @@ ;; license. See the LICENSE. (import + [math [isnan]] [hy.contrib.hy-repr [hy-repr]]) (defn test-hy-repr-roundtrip-from-value [] ; Test that a variety of values round-trip properly. (setv values [ None False True - 5 5.1 '5 '5.1 + 5 5.1 '5 '5.1 Inf -Inf (int 5) 1/2 - 5j 5.1j 2+1j 1.2+3.4j + 5j 5.1j 2+1j 1.2+3.4j Inf-Infj "" b"" '"" 'b"" "apple bloom" b"apple bloom" "⚘" @@ -32,10 +33,17 @@ (for [original-val values] (setv evaled (eval (read-str (hy-repr original-val)))) (assert (= evaled original-val)) - (assert (is (type evaled) (type original-val))))) + (assert (is (type evaled) (type original-val)))) + (assert (isnan (eval (read-str (hy-repr NaN)))))) (defn test-hy-repr-roundtrip-from-str [] (setv strs [ + "'Inf" + "'-Inf" + "'NaN" + "1+2j" + "NaN+NaNj" + "'NaN+NaNj" "[1 2 3]" "'[1 2 3]" "[1 'a 3]"