mirror of
https://github.com/cool-RR/PySnooper.git
synced 2026-01-23 18:24:44 +00:00
Rework code to find a function definition in case of decorated source lines
This commit is contained in:
parent
45db942375
commit
016f4b9976
1 changed files with 18 additions and 11 deletions
|
|
@ -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()))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue