| 1 |
import dis |
| 2 |
from pypy.interpreter.reverse_debugging import * |
| 3 |
from pypy.interpreter import reverse_debugging |
| 4 |
from rpython.rlib import revdb |
| 5 |
|
| 6 |
|
| 7 |
class FakeCode: |
| 8 |
hidden_applevel = False |
| 9 |
def __init__(self, co_code='', co_lnotab='', co_filename='?'): |
| 10 |
self.co_firstlineno = 43 |
| 11 |
self.co_code = co_code |
| 12 |
self.co_lnotab = co_lnotab |
| 13 |
self.co_revdb_linestarts = None |
| 14 |
self.co_filename = co_filename |
| 15 |
|
| 16 |
def get_co_lnotab(self): |
| 17 |
return self.co_lnotab |
| 18 |
|
| 19 |
|
| 20 |
try: |
| 21 |
from hypothesis import given, strategies, example |
| 22 |
except ImportError: |
| 23 |
pass |
| 24 |
else: |
| 25 |
@given(strategies.binary()) |
| 26 |
@example("\x01\x02\x03\x04" |
| 27 |
"\x00\xFF\x20\x30\x00\xFF\x00\x40" |
| 28 |
"\xFF\x00\x0A\x0B\xFF\x00\x0C\x00") |
| 29 |
def test_build_co_revdb_linestarts(lnotab): |
| 30 |
if len(lnotab) & 1: |
| 31 |
lnotab = lnotab + '\x00' |
| 32 |
code = FakeCode("?" * sum(map(ord, lnotab[0::2])), lnotab) |
| 33 |
lstart = build_co_revdb_linestarts(code) |
| 34 |
assert lstart is code.co_revdb_linestarts |
| 35 |
|
| 36 |
expected_starts = set() |
| 37 |
for addr, lineno in dis.findlinestarts(code): |
| 38 |
expected_starts.add(addr) |
| 39 |
|
| 40 |
next_start = len(code.co_code) |
| 41 |
for index in range(len(code.co_code), -1, -1): |
| 42 |
if index in expected_starts: |
| 43 |
next_start = index |
| 44 |
assert lstart[index] == chr(next_start - index |
| 45 |
if next_start - index <= 255 |
| 46 |
else 255) |
| 47 |
|
| 48 |
|
| 49 |
class FakeFrame: |
| 50 |
def __init__(self, code): |
| 51 |
self.__code = code |
| 52 |
def getcode(self): |
| 53 |
return self.__code |
| 54 |
|
| 55 |
def check_add_breakpoint(input, curfilename=None, |
| 56 |
expected_funcname=None, |
| 57 |
expected_fileline=None, |
| 58 |
expected_output=None, |
| 59 |
expected_chbkpt=None): |
| 60 |
dbstate.__dict__.clear() |
| 61 |
prev = revdb.send_answer, reverse_debugging.fetch_cur_frame |
| 62 |
try: |
| 63 |
messages = [] |
| 64 |
def got_message(cmd, arg1=0, arg2=0, arg3=0, extra=""): |
| 65 |
messages.append((cmd, arg1, arg2, arg3, extra)) |
| 66 |
def my_cur_frame(): |
| 67 |
assert curfilename is not None |
| 68 |
return FakeFrame(FakeCode(co_filename=curfilename)) |
| 69 |
revdb.send_answer = got_message |
| 70 |
reverse_debugging.fetch_cur_frame = my_cur_frame |
| 71 |
add_breakpoint(input, 5) |
| 72 |
finally: |
| 73 |
revdb.send_answer, reverse_debugging.fetch_cur_frame = prev |
| 74 |
|
| 75 |
if expected_funcname is None: |
| 76 |
assert dbstate.breakpoint_funcnames is None |
| 77 |
else: |
| 78 |
assert dbstate.breakpoint_funcnames == {expected_funcname: 5} |
| 79 |
|
| 80 |
if expected_fileline is None: |
| 81 |
assert dbstate.breakpoint_filelines is None |
| 82 |
else: |
| 83 |
filename, lineno = expected_fileline |
| 84 |
assert dbstate.breakpoint_filelines == [(filename.upper(), lineno, 5)] |
| 85 |
|
| 86 |
got_output = None |
| 87 |
got_chbkpt = None |
| 88 |
for msg in messages: |
| 89 |
if msg[0] == revdb.ANSWER_TEXT: |
| 90 |
assert got_output is None |
| 91 |
got_output = msg[-1] |
| 92 |
assert msg[1] in (0, 1) |
| 93 |
if msg[1]: |
| 94 |
got_output += "\n" |
| 95 |
elif msg[0] == revdb.ANSWER_CHBKPT: |
| 96 |
assert got_chbkpt is None |
| 97 |
assert msg[1] == 5 |
| 98 |
got_chbkpt = msg[-1] |
| 99 |
|
| 100 |
assert got_output == expected_output |
| 101 |
assert got_chbkpt == expected_chbkpt |
| 102 |
|
| 103 |
def test_add_breakpoint(): |
| 104 |
check_add_breakpoint('', expected_output="Empty breakpoint name\n", |
| 105 |
expected_chbkpt='') |
| 106 |
check_add_breakpoint('foo42', expected_funcname="foo42", |
| 107 |
expected_chbkpt="foo42()") |
| 108 |
check_add_breakpoint('foo42()', expected_funcname="foo42") |
| 109 |
check_add_breakpoint('foo.bar', expected_funcname="foo.bar", |
| 110 |
expected_output='Note: "foo.bar()" doesn''t look like a function name.' |
| 111 |
' Setting breakpoint anyway\n', |
| 112 |
expected_chbkpt="foo.bar()") |
| 113 |
check_add_breakpoint('<foo.bar>', expected_funcname="<foo.bar>") |
| 114 |
check_add_breakpoint('42', curfilename='abcd', |
| 115 |
expected_fileline=('abcd', 42), |
| 116 |
expected_chbkpt='abcd:42') |
| 117 |
check_add_breakpoint(':42', curfilename='abcd', |
| 118 |
expected_fileline=('abcd', 42), |
| 119 |
expected_chbkpt='abcd:42') |
| 120 |
check_add_breakpoint('abcd:42', expected_fileline=('abcd', 42), |
| 121 |
expected_output='Note: "abcd" doesnt look like a Python filename.' |
| 122 |
' Setting breakpoint anyway\n') |
| 123 |
check_add_breakpoint('abcd.py:42', |
| 124 |
expected_fileline=('abcd.py', 42)) |
| 125 |
check_add_breakpoint('42:abc', |
| 126 |
expected_output='expected a line number after colon\n', |
| 127 |
expected_chbkpt='') |