Make with return the last expression from its branch

Thanks @rwtolbert for the bug report
This commit is contained in:
Nicolas Dandrimont 2013-07-19 00:43:06 +02:00
parent ad86dff754
commit a17dcdbffb
2 changed files with 28 additions and 5 deletions

View File

@ -1200,11 +1200,18 @@ class HyASTCompiler(object):
thing = self._storeize(self.compile(args.pop(0))) thing = self._storeize(self.compile(args.pop(0)))
body = self._compile_branch(expr) body = self._compile_branch(expr)
body += body.expr_as_stmt()
if not body.stmts: var = self.get_anon_var()
body += ast.Pass(lineno=expr.start_line, name = ast.Name(id=ast_str(var), arg=ast_str(var),
col_offset=expr.start_column) ctx=ast.Store(),
lineno=expr.start_line,
col_offset=expr.start_column)
# Store the result of the body in a tempvar
body += ast.Assign(targets=[name],
value=body.force_expr,
lineno=expr.start_line,
col_offset=expr.start_column)
the_with = ast.With(context_expr=ctx.force_expr, the_with = ast.With(context_expr=ctx.force_expr,
lineno=expr.start_line, lineno=expr.start_line,
@ -1216,7 +1223,16 @@ class HyASTCompiler(object):
the_with.items = [ast.withitem(context_expr=ctx.force_expr, the_with.items = [ast.withitem(context_expr=ctx.force_expr,
optional_vars=thing)] optional_vars=thing)]
return ctx + the_with ret = ctx + the_with
# And make our expression context our temp variable
expr_name = ast.Name(id=ast_str(var), arg=ast_str(var),
ctx=ast.Load(),
lineno=expr.start_line,
col_offset=expr.start_column)
ret += Result(expr=expr_name, temp_variables=[expr_name, name])
return ret
@builds(",") @builds(",")
def compile_tuple(self, expr): def compile_tuple(self, expr):

View File

@ -411,6 +411,13 @@
(with [(open "README.md" "r")] (do))) (with [(open "README.md" "r")] (do)))
(defn test-with-return []
"NATIVE: test that with returns stuff"
(defn read-file [filename]
(with [fd (open filename "r")] (.read fd)))
(assert (!= 0 (len (read-file "README.md")))))
(defn test-for-doodle [] (defn test-for-doodle []
"NATIVE: test for-do" "NATIVE: test for-do"
(do (do (do (do (do (do (do (do (do (setv (, x y) (, 0 0))))))))))) (do (do (do (do (do (do (do (do (do (setv (, x y) (, 0 0)))))))))))