Fix a crash in HyTypeError.__str__
This commit is contained in:
parent
db210929d0
commit
39785b4657
2
NEWS
2
NEWS
@ -39,6 +39,8 @@ Changes from 0.13.0
|
|||||||
* Fixed a crash when `with` suppresses an exception. `with` now returns
|
* Fixed a crash when `with` suppresses an exception. `with` now returns
|
||||||
`None` in this case.
|
`None` in this case.
|
||||||
* Fixed a crash when --repl-output-fn raises an exception
|
* Fixed a crash when --repl-output-fn raises an exception
|
||||||
|
* Fixed a crash when HyTypeError was raised with objects that had no
|
||||||
|
source position
|
||||||
* `assoc` now evaluates its arguments only once each
|
* `assoc` now evaluates its arguments only once each
|
||||||
* `break` and `continue` now raise an error when given arguments
|
* `break` and `continue` now raise an error when given arguments
|
||||||
instead of silently ignoring them
|
instead of silently ignoring them
|
||||||
|
68
hy/errors.py
68
hy/errors.py
@ -43,41 +43,47 @@ class HyTypeError(TypeError):
|
|||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|
||||||
line = self.expression.start_line
|
|
||||||
start = self.expression.start_column
|
|
||||||
end = self.expression.end_column
|
|
||||||
|
|
||||||
source = []
|
|
||||||
if self.source is not None:
|
|
||||||
source = self.source.split("\n")[line-1:self.expression.end_line]
|
|
||||||
|
|
||||||
if line == self.expression.end_line:
|
|
||||||
length = end - start
|
|
||||||
else:
|
|
||||||
length = len(source[0]) - start
|
|
||||||
|
|
||||||
result = ""
|
result = ""
|
||||||
|
|
||||||
result += ' File "%s", line %d, column %d\n\n' % (self.filename,
|
if all(getattr(self.expression, x, None) is not None
|
||||||
line,
|
for x in ("start_line", "start_column", "end_column")):
|
||||||
start)
|
|
||||||
|
|
||||||
if len(source) == 1:
|
line = self.expression.start_line
|
||||||
result += ' %s\n' % colored.red(source[0])
|
start = self.expression.start_column
|
||||||
result += ' %s%s\n' % (' '*(start-1),
|
end = self.expression.end_column
|
||||||
colored.green('^' + '-'*(length-1) + '^'))
|
|
||||||
if len(source) > 1:
|
|
||||||
result += ' %s\n' % colored.red(source[0])
|
|
||||||
result += ' %s%s\n' % (' '*(start-1),
|
|
||||||
colored.green('^' + '-'*length))
|
|
||||||
if len(source) > 2: # write the middle lines
|
|
||||||
for line in source[1:-1]:
|
|
||||||
result += ' %s\n' % colored.red("".join(line))
|
|
||||||
result += ' %s\n' % colored.green("-"*len(line))
|
|
||||||
|
|
||||||
# write the last line
|
source = []
|
||||||
result += ' %s\n' % colored.red("".join(source[-1]))
|
if self.source is not None:
|
||||||
result += ' %s\n' % colored.green('-'*(end-1) + '^')
|
source = self.source.split("\n")[line-1:self.expression.end_line]
|
||||||
|
|
||||||
|
if line == self.expression.end_line:
|
||||||
|
length = end - start
|
||||||
|
else:
|
||||||
|
length = len(source[0]) - start
|
||||||
|
|
||||||
|
result += ' File "%s", line %d, column %d\n\n' % (self.filename,
|
||||||
|
line,
|
||||||
|
start)
|
||||||
|
|
||||||
|
if len(source) == 1:
|
||||||
|
result += ' %s\n' % colored.red(source[0])
|
||||||
|
result += ' %s%s\n' % (' '*(start-1),
|
||||||
|
colored.green('^' + '-'*(length-1) + '^'))
|
||||||
|
if len(source) > 1:
|
||||||
|
result += ' %s\n' % colored.red(source[0])
|
||||||
|
result += ' %s%s\n' % (' '*(start-1),
|
||||||
|
colored.green('^' + '-'*length))
|
||||||
|
if len(source) > 2: # write the middle lines
|
||||||
|
for line in source[1:-1]:
|
||||||
|
result += ' %s\n' % colored.red("".join(line))
|
||||||
|
result += ' %s\n' % colored.green("-"*len(line))
|
||||||
|
|
||||||
|
# write the last line
|
||||||
|
result += ' %s\n' % colored.red("".join(source[-1]))
|
||||||
|
result += ' %s\n' % colored.green('-'*(end-1) + '^')
|
||||||
|
|
||||||
|
else:
|
||||||
|
result += ' File "%s", unknown location\n' % self.filename
|
||||||
|
|
||||||
result += colored.yellow("%s: %s\n\n" %
|
result += colored.yellow("%s: %s\n\n" %
|
||||||
(self.__class__.__name__,
|
(self.__class__.__name__,
|
||||||
|
@ -138,6 +138,16 @@ def test_bin_hy_stdin_except_do():
|
|||||||
assert "zzz" in output
|
assert "zzz" in output
|
||||||
|
|
||||||
|
|
||||||
|
def test_bin_hy_stdin_unlocatable_hytypeerror():
|
||||||
|
# https://github.com/hylang/hy/issues/1412
|
||||||
|
# The chief test of interest here is the returncode assertion
|
||||||
|
# inside run_cmd.
|
||||||
|
_, err = run_cmd("hy", """
|
||||||
|
(import hy.errors)
|
||||||
|
(raise (hy.errors.HyTypeError '[] (+ "A" "Z")))""")
|
||||||
|
assert "AZ" in err
|
||||||
|
|
||||||
|
|
||||||
def test_bin_hy_stdin_bad_repr():
|
def test_bin_hy_stdin_bad_repr():
|
||||||
# https://github.com/hylang/hy/issues/1389
|
# https://github.com/hylang/hy/issues/1389
|
||||||
output, err = run_cmd("hy", """
|
output, err = run_cmd("hy", """
|
||||||
|
Loading…
Reference in New Issue
Block a user