implmenting with

This commit is contained in:
Paul R. Tagliamonte 2013-03-24 02:04:44 -04:00
parent ed310197dd
commit dd6a883060
2 changed files with 35 additions and 0 deletions

View File

@ -309,6 +309,33 @@ class HyASTCompiler(object):
fn.decorator_list = [self.compile(x) for x in expr]
return fn
@builds("with")
def compile_with_expression(self, expr):
expr.pop(0) # with
return ast.With(context_expr=self.compile(expr.pop(0)),
lineno=expr.start_line,
col_offset=expr.start_column,
optional_vars=None,
body=self._mangle_branch([
self.compile(x) for x in expr]))
@builds("with_as")
def compile_with_as_expression(self, expr):
expr.pop(0) # with-as
ctx = self.compile(expr.pop(0))
thing = self.compile(expr.pop(0))
thing.ctx = ast.Store()
# Module(body=[With(items=[withitem(context_expr=Name(id='foo', ctx=Load()), optional_vars=Name(id='e', ctx=Store()))], body=[Pass()])])
# Module(body=[With(context_expr=Name(id='foo', ctx=Load()), optional_vars=Name(id='e', ctx=Store()), body=[Pass()])])
return ast.With(context_expr=ctx,
lineno=expr.start_line,
col_offset=expr.start_column,
optional_vars=thing,
body=self._mangle_branch([
self.compile(x) for x in expr]))
@builds("kwapply")
def compile_kwapply_expression(self, expr):
expr.pop(0) # kwapply

View File

@ -194,3 +194,11 @@
(defn test-importas []
"NATIVE: test import as"
(assert (!= (len systest.path) 0)))
(defn test-context []
"NATIVE: test with"
(with (open "README.md" "r")
(pass))
(with-as (open "README.md" "r") fd
(pass)))