Added nonlocal keyword for python3.

This commit is contained in:
unmerged 2014-12-14 23:14:19 +03:00
parent cfa805c102
commit 41e5175781
2 changed files with 33 additions and 0 deletions

View File

@ -1033,6 +1033,27 @@ class HyASTCompiler(object):
lineno=expr.start_line,
col_offset=expr.start_column)
@builds("nonlocal")
@checkargs(min=1)
def compile_nonlocal_expression(self, expr):
if not PY3:
raise HyCompileError(
"nonlocal only supported in python 3!")
expr.pop(0) # nonlocal
names = []
while len(expr) > 0:
identifier = expr.pop(0)
name = ast_str(identifier)
names.append(name)
if not isinstance(identifier, HySymbol):
raise HyTypeError(identifier, "(nonlocal) arguments must "
"be Symbols.")
return ast.Nonlocal(names=names,
lineno=expr.start_line,
col_offset=expr.start_column)
@builds("yield")
@checkargs(max=1)
def compile_yield_expression(self, expr):

View File

@ -220,6 +220,18 @@ def test_ast_bad_global():
cant_compile("(global (foo))")
if PY3:
def test_ast_good_nonlocal():
"Make sure AST can compile valid nonlocal"
can_compile("(nonlocal a)")
can_compile("(nonlocal foo bar)")
def test_ast_bad_nonlocal():
"Make sure AST can't compile invalid nonlocal"
cant_compile("(nonlocal)")
cant_compile("(nonlocal (foo))")
def test_ast_good_defclass():
"Make sure AST can compile valid defclass"
can_compile("(defclass a)")