merge branch 'gcl/defclass-allow-builtins'
This commit is contained in:
commit
2c2d679daf
@ -370,6 +370,7 @@ def checkargs(exact=None, min=None, max=None, even=None, multiple=None):
|
||||
class HyASTCompiler(object):
|
||||
|
||||
def __init__(self, module_name):
|
||||
self.allow_builtins = False
|
||||
self.anon_fn_count = 0
|
||||
self.anon_var_count = 0
|
||||
self.imports = defaultdict(set)
|
||||
@ -2012,7 +2013,8 @@ class HyASTCompiler(object):
|
||||
start_line, start_column):
|
||||
|
||||
str_name = "%s" % name
|
||||
if _is_hy_builtin(str_name, self.module_name):
|
||||
if _is_hy_builtin(str_name, self.module_name) and \
|
||||
not self.allow_builtins:
|
||||
raise HyTypeError(name,
|
||||
"Can't assign to a builtin: `%s'" % str_name)
|
||||
|
||||
@ -2282,6 +2284,8 @@ class HyASTCompiler(object):
|
||||
docstring.start_column)
|
||||
body += body.expr_as_stmt()
|
||||
|
||||
allow_builtins = self.allow_builtins
|
||||
self.allow_builtins = True
|
||||
if expressions and isinstance(expressions[0], HyList) \
|
||||
and not isinstance(expressions[0], HyExpression):
|
||||
expr = expressions.pop(0)
|
||||
@ -2294,6 +2298,8 @@ class HyASTCompiler(object):
|
||||
for expression in expressions:
|
||||
body += self.compile(macroexpand(expression, self.module_name))
|
||||
|
||||
self.allow_builtins = allow_builtins
|
||||
|
||||
if not body.stmts:
|
||||
body += ast.Pass(lineno=expressions.start_line,
|
||||
col_offset=expressions.start_column)
|
||||
|
@ -551,3 +551,17 @@ def test_defn():
|
||||
cant_compile("(defn \"hy\" [] 1)")
|
||||
cant_compile("(defn :hy [] 1)")
|
||||
can_compile("(defn &hy [] 1)")
|
||||
|
||||
|
||||
def test_setv_builtins():
|
||||
"""Ensure that assigning to a builtin fails, unless in a class"""
|
||||
cant_compile("(setv nil 42)")
|
||||
cant_compile("(defn get [&rest args] 42)")
|
||||
can_compile("(defclass A [] (defn get [self] 42))")
|
||||
can_compile("""
|
||||
(defclass A []
|
||||
(defn get [self] 42)
|
||||
(defclass B []
|
||||
(defn get [self] 42))
|
||||
(defn if [self] 0))
|
||||
""")
|
||||
|
Loading…
Reference in New Issue
Block a user