| 1 |
import gc |
| 2 |
import pprint |
| 3 |
import sys |
| 4 |
import unittest |
| 5 |
|
| 6 |
|
| 7 |
class TestGetProfile(unittest.TestCase): |
| 8 |
def setUp(self): |
| 9 |
sys.setprofile(None) |
| 10 |
|
| 11 |
def tearDown(self): |
| 12 |
sys.setprofile(None) |
| 13 |
|
| 14 |
def test_empty(self): |
| 15 |
self.assertIsNone(sys.getprofile()) |
| 16 |
|
| 17 |
def test_setget(self): |
| 18 |
def fn(*args): |
| 19 |
pass |
| 20 |
|
| 21 |
sys.setprofile(fn) |
| 22 |
self.assertIs(sys.getprofile(), fn) |
| 23 |
|
| 24 |
class HookWatcher: |
| 25 |
def __init__(self): |
| 26 |
self.frames = [] |
| 27 |
self.events = [] |
| 28 |
|
| 29 |
def callback(self, frame, event, arg): |
| 30 |
if (event == "call" |
| 31 |
or event == "return" |
| 32 |
or event == "exception"): |
| 33 |
self.add_event(event, frame, arg) |
| 34 |
|
| 35 |
def add_event(self, event, frame=None, arg=None): |
| 36 |
"""Add an event to the log.""" |
| 37 |
if frame is None: |
| 38 |
frame = sys._getframe(1) |
| 39 |
|
| 40 |
try: |
| 41 |
frameno = self.frames.index(frame) |
| 42 |
except ValueError: |
| 43 |
frameno = len(self.frames) |
| 44 |
self.frames.append(frame) |
| 45 |
|
| 46 |
self.events.append((frameno, event, ident(frame), arg)) |
| 47 |
|
| 48 |
def get_events(self): |
| 49 |
"""Remove calls to add_event().""" |
| 50 |
disallowed = [ident(self.add_event.__func__), ident(ident)] |
| 51 |
self.frames = None |
| 52 |
|
| 53 |
return [item for item in self.events if item[2] not in disallowed] |
| 54 |
|
| 55 |
|
| 56 |
class ProfileSimulator(HookWatcher): |
| 57 |
def __init__(self, testcase): |
| 58 |
self.testcase = testcase |
| 59 |
self.stack = [] |
| 60 |
HookWatcher.__init__(self) |
| 61 |
|
| 62 |
def callback(self, frame, event, arg): |
| 63 |
# Callback registered with sys.setprofile()/sys.settrace() |
| 64 |
self.dispatch[event](self, frame) |
| 65 |
|
| 66 |
def trace_call(self, frame): |
| 67 |
self.add_event('call', frame) |
| 68 |
self.stack.append(frame) |
| 69 |
|
| 70 |
def trace_return(self, frame): |
| 71 |
self.add_event('return', frame) |
| 72 |
self.stack.pop() |
| 73 |
|
| 74 |
def trace_exception(self, frame): |
| 75 |
self.testcase.fail( |
| 76 |
"the profiler should never receive exception events") |
| 77 |
|
| 78 |
def trace_pass(self, frame): |
| 79 |
pass |
| 80 |
|
| 81 |
dispatch = { |
| 82 |
'call': trace_call, |
| 83 |
'exception': trace_exception, |
| 84 |
'return': trace_return, |
| 85 |
'c_call': trace_pass, |
| 86 |
'c_return': trace_pass, |
| 87 |
'c_exception': trace_pass, |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
class TestCaseBase(unittest.TestCase): |
| 92 |
def check_events(self, callable, expected, check_args=False): |
| 93 |
events = capture_events(callable, self.new_watcher()) |
| 94 |
if check_args: |
| 95 |
if events != expected: |
| 96 |
self.fail("Expected events:\n%s\nReceived events:\n%s" |
| 97 |
% (pprint.pformat(expected), pprint.pformat(events))) |
| 98 |
else: |
| 99 |
if [(frameno, event, ident) for frameno, event, ident, arg in events] != expected: |
| 100 |
self.fail("Expected events:\n%s\nReceived events:\n%s" |
| 101 |
% (pprint.pformat(expected), pprint.pformat(events))) |
| 102 |
|
| 103 |
|
| 104 |
class ProfileHookTestCase(TestCaseBase): |
| 105 |
def new_watcher(self): |
| 106 |
return HookWatcher() |
| 107 |
|
| 108 |
def test_simple(self): |
| 109 |
def f(p): |
| 110 |
pass |
| 111 |
f_ident = ident(f) |
| 112 |
self.check_events(f, [(1, 'call', f_ident), |
| 113 |
(1, 'return', f_ident), |
| 114 |
]) |
| 115 |
|
| 116 |
def test_exception(self): |
| 117 |
def f(p): |
| 118 |
1/0 |
| 119 |
f_ident = ident(f) |
| 120 |
self.check_events(f, [(1, 'call', f_ident), |
| 121 |
(1, 'return', f_ident), |
| 122 |
]) |
| 123 |
|
| 124 |
def test_caught_exception(self): |
| 125 |
def f(p): |
| 126 |
try: 1/0 |
| 127 |
except ZeroDivisionError: pass |
| 128 |
f_ident = ident(f) |
| 129 |
self.check_events(f, [(1, 'call', f_ident), |
| 130 |
(1, 'return', f_ident), |
| 131 |
]) |
| 132 |
|
| 133 |
def test_caught_nested_exception(self): |
| 134 |
def f(p): |
| 135 |
try: 1/0 |
| 136 |
except ZeroDivisionError: pass |
| 137 |
f_ident = ident(f) |
| 138 |
self.check_events(f, [(1, 'call', f_ident), |
| 139 |
(1, 'return', f_ident), |
| 140 |
]) |
| 141 |
|
| 142 |
def test_nested_exception(self): |
| 143 |
def f(p): |
| 144 |
1/0 |
| 145 |
f_ident = ident(f) |
| 146 |
self.check_events(f, [(1, 'call', f_ident), |
| 147 |
# This isn't what I expected: |
| 148 |
# (0, 'exception', protect_ident), |
| 149 |
# I expected this again: |
| 150 |
(1, 'return', f_ident), |
| 151 |
]) |
| 152 |
|
| 153 |
def test_exception_in_except_clause(self): |
| 154 |
def f(p): |
| 155 |
1/0 |
| 156 |
def g(p): |
| 157 |
try: |
| 158 |
f(p) |
| 159 |
except ZeroDivisionError: |
| 160 |
try: f(p) |
| 161 |
except ZeroDivisionError: pass |
| 162 |
f_ident = ident(f) |
| 163 |
g_ident = ident(g) |
| 164 |
self.check_events(g, [(1, 'call', g_ident), |
| 165 |
(2, 'call', f_ident), |
| 166 |
(2, 'return', f_ident), |
| 167 |
(3, 'call', f_ident), |
| 168 |
(3, 'return', f_ident), |
| 169 |
(1, 'return', g_ident), |
| 170 |
]) |
| 171 |
|
| 172 |
def test_exception_propagation(self): |
| 173 |
def f(p): |
| 174 |
1/0 |
| 175 |
def g(p): |
| 176 |
try: f(p) |
| 177 |
finally: p.add_event("falling through") |
| 178 |
f_ident = ident(f) |
| 179 |
g_ident = ident(g) |
| 180 |
self.check_events(g, [(1, 'call', g_ident), |
| 181 |
(2, 'call', f_ident), |
| 182 |
(2, 'return', f_ident), |
| 183 |
(1, 'falling through', g_ident), |
| 184 |
(1, 'return', g_ident), |
| 185 |
]) |
| 186 |
|
| 187 |
def test_raise_twice(self): |
| 188 |
def f(p): |
| 189 |
try: 1/0 |
| 190 |
except ZeroDivisionError: 1/0 |
| 191 |
f_ident = ident(f) |
| 192 |
self.check_events(f, [(1, 'call', f_ident), |
| 193 |
(1, 'return', f_ident), |
| 194 |
]) |
| 195 |
|
| 196 |
def test_raise_reraise(self): |
| 197 |
def f(p): |
| 198 |
try: 1/0 |
| 199 |
except ZeroDivisionError: raise |
| 200 |
f_ident = ident(f) |
| 201 |
self.check_events(f, [(1, 'call', f_ident), |
| 202 |
(1, 'return', f_ident), |
| 203 |
]) |
| 204 |
|
| 205 |
def test_raise(self): |
| 206 |
def f(p): |
| 207 |
raise Exception() |
| 208 |
f_ident = ident(f) |
| 209 |
self.check_events(f, [(1, 'call', f_ident), |
| 210 |
(1, 'return', f_ident), |
| 211 |
]) |
| 212 |
|
| 213 |
def test_distant_exception(self): |
| 214 |
def f(): |
| 215 |
1/0 |
| 216 |
def g(): |
| 217 |
f() |
| 218 |
def h(): |
| 219 |
g() |
| 220 |
def i(): |
| 221 |
h() |
| 222 |
def j(p): |
| 223 |
i() |
| 224 |
f_ident = ident(f) |
| 225 |
g_ident = ident(g) |
| 226 |
h_ident = ident(h) |
| 227 |
i_ident = ident(i) |
| 228 |
j_ident = ident(j) |
| 229 |
self.check_events(j, [(1, 'call', j_ident), |
| 230 |
(2, 'call', i_ident), |
| 231 |
(3, 'call', h_ident), |
| 232 |
(4, 'call', g_ident), |
| 233 |
(5, 'call', f_ident), |
| 234 |
(5, 'return', f_ident), |
| 235 |
(4, 'return', g_ident), |
| 236 |
(3, 'return', h_ident), |
| 237 |
(2, 'return', i_ident), |
| 238 |
(1, 'return', j_ident), |
| 239 |
]) |
| 240 |
|
| 241 |
def test_generator(self): |
| 242 |
def f(): |
| 243 |
for i in range(2): |
| 244 |
yield i |
| 245 |
def g(p): |
| 246 |
for i in f(): |
| 247 |
pass |
| 248 |
f_ident = ident(f) |
| 249 |
g_ident = ident(g) |
| 250 |
self.check_events(g, [(1, 'call', g_ident), |
| 251 |
# call the iterator twice to generate values |
| 252 |
(2, 'call', f_ident), |
| 253 |
(2, 'return', f_ident), |
| 254 |
(2, 'call', f_ident), |
| 255 |
(2, 'return', f_ident), |
| 256 |
# once more; returns end-of-iteration with |
| 257 |
# actually raising an exception |
| 258 |
(2, 'call', f_ident), |
| 259 |
(2, 'return', f_ident), |
| 260 |
(1, 'return', g_ident), |
| 261 |
]) |
| 262 |
|
| 263 |
def test_unfinished_generator(self): |
| 264 |
def f(): |
| 265 |
for i in range(2): |
| 266 |
yield i |
| 267 |
def g(p): |
| 268 |
next(f()) |
| 269 |
|
| 270 |
f_ident = ident(f) |
| 271 |
g_ident = ident(g) |
| 272 |
self.check_events(g, [(1, 'call', g_ident, None), |
| 273 |
(2, 'call', f_ident, None), |
| 274 |
(2, 'return', f_ident, 0), |
| 275 |
(2, 'call', f_ident, None), |
| 276 |
(2, 'return', f_ident, None), |
| 277 |
(1, 'return', g_ident, None), |
| 278 |
], check_args=True) |
| 279 |
|
| 280 |
def test_stop_iteration(self): |
| 281 |
def f(): |
| 282 |
for i in range(2): |
| 283 |
yield i |
| 284 |
def g(p): |
| 285 |
for i in f(): |
| 286 |
pass |
| 287 |
f_ident = ident(f) |
| 288 |
g_ident = ident(g) |
| 289 |
self.check_events(g, [(1, 'call', g_ident), |
| 290 |
# call the iterator twice to generate values |
| 291 |
(2, 'call', f_ident), |
| 292 |
(2, 'return', f_ident), |
| 293 |
(2, 'call', f_ident), |
| 294 |
(2, 'return', f_ident), |
| 295 |
# once more to hit the raise: |
| 296 |
(2, 'call', f_ident), |
| 297 |
(2, 'return', f_ident), |
| 298 |
(1, 'return', g_ident), |
| 299 |
]) |
| 300 |
|
| 301 |
|
| 302 |
class ProfileSimulatorTestCase(TestCaseBase): |
| 303 |
def new_watcher(self): |
| 304 |
return ProfileSimulator(self) |
| 305 |
|
| 306 |
def test_simple(self): |
| 307 |
def f(p): |
| 308 |
pass |
| 309 |
f_ident = ident(f) |
| 310 |
self.check_events(f, [(1, 'call', f_ident), |
| 311 |
(1, 'return', f_ident), |
| 312 |
]) |
| 313 |
|
| 314 |
def test_basic_exception(self): |
| 315 |
def f(p): |
| 316 |
1/0 |
| 317 |
f_ident = ident(f) |
| 318 |
self.check_events(f, [(1, 'call', f_ident), |
| 319 |
(1, 'return', f_ident), |
| 320 |
]) |
| 321 |
|
| 322 |
def test_caught_exception(self): |
| 323 |
def f(p): |
| 324 |
try: 1/0 |
| 325 |
except ZeroDivisionError: pass |
| 326 |
f_ident = ident(f) |
| 327 |
self.check_events(f, [(1, 'call', f_ident), |
| 328 |
(1, 'return', f_ident), |
| 329 |
]) |
| 330 |
|
| 331 |
def test_distant_exception(self): |
| 332 |
def f(): |
| 333 |
1/0 |
| 334 |
def g(): |
| 335 |
f() |
| 336 |
def h(): |
| 337 |
g() |
| 338 |
def i(): |
| 339 |
h() |
| 340 |
def j(p): |
| 341 |
i() |
| 342 |
f_ident = ident(f) |
| 343 |
g_ident = ident(g) |
| 344 |
h_ident = ident(h) |
| 345 |
i_ident = ident(i) |
| 346 |
j_ident = ident(j) |
| 347 |
self.check_events(j, [(1, 'call', j_ident), |
| 348 |
(2, 'call', i_ident), |
| 349 |
(3, 'call', h_ident), |
| 350 |
(4, 'call', g_ident), |
| 351 |
(5, 'call', f_ident), |
| 352 |
(5, 'return', f_ident), |
| 353 |
(4, 'return', g_ident), |
| 354 |
(3, 'return', h_ident), |
| 355 |
(2, 'return', i_ident), |
| 356 |
(1, 'return', j_ident), |
| 357 |
]) |
| 358 |
|
| 359 |
# bpo-34125: profiling method_descriptor with **kwargs |
| 360 |
def test_unbound_method(self): |
| 361 |
kwargs = {} |
| 362 |
def f(p): |
| 363 |
dict.get({}, 42, **kwargs) |
| 364 |
f_ident = ident(f) |
| 365 |
self.check_events(f, [(1, 'call', f_ident), |
| 366 |
(1, 'return', f_ident)]) |
| 367 |
|
| 368 |
# Test an invalid call (bpo-34126) |
| 369 |
def test_unbound_method_no_args(self): |
| 370 |
def f(p): |
| 371 |
dict.get() |
| 372 |
f_ident = ident(f) |
| 373 |
self.check_events(f, [(1, 'call', f_ident), |
| 374 |
(1, 'return', f_ident)]) |
| 375 |
|
| 376 |
# Test an invalid call (bpo-34126) |
| 377 |
def test_unbound_method_invalid_args(self): |
| 378 |
def f(p): |
| 379 |
dict.get(print, 42) |
| 380 |
f_ident = ident(f) |
| 381 |
self.check_events(f, [(1, 'call', f_ident), |
| 382 |
(1, 'return', f_ident)]) |
| 383 |
|
| 384 |
# Test an invalid call (bpo-34125) |
| 385 |
def test_unbound_method_no_keyword_args(self): |
| 386 |
kwargs = {} |
| 387 |
def f(p): |
| 388 |
dict.get(**kwargs) |
| 389 |
f_ident = ident(f) |
| 390 |
self.check_events(f, [(1, 'call', f_ident), |
| 391 |
(1, 'return', f_ident)]) |
| 392 |
|
| 393 |
# Test an invalid call (bpo-34125) |
| 394 |
def test_unbound_method_invalid_keyword_args(self): |
| 395 |
kwargs = {} |
| 396 |
def f(p): |
| 397 |
dict.get(print, 42, **kwargs) |
| 398 |
f_ident = ident(f) |
| 399 |
self.check_events(f, [(1, 'call', f_ident), |
| 400 |
(1, 'return', f_ident)]) |
| 401 |
|
| 402 |
|
| 403 |
def ident(function): |
| 404 |
if hasattr(function, "f_code"): |
| 405 |
code = function.f_code |
| 406 |
else: |
| 407 |
code = function.__code__ |
| 408 |
return code.co_firstlineno, code.co_name |
| 409 |
|
| 410 |
|
| 411 |
def protect(f, p): |
| 412 |
try: f(p) |
| 413 |
except: pass |
| 414 |
|
| 415 |
protect_ident = ident(protect) |
| 416 |
|
| 417 |
|
| 418 |
def capture_events(callable, p=None): |
| 419 |
if p is None: |
| 420 |
p = HookWatcher() |
| 421 |
# Disable the garbage collector. This prevents __del__s from showing up in |
| 422 |
# traces. |
| 423 |
old_gc = gc.isenabled() |
| 424 |
gc.disable() |
| 425 |
try: |
| 426 |
sys.setprofile(p.callback) |
| 427 |
protect(callable, p) |
| 428 |
sys.setprofile(None) |
| 429 |
finally: |
| 430 |
if old_gc: |
| 431 |
gc.enable() |
| 432 |
return p.get_events()[1:-1] |
| 433 |
|
| 434 |
|
| 435 |
def show_events(callable): |
| 436 |
import pprint |
| 437 |
pprint.pprint(capture_events(callable)) |
| 438 |
|
| 439 |
|
| 440 |
class TestEdgeCases(unittest.TestCase): |
| 441 |
|
| 442 |
def setUp(self): |
| 443 |
self.addCleanup(sys.setprofile, sys.getprofile()) |
| 444 |
sys.setprofile(None) |
| 445 |
|
| 446 |
def test_reentrancy(self): |
| 447 |
def foo(*args): |
| 448 |
... |
| 449 |
|
| 450 |
def bar(*args): |
| 451 |
... |
| 452 |
|
| 453 |
class A: |
| 454 |
def __call__(self, *args): |
| 455 |
pass |
| 456 |
|
| 457 |
def __del__(self): |
| 458 |
sys.setprofile(bar) |
| 459 |
|
| 460 |
sys.setprofile(A()) |
| 461 |
sys.setprofile(foo) |
| 462 |
self.assertEqual(sys.getprofile(), bar) |
| 463 |
|
| 464 |
def test_same_object(self): |
| 465 |
def foo(*args): |
| 466 |
... |
| 467 |
|
| 468 |
sys.setprofile(foo) |
| 469 |
del foo |
| 470 |
sys.setprofile(sys.getprofile()) |
| 471 |
|
| 472 |
def test_profile_after_trace_opcodes(self): |
| 473 |
def f(): |
| 474 |
... |
| 475 |
|
| 476 |
sys._getframe().f_trace_opcodes = True |
| 477 |
prev_trace = sys.gettrace() |
| 478 |
sys.settrace(lambda *args: None) |
| 479 |
f() |
| 480 |
sys.settrace(prev_trace) |
| 481 |
sys.setprofile(lambda *args: None) |
| 482 |
f() |
| 483 |
|
| 484 |
def test_method_with_c_function(self): |
| 485 |
# gh-122029 |
| 486 |
# When we have a PyMethodObject whose im_func is a C function, we |
| 487 |
# should record both the call and the return. f = classmethod(repr) |
| 488 |
# is just a way to create a PyMethodObject with a C function. |
| 489 |
class A: |
| 490 |
f = classmethod(repr) |
| 491 |
events = [] |
| 492 |
sys.setprofile(lambda frame, event, args: events.append(event)) |
| 493 |
A().f() |
| 494 |
sys.setprofile(None) |
| 495 |
# The last c_call is the call to sys.setprofile |
| 496 |
self.assertEqual(events, ['c_call', 'c_return', 'c_call']) |
| 497 |
|
| 498 |
class B: |
| 499 |
f = classmethod(max) |
| 500 |
events = [] |
| 501 |
sys.setprofile(lambda frame, event, args: events.append(event)) |
| 502 |
# Not important, we only want to trigger INSTRUMENTED_CALL_KW |
| 503 |
B().f(1, key=lambda x: 0) |
| 504 |
sys.setprofile(None) |
| 505 |
# The last c_call is the call to sys.setprofile |
| 506 |
self.assertEqual( |
| 507 |
events, |
| 508 |
['c_call', |
| 509 |
'call', 'return', |
| 510 |
'call', 'return', |
| 511 |
'c_return', |
| 512 |
'c_call' |
| 513 |
] |
| 514 |
) |
| 515 |
|
| 516 |
# Test CALL_FUNCTION_EX |
| 517 |
events = [] |
| 518 |
sys.setprofile(lambda frame, event, args: events.append(event)) |
| 519 |
# Not important, we only want to trigger INSTRUMENTED_CALL_KW |
| 520 |
args = (1,) |
| 521 |
m = B().f |
| 522 |
m(*args, key=lambda x: 0) |
| 523 |
sys.setprofile(None) |
| 524 |
# The last c_call is the call to sys.setprofile |
| 525 |
# INSTRUMENTED_CALL_FUNCTION_EX has different behavior than the other |
| 526 |
# instrumented call bytecodes, it does not unpack the callable before |
| 527 |
# calling it. This is probably not ideal because it's not consistent, |
| 528 |
# but at least we get a consistent call stack (no unmatched c_call). |
| 529 |
self.assertEqual( |
| 530 |
events, |
| 531 |
['call', 'return', |
| 532 |
'call', 'return', |
| 533 |
'c_call' |
| 534 |
] |
| 535 |
) |
| 536 |
|
| 537 |
|
| 538 |
if __name__ == "__main__": |
| 539 |
unittest.main() |