Allow using snoop as context manager

This commit is contained in:
Alex Hall 2019-05-04 12:25:48 +02:00 committed by Ram Rachum
parent 6d78ee4c84
commit b41b37fbe3
1 changed files with 7 additions and 0 deletions

View File

@ -1,6 +1,7 @@
# Copyright 2019 Ram Rachum and collaborators.
# This program is distributed under the MIT license.
import inspect
import sys
import re
import collections
@ -187,6 +188,7 @@ class Tracer:
self.overwrite = overwrite
self._did_overwrite = False
assert self.depth >= 1
self.target_code_object = None
def __call__(self, function):
self.target_code_object = function.__code__
@ -205,6 +207,11 @@ class Tracer:
self._write(s)
def __enter__(self):
if not self.target_code_object:
calling_frame = inspect.currentframe().f_back
self.target_code_object = calling_frame.f_code
calling_frame.f_trace = self.trace
self.original_trace_function = sys.gettrace()
sys.settrace(self.trace)