| 1 |
# Testing the line trace facility. |
| 2 |
|
| 3 |
from test import support |
| 4 |
import unittest |
| 5 |
import sys |
| 6 |
import difflib |
| 7 |
import gc |
| 8 |
from functools import wraps |
| 9 |
from test.support import import_helper, requires_subprocess, run_no_yield_async_fn |
| 10 |
import contextlib |
| 11 |
import os |
| 12 |
import tempfile |
| 13 |
import textwrap |
| 14 |
import subprocess |
| 15 |
import warnings |
| 16 |
try: |
| 17 |
import _testinternalcapi |
| 18 |
except ImportError: |
| 19 |
_testinternalcapi = None |
| 20 |
|
| 21 |
class tracecontext: |
| 22 |
"""Context manager that traces its enter and exit.""" |
| 23 |
def __init__(self, output, value): |
| 24 |
self.output = output |
| 25 |
self.value = value |
| 26 |
|
| 27 |
def __enter__(self): |
| 28 |
self.output.append(self.value) |
| 29 |
|
| 30 |
def __exit__(self, *exc_info): |
| 31 |
self.output.append(-self.value) |
| 32 |
|
| 33 |
class asynctracecontext: |
| 34 |
"""Asynchronous context manager that traces its aenter and aexit.""" |
| 35 |
def __init__(self, output, value): |
| 36 |
self.output = output |
| 37 |
self.value = value |
| 38 |
|
| 39 |
async def __aenter__(self): |
| 40 |
self.output.append(self.value) |
| 41 |
|
| 42 |
async def __aexit__(self, *exc_info): |
| 43 |
self.output.append(-self.value) |
| 44 |
|
| 45 |
async def asynciter(iterable): |
| 46 |
"""Convert an iterable to an asynchronous iterator.""" |
| 47 |
for x in iterable: |
| 48 |
yield x |
| 49 |
|
| 50 |
def clean_asynciter(test): |
| 51 |
@wraps(test) |
| 52 |
async def wrapper(*args, **kwargs): |
| 53 |
cleanups = [] |
| 54 |
def wrapped_asynciter(iterable): |
| 55 |
it = asynciter(iterable) |
| 56 |
cleanups.append(it.aclose) |
| 57 |
return it |
| 58 |
try: |
| 59 |
return await test(*args, **kwargs, asynciter=wrapped_asynciter) |
| 60 |
finally: |
| 61 |
while cleanups: |
| 62 |
await cleanups.pop()() |
| 63 |
return wrapper |
| 64 |
|
| 65 |
# A very basic example. If this fails, we're in deep trouble. |
| 66 |
def basic(): |
| 67 |
return 1 |
| 68 |
|
| 69 |
basic.events = [(0, 'call'), |
| 70 |
(1, 'line'), |
| 71 |
(1, 'return')] |
| 72 |
|
| 73 |
# Many of the tests below are tricky because they involve pass statements. |
| 74 |
# If there is implicit control flow around a pass statement (in an except |
| 75 |
# clause or else clause) under what conditions do you set a line number |
| 76 |
# following that clause? |
| 77 |
|
| 78 |
|
| 79 |
# Some constructs like "while 0:", "if 0:" or "if 1:...else:..." could be optimized |
| 80 |
# away. Make sure that those lines aren't skipped. |
| 81 |
def arigo_example0(): |
| 82 |
x = 1 |
| 83 |
del x |
| 84 |
while 0: |
| 85 |
pass |
| 86 |
x = 1 |
| 87 |
|
| 88 |
arigo_example0.events = [(0, 'call'), |
| 89 |
(1, 'line'), |
| 90 |
(2, 'line'), |
| 91 |
(3, 'line'), |
| 92 |
(5, 'line'), |
| 93 |
(5, 'return')] |
| 94 |
|
| 95 |
def arigo_example1(): |
| 96 |
x = 1 |
| 97 |
del x |
| 98 |
if 0: |
| 99 |
pass |
| 100 |
x = 1 |
| 101 |
|
| 102 |
arigo_example1.events = [(0, 'call'), |
| 103 |
(1, 'line'), |
| 104 |
(2, 'line'), |
| 105 |
(3, 'line'), |
| 106 |
(5, 'line'), |
| 107 |
(5, 'return')] |
| 108 |
|
| 109 |
def arigo_example2(): |
| 110 |
x = 1 |
| 111 |
del x |
| 112 |
if 1: |
| 113 |
x = 1 |
| 114 |
else: |
| 115 |
pass |
| 116 |
return None |
| 117 |
|
| 118 |
arigo_example2.events = [(0, 'call'), |
| 119 |
(1, 'line'), |
| 120 |
(2, 'line'), |
| 121 |
(3, 'line'), |
| 122 |
(4, 'line'), |
| 123 |
(7, 'line'), |
| 124 |
(7, 'return')] |
| 125 |
|
| 126 |
|
| 127 |
# check that lines consisting of just one instruction get traced: |
| 128 |
def one_instr_line(): |
| 129 |
x = 1 |
| 130 |
del x |
| 131 |
x = 1 |
| 132 |
|
| 133 |
one_instr_line.events = [(0, 'call'), |
| 134 |
(1, 'line'), |
| 135 |
(2, 'line'), |
| 136 |
(3, 'line'), |
| 137 |
(3, 'return')] |
| 138 |
|
| 139 |
def no_pop_tops(): # 0 |
| 140 |
x = 1 # 1 |
| 141 |
for a in range(2): # 2 |
| 142 |
if a: # 3 |
| 143 |
x = 1 # 4 |
| 144 |
else: # 5 |
| 145 |
x = 1 # 6 |
| 146 |
|
| 147 |
no_pop_tops.events = [(0, 'call'), |
| 148 |
(1, 'line'), |
| 149 |
(2, 'line'), |
| 150 |
(3, 'line'), |
| 151 |
(6, 'line'), |
| 152 |
(2, 'line'), |
| 153 |
(3, 'line'), |
| 154 |
(4, 'line'), |
| 155 |
(2, 'line'), |
| 156 |
(2, 'return')] |
| 157 |
|
| 158 |
def no_pop_blocks(): |
| 159 |
y = 1 |
| 160 |
while not y: |
| 161 |
bla |
| 162 |
x = 1 |
| 163 |
|
| 164 |
no_pop_blocks.events = [(0, 'call'), |
| 165 |
(1, 'line'), |
| 166 |
(2, 'line'), |
| 167 |
(4, 'line'), |
| 168 |
(4, 'return')] |
| 169 |
|
| 170 |
def called(): # line -3 |
| 171 |
x = 1 |
| 172 |
|
| 173 |
def call(): # line 0 |
| 174 |
called() |
| 175 |
|
| 176 |
call.events = [(0, 'call'), |
| 177 |
(1, 'line'), |
| 178 |
(-3, 'call'), |
| 179 |
(-2, 'line'), |
| 180 |
(-2, 'return'), |
| 181 |
(1, 'return')] |
| 182 |
|
| 183 |
def raises(): |
| 184 |
raise Exception |
| 185 |
|
| 186 |
def test_raise(): |
| 187 |
try: |
| 188 |
raises() |
| 189 |
except Exception: |
| 190 |
pass |
| 191 |
|
| 192 |
test_raise.events = [(0, 'call'), |
| 193 |
(1, 'line'), |
| 194 |
(2, 'line'), |
| 195 |
(-3, 'call'), |
| 196 |
(-2, 'line'), |
| 197 |
(-2, 'exception'), |
| 198 |
(-2, 'return'), |
| 199 |
(2, 'exception'), |
| 200 |
(3, 'line'), |
| 201 |
(4, 'line'), |
| 202 |
(4, 'return')] |
| 203 |
|
| 204 |
def _settrace_and_return(tracefunc): |
| 205 |
sys.settrace(tracefunc) |
| 206 |
sys._getframe().f_back.f_trace = tracefunc |
| 207 |
def settrace_and_return(tracefunc): |
| 208 |
_settrace_and_return(tracefunc) |
| 209 |
|
| 210 |
settrace_and_return.events = [(1, 'return')] |
| 211 |
|
| 212 |
def _settrace_and_raise(tracefunc): |
| 213 |
sys.settrace(tracefunc) |
| 214 |
sys._getframe().f_back.f_trace = tracefunc |
| 215 |
raise RuntimeError |
| 216 |
def settrace_and_raise(tracefunc): |
| 217 |
try: |
| 218 |
_settrace_and_raise(tracefunc) |
| 219 |
except RuntimeError: |
| 220 |
pass |
| 221 |
|
| 222 |
settrace_and_raise.events = [(2, 'exception'), |
| 223 |
(3, 'line'), |
| 224 |
(4, 'line'), |
| 225 |
(4, 'return')] |
| 226 |
|
| 227 |
# implicit return example |
| 228 |
# This test is interesting because of the else: pass |
| 229 |
# part of the code. The code generate for the true |
| 230 |
# part of the if contains a jump past the else branch. |
| 231 |
# The compiler then generates an implicit "return None" |
| 232 |
# Internally, the compiler visits the pass statement |
| 233 |
# and stores its line number for use on the next instruction. |
| 234 |
# The next instruction is the implicit return None. |
| 235 |
def ireturn_example(): |
| 236 |
a = 5 |
| 237 |
b = 5 |
| 238 |
if a == b: |
| 239 |
b = a+1 |
| 240 |
else: |
| 241 |
pass |
| 242 |
|
| 243 |
ireturn_example.events = [(0, 'call'), |
| 244 |
(1, 'line'), |
| 245 |
(2, 'line'), |
| 246 |
(3, 'line'), |
| 247 |
(4, 'line'), |
| 248 |
(4, 'return')] |
| 249 |
|
| 250 |
# Tight loop with while(1) example (SF #765624) |
| 251 |
def tightloop_example(): |
| 252 |
items = range(0, 3) |
| 253 |
try: |
| 254 |
i = 0 |
| 255 |
while 1: |
| 256 |
b = items[i]; i+=1 |
| 257 |
except IndexError: |
| 258 |
pass |
| 259 |
|
| 260 |
tightloop_example.events = [(0, 'call'), |
| 261 |
(1, 'line'), |
| 262 |
(2, 'line'), |
| 263 |
(3, 'line'), |
| 264 |
(4, 'line'), |
| 265 |
(5, 'line'), |
| 266 |
(4, 'line'), |
| 267 |
(5, 'line'), |
| 268 |
(4, 'line'), |
| 269 |
(5, 'line'), |
| 270 |
(4, 'line'), |
| 271 |
(5, 'line'), |
| 272 |
(5, 'exception'), |
| 273 |
(6, 'line'), |
| 274 |
(7, 'line'), |
| 275 |
(7, 'return')] |
| 276 |
|
| 277 |
def tighterloop_example(): |
| 278 |
items = range(1, 4) |
| 279 |
try: |
| 280 |
i = 0 |
| 281 |
while 1: i = items[i] |
| 282 |
except IndexError: |
| 283 |
pass |
| 284 |
|
| 285 |
tighterloop_example.events = [(0, 'call'), |
| 286 |
(1, 'line'), |
| 287 |
(2, 'line'), |
| 288 |
(3, 'line'), |
| 289 |
(4, 'line'), |
| 290 |
(4, 'line'), |
| 291 |
(4, 'line'), |
| 292 |
(4, 'line'), |
| 293 |
(4, 'exception'), |
| 294 |
(5, 'line'), |
| 295 |
(6, 'line'), |
| 296 |
(6, 'return')] |
| 297 |
|
| 298 |
def generator_function(): |
| 299 |
try: |
| 300 |
yield True |
| 301 |
"continued" |
| 302 |
finally: |
| 303 |
"finally" |
| 304 |
def generator_example(): |
| 305 |
# any() will leave the generator before its end |
| 306 |
x = any(generator_function()) |
| 307 |
|
| 308 |
# the following lines were not traced |
| 309 |
for x in range(10): |
| 310 |
y = x |
| 311 |
|
| 312 |
generator_example.events = ([(0, 'call'), |
| 313 |
(2, 'line'), |
| 314 |
(-6, 'call'), |
| 315 |
(-5, 'line'), |
| 316 |
(-4, 'line'), |
| 317 |
(-4, 'return'), |
| 318 |
(-4, 'call'), |
| 319 |
(-4, 'exception'), |
| 320 |
(-1, 'line'), |
| 321 |
(-1, 'return')] + |
| 322 |
[(5, 'line'), (6, 'line')] * 10 + |
| 323 |
[(5, 'line'), (5, 'return')]) |
| 324 |
|
| 325 |
|
| 326 |
def lineno_matches_lasti(frame): |
| 327 |
last_line = None |
| 328 |
for start, end, line in frame.f_code.co_lines(): |
| 329 |
if start <= frame.f_lasti < end: |
| 330 |
last_line = line |
| 331 |
return last_line == frame.f_lineno |
| 332 |
|
| 333 |
class Tracer: |
| 334 |
def __init__(self, trace_line_events=None, trace_opcode_events=None): |
| 335 |
self.trace_line_events = trace_line_events |
| 336 |
self.trace_opcode_events = trace_opcode_events |
| 337 |
self.events = [] |
| 338 |
|
| 339 |
def _reconfigure_frame(self, frame): |
| 340 |
if self.trace_line_events is not None: |
| 341 |
frame.f_trace_lines = self.trace_line_events |
| 342 |
if self.trace_opcode_events is not None: |
| 343 |
frame.f_trace_opcodes = self.trace_opcode_events |
| 344 |
|
| 345 |
def trace(self, frame, event, arg): |
| 346 |
assert lineno_matches_lasti(frame) |
| 347 |
self._reconfigure_frame(frame) |
| 348 |
self.events.append((frame.f_lineno, event)) |
| 349 |
return self.trace |
| 350 |
|
| 351 |
def traceWithGenexp(self, frame, event, arg): |
| 352 |
self._reconfigure_frame(frame) |
| 353 |
(o for o in [1]) |
| 354 |
self.events.append((frame.f_lineno, event)) |
| 355 |
return self.trace |
| 356 |
|
| 357 |
|
| 358 |
class TraceTestCase(unittest.TestCase): |
| 359 |
|
| 360 |
# Disable gc collection when tracing, otherwise the |
| 361 |
# deallocators may be traced as well. |
| 362 |
def setUp(self): |
| 363 |
self.using_gc = gc.isenabled() |
| 364 |
gc.disable() |
| 365 |
self.addCleanup(sys.settrace, sys.gettrace()) |
| 366 |
|
| 367 |
def tearDown(self): |
| 368 |
if self.using_gc: |
| 369 |
gc.enable() |
| 370 |
|
| 371 |
@staticmethod |
| 372 |
def make_tracer(): |
| 373 |
"""Helper to allow test subclasses to configure tracers differently""" |
| 374 |
return Tracer() |
| 375 |
|
| 376 |
def compare_events(self, line_offset, events, expected_events): |
| 377 |
events = [(l - line_offset if l is not None else None, e) for (l, e) in events] |
| 378 |
if events != expected_events: |
| 379 |
self.fail( |
| 380 |
"events did not match expectation:\n" + |
| 381 |
"\n".join(difflib.ndiff([str(x) for x in expected_events], |
| 382 |
[str(x) for x in events]))) |
| 383 |
|
| 384 |
def run_and_compare(self, func, events): |
| 385 |
tracer = self.make_tracer() |
| 386 |
sys.settrace(tracer.trace) |
| 387 |
func() |
| 388 |
sys.settrace(None) |
| 389 |
self.compare_events(func.__code__.co_firstlineno, |
| 390 |
tracer.events, events) |
| 391 |
|
| 392 |
def run_test(self, func): |
| 393 |
self.run_and_compare(func, func.events) |
| 394 |
|
| 395 |
def run_test2(self, func): |
| 396 |
tracer = self.make_tracer() |
| 397 |
func(tracer.trace) |
| 398 |
sys.settrace(None) |
| 399 |
self.compare_events(func.__code__.co_firstlineno, |
| 400 |
tracer.events, func.events) |
| 401 |
|
| 402 |
def test_set_and_retrieve_none(self): |
| 403 |
sys.settrace(None) |
| 404 |
assert sys.gettrace() is None |
| 405 |
|
| 406 |
def test_set_and_retrieve_func(self): |
| 407 |
def fn(*args): |
| 408 |
pass |
| 409 |
|
| 410 |
sys.settrace(fn) |
| 411 |
try: |
| 412 |
assert sys.gettrace() is fn |
| 413 |
finally: |
| 414 |
sys.settrace(None) |
| 415 |
|
| 416 |
def test_01_basic(self): |
| 417 |
self.run_test(basic) |
| 418 |
def test_02_arigo0(self): |
| 419 |
self.run_test(arigo_example0) |
| 420 |
def test_02_arigo1(self): |
| 421 |
self.run_test(arigo_example1) |
| 422 |
def test_02_arigo2(self): |
| 423 |
self.run_test(arigo_example2) |
| 424 |
def test_03_one_instr(self): |
| 425 |
self.run_test(one_instr_line) |
| 426 |
def test_04_no_pop_blocks(self): |
| 427 |
self.run_test(no_pop_blocks) |
| 428 |
def test_05_no_pop_tops(self): |
| 429 |
self.run_test(no_pop_tops) |
| 430 |
def test_06_call(self): |
| 431 |
self.run_test(call) |
| 432 |
def test_07_raise(self): |
| 433 |
self.run_test(test_raise) |
| 434 |
|
| 435 |
def test_08_settrace_and_return(self): |
| 436 |
self.run_test2(settrace_and_return) |
| 437 |
def test_09_settrace_and_raise(self): |
| 438 |
self.run_test2(settrace_and_raise) |
| 439 |
def test_10_ireturn(self): |
| 440 |
self.run_test(ireturn_example) |
| 441 |
def test_11_tightloop(self): |
| 442 |
self.run_test(tightloop_example) |
| 443 |
def test_12_tighterloop(self): |
| 444 |
self.run_test(tighterloop_example) |
| 445 |
|
| 446 |
def test_13_genexp(self): |
| 447 |
self.run_test(generator_example) |
| 448 |
# issue1265: if the trace function contains a generator, |
| 449 |
# and if the traced function contains another generator |
| 450 |
# that is not completely exhausted, the trace stopped. |
| 451 |
# Worse: the 'finally' clause was not invoked. |
| 452 |
tracer = self.make_tracer() |
| 453 |
sys.settrace(tracer.traceWithGenexp) |
| 454 |
generator_example() |
| 455 |
sys.settrace(None) |
| 456 |
self.compare_events(generator_example.__code__.co_firstlineno, |
| 457 |
tracer.events, generator_example.events) |
| 458 |
|
| 459 |
def test_14_onliner_if(self): |
| 460 |
def onliners(): |
| 461 |
if True: x=False |
| 462 |
else: x=True |
| 463 |
return 0 |
| 464 |
self.run_and_compare( |
| 465 |
onliners, |
| 466 |
[(0, 'call'), |
| 467 |
(1, 'line'), |
| 468 |
(3, 'line'), |
| 469 |
(3, 'return')]) |
| 470 |
|
| 471 |
def test_15_loops(self): |
| 472 |
# issue1750076: "while" expression is skipped by debugger |
| 473 |
def for_example(): |
| 474 |
for x in range(2): |
| 475 |
pass |
| 476 |
self.run_and_compare( |
| 477 |
for_example, |
| 478 |
[(0, 'call'), |
| 479 |
(1, 'line'), |
| 480 |
(2, 'line'), |
| 481 |
(1, 'line'), |
| 482 |
(2, 'line'), |
| 483 |
(1, 'line'), |
| 484 |
(1, 'return')]) |
| 485 |
|
| 486 |
def while_example(): |
| 487 |
# While expression should be traced on every loop |
| 488 |
x = 2 |
| 489 |
while x > 0: |
| 490 |
x -= 1 |
| 491 |
self.run_and_compare( |
| 492 |
while_example, |
| 493 |
[(0, 'call'), |
| 494 |
(2, 'line'), |
| 495 |
(3, 'line'), |
| 496 |
(4, 'line'), |
| 497 |
(3, 'line'), |
| 498 |
(4, 'line'), |
| 499 |
(3, 'line'), |
| 500 |
(3, 'return')]) |
| 501 |
|
| 502 |
def test_16_blank_lines(self): |
| 503 |
namespace = {} |
| 504 |
exec("def f():\n" + "\n" * 256 + " pass", namespace) |
| 505 |
self.run_and_compare( |
| 506 |
namespace["f"], |
| 507 |
[(0, 'call'), |
| 508 |
(257, 'line'), |
| 509 |
(257, 'return')]) |
| 510 |
|
| 511 |
def test_17_none_f_trace(self): |
| 512 |
# Issue 20041: fix TypeError when f_trace is set to None. |
| 513 |
def func(): |
| 514 |
sys._getframe().f_trace = None |
| 515 |
lineno = 2 |
| 516 |
self.run_and_compare(func, |
| 517 |
[(0, 'call'), |
| 518 |
(1, 'line')]) |
| 519 |
|
| 520 |
def test_18_except_with_name(self): |
| 521 |
def func(): |
| 522 |
try: |
| 523 |
try: |
| 524 |
raise Exception |
| 525 |
except Exception as e: |
| 526 |
raise |
| 527 |
x = "Something" |
| 528 |
y = "Something" |
| 529 |
except Exception: |
| 530 |
pass |
| 531 |
|
| 532 |
self.run_and_compare(func, |
| 533 |
[(0, 'call'), |
| 534 |
(1, 'line'), |
| 535 |
(2, 'line'), |
| 536 |
(3, 'line'), |
| 537 |
(3, 'exception'), |
| 538 |
(4, 'line'), |
| 539 |
(5, 'line'), |
| 540 |
(8, 'line'), |
| 541 |
(9, 'line'), |
| 542 |
(9, 'return')]) |
| 543 |
|
| 544 |
def test_19_except_with_finally(self): |
| 545 |
def func(): |
| 546 |
try: |
| 547 |
try: |
| 548 |
raise Exception |
| 549 |
finally: |
| 550 |
y = "Something" |
| 551 |
except Exception: |
| 552 |
b = 23 |
| 553 |
|
| 554 |
self.run_and_compare(func, |
| 555 |
[(0, 'call'), |
| 556 |
(1, 'line'), |
| 557 |
(2, 'line'), |
| 558 |
(3, 'line'), |
| 559 |
(3, 'exception'), |
| 560 |
(5, 'line'), |
| 561 |
(6, 'line'), |
| 562 |
(7, 'line'), |
| 563 |
(7, 'return')]) |
| 564 |
|
| 565 |
def test_20_async_for_loop(self): |
| 566 |
class AsyncIteratorWrapper: |
| 567 |
def __init__(self, obj): |
| 568 |
self._it = iter(obj) |
| 569 |
|
| 570 |
def __aiter__(self): |
| 571 |
return self |
| 572 |
|
| 573 |
async def __anext__(self): |
| 574 |
try: |
| 575 |
return next(self._it) |
| 576 |
except StopIteration: |
| 577 |
raise StopAsyncIteration |
| 578 |
|
| 579 |
async def doit_async(): |
| 580 |
async for letter in AsyncIteratorWrapper("abc"): |
| 581 |
x = letter |
| 582 |
y = 42 |
| 583 |
|
| 584 |
def run(tracer): |
| 585 |
x = doit_async() |
| 586 |
try: |
| 587 |
sys.settrace(tracer) |
| 588 |
x.send(None) |
| 589 |
finally: |
| 590 |
sys.settrace(None) |
| 591 |
|
| 592 |
tracer = self.make_tracer() |
| 593 |
events = [ |
| 594 |
(0, 'call'), |
| 595 |
(1, 'line'), |
| 596 |
(-12, 'call'), |
| 597 |
(-11, 'line'), |
| 598 |
(-11, 'return'), |
| 599 |
(-9, 'call'), |
| 600 |
(-8, 'line'), |
| 601 |
(-8, 'return'), |
| 602 |
(-6, 'call'), |
| 603 |
(-5, 'line'), |
| 604 |
(-4, 'line'), |
| 605 |
(-4, 'return'), |
| 606 |
(1, 'exception'), |
| 607 |
(2, 'line'), |
| 608 |
(1, 'line'), |
| 609 |
(-6, 'call'), |
| 610 |
(-5, 'line'), |
| 611 |
(-4, 'line'), |
| 612 |
(-4, 'return'), |
| 613 |
(1, 'exception'), |
| 614 |
(2, 'line'), |
| 615 |
(1, 'line'), |
| 616 |
(-6, 'call'), |
| 617 |
(-5, 'line'), |
| 618 |
(-4, 'line'), |
| 619 |
(-4, 'return'), |
| 620 |
(1, 'exception'), |
| 621 |
(2, 'line'), |
| 622 |
(1, 'line'), |
| 623 |
(-6, 'call'), |
| 624 |
(-5, 'line'), |
| 625 |
(-4, 'line'), |
| 626 |
(-4, 'exception'), |
| 627 |
(-3, 'line'), |
| 628 |
(-2, 'line'), |
| 629 |
(-2, 'exception'), |
| 630 |
(-2, 'return'), |
| 631 |
(1, 'exception'), |
| 632 |
(3, 'line'), |
| 633 |
(3, 'return')] |
| 634 |
try: |
| 635 |
run(tracer.trace) |
| 636 |
except Exception: |
| 637 |
pass |
| 638 |
self.compare_events(doit_async.__code__.co_firstlineno, |
| 639 |
tracer.events, events) |
| 640 |
|
| 641 |
def test_async_for_backwards_jump_has_no_line(self): |
| 642 |
async def arange(n): |
| 643 |
for i in range(n): |
| 644 |
yield i |
| 645 |
async def f(): |
| 646 |
async for i in arange(3): |
| 647 |
if i > 100: |
| 648 |
break # should never be traced |
| 649 |
|
| 650 |
tracer = self.make_tracer() |
| 651 |
coro = f() |
| 652 |
try: |
| 653 |
sys.settrace(tracer.trace) |
| 654 |
coro.send(None) |
| 655 |
except Exception: |
| 656 |
pass |
| 657 |
finally: |
| 658 |
sys.settrace(None) |
| 659 |
|
| 660 |
events = [ |
| 661 |
(0, 'call'), |
| 662 |
(1, 'line'), |
| 663 |
(-3, 'call'), |
| 664 |
(-2, 'line'), |
| 665 |
(-1, 'line'), |
| 666 |
(-1, 'return'), |
| 667 |
(1, 'exception'), |
| 668 |
(2, 'line'), |
| 669 |
(1, 'line'), |
| 670 |
(-1, 'call'), |
| 671 |
(-2, 'line'), |
| 672 |
(-1, 'line'), |
| 673 |
(-1, 'return'), |
| 674 |
(1, 'exception'), |
| 675 |
(2, 'line'), |
| 676 |
(1, 'line'), |
| 677 |
(-1, 'call'), |
| 678 |
(-2, 'line'), |
| 679 |
(-1, 'line'), |
| 680 |
(-1, 'return'), |
| 681 |
(1, 'exception'), |
| 682 |
(2, 'line'), |
| 683 |
(1, 'line'), |
| 684 |
(-1, 'call'), |
| 685 |
(-2, 'line'), |
| 686 |
(-2, 'return'), |
| 687 |
(1, 'exception'), |
| 688 |
(1, 'return'), |
| 689 |
] |
| 690 |
self.compare_events(f.__code__.co_firstlineno, |
| 691 |
tracer.events, events) |
| 692 |
|
| 693 |
def test_21_repeated_pass(self): |
| 694 |
def func(): |
| 695 |
pass |
| 696 |
pass |
| 697 |
|
| 698 |
self.run_and_compare(func, |
| 699 |
[(0, 'call'), |
| 700 |
(1, 'line'), |
| 701 |
(2, 'line'), |
| 702 |
(2, 'return')]) |
| 703 |
|
| 704 |
def test_loop_in_try_except(self): |
| 705 |
# https://bugs.python.org/issue41670 |
| 706 |
|
| 707 |
def func(): |
| 708 |
try: |
| 709 |
for i in []: pass |
| 710 |
return 1 |
| 711 |
except: |
| 712 |
return 2 |
| 713 |
|
| 714 |
self.run_and_compare(func, |
| 715 |
[(0, 'call'), |
| 716 |
(1, 'line'), |
| 717 |
(2, 'line'), |
| 718 |
(3, 'line'), |
| 719 |
(3, 'return')]) |
| 720 |
|
| 721 |
def test_try_except_no_exception(self): |
| 722 |
|
| 723 |
def func(): |
| 724 |
try: |
| 725 |
2 |
| 726 |
except: |
| 727 |
4 |
| 728 |
else: |
| 729 |
6 |
| 730 |
if False: |
| 731 |
8 |
| 732 |
else: |
| 733 |
10 |
| 734 |
if func.__name__ == 'Fred': |
| 735 |
12 |
| 736 |
finally: |
| 737 |
14 |
| 738 |
|
| 739 |
self.run_and_compare(func, |
| 740 |
[(0, 'call'), |
| 741 |
(1, 'line'), |
| 742 |
(2, 'line'), |
| 743 |
(6, 'line'), |
| 744 |
(7, 'line'), |
| 745 |
(10, 'line'), |
| 746 |
(11, 'line'), |
| 747 |
(14, 'line'), |
| 748 |
(14, 'return')]) |
| 749 |
|
| 750 |
def test_try_exception_in_else(self): |
| 751 |
|
| 752 |
def func(): |
| 753 |
try: |
| 754 |
try: |
| 755 |
3 |
| 756 |
except: |
| 757 |
5 |
| 758 |
else: |
| 759 |
7 |
| 760 |
raise Exception |
| 761 |
finally: |
| 762 |
10 |
| 763 |
except: |
| 764 |
12 |
| 765 |
finally: |
| 766 |
14 |
| 767 |
|
| 768 |
self.run_and_compare(func, |
| 769 |
[(0, 'call'), |
| 770 |
(1, 'line'), |
| 771 |
(2, 'line'), |
| 772 |
(3, 'line'), |
| 773 |
(7, 'line'), |
| 774 |
(8, 'line'), |
| 775 |
(8, 'exception'), |
| 776 |
(10, 'line'), |
| 777 |
(11, 'line'), |
| 778 |
(12, 'line'), |
| 779 |
(14, 'line'), |
| 780 |
(14, 'return')]) |
| 781 |
|
| 782 |
def test_nested_loops(self): |
| 783 |
|
| 784 |
def func(): |
| 785 |
for i in range(2): |
| 786 |
for j in range(2): |
| 787 |
a = i + j |
| 788 |
return a == 1 |
| 789 |
|
| 790 |
self.run_and_compare(func, |
| 791 |
[(0, 'call'), |
| 792 |
(1, 'line'), |
| 793 |
(2, 'line'), |
| 794 |
(3, 'line'), |
| 795 |
(2, 'line'), |
| 796 |
(3, 'line'), |
| 797 |
(2, 'line'), |
| 798 |
(1, 'line'), |
| 799 |
(2, 'line'), |
| 800 |
(3, 'line'), |
| 801 |
(2, 'line'), |
| 802 |
(3, 'line'), |
| 803 |
(2, 'line'), |
| 804 |
(1, 'line'), |
| 805 |
(4, 'line'), |
| 806 |
(4, 'return')]) |
| 807 |
|
| 808 |
def test_if_break(self): |
| 809 |
|
| 810 |
def func(): |
| 811 |
seq = [1, 0] |
| 812 |
while seq: |
| 813 |
n = seq.pop() |
| 814 |
if n: |
| 815 |
break # line 5 |
| 816 |
else: |
| 817 |
n = 99 |
| 818 |
return n # line 8 |
| 819 |
|
| 820 |
self.run_and_compare(func, |
| 821 |
[(0, 'call'), |
| 822 |
(1, 'line'), |
| 823 |
(2, 'line'), |
| 824 |
(3, 'line'), |
| 825 |
(4, 'line'), |
| 826 |
(2, 'line'), |
| 827 |
(3, 'line'), |
| 828 |
(4, 'line'), |
| 829 |
(5, 'line'), |
| 830 |
(8, 'line'), |
| 831 |
(8, 'return')]) |
| 832 |
|
| 833 |
def test_break_through_finally(self): |
| 834 |
|
| 835 |
def func(): |
| 836 |
a, c, d, i = 1, 1, 1, 99 |
| 837 |
try: |
| 838 |
for i in range(3): |
| 839 |
try: |
| 840 |
a = 5 |
| 841 |
if i > 0: |
| 842 |
break # line 7 |
| 843 |
a = 8 |
| 844 |
finally: |
| 845 |
c = 10 |
| 846 |
except: |
| 847 |
d = 12 # line 12 |
| 848 |
assert a == 5 and c == 10 and d == 1 # line 13 |
| 849 |
|
| 850 |
self.run_and_compare(func, |
| 851 |
[(0, 'call'), |
| 852 |
(1, 'line'), |
| 853 |
(2, 'line'), |
| 854 |
(3, 'line'), |
| 855 |
(4, 'line'), |
| 856 |
(5, 'line'), |
| 857 |
(6, 'line'), |
| 858 |
(8, 'line'), |
| 859 |
(10, 'line'), |
| 860 |
(3, 'line'), |
| 861 |
(4, 'line'), |
| 862 |
(5, 'line'), |
| 863 |
(6, 'line'), |
| 864 |
(7, 'line'), |
| 865 |
(10, 'line')] + |
| 866 |
([(13, 'line'), (13, 'return')] if __debug__ else [(10, 'return')])) |
| 867 |
|
| 868 |
def test_continue_through_finally(self): |
| 869 |
|
| 870 |
def func(): |
| 871 |
a, b, c, d, i = 1, 1, 1, 1, 99 |
| 872 |
try: |
| 873 |
for i in range(2): |
| 874 |
try: |
| 875 |
a = 5 |
| 876 |
if i > 0: |
| 877 |
continue # line 7 |
| 878 |
b = 8 |
| 879 |
finally: |
| 880 |
c = 10 |
| 881 |
except: |
| 882 |
d = 12 # line 12 |
| 883 |
assert (a, b, c, d) == (5, 8, 10, 1) # line 13 |
| 884 |
|
| 885 |
self.run_and_compare(func, |
| 886 |
[(0, 'call'), |
| 887 |
(1, 'line'), |
| 888 |
(2, 'line'), |
| 889 |
(3, 'line'), |
| 890 |
(4, 'line'), |
| 891 |
(5, 'line'), |
| 892 |
(6, 'line'), |
| 893 |
(8, 'line'), |
| 894 |
(10, 'line'), |
| 895 |
(3, 'line'), |
| 896 |
(4, 'line'), |
| 897 |
(5, 'line'), |
| 898 |
(6, 'line'), |
| 899 |
(7, 'line'), |
| 900 |
(10, 'line'), |
| 901 |
(3, 'line')] + |
| 902 |
([(13, 'line'), (13, 'return')] if __debug__ else [(3, 'return')])) |
| 903 |
|
| 904 |
def test_return_through_finally(self): |
| 905 |
|
| 906 |
def func(): |
| 907 |
try: |
| 908 |
return 2 |
| 909 |
finally: |
| 910 |
4 |
| 911 |
|
| 912 |
self.run_and_compare(func, |
| 913 |
[(0, 'call'), |
| 914 |
(1, 'line'), |
| 915 |
(2, 'line'), |
| 916 |
(4, 'line'), |
| 917 |
(4, 'return')]) |
| 918 |
|
| 919 |
def test_try_except_with_wrong_type(self): |
| 920 |
|
| 921 |
def func(): |
| 922 |
try: |
| 923 |
try: |
| 924 |
2/0 |
| 925 |
except IndexError: |
| 926 |
5 |
| 927 |
finally: |
| 928 |
7 |
| 929 |
except: |
| 930 |
pass |
| 931 |
return 10 |
| 932 |
|
| 933 |
self.run_and_compare(func, |
| 934 |
[(0, 'call'), |
| 935 |
(1, 'line'), |
| 936 |
(2, 'line'), |
| 937 |
(3, 'line'), |
| 938 |
(3, 'exception'), |
| 939 |
(4, 'line'), |
| 940 |
(7, 'line'), |
| 941 |
(8, 'line'), |
| 942 |
(9, 'line'), |
| 943 |
(10, 'line'), |
| 944 |
(10, 'return')]) |
| 945 |
|
| 946 |
def test_finally_with_conditional(self): |
| 947 |
|
| 948 |
# See gh-105658 |
| 949 |
condition = True |
| 950 |
def func(): |
| 951 |
try: |
| 952 |
try: |
| 953 |
raise Exception |
| 954 |
finally: |
| 955 |
if condition: |
| 956 |
result = 1 |
| 957 |
result = 2 |
| 958 |
except: |
| 959 |
result = 3 |
| 960 |
return result |
| 961 |
|
| 962 |
self.run_and_compare(func, |
| 963 |
[(0, 'call'), |
| 964 |
(1, 'line'), |
| 965 |
(2, 'line'), |
| 966 |
(3, 'line'), |
| 967 |
(3, 'exception'), |
| 968 |
(5, 'line'), |
| 969 |
(6, 'line'), |
| 970 |
(8, 'line'), |
| 971 |
(9, 'line'), |
| 972 |
(10, 'line'), |
| 973 |
(10, 'return')]) |
| 974 |
|
| 975 |
def test_break_to_continue1(self): |
| 976 |
|
| 977 |
def func(): |
| 978 |
TRUE = 1 |
| 979 |
x = [1] |
| 980 |
while x: |
| 981 |
x.pop() |
| 982 |
while TRUE: |
| 983 |
break |
| 984 |
continue |
| 985 |
|
| 986 |
self.run_and_compare(func, |
| 987 |
[(0, 'call'), |
| 988 |
(1, 'line'), |
| 989 |
(2, 'line'), |
| 990 |
(3, 'line'), |
| 991 |
(4, 'line'), |
| 992 |
(5, 'line'), |
| 993 |
(6, 'line'), |
| 994 |
(7, 'line'), |
| 995 |
(3, 'line'), |
| 996 |
(3, 'return')]) |
| 997 |
|
| 998 |
def test_break_to_continue2(self): |
| 999 |
|
| 1000 |
def func(): |
| 1001 |
TRUE = 1 |
| 1002 |
x = [1] |
| 1003 |
while x: |
| 1004 |
x.pop() |
| 1005 |
while TRUE: |
| 1006 |
break |
| 1007 |
else: |
| 1008 |
continue |
| 1009 |
|
| 1010 |
self.run_and_compare(func, |
| 1011 |
[(0, 'call'), |
| 1012 |
(1, 'line'), |
| 1013 |
(2, 'line'), |
| 1014 |
(3, 'line'), |
| 1015 |
(4, 'line'), |
| 1016 |
(5, 'line'), |
| 1017 |
(6, 'line'), |
| 1018 |
(3, 'line'), |
| 1019 |
(3, 'return')]) |
| 1020 |
|
| 1021 |
def test_break_to_break(self): |
| 1022 |
|
| 1023 |
def func(): |
| 1024 |
TRUE = 1 |
| 1025 |
while TRUE: |
| 1026 |
while TRUE: |
| 1027 |
break |
| 1028 |
break |
| 1029 |
|
| 1030 |
self.run_and_compare(func, |
| 1031 |
[(0, 'call'), |
| 1032 |
(1, 'line'), |
| 1033 |
(2, 'line'), |
| 1034 |
(3, 'line'), |
| 1035 |
(4, 'line'), |
| 1036 |
(5, 'line'), |
| 1037 |
(5, 'return')]) |
| 1038 |
|
| 1039 |
def test_nested_ifs(self): |
| 1040 |
|
| 1041 |
def func(): |
| 1042 |
a = b = 1 |
| 1043 |
if a == 1: |
| 1044 |
if b == 1: |
| 1045 |
x = 4 |
| 1046 |
else: |
| 1047 |
y = 6 |
| 1048 |
else: |
| 1049 |
z = 8 |
| 1050 |
|
| 1051 |
self.run_and_compare(func, |
| 1052 |
[(0, 'call'), |
| 1053 |
(1, 'line'), |
| 1054 |
(2, 'line'), |
| 1055 |
(3, 'line'), |
| 1056 |
(4, 'line'), |
| 1057 |
(4, 'return')]) |
| 1058 |
|
| 1059 |
def test_nested_ifs_with_and(self): |
| 1060 |
|
| 1061 |
def func(): |
| 1062 |
if A: |
| 1063 |
if B: |
| 1064 |
if C: |
| 1065 |
if D: |
| 1066 |
return False |
| 1067 |
else: |
| 1068 |
return False |
| 1069 |
elif E and F: |
| 1070 |
return True |
| 1071 |
|
| 1072 |
A = B = True |
| 1073 |
C = False |
| 1074 |
|
| 1075 |
self.run_and_compare(func, |
| 1076 |
[(0, 'call'), |
| 1077 |
(1, 'line'), |
| 1078 |
(2, 'line'), |
| 1079 |
(3, 'line'), |
| 1080 |
(3, 'return')]) |
| 1081 |
|
| 1082 |
def test_nested_try_if(self): |
| 1083 |
|
| 1084 |
def func(): |
| 1085 |
x = "hello" |
| 1086 |
try: |
| 1087 |
3/0 |
| 1088 |
except ZeroDivisionError: |
| 1089 |
if x == 'raise': |
| 1090 |
raise ValueError() # line 6 |
| 1091 |
f = 7 |
| 1092 |
|
| 1093 |
self.run_and_compare(func, |
| 1094 |
[(0, 'call'), |
| 1095 |
(1, 'line'), |
| 1096 |
(2, 'line'), |
| 1097 |
(3, 'line'), |
| 1098 |
(3, 'exception'), |
| 1099 |
(4, 'line'), |
| 1100 |
(5, 'line'), |
| 1101 |
(7, 'line'), |
| 1102 |
(7, 'return')]) |
| 1103 |
|
| 1104 |
def test_if_false_in_with(self): |
| 1105 |
|
| 1106 |
class C: |
| 1107 |
def __enter__(self): |
| 1108 |
return self |
| 1109 |
def __exit__(*args): |
| 1110 |
pass |
| 1111 |
|
| 1112 |
def func(): |
| 1113 |
with C(): |
| 1114 |
if False: |
| 1115 |
pass |
| 1116 |
|
| 1117 |
self.run_and_compare(func, |
| 1118 |
[(0, 'call'), |
| 1119 |
(1, 'line'), |
| 1120 |
(-5, 'call'), |
| 1121 |
(-4, 'line'), |
| 1122 |
(-4, 'return'), |
| 1123 |
(2, 'line'), |
| 1124 |
(1, 'line'), |
| 1125 |
(-3, 'call'), |
| 1126 |
(-2, 'line'), |
| 1127 |
(-2, 'return'), |
| 1128 |
(1, 'return')]) |
| 1129 |
|
| 1130 |
def test_if_false_in_try_except(self): |
| 1131 |
|
| 1132 |
def func(): |
| 1133 |
try: |
| 1134 |
if False: |
| 1135 |
pass |
| 1136 |
except Exception: |
| 1137 |
X |
| 1138 |
|
| 1139 |
self.run_and_compare(func, |
| 1140 |
[(0, 'call'), |
| 1141 |
(1, 'line'), |
| 1142 |
(2, 'line'), |
| 1143 |
(2, 'return')]) |
| 1144 |
|
| 1145 |
def test_implicit_return_in_class(self): |
| 1146 |
|
| 1147 |
def func(): |
| 1148 |
class A: |
| 1149 |
if 3 < 9: |
| 1150 |
a = 1 |
| 1151 |
else: |
| 1152 |
a = 2 |
| 1153 |
|
| 1154 |
self.run_and_compare(func, |
| 1155 |
[(0, 'call'), |
| 1156 |
(1, 'line'), |
| 1157 |
(1, 'call'), |
| 1158 |
(1, 'line'), |
| 1159 |
(2, 'line'), |
| 1160 |
(3, 'line'), |
| 1161 |
(3, 'return'), |
| 1162 |
(1, 'return')]) |
| 1163 |
|
| 1164 |
def test_try_in_try(self): |
| 1165 |
def func(): |
| 1166 |
try: |
| 1167 |
try: |
| 1168 |
pass |
| 1169 |
except Exception as ex: |
| 1170 |
pass |
| 1171 |
except Exception: |
| 1172 |
pass |
| 1173 |
|
| 1174 |
self.run_and_compare(func, |
| 1175 |
[(0, 'call'), |
| 1176 |
(1, 'line'), |
| 1177 |
(2, 'line'), |
| 1178 |
(3, 'line'), |
| 1179 |
(3, 'return')]) |
| 1180 |
|
| 1181 |
def test_try_in_try_with_exception(self): |
| 1182 |
|
| 1183 |
def func(): |
| 1184 |
try: |
| 1185 |
try: |
| 1186 |
raise TypeError |
| 1187 |
except ValueError as ex: |
| 1188 |
5 |
| 1189 |
except TypeError: |
| 1190 |
7 |
| 1191 |
|
| 1192 |
self.run_and_compare(func, |
| 1193 |
[(0, 'call'), |
| 1194 |
(1, 'line'), |
| 1195 |
(2, 'line'), |
| 1196 |
(3, 'line'), |
| 1197 |
(3, 'exception'), |
| 1198 |
(4, 'line'), |
| 1199 |
(6, 'line'), |
| 1200 |
(7, 'line'), |
| 1201 |
(7, 'return')]) |
| 1202 |
|
| 1203 |
def func(): |
| 1204 |
try: |
| 1205 |
try: |
| 1206 |
raise ValueError |
| 1207 |
except ValueError as ex: |
| 1208 |
5 |
| 1209 |
except TypeError: |
| 1210 |
7 |
| 1211 |
|
| 1212 |
self.run_and_compare(func, |
| 1213 |
[(0, 'call'), |
| 1214 |
(1, 'line'), |
| 1215 |
(2, 'line'), |
| 1216 |
(3, 'line'), |
| 1217 |
(3, 'exception'), |
| 1218 |
(4, 'line'), |
| 1219 |
(5, 'line'), |
| 1220 |
(5, 'return')]) |
| 1221 |
|
| 1222 |
def test_if_in_if_in_if(self): |
| 1223 |
def func(a=0, p=1, z=1): |
| 1224 |
if p: |
| 1225 |
if a: |
| 1226 |
if z: |
| 1227 |
pass |
| 1228 |
else: |
| 1229 |
pass |
| 1230 |
else: |
| 1231 |
pass |
| 1232 |
|
| 1233 |
self.run_and_compare(func, |
| 1234 |
[(0, 'call'), |
| 1235 |
(1, 'line'), |
| 1236 |
(2, 'line'), |
| 1237 |
(2, 'return')]) |
| 1238 |
|
| 1239 |
def test_early_exit_with(self): |
| 1240 |
|
| 1241 |
class C: |
| 1242 |
def __enter__(self): |
| 1243 |
return self |
| 1244 |
def __exit__(*args): |
| 1245 |
pass |
| 1246 |
|
| 1247 |
def func_break(): |
| 1248 |
for i in (1,2): |
| 1249 |
with C(): |
| 1250 |
break |
| 1251 |
pass |
| 1252 |
|
| 1253 |
def func_return(): |
| 1254 |
with C(): |
| 1255 |
return |
| 1256 |
|
| 1257 |
self.run_and_compare(func_break, |
| 1258 |
[(0, 'call'), |
| 1259 |
(1, 'line'), |
| 1260 |
(2, 'line'), |
| 1261 |
(-5, 'call'), |
| 1262 |
(-4, 'line'), |
| 1263 |
(-4, 'return'), |
| 1264 |
(3, 'line'), |
| 1265 |
(2, 'line'), |
| 1266 |
(-3, 'call'), |
| 1267 |
(-2, 'line'), |
| 1268 |
(-2, 'return'), |
| 1269 |
(4, 'line'), |
| 1270 |
(4, 'return')]) |
| 1271 |
|
| 1272 |
self.run_and_compare(func_return, |
| 1273 |
[(0, 'call'), |
| 1274 |
(1, 'line'), |
| 1275 |
(-11, 'call'), |
| 1276 |
(-10, 'line'), |
| 1277 |
(-10, 'return'), |
| 1278 |
(2, 'line'), |
| 1279 |
(1, 'line'), |
| 1280 |
(-9, 'call'), |
| 1281 |
(-8, 'line'), |
| 1282 |
(-8, 'return'), |
| 1283 |
(1, 'return')]) |
| 1284 |
|
| 1285 |
def test_flow_converges_on_same_line(self): |
| 1286 |
|
| 1287 |
def foo(x): |
| 1288 |
if x: |
| 1289 |
try: |
| 1290 |
1/(x - 1) |
| 1291 |
except ZeroDivisionError: |
| 1292 |
pass |
| 1293 |
return x |
| 1294 |
|
| 1295 |
def func(): |
| 1296 |
for i in range(2): |
| 1297 |
foo(i) |
| 1298 |
|
| 1299 |
self.run_and_compare(func, |
| 1300 |
[(0, 'call'), |
| 1301 |
(1, 'line'), |
| 1302 |
(2, 'line'), |
| 1303 |
(-8, 'call'), |
| 1304 |
(-7, 'line'), |
| 1305 |
(-2, 'line'), |
| 1306 |
(-2, 'return'), |
| 1307 |
(1, 'line'), |
| 1308 |
(2, 'line'), |
| 1309 |
(-8, 'call'), |
| 1310 |
(-7, 'line'), |
| 1311 |
(-6, 'line'), |
| 1312 |
(-5, 'line'), |
| 1313 |
(-5, 'exception'), |
| 1314 |
(-4, 'line'), |
| 1315 |
(-3, 'line'), |
| 1316 |
(-2, 'line'), |
| 1317 |
(-2, 'return'), |
| 1318 |
(1, 'line'), |
| 1319 |
(1, 'return')]) |
| 1320 |
|
| 1321 |
def test_no_tracing_of_named_except_cleanup(self): |
| 1322 |
|
| 1323 |
def func(): |
| 1324 |
x = 0 |
| 1325 |
try: |
| 1326 |
1/x |
| 1327 |
except ZeroDivisionError as error: |
| 1328 |
if x: |
| 1329 |
raise |
| 1330 |
return "done" |
| 1331 |
|
| 1332 |
self.run_and_compare(func, |
| 1333 |
[(0, 'call'), |
| 1334 |
(1, 'line'), |
| 1335 |
(2, 'line'), |
| 1336 |
(3, 'line'), |
| 1337 |
(3, 'exception'), |
| 1338 |
(4, 'line'), |
| 1339 |
(5, 'line'), |
| 1340 |
(7, 'line'), |
| 1341 |
(7, 'return')]) |
| 1342 |
|
| 1343 |
def test_tracing_exception_raised_in_with(self): |
| 1344 |
|
| 1345 |
class NullCtx: |
| 1346 |
def __enter__(self): |
| 1347 |
return self |
| 1348 |
def __exit__(self, *excinfo): |
| 1349 |
pass |
| 1350 |
|
| 1351 |
def func(): |
| 1352 |
try: |
| 1353 |
with NullCtx(): |
| 1354 |
1/0 |
| 1355 |
except ZeroDivisionError: |
| 1356 |
pass |
| 1357 |
|
| 1358 |
self.run_and_compare(func, |
| 1359 |
[(0, 'call'), |
| 1360 |
(1, 'line'), |
| 1361 |
(2, 'line'), |
| 1362 |
(-5, 'call'), |
| 1363 |
(-4, 'line'), |
| 1364 |
(-4, 'return'), |
| 1365 |
(3, 'line'), |
| 1366 |
(3, 'exception'), |
| 1367 |
(2, 'line'), |
| 1368 |
(-3, 'call'), |
| 1369 |
(-2, 'line'), |
| 1370 |
(-2, 'return'), |
| 1371 |
(4, 'line'), |
| 1372 |
(5, 'line'), |
| 1373 |
(5, 'return')]) |
| 1374 |
|
| 1375 |
def test_try_except_star_no_exception(self): |
| 1376 |
|
| 1377 |
def func(): |
| 1378 |
try: |
| 1379 |
2 |
| 1380 |
except* Exception: |
| 1381 |
4 |
| 1382 |
else: |
| 1383 |
6 |
| 1384 |
if False: |
| 1385 |
8 |
| 1386 |
else: |
| 1387 |
10 |
| 1388 |
if func.__name__ == 'Fred': |
| 1389 |
12 |
| 1390 |
finally: |
| 1391 |
14 |
| 1392 |
|
| 1393 |
self.run_and_compare(func, |
| 1394 |
[(0, 'call'), |
| 1395 |
(1, 'line'), |
| 1396 |
(2, 'line'), |
| 1397 |
(6, 'line'), |
| 1398 |
(7, 'line'), |
| 1399 |
(10, 'line'), |
| 1400 |
(11, 'line'), |
| 1401 |
(14, 'line'), |
| 1402 |
(14, 'return')]) |
| 1403 |
|
| 1404 |
def test_try_except_star_named_no_exception(self): |
| 1405 |
|
| 1406 |
def func(): |
| 1407 |
try: |
| 1408 |
2 |
| 1409 |
except* Exception as e: |
| 1410 |
4 |
| 1411 |
else: |
| 1412 |
6 |
| 1413 |
finally: |
| 1414 |
8 |
| 1415 |
|
| 1416 |
self.run_and_compare(func, |
| 1417 |
[(0, 'call'), |
| 1418 |
(1, 'line'), |
| 1419 |
(2, 'line'), |
| 1420 |
(6, 'line'), |
| 1421 |
(8, 'line'), |
| 1422 |
(8, 'return')]) |
| 1423 |
|
| 1424 |
def test_try_except_star_exception_caught(self): |
| 1425 |
|
| 1426 |
def func(): |
| 1427 |
try: |
| 1428 |
raise ValueError(2) |
| 1429 |
except* ValueError: |
| 1430 |
4 |
| 1431 |
else: |
| 1432 |
6 |
| 1433 |
finally: |
| 1434 |
8 |
| 1435 |
|
| 1436 |
self.run_and_compare(func, |
| 1437 |
[(0, 'call'), |
| 1438 |
(1, 'line'), |
| 1439 |
(2, 'line'), |
| 1440 |
(2, 'exception'), |
| 1441 |
(3, 'line'), |
| 1442 |
(4, 'line'), |
| 1443 |
(8, 'line'), |
| 1444 |
(8, 'return')]) |
| 1445 |
|
| 1446 |
def test_try_except_star_named_exception_caught(self): |
| 1447 |
|
| 1448 |
def func(): |
| 1449 |
try: |
| 1450 |
raise ValueError(2) |
| 1451 |
except* ValueError as e: |
| 1452 |
4 |
| 1453 |
else: |
| 1454 |
6 |
| 1455 |
finally: |
| 1456 |
8 |
| 1457 |
|
| 1458 |
self.run_and_compare(func, |
| 1459 |
[(0, 'call'), |
| 1460 |
(1, 'line'), |
| 1461 |
(2, 'line'), |
| 1462 |
(2, 'exception'), |
| 1463 |
(3, 'line'), |
| 1464 |
(4, 'line'), |
| 1465 |
(8, 'line'), |
| 1466 |
(8, 'return')]) |
| 1467 |
|
| 1468 |
def test_try_except_star_exception_not_caught(self): |
| 1469 |
|
| 1470 |
def func(): |
| 1471 |
try: |
| 1472 |
try: |
| 1473 |
raise ValueError(3) |
| 1474 |
except* TypeError: |
| 1475 |
5 |
| 1476 |
except ValueError: |
| 1477 |
7 |
| 1478 |
|
| 1479 |
self.run_and_compare(func, |
| 1480 |
[(0, 'call'), |
| 1481 |
(1, 'line'), |
| 1482 |
(2, 'line'), |
| 1483 |
(3, 'line'), |
| 1484 |
(3, 'exception'), |
| 1485 |
(4, 'line'), |
| 1486 |
(6, 'line'), |
| 1487 |
(7, 'line'), |
| 1488 |
(7, 'return')]) |
| 1489 |
|
| 1490 |
def test_try_except_star_named_exception_not_caught(self): |
| 1491 |
|
| 1492 |
def func(): |
| 1493 |
try: |
| 1494 |
try: |
| 1495 |
raise ValueError(3) |
| 1496 |
except* TypeError as e: |
| 1497 |
5 |
| 1498 |
except ValueError: |
| 1499 |
7 |
| 1500 |
|
| 1501 |
self.run_and_compare(func, |
| 1502 |
[(0, 'call'), |
| 1503 |
(1, 'line'), |
| 1504 |
(2, 'line'), |
| 1505 |
(3, 'line'), |
| 1506 |
(3, 'exception'), |
| 1507 |
(4, 'line'), |
| 1508 |
(6, 'line'), |
| 1509 |
(7, 'line'), |
| 1510 |
(7, 'return')]) |
| 1511 |
|
| 1512 |
def test_try_except_star_nested(self): |
| 1513 |
|
| 1514 |
def func(): |
| 1515 |
try: |
| 1516 |
try: |
| 1517 |
raise ExceptionGroup( |
| 1518 |
'eg', |
| 1519 |
[ValueError(5), TypeError('bad type')]) |
| 1520 |
except* TypeError as e: |
| 1521 |
7 |
| 1522 |
except* OSError: |
| 1523 |
9 |
| 1524 |
except* ValueError: |
| 1525 |
raise |
| 1526 |
except* ValueError: |
| 1527 |
try: |
| 1528 |
raise TypeError(14) |
| 1529 |
except* OSError: |
| 1530 |
16 |
| 1531 |
except* TypeError as e: |
| 1532 |
18 |
| 1533 |
return 0 |
| 1534 |
|
| 1535 |
self.run_and_compare(func, |
| 1536 |
[(0, 'call'), |
| 1537 |
(1, 'line'), |
| 1538 |
(2, 'line'), |
| 1539 |
(3, 'line'), |
| 1540 |
(4, 'line'), |
| 1541 |
(5, 'line'), |
| 1542 |
(3, 'line'), |
| 1543 |
(3, 'exception'), |
| 1544 |
(6, 'line'), |
| 1545 |
(7, 'line'), |
| 1546 |
(8, 'line'), |
| 1547 |
(10, 'line'), |
| 1548 |
(11, 'line'), |
| 1549 |
(12, 'line'), |
| 1550 |
(13, 'line'), |
| 1551 |
(14, 'line'), |
| 1552 |
(14, 'exception'), |
| 1553 |
(15, 'line'), |
| 1554 |
(17, 'line'), |
| 1555 |
(18, 'line'), |
| 1556 |
(19, 'line'), |
| 1557 |
(19, 'return')]) |
| 1558 |
|
| 1559 |
def test_notrace_lambda(self): |
| 1560 |
#Regression test for issue 46314 |
| 1561 |
|
| 1562 |
def func(): |
| 1563 |
1 |
| 1564 |
lambda x: 2 |
| 1565 |
3 |
| 1566 |
|
| 1567 |
self.run_and_compare(func, |
| 1568 |
[(0, 'call'), |
| 1569 |
(1, 'line'), |
| 1570 |
(2, 'line'), |
| 1571 |
(3, 'line'), |
| 1572 |
(3, 'return')]) |
| 1573 |
|
| 1574 |
def test_class_creation_with_docstrings(self): |
| 1575 |
|
| 1576 |
def func(): |
| 1577 |
class Class_1: |
| 1578 |
''' the docstring. 2''' |
| 1579 |
def __init__(self): |
| 1580 |
''' Another docstring. 4''' |
| 1581 |
self.a = 5 |
| 1582 |
|
| 1583 |
self.run_and_compare(func, |
| 1584 |
[(0, 'call'), |
| 1585 |
(1, 'line'), |
| 1586 |
(1, 'call'), |
| 1587 |
(1, 'line'), |
| 1588 |
(2, 'line'), |
| 1589 |
(3, 'line'), |
| 1590 |
(3, 'return'), |
| 1591 |
(1, 'return')]) |
| 1592 |
|
| 1593 |
def test_class_creation_with_decorator(self): |
| 1594 |
def func(): |
| 1595 |
def decorator(arg): |
| 1596 |
def _dec(c): |
| 1597 |
return c |
| 1598 |
return _dec |
| 1599 |
|
| 1600 |
@decorator(6) |
| 1601 |
@decorator( |
| 1602 |
len([8]), |
| 1603 |
) |
| 1604 |
class MyObject: |
| 1605 |
pass |
| 1606 |
|
| 1607 |
self.run_and_compare(func, [ |
| 1608 |
(0, 'call'), |
| 1609 |
(1, 'line'), |
| 1610 |
(6, 'line'), |
| 1611 |
(1, 'call'), |
| 1612 |
(2, 'line'), |
| 1613 |
(4, 'line'), |
| 1614 |
(4, 'return'), |
| 1615 |
(7, 'line'), |
| 1616 |
(8, 'line'), |
| 1617 |
(7, 'line'), |
| 1618 |
(1, 'call'), |
| 1619 |
(2, 'line'), |
| 1620 |
(4, 'line'), |
| 1621 |
(4, 'return'), |
| 1622 |
(10, 'line'), |
| 1623 |
(6, 'call'), |
| 1624 |
(6, 'line'), |
| 1625 |
(11, 'line'), |
| 1626 |
(11, 'return'), |
| 1627 |
(7, 'line'), |
| 1628 |
(2, 'call'), |
| 1629 |
(3, 'line'), |
| 1630 |
(3, 'return'), |
| 1631 |
(6, 'line'), |
| 1632 |
(2, 'call'), |
| 1633 |
(3, 'line'), |
| 1634 |
(3, 'return'), |
| 1635 |
(10, 'line'), |
| 1636 |
(10, 'return'), |
| 1637 |
]) |
| 1638 |
|
| 1639 |
@support.cpython_only |
| 1640 |
def test_no_line_event_after_creating_generator(self): |
| 1641 |
# Spurious line events before call events only show up with C tracer |
| 1642 |
|
| 1643 |
# Skip this test if the _testcapi module isn't available. |
| 1644 |
_testcapi = import_helper.import_module('_testcapi') |
| 1645 |
|
| 1646 |
def gen(): |
| 1647 |
yield 1 |
| 1648 |
|
| 1649 |
def func(): |
| 1650 |
for _ in ( |
| 1651 |
gen() |
| 1652 |
): |
| 1653 |
pass |
| 1654 |
|
| 1655 |
EXPECTED_EVENTS = [ |
| 1656 |
(0, 'call'), |
| 1657 |
(2, 'line'), |
| 1658 |
(-3, 'call'), |
| 1659 |
(-2, 'line'), |
| 1660 |
(-2, 'return'), |
| 1661 |
(1, 'line'), |
| 1662 |
(4, 'line'), |
| 1663 |
(2, 'line'), |
| 1664 |
(-2, 'call'), |
| 1665 |
(-2, 'return'), |
| 1666 |
(2, 'return'), |
| 1667 |
] |
| 1668 |
|
| 1669 |
# C level events should be the same as expected and the same as Python level. |
| 1670 |
|
| 1671 |
events = [] |
| 1672 |
# Turning on and off tracing must be on same line to avoid unwanted LINE events. |
| 1673 |
_testcapi.settrace_to_record(events); func(); sys.settrace(None) |
| 1674 |
start_line = func.__code__.co_firstlineno |
| 1675 |
events = [ |
| 1676 |
(line-start_line, EVENT_NAMES[what]) |
| 1677 |
for (what, line, arg) in events |
| 1678 |
] |
| 1679 |
self.assertEqual(events, EXPECTED_EVENTS) |
| 1680 |
|
| 1681 |
self.run_and_compare(func, EXPECTED_EVENTS) |
| 1682 |
|
| 1683 |
def test_correct_tracing_quickened_call_class_init(self): |
| 1684 |
|
| 1685 |
class C: |
| 1686 |
def __init__(self): |
| 1687 |
self |
| 1688 |
|
| 1689 |
def func(): |
| 1690 |
C() |
| 1691 |
|
| 1692 |
EXPECTED_EVENTS = [ |
| 1693 |
(0, 'call'), |
| 1694 |
(1, 'line'), |
| 1695 |
(-3, 'call'), |
| 1696 |
(-2, 'line'), |
| 1697 |
(-2, 'return'), |
| 1698 |
(1, 'return')] |
| 1699 |
|
| 1700 |
self.run_and_compare(func, EXPECTED_EVENTS) |
| 1701 |
# Quicken |
| 1702 |
for _ in range(100): |
| 1703 |
func() |
| 1704 |
self.run_and_compare(func, EXPECTED_EVENTS) |
| 1705 |
|
| 1706 |
def test_settrace_error(self): |
| 1707 |
raised = False |
| 1708 |
def error_once(frame, event, arg): |
| 1709 |
nonlocal raised |
| 1710 |
if not raised: |
| 1711 |
raised = True |
| 1712 |
raise Exception |
| 1713 |
return error |
| 1714 |
|
| 1715 |
try: |
| 1716 |
sys._getframe().f_trace = error_once |
| 1717 |
sys.settrace(error_once) |
| 1718 |
len([]) |
| 1719 |
except Exception as ex: |
| 1720 |
count = 0 |
| 1721 |
tb = ex.__traceback__ |
| 1722 |
while tb: |
| 1723 |
if tb.tb_frame.f_code.co_name == "test_settrace_error": |
| 1724 |
count += 1 |
| 1725 |
tb = tb.tb_next |
| 1726 |
if count == 0: |
| 1727 |
self.fail("Traceback is missing frame") |
| 1728 |
elif count > 1: |
| 1729 |
self.fail("Traceback has frame more than once") |
| 1730 |
else: |
| 1731 |
self.fail("No exception raised") |
| 1732 |
finally: |
| 1733 |
sys.settrace(None) |
| 1734 |
|
| 1735 |
@support.cpython_only |
| 1736 |
def test_testcapi_settrace_error(self): |
| 1737 |
|
| 1738 |
# Skip this test if the _testcapi module isn't available. |
| 1739 |
_testcapi = import_helper.import_module('_testcapi') |
| 1740 |
|
| 1741 |
try: |
| 1742 |
_testcapi.settrace_to_error([]) |
| 1743 |
len([]) |
| 1744 |
except Exception as ex: |
| 1745 |
count = 0 |
| 1746 |
tb = ex.__traceback__ |
| 1747 |
while tb: |
| 1748 |
if tb.tb_frame.f_code.co_name == "test_testcapi_settrace_error": |
| 1749 |
count += 1 |
| 1750 |
tb = tb.tb_next |
| 1751 |
if count == 0: |
| 1752 |
self.fail("Traceback is missing frame") |
| 1753 |
elif count > 1: |
| 1754 |
self.fail("Traceback has frame more than once") |
| 1755 |
else: |
| 1756 |
self.fail("No exception raised") |
| 1757 |
finally: |
| 1758 |
sys.settrace(None) |
| 1759 |
|
| 1760 |
def test_very_large_function(self): |
| 1761 |
# There is a separate code path when the number of lines > (1 << 15). |
| 1762 |
d = {} |
| 1763 |
exec("""def f(): # line 0 |
| 1764 |
x = 0 # line 1 |
| 1765 |
y = 1 # line 2 |
| 1766 |
%s # lines 3 through (1 << 16) |
| 1767 |
x += 1 # |
| 1768 |
return""" % ('\n' * (1 << 16),), d) |
| 1769 |
f = d['f'] |
| 1770 |
|
| 1771 |
EXPECTED_EVENTS = [ |
| 1772 |
(0, 'call'), |
| 1773 |
(1, 'line'), |
| 1774 |
(2, 'line'), |
| 1775 |
(65540, 'line'), |
| 1776 |
(65541, 'line'), |
| 1777 |
(65541, 'return'), |
| 1778 |
] |
| 1779 |
|
| 1780 |
self.run_and_compare(f, EXPECTED_EVENTS) |
| 1781 |
|
| 1782 |
|
| 1783 |
EVENT_NAMES = [ |
| 1784 |
'call', |
| 1785 |
'exception', |
| 1786 |
'line', |
| 1787 |
'return' |
| 1788 |
] |
| 1789 |
|
| 1790 |
|
| 1791 |
class SkipLineEventsTraceTestCase(TraceTestCase): |
| 1792 |
"""Repeat the trace tests, but with per-line events skipped""" |
| 1793 |
|
| 1794 |
def compare_events(self, line_offset, events, expected_events): |
| 1795 |
skip_line_events = [e for e in expected_events if e[1] != 'line'] |
| 1796 |
super().compare_events(line_offset, events, skip_line_events) |
| 1797 |
|
| 1798 |
@staticmethod |
| 1799 |
def make_tracer(): |
| 1800 |
return Tracer(trace_line_events=False) |
| 1801 |
|
| 1802 |
|
| 1803 |
@support.cpython_only |
| 1804 |
class TraceOpcodesTestCase(TraceTestCase): |
| 1805 |
"""Repeat the trace tests, but with per-opcodes events enabled""" |
| 1806 |
|
| 1807 |
def compare_events(self, line_offset, events, expected_events): |
| 1808 |
skip_opcode_events = [e for e in events if e[1] != 'opcode'] |
| 1809 |
if len(events) > 1: |
| 1810 |
self.assertLess(len(skip_opcode_events), len(events), |
| 1811 |
msg="No 'opcode' events received by the tracer") |
| 1812 |
super().compare_events(line_offset, skip_opcode_events, expected_events) |
| 1813 |
|
| 1814 |
@staticmethod |
| 1815 |
def make_tracer(): |
| 1816 |
return Tracer(trace_opcode_events=True) |
| 1817 |
|
| 1818 |
@requires_subprocess() |
| 1819 |
def test_trace_opcodes_after_settrace(self): |
| 1820 |
"""Make sure setting f_trace_opcodes after starting trace works even |
| 1821 |
if it's the first time f_trace_opcodes is being set. GH-103615""" |
| 1822 |
|
| 1823 |
code = textwrap.dedent(""" |
| 1824 |
import sys |
| 1825 |
|
| 1826 |
def opcode_trace_func(frame, event, arg): |
| 1827 |
if event == "opcode": |
| 1828 |
print("opcode trace triggered") |
| 1829 |
return opcode_trace_func |
| 1830 |
|
| 1831 |
sys.settrace(opcode_trace_func) |
| 1832 |
sys._getframe().f_trace = opcode_trace_func |
| 1833 |
sys._getframe().f_trace_opcodes = True |
| 1834 |
a = 1 |
| 1835 |
""") |
| 1836 |
|
| 1837 |
# We can't use context manager because Windows can't execute a file while |
| 1838 |
# it's being written |
| 1839 |
tmp = tempfile.NamedTemporaryFile(delete=False, suffix='.py') |
| 1840 |
tmp.write(code.encode('utf-8')) |
| 1841 |
tmp.close() |
| 1842 |
try: |
| 1843 |
p = subprocess.Popen([sys.executable, tmp.name], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 1844 |
p.wait() |
| 1845 |
out = p.stdout.read() |
| 1846 |
finally: |
| 1847 |
os.remove(tmp.name) |
| 1848 |
p.stdout.close() |
| 1849 |
p.stderr.close() |
| 1850 |
self.assertIn(b"opcode trace triggered", out) |
| 1851 |
|
| 1852 |
|
| 1853 |
class RaisingTraceFuncTestCase(unittest.TestCase): |
| 1854 |
def setUp(self): |
| 1855 |
self.addCleanup(sys.settrace, sys.gettrace()) |
| 1856 |
|
| 1857 |
def trace(self, frame, event, arg): |
| 1858 |
"""A trace function that raises an exception in response to a |
| 1859 |
specific trace event.""" |
| 1860 |
if event == self.raiseOnEvent: |
| 1861 |
raise ValueError # just something that isn't RuntimeError |
| 1862 |
else: |
| 1863 |
return self.trace |
| 1864 |
|
| 1865 |
def f(self): |
| 1866 |
"""The function to trace; raises an exception if that's the case |
| 1867 |
we're testing, so that the 'exception' trace event fires.""" |
| 1868 |
if self.raiseOnEvent == 'exception': |
| 1869 |
x = 0 |
| 1870 |
y = 1/x |
| 1871 |
else: |
| 1872 |
return 1 |
| 1873 |
|
| 1874 |
def run_test_for_event(self, event): |
| 1875 |
"""Tests that an exception raised in response to the given event is |
| 1876 |
handled OK.""" |
| 1877 |
self.raiseOnEvent = event |
| 1878 |
try: |
| 1879 |
for i in range(sys.getrecursionlimit() + 1): |
| 1880 |
sys.settrace(self.trace) |
| 1881 |
try: |
| 1882 |
self.f() |
| 1883 |
except ValueError: |
| 1884 |
pass |
| 1885 |
else: |
| 1886 |
self.fail("exception not raised!") |
| 1887 |
except RuntimeError: |
| 1888 |
self.fail("recursion counter not reset") |
| 1889 |
|
| 1890 |
# Test the handling of exceptions raised by each kind of trace event. |
| 1891 |
def test_call(self): |
| 1892 |
self.run_test_for_event('call') |
| 1893 |
def test_line(self): |
| 1894 |
self.run_test_for_event('line') |
| 1895 |
def test_return(self): |
| 1896 |
self.run_test_for_event('return') |
| 1897 |
def test_exception(self): |
| 1898 |
self.run_test_for_event('exception') |
| 1899 |
|
| 1900 |
def test_trash_stack(self): |
| 1901 |
def f(): |
| 1902 |
for i in range(5): |
| 1903 |
print(i) # line tracing will raise an exception at this line |
| 1904 |
|
| 1905 |
def g(frame, why, extra): |
| 1906 |
if (why == 'line' and |
| 1907 |
frame.f_lineno == f.__code__.co_firstlineno + 2): |
| 1908 |
raise RuntimeError("i am crashing") |
| 1909 |
return g |
| 1910 |
|
| 1911 |
sys.settrace(g) |
| 1912 |
try: |
| 1913 |
f() |
| 1914 |
except RuntimeError: |
| 1915 |
# the test is really that this doesn't segfault: |
| 1916 |
import gc |
| 1917 |
gc.collect() |
| 1918 |
else: |
| 1919 |
self.fail("exception not propagated") |
| 1920 |
|
| 1921 |
|
| 1922 |
def test_exception_arguments(self): |
| 1923 |
def f(): |
| 1924 |
x = 0 |
| 1925 |
# this should raise an error |
| 1926 |
x.no_such_attr |
| 1927 |
def g(frame, event, arg): |
| 1928 |
if (event == 'exception'): |
| 1929 |
type, exception, trace = arg |
| 1930 |
self.assertIsInstance(exception, Exception) |
| 1931 |
return g |
| 1932 |
|
| 1933 |
existing = sys.gettrace() |
| 1934 |
try: |
| 1935 |
sys.settrace(g) |
| 1936 |
try: |
| 1937 |
f() |
| 1938 |
except AttributeError: |
| 1939 |
# this is expected |
| 1940 |
pass |
| 1941 |
finally: |
| 1942 |
sys.settrace(existing) |
| 1943 |
|
| 1944 |
def test_line_event_raises_before_opcode_event(self): |
| 1945 |
exception = ValueError("BOOM!") |
| 1946 |
def trace(frame, event, arg): |
| 1947 |
if event == "line": |
| 1948 |
raise exception |
| 1949 |
frame.f_trace_opcodes = True |
| 1950 |
return trace |
| 1951 |
def f(): |
| 1952 |
pass |
| 1953 |
with self.assertRaises(ValueError) as caught: |
| 1954 |
sys.settrace(trace) |
| 1955 |
f() |
| 1956 |
self.assertIs(caught.exception, exception) |
| 1957 |
|
| 1958 |
|
| 1959 |
# 'Jump' tests: assigning to frame.f_lineno within a trace function |
| 1960 |
# moves the execution position - it's how debuggers implement a Jump |
| 1961 |
# command (aka. "Set next statement"). |
| 1962 |
|
| 1963 |
class JumpTracer: |
| 1964 |
"""Defines a trace function that jumps from one place to another.""" |
| 1965 |
|
| 1966 |
def __init__(self, function, jumpFrom, jumpTo, event='line', |
| 1967 |
decorated=False): |
| 1968 |
self.code = function.__code__ |
| 1969 |
self.jumpFrom = jumpFrom |
| 1970 |
self.jumpTo = jumpTo |
| 1971 |
self.event = event |
| 1972 |
self.firstLine = None if decorated else self.code.co_firstlineno |
| 1973 |
self.done = False |
| 1974 |
|
| 1975 |
def trace(self, frame, event, arg): |
| 1976 |
if self.done: |
| 1977 |
return |
| 1978 |
assert lineno_matches_lasti(frame) |
| 1979 |
# frame.f_code.co_firstlineno is the first line of the decorator when |
| 1980 |
# 'function' is decorated and the decorator may be written using |
| 1981 |
# multiple physical lines when it is too long. Use the first line |
| 1982 |
# trace event in 'function' to find the first line of 'function'. |
| 1983 |
if (self.firstLine is None and frame.f_code == self.code and |
| 1984 |
event == 'line'): |
| 1985 |
self.firstLine = frame.f_lineno - 1 |
| 1986 |
if (event == self.event and self.firstLine is not None and |
| 1987 |
frame.f_lineno == self.firstLine + self.jumpFrom): |
| 1988 |
f = frame |
| 1989 |
while f is not None and f.f_code != self.code: |
| 1990 |
f = f.f_back |
| 1991 |
if f is not None: |
| 1992 |
# Cope with non-integer self.jumpTo (because of |
| 1993 |
# no_jump_to_non_integers below). |
| 1994 |
try: |
| 1995 |
frame.f_lineno = self.firstLine + self.jumpTo |
| 1996 |
except TypeError: |
| 1997 |
frame.f_lineno = self.jumpTo |
| 1998 |
self.done = True |
| 1999 |
return self.trace |
| 2000 |
|
| 2001 |
# This verifies the line-numbers-must-be-integers rule. |
| 2002 |
def no_jump_to_non_integers(output): |
| 2003 |
try: |
| 2004 |
output.append(2) |
| 2005 |
except ValueError as e: |
| 2006 |
output.append('integer' in str(e)) |
| 2007 |
|
| 2008 |
# This verifies that you can't set f_lineno via _getframe or similar |
| 2009 |
# trickery. |
| 2010 |
def no_jump_without_trace_function(): |
| 2011 |
try: |
| 2012 |
previous_frame = sys._getframe().f_back |
| 2013 |
previous_frame.f_lineno = previous_frame.f_lineno |
| 2014 |
except ValueError as e: |
| 2015 |
# This is the exception we wanted; make sure the error message |
| 2016 |
# talks about trace functions. |
| 2017 |
if 'trace' not in str(e): |
| 2018 |
raise |
| 2019 |
else: |
| 2020 |
# Something's wrong - the expected exception wasn't raised. |
| 2021 |
raise AssertionError("Trace-function-less jump failed to fail") |
| 2022 |
|
| 2023 |
|
| 2024 |
class JumpTestCase(unittest.TestCase): |
| 2025 |
unbound_locals = r"assigning None to [0-9]+ unbound local" |
| 2026 |
|
| 2027 |
def setUp(self): |
| 2028 |
self.addCleanup(sys.settrace, sys.gettrace()) |
| 2029 |
sys.settrace(None) |
| 2030 |
|
| 2031 |
def compare_jump_output(self, expected, received): |
| 2032 |
if received != expected: |
| 2033 |
self.fail( "Outputs don't match:\n" + |
| 2034 |
"Expected: " + repr(expected) + "\n" + |
| 2035 |
"Received: " + repr(received)) |
| 2036 |
|
| 2037 |
def run_test(self, func, jumpFrom, jumpTo, expected, error=None, |
| 2038 |
event='line', decorated=False, warning=None): |
| 2039 |
wrapped = func |
| 2040 |
while hasattr(wrapped, '__wrapped__'): |
| 2041 |
wrapped = wrapped.__wrapped__ |
| 2042 |
|
| 2043 |
tracer = JumpTracer(wrapped, jumpFrom, jumpTo, event, decorated) |
| 2044 |
sys.settrace(tracer.trace) |
| 2045 |
output = [] |
| 2046 |
|
| 2047 |
with contextlib.ExitStack() as stack: |
| 2048 |
if error is not None: |
| 2049 |
stack.enter_context(self.assertRaisesRegex(*error)) |
| 2050 |
if warning is not None: |
| 2051 |
stack.enter_context(self.assertWarnsRegex(*warning)) |
| 2052 |
else: |
| 2053 |
stack.enter_context(warnings.catch_warnings()) |
| 2054 |
warnings.simplefilter('error') |
| 2055 |
func(output) |
| 2056 |
|
| 2057 |
sys.settrace(None) |
| 2058 |
self.compare_jump_output(expected, output) |
| 2059 |
|
| 2060 |
def run_async_test(self, func, jumpFrom, jumpTo, expected, error=None, |
| 2061 |
event='line', decorated=False, warning=None): |
| 2062 |
wrapped = func |
| 2063 |
while hasattr(wrapped, '__wrapped__'): |
| 2064 |
wrapped = wrapped.__wrapped__ |
| 2065 |
|
| 2066 |
tracer = JumpTracer(wrapped, jumpFrom, jumpTo, event, decorated) |
| 2067 |
sys.settrace(tracer.trace) |
| 2068 |
output = [] |
| 2069 |
|
| 2070 |
with contextlib.ExitStack() as stack: |
| 2071 |
if error is not None: |
| 2072 |
stack.enter_context(self.assertRaisesRegex(*error)) |
| 2073 |
if warning is not None: |
| 2074 |
stack.enter_context(self.assertWarnsRegex(*warning)) |
| 2075 |
run_no_yield_async_fn(func, output) |
| 2076 |
|
| 2077 |
sys.settrace(None) |
| 2078 |
self.compare_jump_output(expected, output) |
| 2079 |
|
| 2080 |
def jump_test(jumpFrom, jumpTo, expected, error=None, event='line', warning=None): |
| 2081 |
"""Decorator that creates a test that makes a jump |
| 2082 |
from one place to another in the following code. |
| 2083 |
""" |
| 2084 |
def decorator(func): |
| 2085 |
@wraps(func) |
| 2086 |
def test(self): |
| 2087 |
self.run_test(func, jumpFrom, jumpTo, expected, |
| 2088 |
error=error, event=event, decorated=True, warning=warning) |
| 2089 |
return test |
| 2090 |
return decorator |
| 2091 |
|
| 2092 |
def async_jump_test(jumpFrom, jumpTo, expected, error=None, event='line', warning=None): |
| 2093 |
"""Decorator that creates a test that makes a jump |
| 2094 |
from one place to another in the following asynchronous code. |
| 2095 |
""" |
| 2096 |
def decorator(func): |
| 2097 |
@wraps(func) |
| 2098 |
def test(self): |
| 2099 |
self.run_async_test(func, jumpFrom, jumpTo, expected, |
| 2100 |
error=error, event=event, decorated=True, warning=warning) |
| 2101 |
return test |
| 2102 |
return decorator |
| 2103 |
|
| 2104 |
## The first set of 'jump' tests are for things that are allowed: |
| 2105 |
|
| 2106 |
@jump_test(1, 3, [3]) |
| 2107 |
def test_jump_simple_forwards(output): |
| 2108 |
output.append(1) |
| 2109 |
output.append(2) |
| 2110 |
output.append(3) |
| 2111 |
|
| 2112 |
@jump_test(2, 1, [1, 1, 2]) |
| 2113 |
def test_jump_simple_backwards(output): |
| 2114 |
output.append(1) |
| 2115 |
output.append(2) |
| 2116 |
|
| 2117 |
@jump_test(1, 4, [5], warning=(RuntimeWarning, unbound_locals)) |
| 2118 |
def test_jump_is_none_forwards(output): |
| 2119 |
x = None |
| 2120 |
if x is None: |
| 2121 |
output.append(3) |
| 2122 |
else: |
| 2123 |
output.append(5) |
| 2124 |
|
| 2125 |
@jump_test(6, 5, [3, 5, 6]) |
| 2126 |
def test_jump_is_none_backwards(output): |
| 2127 |
x = None |
| 2128 |
if x is None: |
| 2129 |
output.append(3) |
| 2130 |
else: |
| 2131 |
output.append(5) |
| 2132 |
output.append(6) |
| 2133 |
|
| 2134 |
@jump_test(2, 4, [5]) |
| 2135 |
def test_jump_is_not_none_forwards(output): |
| 2136 |
x = None |
| 2137 |
if x is not None: |
| 2138 |
output.append(3) |
| 2139 |
else: |
| 2140 |
output.append(5) |
| 2141 |
|
| 2142 |
@jump_test(6, 5, [5, 5, 6]) |
| 2143 |
def test_jump_is_not_none_backwards(output): |
| 2144 |
x = None |
| 2145 |
if x is not None: |
| 2146 |
output.append(3) |
| 2147 |
else: |
| 2148 |
output.append(5) |
| 2149 |
output.append(6) |
| 2150 |
|
| 2151 |
@jump_test(3, 5, [2, 5], warning=(RuntimeWarning, unbound_locals)) |
| 2152 |
def test_jump_out_of_block_forwards(output): |
| 2153 |
for i in 1, 2: |
| 2154 |
output.append(2) |
| 2155 |
for j in [3]: # Also tests jumping over a block |
| 2156 |
output.append(4) |
| 2157 |
output.append(5) |
| 2158 |
|
| 2159 |
@jump_test(6, 1, [1, 3, 5, 1, 3, 5, 6, 7]) |
| 2160 |
def test_jump_out_of_block_backwards(output): |
| 2161 |
output.append(1) |
| 2162 |
for i in [1]: |
| 2163 |
output.append(3) |
| 2164 |
for j in [2]: # Also tests jumping over a block |
| 2165 |
output.append(5) |
| 2166 |
output.append(6) |
| 2167 |
output.append(7) |
| 2168 |
|
| 2169 |
@async_jump_test(4, 5, [3, 5]) |
| 2170 |
@clean_asynciter |
| 2171 |
async def test_jump_out_of_async_for_block_forwards(output, asynciter): |
| 2172 |
for i in [1]: |
| 2173 |
async for i in asynciter([1, 2]): |
| 2174 |
output.append(3) |
| 2175 |
output.append(4) |
| 2176 |
output.append(5) |
| 2177 |
|
| 2178 |
@async_jump_test(5, 2, [2, 4, 2, 4, 5, 6]) |
| 2179 |
@clean_asynciter |
| 2180 |
async def test_jump_out_of_async_for_block_backwards(output, asynciter): |
| 2181 |
for i in [1]: |
| 2182 |
output.append(2) |
| 2183 |
async for i in asynciter([1]): |
| 2184 |
output.append(4) |
| 2185 |
output.append(5) |
| 2186 |
output.append(6) |
| 2187 |
|
| 2188 |
@jump_test(1, 2, [3]) |
| 2189 |
def test_jump_to_codeless_line(output): |
| 2190 |
output.append(1) |
| 2191 |
# Jumping to this line should skip to the next one. |
| 2192 |
output.append(3) |
| 2193 |
|
| 2194 |
@jump_test(2, 2, [1, 2, 3]) |
| 2195 |
def test_jump_to_same_line(output): |
| 2196 |
output.append(1) |
| 2197 |
output.append(2) |
| 2198 |
output.append(3) |
| 2199 |
|
| 2200 |
# Tests jumping within a finally block, and over one. |
| 2201 |
@jump_test(4, 9, [2, 9]) |
| 2202 |
def test_jump_in_nested_finally(output): |
| 2203 |
try: |
| 2204 |
output.append(2) |
| 2205 |
finally: |
| 2206 |
output.append(4) |
| 2207 |
try: |
| 2208 |
output.append(6) |
| 2209 |
finally: |
| 2210 |
output.append(8) |
| 2211 |
output.append(9) |
| 2212 |
|
| 2213 |
@jump_test(6, 7, [2, 7], (ZeroDivisionError, '')) |
| 2214 |
def test_jump_in_nested_finally_2(output): |
| 2215 |
try: |
| 2216 |
output.append(2) |
| 2217 |
1/0 |
| 2218 |
return |
| 2219 |
finally: |
| 2220 |
output.append(6) |
| 2221 |
output.append(7) |
| 2222 |
output.append(8) |
| 2223 |
|
| 2224 |
@jump_test(6, 11, [2, 11], (ZeroDivisionError, '')) |
| 2225 |
def test_jump_in_nested_finally_3(output): |
| 2226 |
try: |
| 2227 |
output.append(2) |
| 2228 |
1/0 |
| 2229 |
return |
| 2230 |
finally: |
| 2231 |
output.append(6) |
| 2232 |
try: |
| 2233 |
output.append(8) |
| 2234 |
finally: |
| 2235 |
output.append(10) |
| 2236 |
output.append(11) |
| 2237 |
output.append(12) |
| 2238 |
|
| 2239 |
@jump_test(3, 4, [1], (ValueError, 'after')) |
| 2240 |
def test_no_jump_infinite_while_loop(output): |
| 2241 |
output.append(1) |
| 2242 |
while True: |
| 2243 |
output.append(3) |
| 2244 |
output.append(4) |
| 2245 |
|
| 2246 |
@jump_test(2, 4, [4, 4]) |
| 2247 |
def test_jump_forwards_into_while_block(output): |
| 2248 |
i = 1 |
| 2249 |
output.append(2) |
| 2250 |
while i <= 2: |
| 2251 |
output.append(4) |
| 2252 |
i += 1 |
| 2253 |
|
| 2254 |
@jump_test(5, 3, [3, 3, 3, 5]) |
| 2255 |
def test_jump_backwards_into_while_block(output): |
| 2256 |
i = 1 |
| 2257 |
while i <= 2: |
| 2258 |
output.append(3) |
| 2259 |
i += 1 |
| 2260 |
output.append(5) |
| 2261 |
|
| 2262 |
@jump_test(2, 3, [1, 3]) |
| 2263 |
def test_jump_forwards_out_of_with_block(output): |
| 2264 |
with tracecontext(output, 1): |
| 2265 |
output.append(2) |
| 2266 |
output.append(3) |
| 2267 |
|
| 2268 |
@async_jump_test(2, 3, [1, 3]) |
| 2269 |
async def test_jump_forwards_out_of_async_with_block(output): |
| 2270 |
async with asynctracecontext(output, 1): |
| 2271 |
output.append(2) |
| 2272 |
output.append(3) |
| 2273 |
|
| 2274 |
@jump_test(3, 1, [1, 2, 1, 2, 3, -2]) |
| 2275 |
def test_jump_backwards_out_of_with_block(output): |
| 2276 |
output.append(1) |
| 2277 |
with tracecontext(output, 2): |
| 2278 |
output.append(3) |
| 2279 |
|
| 2280 |
@async_jump_test(3, 1, [1, 2, 1, 2, 3, -2]) |
| 2281 |
async def test_jump_backwards_out_of_async_with_block(output): |
| 2282 |
output.append(1) |
| 2283 |
async with asynctracecontext(output, 2): |
| 2284 |
output.append(3) |
| 2285 |
|
| 2286 |
@jump_test(2, 5, [5]) |
| 2287 |
def test_jump_forwards_out_of_try_finally_block(output): |
| 2288 |
try: |
| 2289 |
output.append(2) |
| 2290 |
finally: |
| 2291 |
output.append(4) |
| 2292 |
output.append(5) |
| 2293 |
|
| 2294 |
@jump_test(3, 1, [1, 1, 3, 5]) |
| 2295 |
def test_jump_backwards_out_of_try_finally_block(output): |
| 2296 |
output.append(1) |
| 2297 |
try: |
| 2298 |
output.append(3) |
| 2299 |
finally: |
| 2300 |
output.append(5) |
| 2301 |
|
| 2302 |
@jump_test(2, 6, [6]) |
| 2303 |
def test_jump_forwards_out_of_try_except_block(output): |
| 2304 |
try: |
| 2305 |
output.append(2) |
| 2306 |
except: |
| 2307 |
output.append(4) |
| 2308 |
raise |
| 2309 |
output.append(6) |
| 2310 |
|
| 2311 |
@jump_test(3, 1, [1, 1, 3]) |
| 2312 |
def test_jump_backwards_out_of_try_except_block(output): |
| 2313 |
output.append(1) |
| 2314 |
try: |
| 2315 |
output.append(3) |
| 2316 |
except: |
| 2317 |
output.append(5) |
| 2318 |
raise |
| 2319 |
|
| 2320 |
@jump_test(5, 7, [4, 7, 8]) |
| 2321 |
def test_jump_between_except_blocks(output): |
| 2322 |
try: |
| 2323 |
1/0 |
| 2324 |
except ZeroDivisionError: |
| 2325 |
output.append(4) |
| 2326 |
output.append(5) |
| 2327 |
except FloatingPointError: |
| 2328 |
output.append(7) |
| 2329 |
output.append(8) |
| 2330 |
|
| 2331 |
@jump_test(5, 7, [4, 7, 8]) |
| 2332 |
def test_jump_from_except_to_finally(output): |
| 2333 |
try: |
| 2334 |
1/0 |
| 2335 |
except ZeroDivisionError: |
| 2336 |
output.append(4) |
| 2337 |
output.append(5) |
| 2338 |
finally: |
| 2339 |
output.append(7) |
| 2340 |
output.append(8) |
| 2341 |
|
| 2342 |
@jump_test(5, 6, [4, 6, 7]) |
| 2343 |
def test_jump_within_except_block(output): |
| 2344 |
try: |
| 2345 |
1/0 |
| 2346 |
except: |
| 2347 |
output.append(4) |
| 2348 |
output.append(5) |
| 2349 |
output.append(6) |
| 2350 |
output.append(7) |
| 2351 |
|
| 2352 |
@jump_test(6, 1, [1, 5, 1, 5], warning=(RuntimeWarning, unbound_locals)) |
| 2353 |
def test_jump_over_try_except(output): |
| 2354 |
output.append(1) |
| 2355 |
try: |
| 2356 |
1 / 0 |
| 2357 |
except ZeroDivisionError as e: |
| 2358 |
output.append(5) |
| 2359 |
x = 42 # has to be a two-instruction block |
| 2360 |
|
| 2361 |
@jump_test(2, 4, [1, 4, 5, -4]) |
| 2362 |
def test_jump_across_with(output): |
| 2363 |
output.append(1) |
| 2364 |
with tracecontext(output, 2): |
| 2365 |
output.append(3) |
| 2366 |
with tracecontext(output, 4): |
| 2367 |
output.append(5) |
| 2368 |
|
| 2369 |
@async_jump_test(2, 4, [1, 4, 5, -4]) |
| 2370 |
async def test_jump_across_async_with(output): |
| 2371 |
output.append(1) |
| 2372 |
async with asynctracecontext(output, 2): |
| 2373 |
output.append(3) |
| 2374 |
async with asynctracecontext(output, 4): |
| 2375 |
output.append(5) |
| 2376 |
|
| 2377 |
@jump_test(4, 5, [1, 3, 5, 6]) |
| 2378 |
def test_jump_out_of_with_block_within_for_block(output): |
| 2379 |
output.append(1) |
| 2380 |
for i in [1]: |
| 2381 |
with tracecontext(output, 3): |
| 2382 |
output.append(4) |
| 2383 |
output.append(5) |
| 2384 |
output.append(6) |
| 2385 |
|
| 2386 |
@async_jump_test(4, 5, [1, 3, 5, 6]) |
| 2387 |
async def test_jump_out_of_async_with_block_within_for_block(output): |
| 2388 |
output.append(1) |
| 2389 |
for i in [1]: |
| 2390 |
async with asynctracecontext(output, 3): |
| 2391 |
output.append(4) |
| 2392 |
output.append(5) |
| 2393 |
output.append(6) |
| 2394 |
|
| 2395 |
@jump_test(4, 5, [1, 2, 3, 5, -2, 6]) |
| 2396 |
def test_jump_out_of_with_block_within_with_block(output): |
| 2397 |
output.append(1) |
| 2398 |
with tracecontext(output, 2): |
| 2399 |
with tracecontext(output, 3): |
| 2400 |
output.append(4) |
| 2401 |
output.append(5) |
| 2402 |
output.append(6) |
| 2403 |
|
| 2404 |
@async_jump_test(4, 5, [1, 2, 3, 5, -2, 6]) |
| 2405 |
async def test_jump_out_of_async_with_block_within_with_block(output): |
| 2406 |
output.append(1) |
| 2407 |
with tracecontext(output, 2): |
| 2408 |
async with asynctracecontext(output, 3): |
| 2409 |
output.append(4) |
| 2410 |
output.append(5) |
| 2411 |
output.append(6) |
| 2412 |
|
| 2413 |
@jump_test(5, 6, [2, 4, 6, 7]) |
| 2414 |
def test_jump_out_of_with_block_within_finally_block(output): |
| 2415 |
try: |
| 2416 |
output.append(2) |
| 2417 |
finally: |
| 2418 |
with tracecontext(output, 4): |
| 2419 |
output.append(5) |
| 2420 |
output.append(6) |
| 2421 |
output.append(7) |
| 2422 |
|
| 2423 |
@async_jump_test(5, 6, [2, 4, 6, 7]) |
| 2424 |
async def test_jump_out_of_async_with_block_within_finally_block(output): |
| 2425 |
try: |
| 2426 |
output.append(2) |
| 2427 |
finally: |
| 2428 |
async with asynctracecontext(output, 4): |
| 2429 |
output.append(5) |
| 2430 |
output.append(6) |
| 2431 |
output.append(7) |
| 2432 |
|
| 2433 |
@jump_test(8, 11, [1, 3, 5, 11, 12]) |
| 2434 |
def test_jump_out_of_complex_nested_blocks(output): |
| 2435 |
output.append(1) |
| 2436 |
for i in [1]: |
| 2437 |
output.append(3) |
| 2438 |
for j in [1, 2]: |
| 2439 |
output.append(5) |
| 2440 |
try: |
| 2441 |
for k in [1, 2]: |
| 2442 |
output.append(8) |
| 2443 |
finally: |
| 2444 |
output.append(10) |
| 2445 |
output.append(11) |
| 2446 |
output.append(12) |
| 2447 |
|
| 2448 |
@jump_test(3, 5, [1, 2, 5], warning=(RuntimeWarning, unbound_locals)) |
| 2449 |
def test_jump_out_of_with_assignment(output): |
| 2450 |
output.append(1) |
| 2451 |
with tracecontext(output, 2) \ |
| 2452 |
as x: |
| 2453 |
output.append(4) |
| 2454 |
output.append(5) |
| 2455 |
|
| 2456 |
@async_jump_test(3, 5, [1, 2, 5], warning=(RuntimeWarning, unbound_locals)) |
| 2457 |
async def test_jump_out_of_async_with_assignment(output): |
| 2458 |
output.append(1) |
| 2459 |
async with asynctracecontext(output, 2) \ |
| 2460 |
as x: |
| 2461 |
output.append(4) |
| 2462 |
output.append(5) |
| 2463 |
|
| 2464 |
@jump_test(3, 6, [1, 6, 8, 9]) |
| 2465 |
def test_jump_over_return_in_try_finally_block(output): |
| 2466 |
output.append(1) |
| 2467 |
try: |
| 2468 |
output.append(3) |
| 2469 |
if not output: # always false |
| 2470 |
return |
| 2471 |
output.append(6) |
| 2472 |
finally: |
| 2473 |
output.append(8) |
| 2474 |
output.append(9) |
| 2475 |
|
| 2476 |
@jump_test(5, 8, [1, 3, 8, 10, 11, 13]) |
| 2477 |
def test_jump_over_break_in_try_finally_block(output): |
| 2478 |
output.append(1) |
| 2479 |
while True: |
| 2480 |
output.append(3) |
| 2481 |
try: |
| 2482 |
output.append(5) |
| 2483 |
if not output: # always false |
| 2484 |
break |
| 2485 |
output.append(8) |
| 2486 |
finally: |
| 2487 |
output.append(10) |
| 2488 |
output.append(11) |
| 2489 |
break |
| 2490 |
output.append(13) |
| 2491 |
|
| 2492 |
@jump_test(1, 7, [7, 8], warning=(RuntimeWarning, unbound_locals)) |
| 2493 |
def test_jump_over_for_block_before_else(output): |
| 2494 |
output.append(1) |
| 2495 |
if not output: # always false |
| 2496 |
for i in [3]: |
| 2497 |
output.append(4) |
| 2498 |
else: |
| 2499 |
output.append(6) |
| 2500 |
output.append(7) |
| 2501 |
output.append(8) |
| 2502 |
|
| 2503 |
@async_jump_test(1, 7, [7, 8], warning=(RuntimeWarning, unbound_locals)) |
| 2504 |
async def test_jump_over_async_for_block_before_else(output): |
| 2505 |
output.append(1) |
| 2506 |
if not output: # always false |
| 2507 |
async for i in asynciter([3]): |
| 2508 |
output.append(4) |
| 2509 |
else: |
| 2510 |
output.append(6) |
| 2511 |
output.append(7) |
| 2512 |
output.append(8) |
| 2513 |
|
| 2514 |
# The second set of 'jump' tests are for things that are not allowed: |
| 2515 |
|
| 2516 |
@jump_test(2, 3, [1], (ValueError, 'after')) |
| 2517 |
def test_no_jump_too_far_forwards(output): |
| 2518 |
output.append(1) |
| 2519 |
output.append(2) |
| 2520 |
|
| 2521 |
@jump_test(2, -2, [1], (ValueError, 'before')) |
| 2522 |
def test_no_jump_too_far_backwards(output): |
| 2523 |
output.append(1) |
| 2524 |
output.append(2) |
| 2525 |
|
| 2526 |
# Test each kind of 'except' line. |
| 2527 |
@jump_test(2, 3, [4], (ValueError, 'except')) |
| 2528 |
def test_no_jump_to_except_1(output): |
| 2529 |
try: |
| 2530 |
output.append(2) |
| 2531 |
except: |
| 2532 |
output.append(4) |
| 2533 |
raise |
| 2534 |
|
| 2535 |
@jump_test(2, 3, [4], (ValueError, 'except')) |
| 2536 |
def test_no_jump_to_except_2(output): |
| 2537 |
try: |
| 2538 |
output.append(2) |
| 2539 |
except ValueError: |
| 2540 |
output.append(4) |
| 2541 |
raise |
| 2542 |
|
| 2543 |
@jump_test(2, 3, [4], (ValueError, 'except')) |
| 2544 |
def test_no_jump_to_except_3(output): |
| 2545 |
try: |
| 2546 |
output.append(2) |
| 2547 |
except ValueError as e: |
| 2548 |
output.append(4) |
| 2549 |
raise e |
| 2550 |
|
| 2551 |
@jump_test(2, 3, [4], (ValueError, 'except')) |
| 2552 |
def test_no_jump_to_except_4(output): |
| 2553 |
try: |
| 2554 |
output.append(2) |
| 2555 |
except (ValueError, RuntimeError) as e: |
| 2556 |
output.append(4) |
| 2557 |
raise e |
| 2558 |
|
| 2559 |
@jump_test(1, 3, [], (ValueError, 'into')) |
| 2560 |
def test_no_jump_forwards_into_for_block(output): |
| 2561 |
output.append(1) |
| 2562 |
for i in 1, 2: |
| 2563 |
output.append(3) |
| 2564 |
|
| 2565 |
@async_jump_test(1, 3, [], (ValueError, 'into')) |
| 2566 |
async def test_no_jump_forwards_into_async_for_block(output): |
| 2567 |
output.append(1) |
| 2568 |
async for i in asynciter([1, 2]): |
| 2569 |
output.append(3) |
| 2570 |
pass |
| 2571 |
|
| 2572 |
@jump_test(3, 2, [2, 2], (ValueError, 'into')) |
| 2573 |
def test_no_jump_backwards_into_for_block(output): |
| 2574 |
for i in 1, 2: |
| 2575 |
output.append(2) |
| 2576 |
output.append(3) |
| 2577 |
|
| 2578 |
|
| 2579 |
@async_jump_test(3, 2, [2, 2], (ValueError, "can't jump into the body of a for loop")) |
| 2580 |
async def test_no_jump_backwards_into_async_for_block(output): |
| 2581 |
async for i in asynciter([1, 2]): |
| 2582 |
output.append(2) |
| 2583 |
output.append(3) |
| 2584 |
|
| 2585 |
@jump_test(1, 3, [], (ValueError, 'stack')) |
| 2586 |
def test_no_jump_forwards_into_with_block(output): |
| 2587 |
output.append(1) |
| 2588 |
with tracecontext(output, 2): |
| 2589 |
output.append(3) |
| 2590 |
|
| 2591 |
@async_jump_test(1, 3, [], (ValueError, 'stack')) |
| 2592 |
async def test_no_jump_forwards_into_async_with_block(output): |
| 2593 |
output.append(1) |
| 2594 |
async with asynctracecontext(output, 2): |
| 2595 |
output.append(3) |
| 2596 |
|
| 2597 |
@jump_test(3, 2, [1, 2, -1], (ValueError, 'stack')) |
| 2598 |
def test_no_jump_backwards_into_with_block(output): |
| 2599 |
with tracecontext(output, 1): |
| 2600 |
output.append(2) |
| 2601 |
output.append(3) |
| 2602 |
|
| 2603 |
@async_jump_test(3, 2, [1, 2, -1], (ValueError, 'stack')) |
| 2604 |
async def test_no_jump_backwards_into_async_with_block(output): |
| 2605 |
async with asynctracecontext(output, 1): |
| 2606 |
output.append(2) |
| 2607 |
output.append(3) |
| 2608 |
|
| 2609 |
@jump_test(1, 3, [3, 5]) |
| 2610 |
def test_jump_forwards_into_try_finally_block(output): |
| 2611 |
output.append(1) |
| 2612 |
try: |
| 2613 |
output.append(3) |
| 2614 |
finally: |
| 2615 |
output.append(5) |
| 2616 |
|
| 2617 |
@jump_test(5, 2, [2, 4, 2, 4, 5]) |
| 2618 |
def test_jump_backwards_into_try_finally_block(output): |
| 2619 |
try: |
| 2620 |
output.append(2) |
| 2621 |
finally: |
| 2622 |
output.append(4) |
| 2623 |
output.append(5) |
| 2624 |
|
| 2625 |
@jump_test(1, 3, [3]) |
| 2626 |
def test_jump_forwards_into_try_except_block(output): |
| 2627 |
output.append(1) |
| 2628 |
try: |
| 2629 |
output.append(3) |
| 2630 |
except: |
| 2631 |
output.append(5) |
| 2632 |
raise |
| 2633 |
|
| 2634 |
@jump_test(6, 2, [2, 2, 6]) |
| 2635 |
def test_jump_backwards_into_try_except_block(output): |
| 2636 |
try: |
| 2637 |
output.append(2) |
| 2638 |
except: |
| 2639 |
output.append(4) |
| 2640 |
raise |
| 2641 |
output.append(6) |
| 2642 |
|
| 2643 |
# 'except' with a variable creates an implicit finally block |
| 2644 |
@jump_test(5, 7, [4, 7, 8], warning=(RuntimeWarning, unbound_locals)) |
| 2645 |
def test_jump_between_except_blocks_2(output): |
| 2646 |
try: |
| 2647 |
1/0 |
| 2648 |
except ZeroDivisionError: |
| 2649 |
output.append(4) |
| 2650 |
output.append(5) |
| 2651 |
except FloatingPointError as e: |
| 2652 |
output.append(7) |
| 2653 |
output.append(8) |
| 2654 |
|
| 2655 |
@jump_test(1, 5, [5]) |
| 2656 |
def test_jump_into_finally_block(output): |
| 2657 |
output.append(1) |
| 2658 |
try: |
| 2659 |
output.append(3) |
| 2660 |
finally: |
| 2661 |
output.append(5) |
| 2662 |
|
| 2663 |
@jump_test(3, 6, [2, 6, 7]) |
| 2664 |
def test_jump_into_finally_block_from_try_block(output): |
| 2665 |
try: |
| 2666 |
output.append(2) |
| 2667 |
output.append(3) |
| 2668 |
finally: # still executed if the jump is failed |
| 2669 |
output.append(5) |
| 2670 |
output.append(6) |
| 2671 |
output.append(7) |
| 2672 |
|
| 2673 |
@jump_test(5, 1, [1, 3, 1, 3, 5]) |
| 2674 |
def test_jump_out_of_finally_block(output): |
| 2675 |
output.append(1) |
| 2676 |
try: |
| 2677 |
output.append(3) |
| 2678 |
finally: |
| 2679 |
output.append(5) |
| 2680 |
|
| 2681 |
@jump_test(1, 5, [], (ValueError, "can't jump into an 'except' block as there's no exception")) |
| 2682 |
def test_no_jump_into_bare_except_block(output): |
| 2683 |
output.append(1) |
| 2684 |
try: |
| 2685 |
output.append(3) |
| 2686 |
except: |
| 2687 |
output.append(5) |
| 2688 |
|
| 2689 |
@jump_test(1, 5, [], (ValueError, "can't jump into an 'except' block as there's no exception")) |
| 2690 |
def test_no_jump_into_qualified_except_block(output): |
| 2691 |
output.append(1) |
| 2692 |
try: |
| 2693 |
output.append(3) |
| 2694 |
except Exception: |
| 2695 |
output.append(5) |
| 2696 |
|
| 2697 |
@jump_test(3, 6, [2, 5, 6], (ValueError, "can't jump into an 'except' block as there's no exception")) |
| 2698 |
def test_no_jump_into_bare_except_block_from_try_block(output): |
| 2699 |
try: |
| 2700 |
output.append(2) |
| 2701 |
output.append(3) |
| 2702 |
except: # executed if the jump is failed |
| 2703 |
output.append(5) |
| 2704 |
output.append(6) |
| 2705 |
raise |
| 2706 |
output.append(8) |
| 2707 |
|
| 2708 |
@jump_test(3, 6, [2], (ValueError, "can't jump into an 'except' block as there's no exception")) |
| 2709 |
def test_no_jump_into_qualified_except_block_from_try_block(output): |
| 2710 |
try: |
| 2711 |
output.append(2) |
| 2712 |
output.append(3) |
| 2713 |
except ZeroDivisionError: |
| 2714 |
output.append(5) |
| 2715 |
output.append(6) |
| 2716 |
raise |
| 2717 |
output.append(8) |
| 2718 |
|
| 2719 |
@jump_test(7, 1, [1, 3, 6, 1, 3, 6, 7]) |
| 2720 |
def test_jump_out_of_bare_except_block(output): |
| 2721 |
output.append(1) |
| 2722 |
try: |
| 2723 |
output.append(3) |
| 2724 |
1/0 |
| 2725 |
except: |
| 2726 |
output.append(6) |
| 2727 |
output.append(7) |
| 2728 |
|
| 2729 |
@jump_test(7, 1, [1, 3, 6, 1, 3, 6, 7]) |
| 2730 |
def test_jump_out_of_qualified_except_block(output): |
| 2731 |
output.append(1) |
| 2732 |
try: |
| 2733 |
output.append(3) |
| 2734 |
1/0 |
| 2735 |
except Exception: |
| 2736 |
output.append(6) |
| 2737 |
output.append(7) |
| 2738 |
|
| 2739 |
@jump_test(3, 5, [1, 2, 5, -2]) |
| 2740 |
def test_jump_between_with_blocks(output): |
| 2741 |
output.append(1) |
| 2742 |
with tracecontext(output, 2): |
| 2743 |
output.append(3) |
| 2744 |
with tracecontext(output, 4): |
| 2745 |
output.append(5) |
| 2746 |
|
| 2747 |
@async_jump_test(3, 5, [1, 2, 5, -2]) |
| 2748 |
async def test_jump_between_async_with_blocks(output): |
| 2749 |
output.append(1) |
| 2750 |
async with asynctracecontext(output, 2): |
| 2751 |
output.append(3) |
| 2752 |
async with asynctracecontext(output, 4): |
| 2753 |
output.append(5) |
| 2754 |
|
| 2755 |
@jump_test(5, 7, [2, 4], (ValueError, "after")) |
| 2756 |
def test_no_jump_over_return_out_of_finally_block(output): |
| 2757 |
try: |
| 2758 |
output.append(2) |
| 2759 |
finally: |
| 2760 |
output.append(4) |
| 2761 |
output.append(5) |
| 2762 |
return |
| 2763 |
output.append(7) |
| 2764 |
|
| 2765 |
@jump_test(7, 4, [1, 6], (ValueError, 'into')) |
| 2766 |
def test_no_jump_into_for_block_before_else(output): |
| 2767 |
output.append(1) |
| 2768 |
if not output: # always false |
| 2769 |
for i in [3]: |
| 2770 |
output.append(4) |
| 2771 |
else: |
| 2772 |
output.append(6) |
| 2773 |
output.append(7) |
| 2774 |
output.append(8) |
| 2775 |
|
| 2776 |
@async_jump_test(7, 4, [1, 6], (ValueError, 'into')) |
| 2777 |
async def test_no_jump_into_async_for_block_before_else(output): |
| 2778 |
output.append(1) |
| 2779 |
if not output: # always false |
| 2780 |
async for i in asynciter([3]): |
| 2781 |
output.append(4) |
| 2782 |
else: |
| 2783 |
output.append(6) |
| 2784 |
output.append(7) |
| 2785 |
output.append(8) |
| 2786 |
|
| 2787 |
def test_no_jump_to_non_integers(self): |
| 2788 |
self.run_test(no_jump_to_non_integers, 2, "Spam", [True]) |
| 2789 |
|
| 2790 |
def test_no_jump_without_trace_function(self): |
| 2791 |
# Must set sys.settrace(None) in setUp(), else condition is not |
| 2792 |
# triggered. |
| 2793 |
no_jump_without_trace_function() |
| 2794 |
|
| 2795 |
def test_large_function(self): |
| 2796 |
d = {} |
| 2797 |
exec("""def f(output): # line 0 |
| 2798 |
x = 0 # line 1 |
| 2799 |
y = 1 # line 2 |
| 2800 |
''' # line 3 |
| 2801 |
%s # lines 4-1004 |
| 2802 |
''' # line 1005 |
| 2803 |
x += 1 # line 1006 |
| 2804 |
output.append(x) # line 1007 |
| 2805 |
return""" % ('\n' * 1000,), d) |
| 2806 |
f = d['f'] |
| 2807 |
self.run_test(f, 2, 1007, [0], warning=(RuntimeWarning, self.unbound_locals)) |
| 2808 |
|
| 2809 |
def test_jump_to_firstlineno(self): |
| 2810 |
# This tests that PDB can jump back to the first line in a |
| 2811 |
# file. See issue #1689458. It can only be triggered in a |
| 2812 |
# function call if the function is defined on a single line. |
| 2813 |
code = compile(""" |
| 2814 |
# Comments don't count. |
| 2815 |
output.append(2) # firstlineno is here. |
| 2816 |
output.append(3) |
| 2817 |
output.append(4) |
| 2818 |
""", "<fake module>", "exec") |
| 2819 |
class fake_function: |
| 2820 |
__code__ = code |
| 2821 |
tracer = JumpTracer(fake_function, 4, 1) |
| 2822 |
sys.settrace(tracer.trace) |
| 2823 |
namespace = {"output": []} |
| 2824 |
exec(code, namespace) |
| 2825 |
sys.settrace(None) |
| 2826 |
self.compare_jump_output([2, 3, 2, 3, 4], namespace["output"]) |
| 2827 |
|
| 2828 |
@jump_test(2, 3, [1], event='call', error=(ValueError, "can't jump from" |
| 2829 |
" the 'call' trace event of a new frame")) |
| 2830 |
def test_no_jump_from_call(output): |
| 2831 |
output.append(1) |
| 2832 |
def nested(): |
| 2833 |
output.append(3) |
| 2834 |
nested() |
| 2835 |
output.append(5) |
| 2836 |
|
| 2837 |
@jump_test(2, 1, [1], event='return', error=(ValueError, |
| 2838 |
"can only jump from a 'line' trace event")) |
| 2839 |
def test_no_jump_from_return_event(output): |
| 2840 |
output.append(1) |
| 2841 |
return |
| 2842 |
|
| 2843 |
@jump_test(2, 1, [1], event='exception', error=(ValueError, |
| 2844 |
"can only jump from a 'line' trace event")) |
| 2845 |
def test_no_jump_from_exception_event(output): |
| 2846 |
output.append(1) |
| 2847 |
1 / 0 |
| 2848 |
|
| 2849 |
@jump_test(3, 2, [2, 2, 5], event='return') |
| 2850 |
def test_jump_from_yield(output): |
| 2851 |
def gen(): |
| 2852 |
output.append(2) |
| 2853 |
yield 3 |
| 2854 |
next(gen()) |
| 2855 |
output.append(5) |
| 2856 |
|
| 2857 |
@jump_test(2, 3, [1, 3], warning=(RuntimeWarning, unbound_locals)) |
| 2858 |
def test_jump_forward_over_listcomp(output): |
| 2859 |
output.append(1) |
| 2860 |
x = [i for i in range(10)] |
| 2861 |
output.append(3) |
| 2862 |
|
| 2863 |
# checking for segfaults. |
| 2864 |
# See https://github.com/python/cpython/issues/92311 |
| 2865 |
@jump_test(3, 1, [], warning=(RuntimeWarning, unbound_locals)) |
| 2866 |
def test_jump_backward_over_listcomp(output): |
| 2867 |
a = 1 |
| 2868 |
x = [i for i in range(10)] |
| 2869 |
c = 3 |
| 2870 |
|
| 2871 |
@jump_test(8, 2, [2, 7, 2], warning=(RuntimeWarning, unbound_locals)) |
| 2872 |
def test_jump_backward_over_listcomp_v2(output): |
| 2873 |
flag = False |
| 2874 |
output.append(2) |
| 2875 |
if flag: |
| 2876 |
return |
| 2877 |
x = [i for i in range(5)] |
| 2878 |
flag = 6 |
| 2879 |
output.append(7) |
| 2880 |
output.append(8) |
| 2881 |
|
| 2882 |
@async_jump_test(2, 3, [1, 3], warning=(RuntimeWarning, unbound_locals)) |
| 2883 |
async def test_jump_forward_over_async_listcomp(output): |
| 2884 |
output.append(1) |
| 2885 |
x = [i async for i in asynciter(range(10))] |
| 2886 |
output.append(3) |
| 2887 |
|
| 2888 |
@async_jump_test(3, 1, [], warning=(RuntimeWarning, unbound_locals)) |
| 2889 |
async def test_jump_backward_over_async_listcomp(output): |
| 2890 |
a = 1 |
| 2891 |
x = [i async for i in asynciter(range(10))] |
| 2892 |
c = 3 |
| 2893 |
|
| 2894 |
@async_jump_test(8, 2, [2, 7, 2], warning=(RuntimeWarning, unbound_locals)) |
| 2895 |
async def test_jump_backward_over_async_listcomp_v2(output): |
| 2896 |
flag = False |
| 2897 |
output.append(2) |
| 2898 |
if flag: |
| 2899 |
return |
| 2900 |
x = [i async for i in asynciter(range(5))] |
| 2901 |
flag = 6 |
| 2902 |
output.append(7) |
| 2903 |
output.append(8) |
| 2904 |
|
| 2905 |
# checking for segfaults. |
| 2906 |
@jump_test(3, 7, [], error=(ValueError, "stack")) |
| 2907 |
def test_jump_with_null_on_stack_load_global(output): |
| 2908 |
a = 1 |
| 2909 |
print( |
| 2910 |
output.append(3) |
| 2911 |
) |
| 2912 |
output.append(5) |
| 2913 |
( |
| 2914 |
( # 7 |
| 2915 |
a |
| 2916 |
+ |
| 2917 |
10 |
| 2918 |
) |
| 2919 |
+ |
| 2920 |
13 |
| 2921 |
) |
| 2922 |
output.append(15) |
| 2923 |
|
| 2924 |
# checking for segfaults. |
| 2925 |
@jump_test(4, 8, [], error=(ValueError, "stack")) |
| 2926 |
def test_jump_with_null_on_stack_push_null(output): |
| 2927 |
a = 1 |
| 2928 |
f = print |
| 2929 |
f( |
| 2930 |
output.append(4) |
| 2931 |
) |
| 2932 |
output.append(6) |
| 2933 |
( |
| 2934 |
( # 8 |
| 2935 |
a |
| 2936 |
+ |
| 2937 |
11 |
| 2938 |
) |
| 2939 |
+ |
| 2940 |
14 |
| 2941 |
) |
| 2942 |
output.append(16) |
| 2943 |
|
| 2944 |
# checking for segfaults. |
| 2945 |
@jump_test(3, 7, [], error=(ValueError, "stack")) |
| 2946 |
def test_jump_with_null_on_stack_load_attr(output): |
| 2947 |
a = 1 |
| 2948 |
list.append( |
| 2949 |
output, 3 |
| 2950 |
) |
| 2951 |
output.append(5) |
| 2952 |
( |
| 2953 |
( # 7 |
| 2954 |
a |
| 2955 |
+ |
| 2956 |
10 |
| 2957 |
) |
| 2958 |
+ |
| 2959 |
13 |
| 2960 |
) |
| 2961 |
output.append(15) |
| 2962 |
|
| 2963 |
@jump_test(2, 3, [1, 3], warning=(RuntimeWarning, unbound_locals)) |
| 2964 |
def test_jump_extended_args_unpack_ex_simple(output): |
| 2965 |
output.append(1) |
| 2966 |
_, *_, _ = output.append(2) or "Spam" |
| 2967 |
output.append(3) |
| 2968 |
|
| 2969 |
@jump_test(3, 4, [1, 4, 4, 5], warning=(RuntimeWarning, unbound_locals)) |
| 2970 |
def test_jump_extended_args_unpack_ex_tricky(output): |
| 2971 |
output.append(1) |
| 2972 |
( |
| 2973 |
_, *_, _ |
| 2974 |
) = output.append(4) or "Spam" |
| 2975 |
output.append(5) |
| 2976 |
|
| 2977 |
@support.requires_resource('cpu') |
| 2978 |
def test_jump_extended_args_for_iter(self): |
| 2979 |
# In addition to failing when extended arg handling is broken, this can |
| 2980 |
# also hang for a *very* long time: |
| 2981 |
source = [ |
| 2982 |
"def f(output):", |
| 2983 |
" output.append(1)", |
| 2984 |
" for _ in spam:", |
| 2985 |
*(f" output.append({i})" for i in range(3, 100_000)), |
| 2986 |
f" output.append(100_000)", |
| 2987 |
] |
| 2988 |
namespace = {} |
| 2989 |
exec("\n".join(source), namespace) |
| 2990 |
f = namespace["f"] |
| 2991 |
self.run_test(f, 2, 100_000, [1, 100_000], warning=(RuntimeWarning, self.unbound_locals)) |
| 2992 |
|
| 2993 |
@jump_test(2, 3, [1, 3], warning=(RuntimeWarning, unbound_locals)) |
| 2994 |
def test_jump_or_pop(output): |
| 2995 |
output.append(1) |
| 2996 |
_ = output.append(2) and "Spam" |
| 2997 |
output.append(3) |
| 2998 |
|
| 2999 |
|
| 3000 |
class TestExtendedArgs(unittest.TestCase): |
| 3001 |
|
| 3002 |
def setUp(self): |
| 3003 |
self.addCleanup(sys.settrace, sys.gettrace()) |
| 3004 |
sys.settrace(None) |
| 3005 |
|
| 3006 |
def count_traces(self, func): |
| 3007 |
# warmup |
| 3008 |
for _ in range(20): |
| 3009 |
func() |
| 3010 |
|
| 3011 |
counts = {"call": 0, "line": 0, "return": 0} |
| 3012 |
def trace(frame, event, arg): |
| 3013 |
counts[event] += 1 |
| 3014 |
return trace |
| 3015 |
|
| 3016 |
sys.settrace(trace) |
| 3017 |
func() |
| 3018 |
sys.settrace(None) |
| 3019 |
|
| 3020 |
return counts |
| 3021 |
|
| 3022 |
def test_trace_unpack_long_sequence(self): |
| 3023 |
ns = {} |
| 3024 |
code = "def f():\n (" + "y,\n "*300 + ") = range(300)" |
| 3025 |
exec(code, ns) |
| 3026 |
counts = self.count_traces(ns["f"]) |
| 3027 |
self.assertEqual(counts, {'call': 1, 'line': 301, 'return': 1}) |
| 3028 |
|
| 3029 |
def test_trace_lots_of_globals(self): |
| 3030 |
|
| 3031 |
count = 1000 |
| 3032 |
|
| 3033 |
code = """if 1: |
| 3034 |
def f(): |
| 3035 |
return ( |
| 3036 |
{} |
| 3037 |
) |
| 3038 |
""".format("\n,\n".join(f"var{i}\n" for i in range(count))) |
| 3039 |
ns = {f"var{i}": i for i in range(count)} |
| 3040 |
exec(code, ns) |
| 3041 |
counts = self.count_traces(ns["f"]) |
| 3042 |
self.assertEqual(counts, {'call': 1, 'line': count * 2 + 1, 'return': 1}) |
| 3043 |
|
| 3044 |
|
| 3045 |
class TestEdgeCases(unittest.TestCase): |
| 3046 |
|
| 3047 |
def setUp(self): |
| 3048 |
self.addCleanup(sys.settrace, sys.gettrace()) |
| 3049 |
sys.settrace(None) |
| 3050 |
|
| 3051 |
def test_reentrancy(self): |
| 3052 |
def foo(*args): |
| 3053 |
... |
| 3054 |
|
| 3055 |
def bar(*args): |
| 3056 |
... |
| 3057 |
|
| 3058 |
class A: |
| 3059 |
def __call__(self, *args): |
| 3060 |
pass |
| 3061 |
|
| 3062 |
def __del__(self): |
| 3063 |
sys.settrace(bar) |
| 3064 |
|
| 3065 |
sys.settrace(A()) |
| 3066 |
sys.settrace(foo) |
| 3067 |
self.assertEqual(sys.gettrace(), bar) |
| 3068 |
|
| 3069 |
|
| 3070 |
def test_same_object(self): |
| 3071 |
def foo(*args): |
| 3072 |
... |
| 3073 |
|
| 3074 |
sys.settrace(foo) |
| 3075 |
del foo |
| 3076 |
sys.settrace(sys.gettrace()) |
| 3077 |
|
| 3078 |
|
| 3079 |
class TestLinesAfterTraceStarted(TraceTestCase): |
| 3080 |
|
| 3081 |
def test_events(self): |
| 3082 |
tracer = Tracer() |
| 3083 |
sys._getframe().f_trace = tracer.trace |
| 3084 |
sys.settrace(tracer.trace) |
| 3085 |
line = 4 |
| 3086 |
line = 5 |
| 3087 |
sys.settrace(None) |
| 3088 |
self.compare_events( |
| 3089 |
TestLinesAfterTraceStarted.test_events.__code__.co_firstlineno, |
| 3090 |
tracer.events, [ |
| 3091 |
(4, 'line'), |
| 3092 |
(5, 'line'), |
| 3093 |
(6, 'line')]) |
| 3094 |
|
| 3095 |
|
| 3096 |
class TestSetLocalTrace(TraceTestCase): |
| 3097 |
|
| 3098 |
def test_with_branches(self): |
| 3099 |
|
| 3100 |
def tracefunc(frame, event, arg): |
| 3101 |
if frame.f_code.co_name == "func": |
| 3102 |
frame.f_trace = tracefunc |
| 3103 |
line = frame.f_lineno - frame.f_code.co_firstlineno |
| 3104 |
events.append((line, event)) |
| 3105 |
return tracefunc |
| 3106 |
|
| 3107 |
def func(arg = 1): |
| 3108 |
N = 1 |
| 3109 |
if arg >= 2: |
| 3110 |
not_reached = 3 |
| 3111 |
else: |
| 3112 |
reached = 5 |
| 3113 |
if arg >= 3: |
| 3114 |
not_reached = 7 |
| 3115 |
else: |
| 3116 |
reached = 9 |
| 3117 |
the_end = 10 |
| 3118 |
|
| 3119 |
EXPECTED_EVENTS = [ |
| 3120 |
(0, 'call'), |
| 3121 |
(1, 'line'), |
| 3122 |
(2, 'line'), |
| 3123 |
(5, 'line'), |
| 3124 |
(6, 'line'), |
| 3125 |
(9, 'line'), |
| 3126 |
(10, 'line'), |
| 3127 |
(10, 'return'), |
| 3128 |
] |
| 3129 |
|
| 3130 |
events = [] |
| 3131 |
sys.settrace(tracefunc) |
| 3132 |
sys._getframe().f_trace = tracefunc |
| 3133 |
func() |
| 3134 |
self.assertEqual(events, EXPECTED_EVENTS) |
| 3135 |
sys.settrace(None) |
| 3136 |
|
| 3137 |
|
| 3138 |
if __name__ == "__main__": |
| 3139 |
unittest.main() |