Type check `eval` parameters

This commit is contained in:
Gregor Best 2015-07-28 16:49:22 +02:00
parent d95bfbaa8e
commit 48e654596d
2 changed files with 29 additions and 0 deletions

View File

@ -723,13 +723,20 @@ class HyASTCompiler(object):
def compile_eval(self, expr):
expr.pop(0)
if not isinstance(expr[0], (HyExpression, HySymbol)):
raise HyTypeError(expr, "expression expected as first argument")
elist = [HySymbol("hy_eval")] + [expr[0]]
if len(expr) >= 2:
if not isinstance(expr[1], (HyDict, HySymbol)):
raise HyTypeError(expr, "Globals must be a dictionary")
elist.append(expr[1])
else:
elist.append(HyExpression([HySymbol("locals")]))
if len(expr) == 3:
if not isinstance(expr[2], HyString):
raise HyTypeError(expr, "Module name must be a string")
elist.append(expr[2])
else:
elist.append(HyString(self.module_name))

View File

@ -833,6 +833,19 @@
(assert (= 27 (eval (+ (quote (*)) (* [(quote 3)] 3)))))
(assert (= None (eval (quote (print ""))))))
(defmacro assert-throw [exc-type &rest body]
`(try
(do
(eval ~@body)
(assert False "we shouldn't have arrived here"))
(catch [e Exception]
(assert (instance? ~exc-type e)
(.format "Expected exception of type {}, got {}: {}"
(. ~exc-type --name--)
(. (type e) --name--)
(str e))))))
(defn test-eval-globals []
"NATIVE: test eval with explicit global dict"
(assert (= 'bar (eval (quote foo) {'foo 'bar})))
@ -848,6 +861,15 @@
(catch [e Exception]
(assert (isinstance e NameError))))))
(defn test-eval-failure []
"NATIVE: test eval failure modes"
(import [hy.errors [HyTypeError]])
(assert-throw HyTypeError '(eval))
(assert-throw HyTypeError '(eval "snafu"))
(assert-throw HyTypeError '(eval 'false []))
(assert-throw HyTypeError '(eval 'false {} 1)))
(defn test-import-syntax []
"NATIVE: test the import syntax."