Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make setv always return None #1257

Merged
merged 1 commit into from
Mar 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Changes from 0.12.1
* Added bytestring literals, which create `bytes` objects under Python 3
and `str` objects under Python 2
* Commas and underscores are allowed in numeric literals
* `setv` always returns None
* with-decorator: Allow a `setv` form as the form to be decorated
* xor: If exactly one argument is true, return it
* hy.core.reserved is now hy.extra.reserved
Expand All @@ -19,6 +20,7 @@ Changes from 0.12.1
parses to a false value (e.g., the empty string)
* A `yield` inside of a `with` statement will properly suppress implicit
returns.
* `setv` no longer unnecessarily tries to get attributes

Changes from 0.12.0

Expand Down
12 changes: 2 additions & 10 deletions hy/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2156,16 +2156,9 @@ def compile_def_expression(self, expression):
root))
else:
result = Result()
exprs = []
for tgt, target in zip(expression[::2], expression[1::2]):
item = self._compile_assign(tgt, target,
tgt.start_line, tgt.start_column)
result += item
exprs.append(item.force_expr)

result += ast.Tuple(elts=exprs, lineno=expression.start_line,
col_offset=expression.start_column,
ctx=ast.Load())
result += self._compile_assign(tgt, target, tgt.start_line,
tgt.start_column)
return result

def _compile_assign(self, name, result,
Expand Down Expand Up @@ -2196,7 +2189,6 @@ def _compile_assign(self, name, result,
targets=[st_name],
value=result.force_expr)

result += ld_name
return result

@builds("for*")
Expand Down
6 changes: 4 additions & 2 deletions tests/native_tests/core.hy
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@
(setv x 1)
(setv y 1)
(assert-equal x y)
(setv x (setv y 12))
(setv y 12)
(setv x y)
(assert-equal x 12)
(assert-equal y 12)
(setv x (setv y (fn [x] 9)))
(setv y (fn [x] 9))
(setv x y)
(assert-equal (x y) 9)
(assert-equal (y x) 9)
(try (do (setv a.b 1) (assert False))
Expand Down
43 changes: 36 additions & 7 deletions tests/native_tests/language.hy
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@
(assert (= #{} (set))))


(defn test-setv-empty []
"NATIVE: test setv works with no arguments"
(assert (is (setv) None)))


(defn test-setv-get []
"NATIVE: test setv works on a get expression"
(setv foo [0 1 2])
Expand All @@ -81,15 +76,49 @@

(defn test-setv-pairs []
"NATIVE: test that setv works on pairs of arguments"
(assert (= (setv a 1 b 2) (, 1 2)))
(setv a 1 b 2)
(assert (= a 1))
(assert (= b 2))
(setv y 0 x 1 y x)
(assert y)
(assert (= y 1))
(try (eval '(setv a 1 b))
(except [e [TypeError]] (assert (in "`setv' needs an even number of arguments" (str e))))))


(defn test-setv-returns-none []
"NATIVE: test that setv always returns None"

(assert (none? (setv)))
(assert (none? (setv x 1)))
(assert (= x 1))
(assert (none? (setv x 2)))
(assert (= x 2))
(assert (none? (setv y 2 z 3)))
(assert (= y 2))
(assert (= z 3))
(assert (none? (setv [y z] [7 8])))
(assert (= y 7))
(assert (= z 8))
(assert (none? (setv (, y z) [9 10])))
(assert (= y 9))
(assert (= z 10))

(setv p 11)
(setv p (setv q 12))
(assert (= q 12))
(assert (none? p))

; https://github.com/hylang/hy/issues/1052
(assert (none? (setv (get {} "x") 42)))
(setv l [])
(defclass Foo [object]
[__setattr__ (fn [self attr val]
(.append l [attr val]))])
(setv x (Foo))
(assert (none? (setv x.eggs "ham")))
(assert (not (hasattr x "eggs")))
(assert (= l [["eggs" "ham"]])))

(defn test-store-errors []
"NATIVE: test that setv raises the correct errors when given wrong argument types"
(try
Expand Down
4 changes: 2 additions & 2 deletions tests/native_tests/reader_macros.hy
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
(defn test-builtin-decorator-reader []
(defn increment-arguments [func]
"Increments each argument passed to the decorated function."
#@((wraps func)
(defn wrapper [&rest args &kwargs kwargs]
((wraps func)
(fn [&rest args &kwargs kwargs]
(apply func
(map inc args)
(dict-comp k (inc v) [[k v] (.items kwargs)])))))
Expand Down
4 changes: 2 additions & 2 deletions tests/native_tests/with_decorator.hy
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
(defn test-decorator-clobbing []
"NATIVE: Tests whether nested decorators work"
(do
(defn dec1 [f] (defn k [] (+ (f) 1)))
(defn dec2 [f] (defn k [] (+ (f) 2)))
(defn dec1 [f] (fn [] (+ (f) 1)))
(defn dec2 [f] (fn [] (+ (f) 2)))
(with-decorator dec1
(with-decorator dec2
(defn f [] 1)))
Expand Down