From a746ccb42c5f86134453c6f8a58060b498c7a2a2 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Sun, 14 May 2017 10:08:45 -0400 Subject: [PATCH 1/3] Refactor test_lex_expression_complex --- tests/test_lex.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/test_lex.py b/tests/test_lex.py index e5f4322..121eb9b 100644 --- a/tests/test_lex.py +++ b/tests/test_lex.py @@ -93,14 +93,15 @@ def test_lex_expression_float(): def test_lex_expression_complex(): """ Make sure expressions can produce complex """ - objs = tokenize("(foo 2.j)") - assert objs == [HyExpression([HySymbol("foo"), HyComplex(2.j)])] - objs = tokenize("(foo -0.5j)") - assert objs == [HyExpression([HySymbol("foo"), HyComplex(-0.5j)])] - objs = tokenize("(foo 1.e7j)") - assert objs == [HyExpression([HySymbol("foo"), HyComplex(1.e7j)])] - objs = tokenize("(foo j)") - assert objs == [HyExpression([HySymbol("foo"), HySymbol("j")])] + + def t(x): return tokenize("(foo {})".format(x)) + + def f(x): return [HyExpression([HySymbol("foo"), x])] + + assert t("2.j") == f(HyComplex(2.j)) + assert t("-0.5j") == f(HyComplex(-0.5j)) + assert t("1.e7j") == f(HyComplex(1e7j)) + assert t("j") == f(HySymbol("j")) def test_lex_digit_separators(): From bb91b57dca88c035fcf27a1669b38597dad4de7f Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Sun, 14 May 2017 11:12:28 -0400 Subject: [PATCH 2/3] Require capitalizing NaN and Inf like so --- NEWS | 1 + docs/language/api.rst | 3 +++ hy/models.py | 31 +++++++++++++++++++++++++------ tests/test_lex.py | 22 ++++++++++++++++++++++ 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/NEWS b/NEWS index a21c514..4c963f4 100644 --- a/NEWS +++ b/NEWS @@ -8,6 +8,7 @@ Changes from 0.13.0 * The compiler now automatically promotes values to Hy model objects as necessary, so you can write ``(eval `(+ 1 ~n))`` instead of ``(eval `(+ 1 ~(HyInteger n)))`` + * Literal `Inf`s and `NaN`s must now be capitalized like that [ Bug Fixes ] * Numeric literals are no longer parsed as symbols when followed by a dot diff --git a/docs/language/api.rst b/docs/language/api.rst index 5ed646d..5241b3a 100644 --- a/docs/language/api.rst +++ b/docs/language/api.rst @@ -52,6 +52,9 @@ digits. (print 10,000,000,000 10_000_000_000) +Unlike Python, Hy provides literal forms for NaN and infinity: `NaN`, `Inf`, +and `-Inf`. + string literals --------------- diff --git a/hy/models.py b/hy/models.py index 93cce18..77c580f 100644 --- a/hy/models.py +++ b/hy/models.py @@ -3,6 +3,7 @@ # license. See the LICENSE. from __future__ import unicode_literals +from math import isnan, isinf from hy._compat import PY3, str_type, bytes_type, long_type, string_types from fractions import Fraction @@ -142,15 +143,24 @@ if not PY3: # do not add long on python3 _wrappers[long_type] = HyInteger +def check_inf_nan_cap(arg, value): + if isinstance(arg, string_types): + if isinf(value) and "Inf" not in arg: + raise ValueError('Inf must be capitalized as "Inf"') + if isnan(value) and "NaN" not in arg: + raise ValueError('NaN must be capitalized as "NaN"') + + class HyFloat(HyObject, float): """ Internal representation of a Hy Float. May raise a ValueError as if float(foo) was called, given HyFloat(foo). """ - def __new__(cls, number, *args, **kwargs): - number = float(strip_digit_separators(number)) - return super(HyFloat, cls).__new__(cls, number) + def __new__(cls, num, *args, **kwargs): + value = super(HyFloat, cls).__new__(cls, strip_digit_separators(num)) + check_inf_nan_cap(num, value) + return value _wrappers[float] = HyFloat @@ -161,9 +171,18 @@ class HyComplex(HyObject, complex): complex(foo) was called, given HyComplex(foo). """ - def __new__(cls, number, *args, **kwargs): - number = complex(strip_digit_separators(number)) - return super(HyComplex, cls).__new__(cls, number) + def __new__(cls, num, *args, **kwargs): + value = super(HyComplex, cls).__new__(cls, strip_digit_separators(num)) + if isinstance(num, string_types): + p1, _, p2 = num.lstrip("+-").replace("-", "+").partition("+") + if p2: + check_inf_nan_cap(p1, value.real) + check_inf_nan_cap(p2, value.imag) + elif "j" in p1: + check_inf_nan_cap(p1, value.imag) + else: + check_inf_nan_cap(p1, value.real) + return value _wrappers[complex] = HyComplex diff --git a/tests/test_lex.py b/tests/test_lex.py index 121eb9b..dfac923 100644 --- a/tests/test_lex.py +++ b/tests/test_lex.py @@ -2,6 +2,7 @@ # This file is part of Hy, which is free software licensed under the Expat # license. See the LICENSE. +from math import isnan from hy.models import (HyExpression, HyInteger, HyFloat, HyComplex, HySymbol, HyString, HyDict, HyList, HySet, HyCons) from hy.lex import LexException, PrematureEndOfInput, tokenize @@ -91,6 +92,22 @@ def test_lex_expression_float(): assert objs == [HyExpression([HySymbol("foo"), HyFloat(1.e7)])] +def test_lex_nan_and_inf(): + + assert isnan(tokenize("NaN")[0]) + assert tokenize("Nan") == [HySymbol("Nan")] + assert tokenize("nan") == [HySymbol("nan")] + assert tokenize("NAN") == [HySymbol("NAN")] + + assert tokenize("Inf") == [HyFloat(float("inf"))] + assert tokenize("inf") == [HySymbol("inf")] + assert tokenize("INF") == [HySymbol("INF")] + + assert tokenize("-Inf") == [HyFloat(float("-inf"))] + assert tokenize("-inf") == [HySymbol("_inf")] + assert tokenize("-INF") == [HySymbol("_INF")] + + def test_lex_expression_complex(): """ Make sure expressions can produce complex """ @@ -102,6 +119,11 @@ def test_lex_expression_complex(): assert t("-0.5j") == f(HyComplex(-0.5j)) assert t("1.e7j") == f(HyComplex(1e7j)) assert t("j") == f(HySymbol("j")) + assert isnan(t("NaNj")[0][1].imag) + assert t("nanj") == f(HySymbol("nanj")) + assert t("Inf+Infj") == f(HyComplex(complex(float("inf"), float("inf")))) + assert t("Inf-Infj") == f(HyComplex(complex(float("inf"), float("-inf")))) + assert t("Inf-INFj") == f(HySymbol("Inf_INFj")) def test_lex_digit_separators(): From 5a1e6a7c6a4fd5345a6de629f1efc199f51bc95c Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Sun, 14 May 2017 11:08:27 -0400 Subject: [PATCH 3/3] 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]"