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
|
||||
;; 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
|
||||
|
@ -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]"
|
||||
|
Loading…
Reference in New Issue
Block a user