Merge branch master onto pr/284

Conflicts:
	hy/core/language.hy
This commit is contained in:
Abhishek L 2013-12-06 20:07:39 +05:30
commit 948e6d34c7
6 changed files with 29 additions and 14 deletions

View File

@ -37,7 +37,7 @@ from hy.models.list import HyList
from hy.models.dict import HyDict from hy.models.dict import HyDict
from hy.macros import require, macroexpand from hy.macros import require, macroexpand
from hy._compat import str_type from hy._compat import str_type, long_type
import hy.importer import hy.importer
import traceback import traceback
@ -1753,7 +1753,7 @@ class HyASTCompiler(object):
@builds(HyInteger) @builds(HyInteger)
def compile_integer(self, number): def compile_integer(self, number):
return ast.Num(n=int(number), return ast.Num(n=long_type(number),
lineno=number.start_line, lineno=number.start_line,
col_offset=number.start_column) col_offset=number.start_column)

View File

@ -23,6 +23,8 @@
;;;; to make functional programming slightly easier. ;;;; to make functional programming slightly easier.
;;;; ;;;;
(import [hy._compat [long-type]]) ; long for python2, int for python3
(defn _numeric-check [x] (defn _numeric-check [x]
(if (not (numeric? x)) (if (not (numeric? x))
(raise (TypeError (.format "{0!r} is not a number" x))))) (raise (TypeError (.format "{0!r} is not a number" x)))))
@ -97,11 +99,13 @@
(defn instance? [klass x] (defn instance? [klass x]
(isinstance x klass)) (isinstance x klass))
(defn integer [x]
"Return Hy kind of integer"
(long-type x))
(defn integer? [x] (defn integer? [x]
"Return True if x in an integer" "Return True if x in an integer"
(if-python2 (isinstance x (, int long-type)))
(isinstance x (, int long))
(isinstance x int)))
(defn iterable? [x] (defn iterable? [x]
"Return true if x is iterable" "Return true if x is iterable"
@ -219,6 +223,6 @@
(= n 0)) (= n 0))
(def *exports* '[cycle dec distinct drop drop-while empty? even? filter float? (def *exports* '[cycle dec distinct drop drop-while empty? even? filter float?
inc instance? integer? iterable? iterate iterator? neg? none? inc instance? integer integer? iterable? iterate iterator? neg?
nth numeric? odd? pos? remove repeat repeatedly second string none? nth numeric? odd? pos? remove repeat repeatedly second
string? take take-nth take-while zero?]) string string? take take-nth take-while zero?])

View File

@ -26,10 +26,10 @@ from hy.models.integer import HyInteger
from hy.models.float import HyFloat from hy.models.float import HyFloat
from hy.models.complex import HyComplex from hy.models.complex import HyComplex
from hy.models.dict import HyDict from hy.models.dict import HyDict
from hy._compat import str_type from hy._compat import str_type, long_type
from collections import defaultdict from collections import defaultdict
import sys
CORE_MACROS = [ CORE_MACROS = [
"hy.core.bootstrap", "hy.core.bootstrap",
@ -88,6 +88,9 @@ _wrappers = {
type(None): lambda foo: HySymbol("None"), type(None): lambda foo: HySymbol("None"),
} }
if sys.version_info[0] < 3: # do not add long on python3
_wrappers[long_type] = HyInteger
def _wrap_value(x): def _wrap_value(x):
"""Wrap `x` into the corresponding Hy type. """Wrap `x` into the corresponding Hy type.

View File

@ -19,14 +19,16 @@
# DEALINGS IN THE SOFTWARE. # DEALINGS IN THE SOFTWARE.
from hy.models import HyObject 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 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): def __new__(cls, number, *args, **kwargs):
number = int(number) number = long_type(number)
return super(HyInteger, cls).__new__(cls, number) return super(HyInteger, cls).__new__(cls, number)

View File

@ -163,7 +163,7 @@
(assert-false (instance? Foo2 foo)) (assert-false (instance? Foo2 foo))
(assert-true (instance? Foo foo3)) (assert-true (instance? Foo foo3))
(assert-true (instance? float 1.0)) (assert-true (instance? float 1.0))
(assert-true (instance? int 3)) (assert-true (instance? int (int 3)))
(assert-true (instance? str (str "hello")))) (assert-true (instance? str (str "hello"))))
(defn test-integer? [] (defn test-integer? []
@ -171,6 +171,8 @@
(assert-true (integer? 0)) (assert-true (integer? 0))
(assert-true (integer? 3)) (assert-true (integer? 3))
(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? 4.2))
(assert-false (integer? None)) (assert-false (integer? None))
(assert-false (integer? "foo"))) (assert-false (integer? "foo")))

View File

@ -132,3 +132,7 @@
(let [[x 1]] (let [[x 1]]
(^= x 1) (^= x 1)
(assert (= x 0)))) (assert (= x 0))))
(defn overflow-int-to-long []
"NATIVE: test if int does not raise an overflow exception"
(assert (integer? (+ 1 1000000000000000000000000))))