Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_previous_trace(self):
self.pmt.kill()
self.assertIsNone(gettrace())
called = []
def f(*args):
called.append(args)
settrace(f)
self.pmt = monitor.PeriodicMonitoringThread(self.hub)
self.assertEqual(gettrace(), self.pmt._greenlet_tracer)
self.assertIs(self.pmt._greenlet_tracer.previous_trace_function, f)
self.pmt._greenlet_tracer('event', ('args',))
self.assertEqual([('event', ('args',))], called)
def tearDown(self):
monitor.start_new_thread = self._orig_start_new_thread
monitor.thread_sleep = self._orig_thread_sleep
prev = self.pmt._greenlet_tracer.previous_trace_function
self.pmt.kill()
assert gettrace() is prev, (gettrace(), prev)
settrace(None)
super(_AbstractTestPeriodicMonitoringThread, self).tearDown()
def test_kill_removes_trace(self):
from greenlet import gettrace
hub = get_hub()
hub.start_periodic_monitoring_thread()
self.assertIsNotNone(gettrace())
hub.periodic_monitoring_thread.kill()
self.assertIsNone(gettrace())
def test_kill_removes_trace(self):
from greenlet import gettrace
hub = get_hub()
hub.start_periodic_monitoring_thread()
self.assertIsNotNone(gettrace())
hub.periodic_monitoring_thread.kill()
self.assertIsNone(gettrace())
def test_previous_trace(self):
self.pmt.kill()
self.assertIsNone(gettrace())
called = []
def f(*args):
called.append(args)
settrace(f)
self.pmt = monitor.PeriodicMonitoringThread(self.hub)
self.assertEqual(gettrace(), self.pmt._greenlet_tracer)
self.assertIs(self.pmt._greenlet_tracer.previous_trace_function, f)
self.pmt._greenlet_tracer('event', ('args',))
self.assertEqual([('event', ('args',))], called)
self.sample_interval = sample_interval
self.last_timestamp = time.time()
# Save the old frame to have proper stack reporting. If the following
# code is executed:
#
# slow() # At this point a new sample is *not* needed
# f2() # When this calls happens a new sample is needed, *because
# # of the previous function*
#
# The above gets worse because a context switch can happen after the
# call to slow, if this is not taken into account a completely wrong
# stack trace will be reported.
self.old_frame = None
self.previous_callback = greenlet.gettrace()
greenlet.settrace(self._greenlet_profiler) # pylint: disable=c-extension-no-member
sys.setprofile(self._thread_profiler)
# threading.setprofile(self._thread_profiler)
def greenlet_settrace(self):
'Check if any greenlet trace function is registered.'
return bool(greenlet.gettrace())
def install_switch_log():
# Do not overwrite the previous installed tracing function, this could be
# another profiling tool, and if the callback is overwriten the tool would
# not work as expected (e.g. a trace sampler)
previous_callback = greenlet.gettrace()
def log_every_switch(event: str, args: Any) -> None:
if event == "switch":
origin, target = args
# Collecting the complete stack frame because the top-level
# function will be a greenlet, and the bottom function may be an
# external library. To understand what is going on the application
# code, the whole stack is necessary.
frame = sys._getframe(0)
callstack = collect_frames(frame)
# Using `print` because logging will not work here, the logger may
# not be properly initialized on a fresh greenlet and will try to
# use a nullable variable.
print(