File size: 8,250 Bytes
db4a26f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
from __future__ import print_function
import sys
import greenlet
import unittest
from . import TestCase
from . import PY312
# https://discuss.python.org/t/cpython-3-12-greenlet-and-tracing-profiling-how-to-not-crash-and-get-correct-results/33144/2
DEBUG_BUILD_PY312 = (
PY312 and hasattr(sys, 'gettotalrefcount'),
"Broken on debug builds of Python 3.12"
)
class SomeError(Exception):
pass
class GreenletTracer(object):
oldtrace = None
def __init__(self, error_on_trace=False):
self.actions = []
self.error_on_trace = error_on_trace
def __call__(self, *args):
self.actions.append(args)
if self.error_on_trace:
raise SomeError
def __enter__(self):
self.oldtrace = greenlet.settrace(self)
return self.actions
def __exit__(self, *args):
greenlet.settrace(self.oldtrace)
class TestGreenletTracing(TestCase):
"""
Tests of ``greenlet.settrace()``
"""
def test_a_greenlet_tracing(self):
main = greenlet.getcurrent()
def dummy():
pass
def dummyexc():
raise SomeError()
with GreenletTracer() as actions:
g1 = greenlet.greenlet(dummy)
g1.switch()
g2 = greenlet.greenlet(dummyexc)
self.assertRaises(SomeError, g2.switch)
self.assertEqual(actions, [
('switch', (main, g1)),
('switch', (g1, main)),
('switch', (main, g2)),
('throw', (g2, main)),
])
def test_b_exception_disables_tracing(self):
main = greenlet.getcurrent()
def dummy():
main.switch()
g = greenlet.greenlet(dummy)
g.switch()
with GreenletTracer(error_on_trace=True) as actions:
self.assertRaises(SomeError, g.switch)
self.assertEqual(greenlet.gettrace(), None)
self.assertEqual(actions, [
('switch', (main, g)),
])
def test_set_same_tracer_twice(self):
# https://github.com/python-greenlet/greenlet/issues/332
# Our logic in asserting that the tracefunction should
# gain a reference was incorrect if the same tracefunction was set
# twice.
tracer = GreenletTracer()
with tracer:
greenlet.settrace(tracer)
class PythonTracer(object):
oldtrace = None
def __init__(self):
self.actions = []
def __call__(self, frame, event, arg):
# Record the co_name so we have an idea what function we're in.
self.actions.append((event, frame.f_code.co_name))
def __enter__(self):
self.oldtrace = sys.setprofile(self)
return self.actions
def __exit__(self, *args):
sys.setprofile(self.oldtrace)
def tpt_callback():
return 42
class TestPythonTracing(TestCase):
"""
Tests of the interaction of ``sys.settrace()``
with greenlet facilities.
NOTE: Most of this is probably CPython specific.
"""
maxDiff = None
def test_trace_events_trivial(self):
with PythonTracer() as actions:
tpt_callback()
# If we use the sys.settrace instead of setprofile, we get
# this:
# self.assertEqual(actions, [
# ('call', 'tpt_callback'),
# ('call', '__exit__'),
# ])
self.assertEqual(actions, [
('return', '__enter__'),
('call', 'tpt_callback'),
('return', 'tpt_callback'),
('call', '__exit__'),
('c_call', '__exit__'),
])
def _trace_switch(self, glet):
with PythonTracer() as actions:
glet.switch()
return actions
def _check_trace_events_func_already_set(self, glet):
actions = self._trace_switch(glet)
self.assertEqual(actions, [
('return', '__enter__'),
('c_call', '_trace_switch'),
('call', 'run'),
('call', 'tpt_callback'),
('return', 'tpt_callback'),
('return', 'run'),
('c_return', '_trace_switch'),
('call', '__exit__'),
('c_call', '__exit__'),
])
def test_trace_events_into_greenlet_func_already_set(self):
def run():
return tpt_callback()
self._check_trace_events_func_already_set(greenlet.greenlet(run))
def test_trace_events_into_greenlet_subclass_already_set(self):
class X(greenlet.greenlet):
def run(self):
return tpt_callback()
self._check_trace_events_func_already_set(X())
def _check_trace_events_from_greenlet_sets_profiler(self, g, tracer):
g.switch()
tpt_callback()
tracer.__exit__()
self.assertEqual(tracer.actions, [
('return', '__enter__'),
('call', 'tpt_callback'),
('return', 'tpt_callback'),
('return', 'run'),
('call', 'tpt_callback'),
('return', 'tpt_callback'),
('call', '__exit__'),
('c_call', '__exit__'),
])
def test_trace_events_from_greenlet_func_sets_profiler(self):
tracer = PythonTracer()
def run():
tracer.__enter__()
return tpt_callback()
self._check_trace_events_from_greenlet_sets_profiler(greenlet.greenlet(run),
tracer)
def test_trace_events_from_greenlet_subclass_sets_profiler(self):
tracer = PythonTracer()
class X(greenlet.greenlet):
def run(self):
tracer.__enter__()
return tpt_callback()
self._check_trace_events_from_greenlet_sets_profiler(X(), tracer)
@unittest.skipIf(*DEBUG_BUILD_PY312)
def test_trace_events_multiple_greenlets_switching(self):
tracer = PythonTracer()
g1 = None
g2 = None
def g1_run():
tracer.__enter__()
tpt_callback()
g2.switch()
tpt_callback()
return 42
def g2_run():
tpt_callback()
tracer.__exit__()
tpt_callback()
g1.switch()
g1 = greenlet.greenlet(g1_run)
g2 = greenlet.greenlet(g2_run)
x = g1.switch()
self.assertEqual(x, 42)
tpt_callback() # ensure not in the trace
self.assertEqual(tracer.actions, [
('return', '__enter__'),
('call', 'tpt_callback'),
('return', 'tpt_callback'),
('c_call', 'g1_run'),
('call', 'g2_run'),
('call', 'tpt_callback'),
('return', 'tpt_callback'),
('call', '__exit__'),
('c_call', '__exit__'),
])
@unittest.skipIf(*DEBUG_BUILD_PY312)
def test_trace_events_multiple_greenlets_switching_siblings(self):
# Like the first version, but get both greenlets running first
# as "siblings" and then establish the tracing.
tracer = PythonTracer()
g1 = None
g2 = None
def g1_run():
greenlet.getcurrent().parent.switch()
tracer.__enter__()
tpt_callback()
g2.switch()
tpt_callback()
return 42
def g2_run():
greenlet.getcurrent().parent.switch()
tpt_callback()
tracer.__exit__()
tpt_callback()
g1.switch()
g1 = greenlet.greenlet(g1_run)
g2 = greenlet.greenlet(g2_run)
# Start g1
g1.switch()
# And it immediately returns control to us.
# Start g2
g2.switch()
# Which also returns. Now kick of the real part of the
# test.
x = g1.switch()
self.assertEqual(x, 42)
tpt_callback() # ensure not in the trace
self.assertEqual(tracer.actions, [
('return', '__enter__'),
('call', 'tpt_callback'),
('return', 'tpt_callback'),
('c_call', 'g1_run'),
('call', 'tpt_callback'),
('return', 'tpt_callback'),
('call', '__exit__'),
('c_call', '__exit__'),
])
if __name__ == '__main__':
unittest.main()
|