Fix package error message line numbers (#8271)

Line numbers were reported as zero-indexed, but we need to adjust.
This commit is contained in:
Adam J. Stewart 2018-06-02 22:53:18 -05:00 committed by Todd Gamblin
parent ff56d739a0
commit 9862c97d38

View file

@ -775,23 +775,31 @@ def make_stack(tb, stack=None):
if isinstance(obj, spack.package.PackageBase): if isinstance(obj, spack.package.PackageBase):
break break
# we found obj, the Package implementation we care about. # We found obj, the Package implementation we care about.
# point out the location in the install method where we failed. # Point out the location in the install method where we failed.
lines = [] lines = [
lines.append("%s:%d, in %s:" % ( '{0}:{1:d}, in {2}:'.format(
inspect.getfile(frame.f_code), frame.f_lineno, frame.f_code.co_name inspect.getfile(frame.f_code),
)) frame.f_lineno - 1, # subtract 1 because f_lineno is 0-indexed
frame.f_code.co_name
)
]
# Build a message showing context in the install method. # Build a message showing context in the install method.
sourcelines, start = inspect.getsourcelines(frame) sourcelines, start = inspect.getsourcelines(frame)
fl = frame.f_lineno - start # Calculate lineno of the error relative to the start of the function.
start_ctx = max(0, fl - context) # Subtract 1 because f_lineno is 0-indexed.
sourcelines = sourcelines[start_ctx:fl + context + 1] fun_lineno = frame.f_lineno - start - 1
start_ctx = max(0, fun_lineno - context)
sourcelines = sourcelines[start_ctx:fun_lineno + context + 1]
for i, line in enumerate(sourcelines): for i, line in enumerate(sourcelines):
is_error = start_ctx + i == fl is_error = start_ctx + i == fun_lineno
mark = ">> " if is_error else " " mark = '>> ' if is_error else ' '
marked = " %s%-6d%s" % (mark, start_ctx + i, line.rstrip()) # Add start to get lineno relative to start of file, not function.
marked = ' {0}{1:-6d}{2}'.format(
mark, start + start_ctx + i, line.rstrip())
if is_error: if is_error:
marked = colorize('@R{%s}' % marked) marked = colorize('@R{%s}' % marked)
lines.append(marked) lines.append(marked)