| 1 |
"""Apptests for f_lineno jump validation (sys.settrace frame jumping). |
| 2 |
|
| 3 |
Mirrors the lib-python JumpTestCase structure: functions defined at module |
| 4 |
level, JumpTracer with decorated=True, so firstLine is set dynamically. |
| 5 |
""" |
| 6 |
import sys |
| 7 |
import re |
| 8 |
|
| 9 |
|
| 10 |
class JumpTracer: |
| 11 |
def __init__(self, function, jumpFrom, jumpTo, event='line', decorated=False): |
| 12 |
self.code = function.__code__ |
| 13 |
self.jumpFrom = jumpFrom |
| 14 |
self.jumpTo = jumpTo |
| 15 |
self.event = event |
| 16 |
self.firstLine = None if decorated else self.code.co_firstlineno |
| 17 |
self.done = False |
| 18 |
|
| 19 |
def trace(self, frame, event, arg): |
| 20 |
if self.done: |
| 21 |
return |
| 22 |
if (self.firstLine is None and frame.f_code == self.code and event == 'line'): |
| 23 |
self.firstLine = frame.f_lineno - 1 |
| 24 |
if (event == self.event and self.firstLine is not None and |
| 25 |
frame.f_lineno == self.firstLine + self.jumpFrom): |
| 26 |
f = frame |
| 27 |
while f is not None and f.f_code != self.code: |
| 28 |
f = f.f_back |
| 29 |
if f is not None: |
| 30 |
try: |
| 31 |
frame.f_lineno = self.firstLine + self.jumpTo |
| 32 |
except TypeError: |
| 33 |
frame.f_lineno = self.jumpTo |
| 34 |
self.done = True |
| 35 |
return self.trace |
| 36 |
|
| 37 |
|
| 38 |
def run_jump_test(func, jumpFrom, jumpTo, expected, error=None, decorated=False): |
| 39 |
wrapped = func |
| 40 |
while hasattr(wrapped, '__wrapped__'): |
| 41 |
wrapped = wrapped.__wrapped__ |
| 42 |
|
| 43 |
tracer = JumpTracer(wrapped, jumpFrom, jumpTo, decorated=decorated) |
| 44 |
sys.settrace(tracer.trace) |
| 45 |
output = [] |
| 46 |
exc_raised = None |
| 47 |
try: |
| 48 |
if error is None: |
| 49 |
func(output) |
| 50 |
else: |
| 51 |
try: |
| 52 |
func(output) |
| 53 |
except error[0] as e: |
| 54 |
exc_raised = str(e) |
| 55 |
else: |
| 56 |
raise AssertionError("expected %s, got no exception" % error[0]) |
| 57 |
finally: |
| 58 |
sys.settrace(None) |
| 59 |
|
| 60 |
if error is not None: |
| 61 |
exc_type, pattern = error |
| 62 |
if not re.search(pattern, exc_raised): |
| 63 |
raise AssertionError( |
| 64 |
"expected %s matching %r, got: %r" % (exc_type.__name__, pattern, exc_raised)) |
| 65 |
|
| 66 |
if output != expected: |
| 67 |
raise AssertionError("output mismatch: expected %r, got %r" % (expected, output)) |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
def _no_jump_over_return_try_finally_in_finally_block(output): |
| 73 |
try: |
| 74 |
output.append(2) |
| 75 |
finally: |
| 76 |
output.append(4) |
| 77 |
output.append(5) |
| 78 |
return |
| 79 |
try: |
| 80 |
output.append(8) |
| 81 |
finally: |
| 82 |
output.append(10) |
| 83 |
pass |
| 84 |
output.append(12) |
| 85 |
|
| 86 |
|
| 87 |
def test_no_jump_over_return_try_finally_in_finally_block(): |
| 88 |
run_jump_test( |
| 89 |
_no_jump_over_return_try_finally_in_finally_block, |
| 90 |
jumpFrom=5, jumpTo=11, |
| 91 |
expected=[2, 4], |
| 92 |
error=(ValueError, 'exception'), |
| 93 |
decorated=True, |
| 94 |
) |