Rework code to find a function definition in case of decorated source lines

This commit is contained in:
Christian Zietz 2019-04-23 18:41:41 +01:00 committed by Ram Rachum
parent 45db942375
commit 016f4b9976

View file

@ -184,17 +184,24 @@ class Tracer:
line_no = frame.f_lineno
source_line = get_source_from_frame(frame)[line_no - 1]
if event == 'call':
# Skip lines containing function decorators to print actual
# function name
while source_line.lstrip()[0] == '@':
line_no += 1
source_line = get_source_from_frame(frame)[line_no - 1]
# Check that source_line is actually a function definition,
# otherwise fall back to original line
if not source_line.lstrip().startswith('def'):
line_no = frame.f_lineno
source_line = get_source_from_frame(frame)[line_no - 1]
if event == 'call' and len(source_line.lstrip()) > 0 and \
source_line.lstrip()[0] == '@':
# If a function decorator is found, skip lines until
# an actual function definition is found.
try:
def_line_no = line_no
def_source_line = get_source_from_frame(frame)[def_line_no]
while not def_source_line.lstrip().startswith('def'):
def_line_no += 1
def_source_line = get_source_from_frame(frame)[def_line_no]
else:
# Found a function definition
line_no = def_line_no
source_line = def_source_line
except IndexError:
# End of source file reached without finding a function definition.
# Fall back to original source line.
pass
self.write('{indent}{now_string} {event:9} '
'{line_no:4} {source_line}'.format(**locals()))