From 45db942375b411c0a037e9f59d649fde4e2a89ef Mon Sep 17 00:00:00 2001 From: Christian Zietz Date: Tue, 23 Apr 2019 17:51:46 +0100 Subject: [PATCH] Be safe and check that a function definition is found --- pysnooper/tracer.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pysnooper/tracer.py b/pysnooper/tracer.py index d69817e..56e36d4 100644 --- a/pysnooper/tracer.py +++ b/pysnooper/tracer.py @@ -183,12 +183,19 @@ class Tracer: now_string = datetime_module.datetime.now().time().isoformat() 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] + self.write('{indent}{now_string} {event:9} ' '{line_no:4} {source_line}'.format(**locals())) return self.trace