From b886f2b504c4b0ce89df6199e4deb8b3150fe04d Mon Sep 17 00:00:00 2001 From: iory Date: Sat, 18 Apr 2020 09:12:44 +0900 Subject: [PATCH] Enable multi call --- pysnooper/tracer.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pysnooper/tracer.py b/pysnooper/tracer.py index 8d303ae..8530589 100644 --- a/pysnooper/tracer.py +++ b/pysnooper/tracer.py @@ -217,6 +217,7 @@ class Tracer: for v in utils.ensure_tuple(watch_explode) ] self.frame_to_local_reprs = {} + self.start_times = {} self.depth = depth self.prefix = prefix self.thread_info = thread_info @@ -302,7 +303,7 @@ class Tracer: 'original_trace_functions', [] ) stack.append(sys.gettrace()) - self.start_time = datetime_module.datetime.now() + self.start_times[id(calling_frame)] = datetime_module.datetime.now() sys.settrace(self.trace) def __exit__(self, exc_type, exc_value, exc_traceback): @@ -314,10 +315,10 @@ class Tracer: self.target_frames.discard(calling_frame) self.frame_to_local_reprs.pop(calling_frame, None) - if thread_global.depth == -1: - duration = datetime_module.datetime.now() - self.start_time - now_string = pycompat.timedelta_isoformat(duration, timespec='microseconds') - self.write('Total elapsed time: {now_string}'.format(**locals())) + start_time = self.start_times.pop(id(calling_frame)) + duration = datetime_module.datetime.now() - start_time + now_string = pycompat.timedelta_isoformat(duration, timespec='microseconds') + self.write('Total elapsed time: {now_string}'.format(**locals())) def _is_internal_frame(self, frame): return frame.f_code.co_filename == Tracer.__enter__.__code__.co_filename @@ -364,7 +365,9 @@ class Tracer: ### Finished checking whether we should trace this line. ############## if self.elapsed_time: - duration = datetime_module.datetime.now() - self.start_time + calling_frame = frame.f_back + duration = datetime_module.datetime.now() - self.start_times[ + id(calling_frame)] now_string = pycompat.timedelta_isoformat( duration, timespec='microseconds') if not self.normalize else ' ' * 15 else: