From f61d7027880dd6c04fd81bfc36eb3e77689611dc Mon Sep 17 00:00:00 2001 From: Guillermo Vaya Date: Wed, 21 Aug 2013 01:09:03 +0200 Subject: [PATCH 1/2] Int conversion to long in py2.x Updated to current master Droped HyInt/HyLong commit --- hy/compiler.py | 4 ++-- hy/macros.py | 7 +++++-- hy/models/integer.py | 8 +++++--- tests/native_tests/core.hy | 2 +- tests/native_tests/math.hy | 4 ++++ 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/hy/compiler.py b/hy/compiler.py index f14c79f..67918e0 100644 --- a/hy/compiler.py +++ b/hy/compiler.py @@ -37,7 +37,7 @@ from hy.models.list import HyList from hy.models.dict import HyDict from hy.macros import require, macroexpand -from hy._compat import str_type +from hy._compat import str_type, long_type import hy.importer import traceback @@ -1753,7 +1753,7 @@ class HyASTCompiler(object): @builds(HyInteger) def compile_integer(self, number): - return ast.Num(n=int(number), + return ast.Num(n=long_type(number), lineno=number.start_line, col_offset=number.start_column) diff --git a/hy/macros.py b/hy/macros.py index 20d0cac..046320a 100644 --- a/hy/macros.py +++ b/hy/macros.py @@ -26,10 +26,10 @@ from hy.models.integer import HyInteger from hy.models.float import HyFloat from hy.models.complex import HyComplex from hy.models.dict import HyDict -from hy._compat import str_type +from hy._compat import str_type, long_type from collections import defaultdict - +import sys CORE_MACROS = [ "hy.core.bootstrap", @@ -87,6 +87,9 @@ _wrappers = { list: lambda l: HyList(_wrap_value(x) for x in l) } +if sys.version_info[0] < 3: # do not add long on python3 + _wrappers[long_type] = HyInteger + def _wrap_value(x): """Wrap `x` into the corresponding Hy type. diff --git a/hy/models/integer.py b/hy/models/integer.py index cb46af4..0eacef7 100644 --- a/hy/models/integer.py +++ b/hy/models/integer.py @@ -19,14 +19,16 @@ # DEALINGS IN THE SOFTWARE. from hy.models import HyObject +from hy._compat import long_type -class HyInteger(HyObject, int): +class HyInteger(HyObject, long_type): """ Internal represntation of a Hy Integer. May raise a ValueError as if - int(foo) was caled, given HyInteger(foo). + int(foo) was called, given HyInteger(foo). On python 2.x long will + be used instead """ def __new__(cls, number, *args, **kwargs): - number = int(number) + number = long_type(number) return super(HyInteger, cls).__new__(cls, number) diff --git a/tests/native_tests/core.hy b/tests/native_tests/core.hy index 6e81f39..e9d73c7 100644 --- a/tests/native_tests/core.hy +++ b/tests/native_tests/core.hy @@ -163,7 +163,7 @@ (assert-false (instance? Foo2 foo)) (assert-true (instance? Foo foo3)) (assert-true (instance? float 1.0)) - (assert-true (instance? int 3)) + (assert-true (instance? int (int 3))) (assert-true (instance? str (str "hello")))) (defn test-integer? [] diff --git a/tests/native_tests/math.hy b/tests/native_tests/math.hy index ba350e6..eda77b0 100644 --- a/tests/native_tests/math.hy +++ b/tests/native_tests/math.hy @@ -132,3 +132,7 @@ (let [[x 1]] (^= x 1) (assert (= x 0)))) + +(defn overflow-int-to-long [] + "NATIVE: test if int does not raise an overflow exception" + (assert (integer? (+ 1 1000000000000000000000000)))) From 34275fab606e1e7c3f48112d9ea7d7422a249fa1 Mon Sep 17 00:00:00 2001 From: Guillermo Vaya Date: Fri, 11 Oct 2013 08:35:32 +0200 Subject: [PATCH 2/2] Added type coercing to the right integer for the platform --- hy/core/language.hy | 19 +++++++++++-------- tests/native_tests/core.hy | 2 ++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/hy/core/language.hy b/hy/core/language.hy index a96b25d..ff98a11 100644 --- a/hy/core/language.hy +++ b/hy/core/language.hy @@ -23,6 +23,8 @@ ;;;; to make functional programming slightly easier. ;;;; +(import [hy._compat [long-type]]) ; long for python2, int for python3 + (defn _numeric-check [x] (if (not (numeric? x)) (raise (TypeError (.format "{0!r} is not a number" x))))) @@ -97,11 +99,13 @@ (defn instance? [klass x] (isinstance x klass)) +(defn integer [x] + "Return Hy kind of integer" + (long-type x)) + (defn integer? [x] "Return True if x in an integer" - (if-python2 - (isinstance x (, int long)) - (isinstance x int))) + (isinstance x (, int long-type))) (defn iterable? [x] "Return true if x is iterable" @@ -213,8 +217,7 @@ (= n 0)) (def *exports* ["cycle" "dec" "distinct" "drop" "drop_while" "empty?" - "even?" "filter" "float?" "inc" - "instance?" "integer?" "iterable?" "iterate" "iterator?" "neg?" - "none?" "nth" "numeric?" "odd?" "pos?" "remove" "repeat" - "repeatedly" "second" "string?" "take" "take_nth" "take_while" - "zero?"]) + "even?" "filter" "float?" "inc" "instance?" "integer" "integer?" + "iterable?" "iterate" "iterator?" "neg?" "none?" "nth" + "numeric?" "odd?" "pos?" "remove" "repeat" "repeatedly" "second" + "string?" "take" "take_nth" "take_while" "zero?"]) diff --git a/tests/native_tests/core.hy b/tests/native_tests/core.hy index e9d73c7..2f6b717 100644 --- a/tests/native_tests/core.hy +++ b/tests/native_tests/core.hy @@ -171,6 +171,8 @@ (assert-true (integer? 0)) (assert-true (integer? 3)) (assert-true (integer? -3)) + (assert-true (integer? (integer "-3"))) + (assert-true (integer? (integer 3))) (assert-false (integer? 4.2)) (assert-false (integer? None)) (assert-false (integer? "foo")))