Give an error on empty attributes (#1138)

Give an error on empty attributes

closes #1137
This commit is contained in:
Ryan Gonzalez 2016-11-29 23:00:48 -06:00 committed by Tuukka Turto
parent 4e93fcba8a
commit 24ebbc611e
2 changed files with 22 additions and 0 deletions

View File

@ -2588,6 +2588,19 @@ class HyASTCompiler(object):
def compile_symbol(self, symbol): def compile_symbol(self, symbol):
if "." in symbol: if "." in symbol:
glob, local = symbol.rsplit(".", 1) glob, local = symbol.rsplit(".", 1)
if not glob:
raise HyTypeError(symbol, 'cannot access attribute on '
'anything other than a name '
'(in order to get attributes of'
'expressions, use '
'`(. <expression> {attr})` or '
'`(.{attr} <expression>)`)'.format(
attr=local))
if not local:
raise HyTypeError(symbol, 'cannot access empty attribute')
glob = HySymbol(glob).replace(symbol) glob = HySymbol(glob).replace(symbol)
ret = self.compile_symbol(glob) ret = self.compile_symbol(glob)

View File

@ -542,6 +542,15 @@ def test_attribute_access():
cant_compile("(. foo bar baz [0] quux {frob})") cant_compile("(. foo bar baz [0] quux {frob})")
def test_attribute_empty():
"""Ensure using dot notation with a non-expression is an error"""
cant_compile(".")
cant_compile("foo.")
cant_compile(".foo")
cant_compile('"bar".foo')
cant_compile('[2].foo')
def test_cons_correct(): def test_cons_correct():
"""Ensure cons gets compiled correctly""" """Ensure cons gets compiled correctly"""
can_compile("(cons a b)") can_compile("(cons a b)")