From f61d7027880dd6c04fd81bfc36eb3e77689611dc Mon Sep 17 00:00:00 2001 From: Guillermo Vaya Date: Wed, 21 Aug 2013 01:09:03 +0200 Subject: [PATCH] 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))))