More updates, including from Foxboron, for errors like (for)

This commit is contained in:
Bob Tolbert 2013-12-26 09:36:45 -07:00
parent f064d3f121
commit 765dba3e56
8 changed files with 60 additions and 13 deletions

View File

@ -80,6 +80,7 @@ class HyREPL(code.InteractiveConsole):
filename=filename)
def runsource(self, source, filename='<input>', symbol='single'):
global SIMPLE_TRACEBACKS
try:
tokens = tokenize(source)
except PrematureEndOfInput:
@ -88,7 +89,7 @@ class HyREPL(code.InteractiveConsole):
if e.source is None:
e.source = source
e.filename = filename
print(e)
sys.stderr.write(str(e))
return False
try:
@ -100,7 +101,10 @@ class HyREPL(code.InteractiveConsole):
if e.source is None:
e.source = source
e.filename = filename
print(e)
if SIMPLE_TRACEBACKS:
sys.stderr.write(str(e))
else:
self.showtraceback()
return False
except Exception:
self.showtraceback()
@ -172,7 +176,7 @@ def run_command(source):
import_buffer_to_module("__main__", source)
except (HyTypeError, LexException) as e:
if SIMPLE_TRACEBACKS:
print(e)
sys.stderr.write(str(e))
return 1
raise
except Exception:
@ -186,7 +190,7 @@ def run_file(filename):
import_file_to_module("__main__", filename)
except (HyTypeError, LexException) as e:
if SIMPLE_TRACEBACKS:
print(e)
sys.stderr.write(str(e))
return 1
raise
except Exception:

View File

@ -35,13 +35,14 @@ from hy.models.float import HyFloat
from hy.models.list import HyList
from hy.models.dict import HyDict
from hy.errors import HyMacroExpansionError, HyCompileError, HyTypeError
from hy.errors import HyCompileError, HyTypeError
import hy.macros
from hy.macros import require, macroexpand
from hy._compat import str_type, long_type
import hy.importer
import traceback
import importlib
import codecs
import ast

View File

@ -28,7 +28,7 @@
(defmacro macro-error [location reason]
"error out properly within a macro"
`(raise (hy.compiler.HyTypeError ~location ~reason)))
`(raise (hy.errors.HyMacroExpansionError ~location ~reason)))
(defmacro defmacro-alias [names lambda-list &rest body]

View File

@ -31,6 +31,8 @@
(for* [x foo]
(for* [y bar]
baz))"
(if (empty? body)
(macro-error None "`for' requires a body to evaluate"))
(if args
`(for* ~(.pop args 0) (for ~args ~@body))
`(do ~@body)))

View File

@ -1,3 +1,5 @@
# -*- encoding: utf-8 -*-
#
# Copyright (c) 2013 Paul Tagliamonte <paultag@debian.org>
# Copyright (c) 2013 Bob Tolbert <bob@tolbert.org>
#
@ -131,9 +133,9 @@ class HyTypeError(TypeError):
result += ' %s\n' % colored.red("".join(source[-1]))
result += ' %s\n' % colored.green('-'*(end-1) + '^')
result += colored.yellow("%s: %s\n\n" % (self.__class__.__name__,
self.message))
result += colored.yellow("%s: %s\n\n" %
(self.__class__.__name__,
self.message))
return result

View File

@ -28,7 +28,7 @@ from hy.models.complex import HyComplex
from hy.models.dict import HyDict
from hy._compat import str_type, long_type
from hy.errors import HyMacroExpansionError
from hy.errors import HyTypeError, HyMacroExpansionError
from collections import defaultdict
import sys
@ -196,8 +196,13 @@ def macroexpand_1(tree, module_name):
if m is not None:
try:
obj = _wrap_value(m(*ntree[1:]))
except HyTypeError as e:
if e.expression is None:
e.expression = tree
raise
except Exception as e:
msg = str(tree[0]) + " " + " ".join(str(e).split()[1:])
msg = "`" + str(tree[0]) + "' " + \
" ".join(str(e).split()[1:])
raise HyMacroExpansionError(tree, msg)
obj.replace(tree)
return obj

View File

@ -23,7 +23,9 @@ from __future__ import unicode_literals
from hy import HyString
from hy.models import HyObject
from hy.compiler import hy_compile, HyCompileError, HyTypeError
from hy.compiler import hy_compile
from hy.errors import HyCompileError, HyTypeError
from hy.lex.exceptions import LexException
from hy.lex import tokenize
import ast
@ -431,3 +433,34 @@ def test_compile_error():
assert(e.message == "`=' needs at least 2 arguments, got 1.")
else:
assert(False)
def test_for_compile_error():
"""Ensure we get compile error in tricky 'for' cases"""
try:
can_compile("(fn [] (for)")
except LexException as e:
assert(e.message == "Premature end of input")
else:
assert(False)
try:
can_compile("(fn [] (for)))")
except LexException as e:
assert(e.message == "Ran into a RPAREN where it wasn't expected.")
else:
assert(False)
try:
can_compile("(fn [] (for [x]))")
except HyTypeError as e:
assert(e.message == "`for' requires an even number of elements in its first argument") # noqa
else:
assert(False)
try:
can_compile("(fn [] (for [x xx]))")
except HyTypeError as e:
assert(e.message == "`for' requires a body to evaluate")
else:
assert(False)

View File

@ -64,7 +64,7 @@ def test_bin_hy_cmd():
ret = run_cmd("hy -c \"(koan\"")
assert ret[0] == 1
assert "Premature end of input" in ret[1]
assert "Premature end of input" in ret[2]
def test_bin_hy_icmd():