Keyword arguments to functions are required to be strings (#1023)

See discussion in #961
This commit is contained in:
timmartin 2016-04-11 16:38:13 +01:00 committed by Berker Peksag
parent 86db5e33ff
commit 9d6e04fab0
2 changed files with 10 additions and 0 deletions

View File

@ -547,6 +547,10 @@ class HyASTCompiler(object):
# defining keyword arguments. # defining keyword arguments.
it = iter(expr) it = iter(expr)
for k, v in zip(it, it): for k, v in zip(it, it):
if not isinstance(k, HyString):
raise HyTypeError(expr,
"Only strings can be used "
"as parameter names")
args.append(k) args.append(k)
ret += self.compile(v) ret += self.compile(v)
defaults.append(ret.force_expr) defaults.append(ret.force_expr)
@ -560,6 +564,10 @@ class HyASTCompiler(object):
else: else:
k = expr k = expr
v = HySymbol("None").replace(k) v = HySymbol("None").replace(k)
if not isinstance(k, HyString):
raise HyTypeError(expr,
"Only strings can be used as "
"parameter names")
args.append(k) args.append(k)
ret += self.compile(v) ret += self.compile(v)
defaults.append(ret.force_expr) defaults.append(ret.force_expr)

View File

@ -372,6 +372,7 @@ def test_ast_lambda_lists():
cant_compile('(fn [&key {"a" b} &key {"foo" bar}] [a foo])') cant_compile('(fn [&key {"a" b} &key {"foo" bar}] [a foo])')
cant_compile('(fn [&optional a &key {"foo" bar}] [a foo])') cant_compile('(fn [&optional a &key {"foo" bar}] [a foo])')
cant_compile('(fn [&optional [a b c]] a)') cant_compile('(fn [&optional [a b c]] a)')
cant_compile('(fn [&optional [1 2]] (list 1 2))')
def test_ast_print(): def test_ast_print():
@ -402,6 +403,7 @@ def test_lambda_list_keywords_key():
""" Ensure we can compile functions with &key.""" """ Ensure we can compile functions with &key."""
can_compile("(fn (x &key {foo True}) (list x foo))") can_compile("(fn (x &key {foo True}) (list x foo))")
cant_compile("(fn (x &key {bar \"baz\"} &key {foo 42}) (list x bar foo))") cant_compile("(fn (x &key {bar \"baz\"} &key {foo 42}) (list x bar foo))")
cant_compile("(fn (x &key {1 2 3 4}) (list x))")
def test_lambda_list_keywords_kwargs(): def test_lambda_list_keywords_kwargs():