Skip to content

Commit

Permalink
Merge pull request #1520 from Kodiologist/test-parens
Browse files Browse the repository at this point in the history
Make unary comparison ops evaluate their argument
  • Loading branch information
Kodiologist authored Mar 13, 2018
2 parents 6d977ab + 7fcc7ac commit d947a27
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
6 changes: 6 additions & 0 deletions NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
Unreleased
==============================

Other Breaking Changes
------------------------------
* Non-shadow unary `=`, `is`, `<`, etc. now evaluate their argument
instead of ignoring it. This change increases consistency a bit
and makes accidental unary uses easier to notice.

Bug Fixes
------------------------------
* Fix `(return)` so it works correctly to exit a Python 2 generator
Expand Down
3 changes: 2 additions & 1 deletion hy/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1574,7 +1574,8 @@ def _compile_compare_op_expression(self, expression):
@checkargs(min=1)
def compile_compare_op_expression(self, expression):
if len(expression) == 2:
return asty.Name(expression, id="True", ctx=ast.Load())
return (self.compile(expression[1]) +
asty.Name(expression, id="True", ctx=ast.Load()))
return self._compile_compare_op_expression(expression)

@builds("!=", "is_not")
Expand Down
4 changes: 2 additions & 2 deletions tests/native_tests/language.hy
Original file line number Diff line number Diff line change
Expand Up @@ -1815,5 +1815,5 @@ macros()

(defn test-relative-import []
"Make sure relative imports work properly"
(import [..resources [tlib]]))
(assert (= (tlib.*secret-message* "Hello World")))
(import [..resources [tlib]])
(assert (= tlib.*secret-message* "Hello World")))
15 changes: 12 additions & 3 deletions tests/native_tests/operators.hy
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@

(op-and-shadow-test ~
(forbid (f))
(assert (= (f (chr 0b00101111)
(chr 0b11010000))))
(forbid (f (chr 0b00101111) (chr 0b11010000))))
(assert (= (& (f 0b00101111) 0xFF)
0b11010000))
(forbid (f 0b00101111 0b11010000)))


(op-and-shadow-test <
Expand Down Expand Up @@ -219,7 +219,15 @@

(op-and-shadow-test [= is]
(forbid (f))

(assert (is (f "hello") True))

; Unary comparison operators, despite always returning True,
; should evaluate their argument.
(setv p "a")
(assert (is (f (do (setv p "b") "hello")) True))
(assert (= p "b"))

(defclass C)
(setv x (get {"is" (C) "=" 0} f-name))
(setv y (get {"is" (C) "=" 1} f-name))
Expand All @@ -229,6 +237,7 @@
(assert (is (f y x) False))
(assert (is (f x x x x x) True))
(assert (is (f x x x y x) False))

(setv n None)
(assert (is (f n None) True))
(assert (is (f n "b") False)))
Expand Down
2 changes: 1 addition & 1 deletion tests/native_tests/tag_macros.hy
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
(deftag + [n]
(+ n 1))

(assert (= #+2 3)))
(assert (= #+ 2 3)))


(defn test-tag-macros-macros []
Expand Down

0 comments on commit d947a27

Please sign in to comment.