Merge pull request #1586 from Kodiologist/multi-expr-try
Fix a bug that caused `try` to drop expressions
This commit is contained in:
commit
7e1b890d7c
1
NEWS.rst
1
NEWS.rst
@ -50,6 +50,7 @@ Bug Fixes
|
|||||||
* Fixed bugs that caused `defclass` to drop statements or crash
|
* Fixed bugs that caused `defclass` to drop statements or crash
|
||||||
* Fixed a REPL crash caused by illegle unicode escape string inputs
|
* Fixed a REPL crash caused by illegle unicode escape string inputs
|
||||||
* `NaN` can no longer create an infinite loop during macro-expansion
|
* `NaN` can no longer create an infinite loop during macro-expansion
|
||||||
|
* Fixed a bug that caused `try` to drop expressions
|
||||||
|
|
||||||
Misc. Improvements
|
Misc. Improvements
|
||||||
----------------------------
|
----------------------------
|
||||||
|
@ -790,13 +790,14 @@ class HyASTCompiler(object):
|
|||||||
expr.pop(0) # try
|
expr.pop(0) # try
|
||||||
|
|
||||||
# (try something somethingelse…)
|
# (try something somethingelse…)
|
||||||
body = Result()
|
body = []
|
||||||
# Check against HyExpression and HySymbol to avoid incorrectly
|
# Check against HyExpression and HySymbol to avoid incorrectly
|
||||||
# matching [except ...] or ("except" ...)
|
# matching [except ...] or ("except" ...)
|
||||||
while expr and not (isinstance(expr[0], HyExpression)
|
while expr and not (isinstance(expr[0], HyExpression)
|
||||||
and isinstance(expr[0][0], HySymbol)
|
and isinstance(expr[0][0], HySymbol)
|
||||||
and expr[0][0] in ("except", "else", "finally")):
|
and expr[0][0] in ("except", "else", "finally")):
|
||||||
body += self.compile(expr.pop(0))
|
body.append(expr.pop(0))
|
||||||
|
body = self._compile_branch(body)
|
||||||
|
|
||||||
var = self.get_anon_var()
|
var = self.get_anon_var()
|
||||||
name = asty.Name(expr, id=ast_str(var), ctx=ast.Store())
|
name = asty.Name(expr, id=ast_str(var), ctx=ast.Store())
|
||||||
@ -805,8 +806,6 @@ class HyASTCompiler(object):
|
|||||||
returnable = Result(expr=expr_name, temp_variables=[expr_name, name],
|
returnable = Result(expr=expr_name, temp_variables=[expr_name, name],
|
||||||
contains_yield=body.contains_yield)
|
contains_yield=body.contains_yield)
|
||||||
|
|
||||||
if not all(expr):
|
|
||||||
raise HyTypeError(expr, "Empty list not allowed in `try'")
|
|
||||||
handler_results = Result()
|
handler_results = Result()
|
||||||
handlers = []
|
handlers = []
|
||||||
while expr and expr[0][0] == HySymbol("except"):
|
while expr and expr[0][0] == HySymbol("except"):
|
||||||
@ -846,7 +845,6 @@ class HyASTCompiler(object):
|
|||||||
|
|
||||||
body += body.expr_as_stmt() if orelse else asty.Assign(
|
body += body.expr_as_stmt() if orelse else asty.Assign(
|
||||||
expr, targets=[name], value=body.force_expr)
|
expr, targets=[name], value=body.force_expr)
|
||||||
|
|
||||||
body = body.stmts or [asty.Pass(expr)]
|
body = body.stmts or [asty.Pass(expr)]
|
||||||
|
|
||||||
if PY3:
|
if PY3:
|
||||||
|
@ -584,18 +584,27 @@
|
|||||||
(do))
|
(do))
|
||||||
|
|
||||||
|
|
||||||
(defn test-exceptions []
|
(defn test-try []
|
||||||
"NATIVE: test Exceptions"
|
|
||||||
|
|
||||||
(try (do) (except []))
|
(try (do) (except []))
|
||||||
|
|
||||||
(try (do) (except [IOError]) (except []))
|
(try (do) (except [IOError]) (except []))
|
||||||
|
|
||||||
; test that multiple expressions in a try get evaluated
|
; test that multiple statements in a try get evaluated
|
||||||
(setv value 0)
|
(setv value 0)
|
||||||
(try (+= value 1) (+= value 2) (except [IOError]) (except []))
|
(try (+= value 1) (+= value 2) (except [IOError]) (except []))
|
||||||
(assert (= value 3))
|
(assert (= value 3))
|
||||||
|
|
||||||
|
; test that multiple expressions in a try get evaluated
|
||||||
|
; https://github.com/hylang/hy/issues/1584
|
||||||
|
(setv l [])
|
||||||
|
(defn f [] (.append l 1))
|
||||||
|
(try (f) (f) (f) (except [IOError]))
|
||||||
|
(assert (= l [1 1 1]))
|
||||||
|
(setv l [])
|
||||||
|
(try (f) (f) (f) (except [IOError]) (else (f)))
|
||||||
|
(assert (= l [1 1 1 1]))
|
||||||
|
|
||||||
;; Test correct (raise)
|
;; Test correct (raise)
|
||||||
(setv passed False)
|
(setv passed False)
|
||||||
(try
|
(try
|
||||||
|
Loading…
Reference in New Issue
Block a user