Merge branch 'master' into pr/1135

This commit is contained in:
Tuukka Turto 2016-10-25 15:10:21 +03:00
commit 0abc218ae0
6 changed files with 44 additions and 62 deletions

View File

@ -503,19 +503,13 @@ class HyASTCompiler(object):
for expr in exprs: for expr in exprs:
if expr in ll_keywords: if expr in ll_keywords:
if expr == "&rest" and lambda_keyword is None: if expr == "&optional":
lambda_keyword = expr
elif expr == "&optional":
if len(defaults) > 0: if len(defaults) > 0:
raise HyTypeError(expr, raise HyTypeError(expr,
"There can only be &optional " "There can only be &optional "
"arguments or one &key argument") "arguments or one &key argument")
lambda_keyword = expr lambda_keyword = expr
elif expr == "&key": elif expr in ("&rest", "&key", "&kwonly", "&kwargs"):
lambda_keyword = expr
elif expr == "&kwonly":
lambda_keyword = expr
elif expr == "&kwargs":
lambda_keyword = expr lambda_keyword = expr
else: else:
raise HyTypeError(expr, raise HyTypeError(expr,

View File

@ -39,10 +39,6 @@
(import [hy.lex [LexException PrematureEndOfInput tokenize]]) (import [hy.lex [LexException PrematureEndOfInput tokenize]])
(import [hy.compiler [HyASTCompiler]]) (import [hy.compiler [HyASTCompiler]])
(defn _numeric-check [x]
(if (not (numeric? x))
(raise (TypeError (.format "{0!r} is not a number" x)))))
(defn butlast [coll] (defn butlast [coll]
"Returns coll except of last element." "Returns coll except of last element."
(drop-last 1 coll)) (drop-last 1 coll))
@ -66,7 +62,6 @@
(defn dec [n] (defn dec [n]
"Decrement n by 1" "Decrement n by 1"
(_numeric-check n)
(- n 1)) (- n 1))
(defn disassemble [tree &optional [codegen false]] (defn disassemble [tree &optional [codegen false]]
@ -170,7 +165,6 @@
(defn even? [n] (defn even? [n]
"Return true if n is an even number" "Return true if n is an even number"
(_numeric-check n)
(= (% n 2) 0)) (= (% n 2) 0))
(defn every? [pred coll] (defn every? [pred coll]
@ -239,7 +233,6 @@
(defn inc [n] (defn inc [n]
"Increment n by 1" "Increment n by 1"
(_numeric-check n)
(+ n 1)) (+ n 1))
(defn instance? [klass x] (defn instance? [klass x]
@ -325,7 +318,6 @@
(defn neg? [n] (defn neg? [n]
"Return true if n is < 0" "Return true if n is < 0"
(_numeric-check n)
(< n 0)) (< n 0))
(defn none? [x] (defn none? [x]
@ -347,7 +339,6 @@
(defn odd? [n] (defn odd? [n]
"Return true if n is an odd number" "Return true if n is an odd number"
(_numeric-check n)
(= (% n 2) 1)) (= (% n 2) 1))
(def -sentinel (object)) (def -sentinel (object))
@ -364,7 +355,6 @@
(defn pos? [n] (defn pos? [n]
"Return true if n is > 0" "Return true if n is > 0"
(_numeric_check n)
(> n 0)) (> n 0))
(defn rest [coll] (defn rest [coll]
@ -415,7 +405,6 @@
(defn zero? [n] (defn zero? [n]
"Return true if n is 0" "Return true if n is 0"
(_numeric_check n)
(= n 0)) (= n 0))
(defn read [&optional [from-file sys.stdin] (defn read [&optional [from-file sys.stdin]

View File

@ -30,6 +30,16 @@
[hy.models.symbol [HySymbol]] [hy.models.symbol [HySymbol]]
[hy._compat [PY33 PY34]]) [hy._compat [PY33 PY34]])
(defmacro as-> [head name &rest rest]
"Expands to sequence of assignments to the provided name, starting with head.
The previous result is thus available in the subsequent form. Returns the
final result, and leaves the name bound to it in the local scope. This behaves
much like the other threading macros, but requires you to specify the threading
point per form via the name instead of always the first or last arument."
`(do (setv
~name ~head
~@(interleave (repeat name) rest))
~name))
(defmacro with [args &rest body] (defmacro with [args &rest body]
"shorthand for nested with* loops: "shorthand for nested with* loops:

View File

@ -89,6 +89,15 @@ def import_file_to_module(module_name, fpath):
return mod return mod
def import_file_to_globals(env, module_name, fpath):
""" Import content from fpath and puts it into the dict provided
(e.g., for use in a REPL)
"""
mod = import_file_to_module(module_name, fpath)
for k, v in mod.__dict__.items():
env[k] = v
def import_buffer_to_module(module_name, buf): def import_buffer_to_module(module_name, buf):
try: try:
_ast = import_buffer_to_ast(buf, module_name) _ast = import_buffer_to_ast(buf, module_name)

View File

@ -397,6 +397,7 @@ def test_lambda_list_keywords_rest():
""" Ensure we can compile functions with lambda list keywords.""" """ Ensure we can compile functions with lambda list keywords."""
can_compile("(fn (x &rest xs) (print xs))") can_compile("(fn (x &rest xs) (print xs))")
cant_compile("(fn (x &rest xs &rest ys) (print xs))") cant_compile("(fn (x &rest xs &rest ys) (print xs))")
can_compile("(fn (&optional a &rest xs) (print xs))")
def test_lambda_list_keywords_key(): def test_lambda_list_keywords_key():
@ -410,6 +411,7 @@ def test_lambda_list_keywords_kwargs():
""" Ensure we can compile functions with &kwargs.""" """ Ensure we can compile functions with &kwargs."""
can_compile("(fn (x &kwargs kw) (list x kw))") can_compile("(fn (x &kwargs kw) (list x kw))")
cant_compile("(fn (x &kwargs xs &kwargs ys) (list x xs ys))") cant_compile("(fn (x &kwargs xs &kwargs ys) (list x xs ys))")
can_compile("(fn (&optional x &kwargs kw) (list x kw))")
def test_lambda_list_keywords_kwonly(): def test_lambda_list_keywords_kwonly():

View File

@ -19,6 +19,8 @@
;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER ;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;; DEALINGS IN THE SOFTWARE. ;; DEALINGS IN THE SOFTWARE.
(import [hy._compat [PY3]])
;;;; some simple helpers ;;;; some simple helpers
(defn assert-true [x] (defn assert-true [x]
@ -33,6 +35,12 @@
(defn assert-nil [x] (defn assert-nil [x]
(assert (is x nil))) (assert (is x nil)))
(defn assert-requires-num [f]
(for [x ["foo" [] None]]
(try (f x)
(except [TypeError] True)
(else (assert False)))))
(defn test-coll? [] (defn test-coll? []
"NATIVE: testing coll?" "NATIVE: testing coll?"
(assert-true (coll? [1 2 3])) (assert-true (coll? [1 2 3]))
@ -66,12 +74,7 @@
(assert-equal 0 (dec 1)) (assert-equal 0 (dec 1))
(assert-equal -1 (dec 0)) (assert-equal -1 (dec 0))
(assert-equal 0 (dec (dec 2))) (assert-equal 0 (dec (dec 2)))
(try (do (dec "foo") (assert False)) (assert-requires-num dec))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (dec []) (assert False))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (dec None) (assert False))
(except [e [TypeError]] (assert (in "not a number" (str e))))))
(defn test-setv [] (defn test-setv []
"NATIVE: testing setv mutation" "NATIVE: testing setv mutation"
@ -173,12 +176,7 @@
(assert-true (even? -2)) (assert-true (even? -2))
(assert-false (even? 1)) (assert-false (even? 1))
(assert-true (even? 0)) (assert-true (even? 0))
(try (even? "foo") (assert-requires-num even?))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (even? [])
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (even? None)
(except [e [TypeError]] (assert (in "not a number" (str e))))))
(defn test-every? [] (defn test-every? []
"NATIVE: testing the every? function" "NATIVE: testing the every? function"
@ -263,12 +261,11 @@
"NATIVE: testing the inc function" "NATIVE: testing the inc function"
(assert-equal 3 (inc 2)) (assert-equal 3 (inc 2))
(assert-equal 0 (inc -1)) (assert-equal 0 (inc -1))
(try (do (inc "foo") (assert False)) (assert-requires-num inc)
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (inc []) (assert False)) (defclass X [object]
(except [e [TypeError]] (assert (in "not a number" (str e))))) [__add__ (fn [self other] (.format "__add__ got {}" other))])
(try (do (inc None) (assert False)) (assert-equal (inc (X)) "__add__ got 1"))
(except [e [TypeError]] (assert (in "not a number" (str e))))))
(defn test-instance [] (defn test-instance []
"NATIVE: testing instance? function" "NATIVE: testing instance? function"
@ -394,24 +391,14 @@
(assert-true (neg? -2)) (assert-true (neg? -2))
(assert-false (neg? 1)) (assert-false (neg? 1))
(assert-false (neg? 0)) (assert-false (neg? 0))
(try (do (neg? "foo") (assert False)) (when PY3
(except [e [TypeError]] (assert (in "not a number" (str e))))) (assert-requires-num neg?)))
(try (do (neg? []) (assert False))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (neg? None) (assert False))
(except [e [TypeError]] (assert (in "not a number" (str e))))))
(defn test-zero [] (defn test-zero []
"NATIVE: testing the zero? function" "NATIVE: testing the zero? function"
(assert-false (zero? -2)) (assert-false (zero? -2))
(assert-false (zero? 1)) (assert-false (zero? 1))
(assert-true (zero? 0)) (assert-true (zero? 0)))
(try (do (zero? "foo") (assert False))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (zero? []) (assert False))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (zero? None) (assert False))
(except [e [TypeError]] (assert (in "not a number" (str e))))))
(defn test-none [] (defn test-none []
"NATIVE: testing for `is None`" "NATIVE: testing for `is None`"
@ -463,12 +450,7 @@
(assert-true (odd? -3)) (assert-true (odd? -3))
(assert-true (odd? 1)) (assert-true (odd? 1))
(assert-false (odd? 0)) (assert-false (odd? 0))
(try (do (odd? "foo") (assert False)) (assert-requires-num odd?))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (odd? []) (assert False))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (odd? None) (assert False))
(except [e [TypeError]] (assert (in "not a number" (str e))))))
(defn test-partition [] (defn test-partition []
"NATIVE: testing the partition function" "NATIVE: testing the partition function"
@ -500,12 +482,8 @@
(assert-true (pos? 2)) (assert-true (pos? 2))
(assert-false (pos? -1)) (assert-false (pos? -1))
(assert-false (pos? 0)) (assert-false (pos? 0))
(try (do (pos? "foo") (assert False)) (when PY3
(except [e [TypeError]] (assert (in "not a number" (str e))))) (assert-requires-num pos?)))
(try (do (pos? []) (assert False))
(except [e [TypeError]] (assert (in "not a number" (str e)))))
(try (do (pos? None) (assert False))
(except [e [TypeError]] (assert (in "not a number" (str e))))))
(defn test-remove [] (defn test-remove []
"NATIVE: testing the remove function" "NATIVE: testing the remove function"