hy-repr: Support NaN and Inf
This commit is contained in:
parent
bb91b57dca
commit
5a1e6a7c6a
@ -2,8 +2,10 @@
|
|||||||
;; This file is part of Hy, which is free software licensed under the Expat
|
;; This file is part of Hy, which is free software licensed under the Expat
|
||||||
;; license. See the LICENSE.
|
;; license. See the LICENSE.
|
||||||
|
|
||||||
(import [hy._compat [PY3 str-type bytes-type long-type]])
|
(import
|
||||||
(import [hy.models [HyObject HyExpression HySymbol HyKeyword HyInteger HyList HyDict HySet HyString HyBytes]])
|
[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]
|
(defn hy-repr [obj]
|
||||||
(setv seen (set))
|
(setv seen (set))
|
||||||
@ -72,8 +74,14 @@
|
|||||||
(.format "(int {})" (repr x))
|
(.format "(int {})" (repr x))
|
||||||
(and (not PY3) (in t [long_type HyInteger]))
|
(and (not PY3) (in t [long_type HyInteger]))
|
||||||
(.rstrip (repr x) "L")
|
(.rstrip (repr x) "L")
|
||||||
(is t complex)
|
(and (in t [float HyFloat]) (isnan x))
|
||||||
(.strip (repr x) "()")
|
"NaN"
|
||||||
|
(= x Inf)
|
||||||
|
"Inf"
|
||||||
|
(= x -Inf)
|
||||||
|
"-Inf"
|
||||||
|
(in t [complex HyComplex])
|
||||||
|
(.replace (.replace (.strip (repr x) "()") "inf" "Inf") "nan" "NaN")
|
||||||
(is t fraction)
|
(is t fraction)
|
||||||
(.format "{}/{}" (f x.numerator q) (f x.denominator q))
|
(.format "{}/{}" (f x.numerator q) (f x.denominator q))
|
||||||
; else
|
; else
|
||||||
|
@ -3,16 +3,17 @@
|
|||||||
;; license. See the LICENSE.
|
;; license. See the LICENSE.
|
||||||
|
|
||||||
(import
|
(import
|
||||||
|
[math [isnan]]
|
||||||
[hy.contrib.hy-repr [hy-repr]])
|
[hy.contrib.hy-repr [hy-repr]])
|
||||||
|
|
||||||
(defn test-hy-repr-roundtrip-from-value []
|
(defn test-hy-repr-roundtrip-from-value []
|
||||||
; Test that a variety of values round-trip properly.
|
; Test that a variety of values round-trip properly.
|
||||||
(setv values [
|
(setv values [
|
||||||
None False True
|
None False True
|
||||||
5 5.1 '5 '5.1
|
5 5.1 '5 '5.1 Inf -Inf
|
||||||
(int 5)
|
(int 5)
|
||||||
1/2
|
1/2
|
||||||
5j 5.1j 2+1j 1.2+3.4j
|
5j 5.1j 2+1j 1.2+3.4j Inf-Infj
|
||||||
"" b""
|
"" b""
|
||||||
'"" 'b""
|
'"" 'b""
|
||||||
"apple bloom" b"apple bloom" "⚘"
|
"apple bloom" b"apple bloom" "⚘"
|
||||||
@ -32,10 +33,17 @@
|
|||||||
(for [original-val values]
|
(for [original-val values]
|
||||||
(setv evaled (eval (read-str (hy-repr original-val))))
|
(setv evaled (eval (read-str (hy-repr original-val))))
|
||||||
(assert (= evaled 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 []
|
(defn test-hy-repr-roundtrip-from-str []
|
||||||
(setv strs [
|
(setv strs [
|
||||||
|
"'Inf"
|
||||||
|
"'-Inf"
|
||||||
|
"'NaN"
|
||||||
|
"1+2j"
|
||||||
|
"NaN+NaNj"
|
||||||
|
"'NaN+NaNj"
|
||||||
"[1 2 3]"
|
"[1 2 3]"
|
||||||
"'[1 2 3]"
|
"'[1 2 3]"
|
||||||
"[1 'a 3]"
|
"[1 'a 3]"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user