| 1 |
""" Test the bdb module. |
| 2 |
|
| 3 |
A test defines a list of tuples that may be seen as paired tuples, each |
| 4 |
pair being defined by 'expect_tuple, set_tuple' as follows: |
| 5 |
|
| 6 |
([event, [lineno[, co_name[, eargs]]]]), (set_type, [sargs]) |
| 7 |
|
| 8 |
* 'expect_tuple' describes the expected current state of the Bdb instance. |
| 9 |
It may be the empty tuple and no check is done in that case. |
| 10 |
* 'set_tuple' defines the set_*() method to be invoked when the Bdb |
| 11 |
instance reaches this state. |
| 12 |
|
| 13 |
Example of an 'expect_tuple, set_tuple' pair: |
| 14 |
|
| 15 |
('line', 2, 'tfunc_main'), ('step', ) |
| 16 |
|
| 17 |
Definitions of the members of the 'expect_tuple': |
| 18 |
event: |
| 19 |
Name of the trace event. The set methods that do not give back |
| 20 |
control to the tracer [1] do not trigger a tracer event and in |
| 21 |
that case the next 'event' may be 'None' by convention, its value |
| 22 |
is not checked. |
| 23 |
[1] Methods that trigger a trace event are set_step(), set_next(), |
| 24 |
set_return(), set_until() and set_continue(). |
| 25 |
lineno: |
| 26 |
Line number. Line numbers are relative to the start of the |
| 27 |
function when tracing a function in the test_bdb module (i.e. this |
| 28 |
module). |
| 29 |
co_name: |
| 30 |
Name of the function being currently traced. |
| 31 |
eargs: |
| 32 |
A tuple: |
| 33 |
* On an 'exception' event the tuple holds a class object, the |
| 34 |
current exception must be an instance of this class. |
| 35 |
* On a 'line' event, the tuple holds a dictionary and a list. The |
| 36 |
dictionary maps each breakpoint number that has been hit on this |
| 37 |
line to its hits count. The list holds the list of breakpoint |
| 38 |
number temporaries that are being deleted. |
| 39 |
|
| 40 |
Definitions of the members of the 'set_tuple': |
| 41 |
set_type: |
| 42 |
The type of the set method to be invoked. This may |
| 43 |
be the type of one of the Bdb set methods: 'step', 'next', |
| 44 |
'until', 'return', 'continue', 'break', 'quit', or the type of one |
| 45 |
of the set methods added by test_bdb.Bdb: 'ignore', 'enable', |
| 46 |
'disable', 'clear', 'up', 'down'. |
| 47 |
sargs: |
| 48 |
The arguments of the set method if any, packed in a tuple. |
| 49 |
""" |
| 50 |
|
| 51 |
from __future__ import absolute_import |
| 52 |
from __future__ import print_function |
| 53 |
|
| 54 |
import bdb as _bdb |
| 55 |
import sys |
| 56 |
import os |
| 57 |
import unittest |
| 58 |
import textwrap |
| 59 |
import importlib |
| 60 |
import linecache |
| 61 |
from contextlib import contextmanager |
| 62 |
from itertools import islice, repeat |
| 63 |
import test.support |
| 64 |
|
| 65 |
class BdbException(Exception): pass |
| 66 |
class BdbError(BdbException): """Error raised by the Bdb instance.""" |
| 67 |
class BdbSyntaxError(BdbException): """Syntax error in the test case.""" |
| 68 |
class BdbNotExpectedError(BdbException): """Unexpected result.""" |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
dry_run = 0 |
| 75 |
|
| 76 |
__file__ = os.path.splitext(__file__)[0] + '.py' |
| 77 |
|
| 78 |
def reset_Breakpoint(): |
| 79 |
_bdb.Breakpoint.next = 1 |
| 80 |
_bdb.Breakpoint.bplist = {} |
| 81 |
_bdb.Breakpoint.bpbynumber = [None] |
| 82 |
|
| 83 |
def info_breakpoints(): |
| 84 |
bp_list = [bp for bp in _bdb.Breakpoint.bpbynumber if bp] |
| 85 |
if not bp_list: |
| 86 |
return '' |
| 87 |
|
| 88 |
header_added = False |
| 89 |
for bp in bp_list: |
| 90 |
if not header_added: |
| 91 |
info = 'BpNum Temp Enb Hits Ignore Where\n' |
| 92 |
header_added = True |
| 93 |
|
| 94 |
disp = 'yes ' if bp.temporary else 'no ' |
| 95 |
enab = 'yes' if bp.enabled else 'no ' |
| 96 |
info += ('%-5d %s %s %-4d %-6d at %s:%d' % |
| 97 |
(bp.number, disp, enab, bp.hits, bp.ignore, |
| 98 |
os.path.basename(bp.file), bp.line)) |
| 99 |
if bp.cond: |
| 100 |
info += '\n\tstop only if %s' % (bp.cond,) |
| 101 |
info += '\n' |
| 102 |
return info |
| 103 |
|
| 104 |
class Bdb(_bdb.Bdb, object): |
| 105 |
"""Extend Bdb to enhance test coverage.""" |
| 106 |
|
| 107 |
def trace_dispatch(self, frame, event, arg): |
| 108 |
self.currentbp = None |
| 109 |
return super(Bdb, self).trace_dispatch(frame, event, arg) |
| 110 |
|
| 111 |
def set_break(self, filename, lineno, temporary=False, cond=None, |
| 112 |
funcname=None): |
| 113 |
if isinstance(funcname, str): |
| 114 |
if filename == __file__: |
| 115 |
globals_ = globals() |
| 116 |
else: |
| 117 |
module = importlib.import_module(filename[:-3]) |
| 118 |
globals_ = module.__dict__ |
| 119 |
func = eval(funcname, globals_) |
| 120 |
code = func.__code__ |
| 121 |
filename = code.co_filename |
| 122 |
lineno = code.co_firstlineno |
| 123 |
funcname = code.co_name |
| 124 |
|
| 125 |
res = super(Bdb, self).set_break(filename, lineno, |
| 126 |
temporary=temporary, cond=cond, funcname=funcname) |
| 127 |
if isinstance(res, str): |
| 128 |
raise BdbError(res) |
| 129 |
return res |
| 130 |
|
| 131 |
|
| 132 |
def get_bpbynumber(self, arg): |
| 133 |
"""Return a breakpoint by its index in Breakpoint.bybpnumber. |
| 134 |
|
| 135 |
For invalid arg values or if the breakpoint doesn't exist, |
| 136 |
raise a ValueError. |
| 137 |
""" |
| 138 |
if not arg: |
| 139 |
raise ValueError('Breakpoint number expected') |
| 140 |
try: |
| 141 |
number = int(arg) |
| 142 |
except ValueError: |
| 143 |
raise ValueError('Non-numeric breakpoint number %s' % arg) |
| 144 |
try: |
| 145 |
bp = _bdb.Breakpoint.bpbynumber[number] |
| 146 |
except IndexError: |
| 147 |
raise ValueError('Breakpoint number %d out of range' % number) |
| 148 |
if bp is None: |
| 149 |
raise ValueError('Breakpoint %d already deleted' % number) |
| 150 |
return bp |
| 151 |
|
| 152 |
def get_stack(self, f, t): |
| 153 |
self.stack, self.index = super(Bdb, self).get_stack(f, t) |
| 154 |
self.frame = self.stack[self.index][0] |
| 155 |
return self.stack, self.index |
| 156 |
|
| 157 |
def set_ignore(self, bpnum): |
| 158 |
"""Increment the ignore count of Breakpoint number 'bpnum'.""" |
| 159 |
bp = self.get_bpbynumber(bpnum) |
| 160 |
bp.ignore += 1 |
| 161 |
|
| 162 |
def set_enable(self, bpnum): |
| 163 |
bp = self.get_bpbynumber(bpnum) |
| 164 |
bp.enabled = True |
| 165 |
|
| 166 |
def set_disable(self, bpnum): |
| 167 |
bp = self.get_bpbynumber(bpnum) |
| 168 |
bp.enabled = False |
| 169 |
|
| 170 |
def set_clear(self, fname, lineno): |
| 171 |
err = self.clear_break(fname, lineno) |
| 172 |
if err: |
| 173 |
raise BdbError(err) |
| 174 |
|
| 175 |
def set_up(self): |
| 176 |
"""Move up in the frame stack.""" |
| 177 |
if not self.index: |
| 178 |
raise BdbError('Oldest frame') |
| 179 |
self.index -= 1 |
| 180 |
self.frame = self.stack[self.index][0] |
| 181 |
|
| 182 |
def set_down(self): |
| 183 |
"""Move down in the frame stack.""" |
| 184 |
if self.index + 1 == len(self.stack): |
| 185 |
raise BdbError('Newest frame') |
| 186 |
self.index += 1 |
| 187 |
self.frame = self.stack[self.index][0] |
| 188 |
|
| 189 |
class Tracer(Bdb): |
| 190 |
"""A tracer for testing the bdb module.""" |
| 191 |
|
| 192 |
def __init__(self, expect_set, skip=None, dry_run=False, test_case=None): |
| 193 |
super(Tracer, self).__init__(skip=skip) |
| 194 |
self.expect_set = expect_set |
| 195 |
self.dry_run = dry_run |
| 196 |
self.header = ('Dry-run results for %s:' % test_case if |
| 197 |
test_case is not None else None) |
| 198 |
self.init_test() |
| 199 |
|
| 200 |
def init_test(self): |
| 201 |
self.cur_except = None |
| 202 |
self.expect_set_no = 0 |
| 203 |
self.breakpoint_hits = None |
| 204 |
self.expected_list = list(islice(self.expect_set, 0, None, 2)) |
| 205 |
self.set_list = list(islice(self.expect_set, 1, None, 2)) |
| 206 |
|
| 207 |
def trace_dispatch(self, frame, event, arg): |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
if self.cur_except is not None: |
| 213 |
raise self.cur_except |
| 214 |
|
| 215 |
if event == 'exception': |
| 216 |
try: |
| 217 |
res = super(Tracer, self).trace_dispatch(frame, event, arg) |
| 218 |
return res |
| 219 |
except BdbException as e: |
| 220 |
self.cur_except = e |
| 221 |
return self.trace_dispatch |
| 222 |
else: |
| 223 |
return super(Tracer, self).trace_dispatch(frame, event, arg) |
| 224 |
|
| 225 |
def user_call(self, frame, argument_list): |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
if not self.stop_here(frame): |
| 230 |
return |
| 231 |
self.process_event('call', frame, argument_list) |
| 232 |
self.next_set_method() |
| 233 |
|
| 234 |
def user_line(self, frame): |
| 235 |
self.process_event('line', frame) |
| 236 |
|
| 237 |
if self.dry_run and self.breakpoint_hits: |
| 238 |
info = info_breakpoints().strip('\n') |
| 239 |
|
| 240 |
for line in info.split('\n'): |
| 241 |
print(' ' + line) |
| 242 |
self.delete_temporaries() |
| 243 |
self.breakpoint_hits = None |
| 244 |
|
| 245 |
self.next_set_method() |
| 246 |
|
| 247 |
def user_return(self, frame, return_value): |
| 248 |
self.process_event('return', frame, return_value) |
| 249 |
self.next_set_method() |
| 250 |
|
| 251 |
def user_exception(self, frame, exc_info): |
| 252 |
self.exc_info = exc_info |
| 253 |
self.process_event('exception', frame) |
| 254 |
self.next_set_method() |
| 255 |
|
| 256 |
def do_clear(self, arg): |
| 257 |
|
| 258 |
bp_list = [self.currentbp] |
| 259 |
self.breakpoint_hits = (bp_list, bp_list) |
| 260 |
|
| 261 |
def delete_temporaries(self): |
| 262 |
if self.breakpoint_hits: |
| 263 |
for n in self.breakpoint_hits[1]: |
| 264 |
self.clear_bpbynumber(n) |
| 265 |
|
| 266 |
def pop_next(self): |
| 267 |
self.expect_set_no += 1 |
| 268 |
try: |
| 269 |
self.expect = self.expected_list.pop(0) |
| 270 |
except IndexError: |
| 271 |
raise BdbNotExpectedError( |
| 272 |
'expect_set list exhausted, cannot pop item %d' % |
| 273 |
self.expect_set_no) |
| 274 |
self.set_tuple = self.set_list.pop(0) |
| 275 |
|
| 276 |
def process_event(self, event, frame, *args): |
| 277 |
|
| 278 |
|
| 279 |
tb = None |
| 280 |
if event == 'exception': |
| 281 |
tb = self.exc_info[2] |
| 282 |
self.get_stack(frame, tb) |
| 283 |
|
| 284 |
|
| 285 |
if self.currentbp is not None and not self.breakpoint_hits: |
| 286 |
bp_list = [self.currentbp] |
| 287 |
self.breakpoint_hits = (bp_list, []) |
| 288 |
|
| 289 |
|
| 290 |
self.event= event |
| 291 |
self.pop_next() |
| 292 |
if self.dry_run: |
| 293 |
self.print_state(self.header) |
| 294 |
return |
| 295 |
|
| 296 |
|
| 297 |
if self.expect: |
| 298 |
self.check_equal(self.expect[0], event, 'Wrong event type') |
| 299 |
self.check_lno_name() |
| 300 |
|
| 301 |
if event in ('call', 'return'): |
| 302 |
self.check_expect_max_size(3) |
| 303 |
elif len(self.expect) > 3: |
| 304 |
if event == 'line': |
| 305 |
bps, temporaries = self.expect[3] |
| 306 |
bpnums = sorted(bps.keys()) |
| 307 |
if not self.breakpoint_hits: |
| 308 |
self.raise_not_expected( |
| 309 |
'No breakpoints hit at expect_set item %d' % |
| 310 |
self.expect_set_no) |
| 311 |
self.check_equal(bpnums, self.breakpoint_hits[0], |
| 312 |
'Breakpoint numbers do not match') |
| 313 |
self.check_equal([bps[n] for n in bpnums], |
| 314 |
[self.get_bpbynumber(n).hits for |
| 315 |
n in self.breakpoint_hits[0]], |
| 316 |
'Wrong breakpoint hit count') |
| 317 |
self.check_equal(sorted(temporaries), self.breakpoint_hits[1], |
| 318 |
'Wrong temporary breakpoints') |
| 319 |
|
| 320 |
elif event == 'exception': |
| 321 |
if not isinstance(self.exc_info[1], self.expect[3]): |
| 322 |
self.raise_not_expected( |
| 323 |
"Wrong exception at expect_set item %d, got '%s'" % |
| 324 |
(self.expect_set_no, self.exc_info)) |
| 325 |
|
| 326 |
def check_equal(self, expected, result, msg): |
| 327 |
if expected == result: |
| 328 |
return |
| 329 |
self.raise_not_expected("%s at expect_set item %d, got '%s'" % |
| 330 |
(msg, self.expect_set_no, result)) |
| 331 |
|
| 332 |
def check_lno_name(self): |
| 333 |
"""Check the line number and function co_name.""" |
| 334 |
s = len(self.expect) |
| 335 |
if s > 1: |
| 336 |
lineno = self.lno_abs2rel() |
| 337 |
self.check_equal(self.expect[1], lineno, 'Wrong line number') |
| 338 |
if s > 2: |
| 339 |
self.check_equal(self.expect[2], self.frame.f_code.co_name, |
| 340 |
'Wrong function name') |
| 341 |
|
| 342 |
def check_expect_max_size(self, size): |
| 343 |
if len(self.expect) > size: |
| 344 |
raise BdbSyntaxError('Invalid size of the %s expect tuple: %s' % |
| 345 |
(self.event, self.expect)) |
| 346 |
|
| 347 |
def lno_abs2rel(self): |
| 348 |
fname = self.canonic(self.frame.f_code.co_filename) |
| 349 |
lineno = self.frame.f_lineno |
| 350 |
return ((lineno - self.frame.f_code.co_firstlineno + 1) |
| 351 |
if fname == self.canonic(__file__) else lineno) |
| 352 |
|
| 353 |
def lno_rel2abs(self, fname, lineno): |
| 354 |
return (self.frame.f_code.co_firstlineno + lineno - 1 |
| 355 |
if (lineno and self.canonic(fname) == self.canonic(__file__)) |
| 356 |
else lineno) |
| 357 |
|
| 358 |
def get_state(self): |
| 359 |
lineno = self.lno_abs2rel() |
| 360 |
co_name = self.frame.f_code.co_name |
| 361 |
state = "('%s', %d, '%s'" % (self.event, lineno, co_name) |
| 362 |
if self.breakpoint_hits: |
| 363 |
bps = '{' |
| 364 |
for n in self.breakpoint_hits[0]: |
| 365 |
if bps != '{': |
| 366 |
bps += ', ' |
| 367 |
bps += '%s: %s' % (n, self.get_bpbynumber(n).hits) |
| 368 |
bps += '}' |
| 369 |
bps = '(' + bps + ', ' + str(self.breakpoint_hits[1]) + ')' |
| 370 |
state += ', ' + bps |
| 371 |
elif self.event == 'exception': |
| 372 |
state += ', ' + self.exc_info[0].__name__ |
| 373 |
state += '), ' |
| 374 |
return state.ljust(32) + str(self.set_tuple) + ',' |
| 375 |
|
| 376 |
def print_state(self, header=None): |
| 377 |
if header is not None and self.expect_set_no == 1: |
| 378 |
print() |
| 379 |
print(header) |
| 380 |
print('%d: %s' % (self.expect_set_no, self.get_state())) |
| 381 |
|
| 382 |
def raise_not_expected(self, msg): |
| 383 |
msg += '\n' |
| 384 |
msg += ' Expected: %s\n' % str(self.expect) |
| 385 |
msg += ' Got: ' + self.get_state() |
| 386 |
raise BdbNotExpectedError(msg) |
| 387 |
|
| 388 |
def next_set_method(self): |
| 389 |
set_type = self.set_tuple[0] |
| 390 |
args = self.set_tuple[1] if len(self.set_tuple) == 2 else None |
| 391 |
set_method = getattr(self, 'set_' + set_type) |
| 392 |
|
| 393 |
|
| 394 |
if set_type in ('step', 'continue', 'quit'): |
| 395 |
set_method() |
| 396 |
return |
| 397 |
elif set_type in ('next', 'return'): |
| 398 |
set_method(self.frame) |
| 399 |
return |
| 400 |
elif set_type == 'until': |
| 401 |
set_method(self.frame) |
| 402 |
return |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
if (args and set_type in ('break', 'clear', 'ignore', 'enable', |
| 407 |
'disable')) or set_type in ('up', 'down'): |
| 408 |
if set_type in ('break', 'clear'): |
| 409 |
fname = args[0] |
| 410 |
lineno = args[1] |
| 411 |
lineno = self.lno_rel2abs(fname, lineno) |
| 412 |
args = [fname, lineno] + list(args[2:]) |
| 413 |
set_method(*args) |
| 414 |
elif set_type in ('ignore', 'enable', 'disable'): |
| 415 |
set_method(*args) |
| 416 |
elif set_type in ('up', 'down'): |
| 417 |
set_method() |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
self.event= None |
| 422 |
self.pop_next() |
| 423 |
if self.dry_run: |
| 424 |
self.print_state() |
| 425 |
else: |
| 426 |
if self.expect: |
| 427 |
self.check_lno_name() |
| 428 |
self.check_expect_max_size(3) |
| 429 |
self.next_set_method() |
| 430 |
else: |
| 431 |
raise BdbSyntaxError('"%s" is an invalid set_tuple' % |
| 432 |
self.set_tuple) |
| 433 |
|
| 434 |
class TracerRun(): |
| 435 |
"""Provide a context for running a Tracer instance with a test case.""" |
| 436 |
|
| 437 |
def __init__(self, test_case, skip=None): |
| 438 |
self.test_case = test_case |
| 439 |
self.dry_run = test_case.dry_run |
| 440 |
self.tracer = Tracer(test_case.expect_set, skip=skip, |
| 441 |
dry_run=self.dry_run, test_case=test_case.id()) |
| 442 |
|
| 443 |
def __enter__(self): |
| 444 |
|
| 445 |
reset_Breakpoint() |
| 446 |
return self.tracer |
| 447 |
|
| 448 |
def __exit__(self, type_=None, value=None, traceback=None): |
| 449 |
reset_Breakpoint() |
| 450 |
sys.settrace(None) |
| 451 |
|
| 452 |
not_empty = '' |
| 453 |
if self.tracer.set_list: |
| 454 |
not_empty += 'All paired tuples have not been processed, ' |
| 455 |
not_empty += ('the last one was number %d' % |
| 456 |
self.tracer.expect_set_no) |
| 457 |
|
| 458 |
|
| 459 |
if type_ is not None and issubclass(BdbNotExpectedError, type_): |
| 460 |
if isinstance(value, BaseException) and value.args: |
| 461 |
err_msg = value.args[0] |
| 462 |
if not_empty: |
| 463 |
err_msg += '\n' + not_empty |
| 464 |
if self.dry_run: |
| 465 |
print(err_msg) |
| 466 |
return True |
| 467 |
else: |
| 468 |
self.test_case.fail(err_msg) |
| 469 |
else: |
| 470 |
assert False, 'BdbNotExpectedError with empty args' |
| 471 |
|
| 472 |
if not_empty: |
| 473 |
if self.dry_run: |
| 474 |
print(not_empty) |
| 475 |
else: |
| 476 |
self.test_case.fail(not_empty) |
| 477 |
|
| 478 |
def run_test(modules, set_list, skip=None): |
| 479 |
"""Run a test and print the dry-run results. |
| 480 |
|
| 481 |
'modules': A dictionary mapping module names to their source code as a |
| 482 |
string. The dictionary MUST include one module named |
| 483 |
'test_module' with a main() function. |
| 484 |
'set_list': A list of set_type tuples to be run on the module. |
| 485 |
|
| 486 |
For example, running the following script outputs the following results: |
| 487 |
|
| 488 |
***************************** SCRIPT ******************************** |
| 489 |
|
| 490 |
from test.test_bdb import run_test, break_in_func |
| 491 |
|
| 492 |
code = ''' |
| 493 |
def func(): |
| 494 |
lno = 3 |
| 495 |
|
| 496 |
def main(): |
| 497 |
func() |
| 498 |
lno = 7 |
| 499 |
''' |
| 500 |
|
| 501 |
set_list = [ |
| 502 |
break_in_func('func', 'test_module.py'), |
| 503 |
('continue', ), |
| 504 |
('step', ), |
| 505 |
('step', ), |
| 506 |
('step', ), |
| 507 |
('quit', ), |
| 508 |
] |
| 509 |
|
| 510 |
modules = { 'test_module': code } |
| 511 |
run_test(modules, set_list) |
| 512 |
|
| 513 |
**************************** results ******************************** |
| 514 |
|
| 515 |
1: ('line', 2, 'tfunc_import'), ('next',), |
| 516 |
2: ('line', 3, 'tfunc_import'), ('step',), |
| 517 |
3: ('call', 5, 'main'), ('break', ('test_module.py', None, False, None, 'func')), |
| 518 |
4: ('None', 5, 'main'), ('continue',), |
| 519 |
5: ('line', 3, 'func', ({1: 1}, [])), ('step',), |
| 520 |
BpNum Temp Enb Hits Ignore Where |
| 521 |
1 no yes 1 0 at test_module.py:2 |
| 522 |
6: ('return', 3, 'func'), ('step',), |
| 523 |
7: ('line', 7, 'main'), ('step',), |
| 524 |
8: ('return', 7, 'main'), ('quit',), |
| 525 |
|
| 526 |
************************************************************************* |
| 527 |
|
| 528 |
""" |
| 529 |
def gen(a, b): |
| 530 |
try: |
| 531 |
while 1: |
| 532 |
x = next(a) |
| 533 |
y = next(b) |
| 534 |
yield x |
| 535 |
yield y |
| 536 |
except StopIteration: |
| 537 |
return |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
sl = [('next', ), ('step', )] |
| 542 |
sl.extend(set_list) |
| 543 |
|
| 544 |
test = BaseTestCase() |
| 545 |
test.dry_run = True |
| 546 |
test.id = lambda : None |
| 547 |
test.expect_set = list(gen(repeat(()), iter(sl))) |
| 548 |
with create_modules(modules): |
| 549 |
sys.path.append(os.getcwd()) |
| 550 |
with TracerRun(test, skip=skip) as tracer: |
| 551 |
tracer.runcall(tfunc_import) |
| 552 |
|
| 553 |
@contextmanager |
| 554 |
def create_modules(modules): |
| 555 |
with test.support.temp_cwd(): |
| 556 |
try: |
| 557 |
sys.path.insert(0, os.getcwd()) |
| 558 |
for m in modules: |
| 559 |
fname = m + '.py' |
| 560 |
with open(fname, 'w') as f: |
| 561 |
f.write(textwrap.dedent(modules[m])) |
| 562 |
linecache.checkcache(fname) |
| 563 |
yield |
| 564 |
finally: |
| 565 |
sys.path.pop(0) |
| 566 |
for m in modules: |
| 567 |
test.support.forget(m) |
| 568 |
|
| 569 |
def break_in_func(funcname, fname=__file__, temporary=False, cond=None): |
| 570 |
return 'break', (fname, None, temporary, cond, funcname) |
| 571 |
|
| 572 |
TEST_MODULE = 'test_module_for_bdb' |
| 573 |
TEST_MODULE_FNAME = TEST_MODULE + '.py' |
| 574 |
def tfunc_import(): |
| 575 |
import test_module_for_bdb |
| 576 |
test_module_for_bdb.main() |
| 577 |
|
| 578 |
def tfunc_main(): |
| 579 |
lno = 2 |
| 580 |
tfunc_first() |
| 581 |
tfunc_second() |
| 582 |
lno = 5 |
| 583 |
lno = 6 |
| 584 |
lno = 7 |
| 585 |
|
| 586 |
def tfunc_first(): |
| 587 |
lno = 2 |
| 588 |
lno = 3 |
| 589 |
lno = 4 |
| 590 |
|
| 591 |
def tfunc_second(): |
| 592 |
lno = 2 |
| 593 |
|
| 594 |
class BaseTestCase(unittest.TestCase): |
| 595 |
"""Base class for all tests.""" |
| 596 |
dry_run = dry_run |
| 597 |
|
| 598 |
class StateTestCase(BaseTestCase): |
| 599 |
"""Test the step, next, return, until and quit 'set_' methods.""" |
| 600 |
|
| 601 |
def test_step(self): |
| 602 |
self.expect_set = [ |
| 603 |
('line', 2, 'tfunc_main'), ('step', ), |
| 604 |
('line', 3, 'tfunc_main'), ('step', ), |
| 605 |
('call', 1, 'tfunc_first'), ('step', ), |
| 606 |
('line', 2, 'tfunc_first'), ('quit', ), |
| 607 |
] |
| 608 |
with TracerRun(self) as tracer: |
| 609 |
tracer.runcall(tfunc_main) |
| 610 |
|
| 611 |
def test_step_on_last_statement(self): |
| 612 |
self.expect_set = [ |
| 613 |
('line', 2, 'tfunc_main'), ('step', ), |
| 614 |
('line', 3, 'tfunc_main'), ('step', ), |
| 615 |
('call', 1, 'tfunc_first'), ('break', (__file__, 3)), |
| 616 |
('None', 1, 'tfunc_first'), ('continue', ), |
| 617 |
('line', 3, 'tfunc_first', ({1:1}, [])), ('step', ), |
| 618 |
('line', 4, 'tfunc_first'), ('quit', ), |
| 619 |
] |
| 620 |
with TracerRun(self) as tracer: |
| 621 |
tracer.runcall(tfunc_main) |
| 622 |
|
| 623 |
def test_next_on_last_statement(self): |
| 624 |
self.expect_set = [ |
| 625 |
('line', 2, 'tfunc_main'), ('step', ), |
| 626 |
('line', 3, 'tfunc_main'), ('step', ), |
| 627 |
('call', 1, 'tfunc_first'), ('break', (__file__, 3)), |
| 628 |
('None', 1, 'tfunc_first'), ('continue', ), |
| 629 |
('line', 3, 'tfunc_first', ({1:1}, [])), ('next', ), |
| 630 |
('line', 4, 'tfunc_first'), ('quit', ), |
| 631 |
] |
| 632 |
with TracerRun(self) as tracer: |
| 633 |
tracer.runcall(tfunc_main) |
| 634 |
|
| 635 |
def test_next(self): |
| 636 |
self.expect_set = [ |
| 637 |
('line', 2, 'tfunc_main'), ('step', ), |
| 638 |
('line', 3, 'tfunc_main'), ('next', ), |
| 639 |
('line', 4, 'tfunc_main'), ('step', ), |
| 640 |
('call', 1, 'tfunc_second'), ('step', ), |
| 641 |
('line', 2, 'tfunc_second'), ('quit', ), |
| 642 |
] |
| 643 |
with TracerRun(self) as tracer: |
| 644 |
tracer.runcall(tfunc_main) |
| 645 |
|
| 646 |
def test_next_over_import(self): |
| 647 |
code = """ |
| 648 |
def main(): |
| 649 |
lno = 3 |
| 650 |
""" |
| 651 |
modules = { TEST_MODULE: code } |
| 652 |
with create_modules(modules): |
| 653 |
self.expect_set = [ |
| 654 |
('line', 2, 'tfunc_import'), ('next', ), |
| 655 |
('line', 3, 'tfunc_import'), ('quit', ), |
| 656 |
] |
| 657 |
with TracerRun(self) as tracer: |
| 658 |
tracer.runcall(tfunc_import) |
| 659 |
|
| 660 |
def test_next_on_plain_statement(self): |
| 661 |
|
| 662 |
|
| 663 |
self.expect_set = [ |
| 664 |
('line', 2, 'tfunc_main'), ('step', ), |
| 665 |
('line', 3, 'tfunc_main'), ('step', ), |
| 666 |
('call', 1, 'tfunc_first'), ('next', ), |
| 667 |
('line', 2, 'tfunc_first'), ('quit', ), |
| 668 |
] |
| 669 |
with TracerRun(self) as tracer: |
| 670 |
tracer.runcall(tfunc_main) |
| 671 |
|
| 672 |
def test_next_in_caller_frame(self): |
| 673 |
|
| 674 |
|
| 675 |
self.expect_set = [ |
| 676 |
('line', 2, 'tfunc_main'), ('step', ), |
| 677 |
('line', 3, 'tfunc_main'), ('step', ), |
| 678 |
('call', 1, 'tfunc_first'), ('up', ), |
| 679 |
('None', 3, 'tfunc_main'), ('next', ), |
| 680 |
('line', 4, 'tfunc_main'), ('quit', ), |
| 681 |
] |
| 682 |
with TracerRun(self) as tracer: |
| 683 |
tracer.runcall(tfunc_main) |
| 684 |
|
| 685 |
def test_return(self): |
| 686 |
self.expect_set = [ |
| 687 |
('line', 2, 'tfunc_main'), ('step', ), |
| 688 |
('line', 3, 'tfunc_main'), ('step', ), |
| 689 |
('call', 1, 'tfunc_first'), ('step', ), |
| 690 |
('line', 2, 'tfunc_first'), ('return', ), |
| 691 |
('return', 4, 'tfunc_first'), ('step', ), |
| 692 |
('line', 4, 'tfunc_main'), ('quit', ), |
| 693 |
] |
| 694 |
with TracerRun(self) as tracer: |
| 695 |
tracer.runcall(tfunc_main) |
| 696 |
|
| 697 |
def test_return_in_caller_frame(self): |
| 698 |
self.expect_set = [ |
| 699 |
('line', 2, 'tfunc_main'), ('step', ), |
| 700 |
('line', 3, 'tfunc_main'), ('step', ), |
| 701 |
('call', 1, 'tfunc_first'), ('up', ), |
| 702 |
('None', 3, 'tfunc_main'), ('return', ), |
| 703 |
('return', 7, 'tfunc_main'), ('quit', ), |
| 704 |
] |
| 705 |
with TracerRun(self) as tracer: |
| 706 |
tracer.runcall(tfunc_main) |
| 707 |
|
| 708 |
def test_until(self): |
| 709 |
self.expect_set = [ |
| 710 |
('line', 2, 'tfunc_main'), ('step', ), |
| 711 |
('line', 3, 'tfunc_main'), ('step', ), |
| 712 |
('call', 1, 'tfunc_first'), ('step', ), |
| 713 |
('line', 2, 'tfunc_first'), ('until', ), |
| 714 |
('line', 3, 'tfunc_first'), ('quit', ), |
| 715 |
] |
| 716 |
with TracerRun(self) as tracer: |
| 717 |
tracer.runcall(tfunc_main) |
| 718 |
|
| 719 |
def test_until_in_caller_frame(self): |
| 720 |
self.expect_set = [ |
| 721 |
('line', 2, 'tfunc_main'), ('step', ), |
| 722 |
('line', 3, 'tfunc_main'), ('step', ), |
| 723 |
('call', 1, 'tfunc_first'), ('up', ), |
| 724 |
('None', 3, 'tfunc_main'), ('until', ), |
| 725 |
('line', 4, 'tfunc_main'), ('quit', ), |
| 726 |
] |
| 727 |
with TracerRun(self) as tracer: |
| 728 |
tracer.runcall(tfunc_main) |
| 729 |
|
| 730 |
def test_skip(self): |
| 731 |
|
| 732 |
|
| 733 |
code = """ |
| 734 |
def main(): |
| 735 |
lno = 3 |
| 736 |
""" |
| 737 |
modules = { TEST_MODULE: code } |
| 738 |
with create_modules(modules): |
| 739 |
self.expect_set = [ |
| 740 |
('line', 2, 'tfunc_import'), ('step', ), |
| 741 |
('line', 3, 'tfunc_import'), ('quit', ), |
| 742 |
] |
| 743 |
skip = ('importlib*', TEST_MODULE) |
| 744 |
with TracerRun(self, skip=skip) as tracer: |
| 745 |
tracer.runcall(tfunc_import) |
| 746 |
|
| 747 |
def test_down(self): |
| 748 |
|
| 749 |
self.expect_set = [ |
| 750 |
('line', 2, 'tfunc_main'), ('down', ), |
| 751 |
] |
| 752 |
with TracerRun(self) as tracer: |
| 753 |
self.assertRaises(BdbError, tracer.runcall, tfunc_main) |
| 754 |
|
| 755 |
def test_up(self): |
| 756 |
self.expect_set = [ |
| 757 |
('line', 2, 'tfunc_main'), ('step', ), |
| 758 |
('line', 3, 'tfunc_main'), ('step', ), |
| 759 |
('call', 1, 'tfunc_first'), ('up', ), |
| 760 |
('None', 3, 'tfunc_main'), ('quit', ), |
| 761 |
] |
| 762 |
with TracerRun(self) as tracer: |
| 763 |
tracer.runcall(tfunc_main) |
| 764 |
|
| 765 |
class BreakpointTestCase(BaseTestCase): |
| 766 |
"""Test the breakpoint set method.""" |
| 767 |
|
| 768 |
def test_bp_on_non_existent_module(self): |
| 769 |
self.expect_set = [ |
| 770 |
('line', 2, 'tfunc_import'), ('break', ('/non/existent/module.py', 1)) |
| 771 |
] |
| 772 |
with TracerRun(self) as tracer: |
| 773 |
self.assertRaises(BdbError, tracer.runcall, tfunc_import) |
| 774 |
|
| 775 |
def test_bp_after_last_statement(self): |
| 776 |
code = """ |
| 777 |
def main(): |
| 778 |
lno = 3 |
| 779 |
""" |
| 780 |
modules = { TEST_MODULE: code } |
| 781 |
with create_modules(modules): |
| 782 |
self.expect_set = [ |
| 783 |
('line', 2, 'tfunc_import'), ('break', (TEST_MODULE_FNAME, 4)) |
| 784 |
] |
| 785 |
with TracerRun(self) as tracer: |
| 786 |
self.assertRaises(BdbError, tracer.runcall, tfunc_import) |
| 787 |
|
| 788 |
def test_temporary_bp(self): |
| 789 |
code = """ |
| 790 |
def func(): |
| 791 |
lno = 3 |
| 792 |
|
| 793 |
def main(): |
| 794 |
for i in range(2): |
| 795 |
func() |
| 796 |
""" |
| 797 |
modules = { TEST_MODULE: code } |
| 798 |
with create_modules(modules): |
| 799 |
self.expect_set = [ |
| 800 |
('line', 2, 'tfunc_import'), |
| 801 |
break_in_func('func', TEST_MODULE_FNAME, True), |
| 802 |
('None', 2, 'tfunc_import'), |
| 803 |
break_in_func('func', TEST_MODULE_FNAME, True), |
| 804 |
('None', 2, 'tfunc_import'), ('continue', ), |
| 805 |
('line', 3, 'func', ({1:1}, [1])), ('continue', ), |
| 806 |
('line', 3, 'func', ({2:1}, [2])), ('quit', ), |
| 807 |
] |
| 808 |
with TracerRun(self) as tracer: |
| 809 |
tracer.runcall(tfunc_import) |
| 810 |
|
| 811 |
def test_disabled_temporary_bp(self): |
| 812 |
code = """ |
| 813 |
def func(): |
| 814 |
lno = 3 |
| 815 |
|
| 816 |
def main(): |
| 817 |
for i in range(3): |
| 818 |
func() |
| 819 |
""" |
| 820 |
modules = { TEST_MODULE: code } |
| 821 |
with create_modules(modules): |
| 822 |
self.expect_set = [ |
| 823 |
('line', 2, 'tfunc_import'), |
| 824 |
break_in_func('func', TEST_MODULE_FNAME), |
| 825 |
('None', 2, 'tfunc_import'), |
| 826 |
break_in_func('func', TEST_MODULE_FNAME, True), |
| 827 |
('None', 2, 'tfunc_import'), ('disable', (2, )), |
| 828 |
('None', 2, 'tfunc_import'), ('continue', ), |
| 829 |
('line', 3, 'func', ({1:1}, [])), ('enable', (2, )), |
| 830 |
('None', 3, 'func'), ('disable', (1, )), |
| 831 |
('None', 3, 'func'), ('continue', ), |
| 832 |
('line', 3, 'func', ({2:1}, [2])), ('enable', (1, )), |
| 833 |
('None', 3, 'func'), ('continue', ), |
| 834 |
('line', 3, 'func', ({1:2}, [])), ('quit', ), |
| 835 |
] |
| 836 |
with TracerRun(self) as tracer: |
| 837 |
tracer.runcall(tfunc_import) |
| 838 |
|
| 839 |
def test_bp_condition(self): |
| 840 |
code = """ |
| 841 |
def func(a): |
| 842 |
lno = 3 |
| 843 |
|
| 844 |
def main(): |
| 845 |
for i in range(3): |
| 846 |
func(i) |
| 847 |
""" |
| 848 |
modules = { TEST_MODULE: code } |
| 849 |
with create_modules(modules): |
| 850 |
self.expect_set = [ |
| 851 |
('line', 2, 'tfunc_import'), |
| 852 |
break_in_func('func', TEST_MODULE_FNAME, False, 'a == 2'), |
| 853 |
('None', 2, 'tfunc_import'), ('continue', ), |
| 854 |
('line', 3, 'func', ({1:3}, [])), ('quit', ), |
| 855 |
] |
| 856 |
with TracerRun(self) as tracer: |
| 857 |
tracer.runcall(tfunc_import) |
| 858 |
|
| 859 |
def test_bp_exception_on_condition_evaluation(self): |
| 860 |
code = """ |
| 861 |
def func(a): |
| 862 |
lno = 3 |
| 863 |
|
| 864 |
def main(): |
| 865 |
func(0) |
| 866 |
""" |
| 867 |
modules = { TEST_MODULE: code } |
| 868 |
with create_modules(modules): |
| 869 |
self.expect_set = [ |
| 870 |
('line', 2, 'tfunc_import'), |
| 871 |
break_in_func('func', TEST_MODULE_FNAME, False, '1 // 0'), |
| 872 |
('None', 2, 'tfunc_import'), ('continue', ), |
| 873 |
('line', 3, 'func', ({1:1}, [])), ('quit', ), |
| 874 |
] |
| 875 |
with TracerRun(self) as tracer: |
| 876 |
tracer.runcall(tfunc_import) |
| 877 |
|
| 878 |
def test_bp_ignore_count(self): |
| 879 |
code = """ |
| 880 |
def func(): |
| 881 |
lno = 3 |
| 882 |
|
| 883 |
def main(): |
| 884 |
for i in range(2): |
| 885 |
func() |
| 886 |
""" |
| 887 |
modules = { TEST_MODULE: code } |
| 888 |
with create_modules(modules): |
| 889 |
self.expect_set = [ |
| 890 |
('line', 2, 'tfunc_import'), |
| 891 |
break_in_func('func', TEST_MODULE_FNAME), |
| 892 |
('None', 2, 'tfunc_import'), ('ignore', (1, )), |
| 893 |
('None', 2, 'tfunc_import'), ('continue', ), |
| 894 |
('line', 3, 'func', ({1:2}, [])), ('quit', ), |
| 895 |
] |
| 896 |
with TracerRun(self) as tracer: |
| 897 |
tracer.runcall(tfunc_import) |
| 898 |
|
| 899 |
def test_ignore_count_on_disabled_bp(self): |
| 900 |
code = """ |
| 901 |
def func(): |
| 902 |
lno = 3 |
| 903 |
|
| 904 |
def main(): |
| 905 |
for i in range(3): |
| 906 |
func() |
| 907 |
""" |
| 908 |
modules = { TEST_MODULE: code } |
| 909 |
with create_modules(modules): |
| 910 |
self.expect_set = [ |
| 911 |
('line', 2, 'tfunc_import'), |
| 912 |
break_in_func('func', TEST_MODULE_FNAME), |
| 913 |
('None', 2, 'tfunc_import'), |
| 914 |
break_in_func('func', TEST_MODULE_FNAME), |
| 915 |
('None', 2, 'tfunc_import'), ('ignore', (1, )), |
| 916 |
('None', 2, 'tfunc_import'), ('disable', (1, )), |
| 917 |
('None', 2, 'tfunc_import'), ('continue', ), |
| 918 |
('line', 3, 'func', ({2:1}, [])), ('enable', (1, )), |
| 919 |
('None', 3, 'func'), ('continue', ), |
| 920 |
('line', 3, 'func', ({2:2}, [])), ('continue', ), |
| 921 |
('line', 3, 'func', ({1:2}, [])), ('quit', ), |
| 922 |
] |
| 923 |
with TracerRun(self) as tracer: |
| 924 |
tracer.runcall(tfunc_import) |
| 925 |
|
| 926 |
def test_clear_two_bp_on_same_line(self): |
| 927 |
code = """ |
| 928 |
def func(): |
| 929 |
lno = 3 |
| 930 |
lno = 4 |
| 931 |
|
| 932 |
def main(): |
| 933 |
for i in range(3): |
| 934 |
func() |
| 935 |
""" |
| 936 |
modules = { TEST_MODULE: code } |
| 937 |
with create_modules(modules): |
| 938 |
self.expect_set = [ |
| 939 |
('line', 2, 'tfunc_import'), ('break', (TEST_MODULE_FNAME, 3)), |
| 940 |
('None', 2, 'tfunc_import'), ('break', (TEST_MODULE_FNAME, 3)), |
| 941 |
('None', 2, 'tfunc_import'), ('break', (TEST_MODULE_FNAME, 4)), |
| 942 |
('None', 2, 'tfunc_import'), ('continue', ), |
| 943 |
('line', 3, 'func', ({1:1}, [])), ('continue', ), |
| 944 |
('line', 4, 'func', ({3:1}, [])), ('clear', (TEST_MODULE_FNAME, 3)), |
| 945 |
('None', 4, 'func'), ('continue', ), |
| 946 |
('line', 4, 'func', ({3:2}, [])), ('quit', ), |
| 947 |
] |
| 948 |
with TracerRun(self) as tracer: |
| 949 |
tracer.runcall(tfunc_import) |
| 950 |
|
| 951 |
def test_clear_at_no_bp(self): |
| 952 |
self.expect_set = [ |
| 953 |
('line', 2, 'tfunc_import'), ('clear', (__file__, 1)) |
| 954 |
] |
| 955 |
with TracerRun(self) as tracer: |
| 956 |
self.assertRaises(BdbError, tracer.runcall, tfunc_import) |
| 957 |
|
| 958 |
class RunTestCase(BaseTestCase): |
| 959 |
"""Test run, runeval and set_trace.""" |
| 960 |
|
| 961 |
def test_run_step(self): |
| 962 |
|
| 963 |
code = """ |
| 964 |
lno = 2 |
| 965 |
""" |
| 966 |
self.expect_set = [ |
| 967 |
('line', 2, '<module>'), ('step', ), |
| 968 |
('return', 2, '<module>'), ('quit', ), |
| 969 |
] |
| 970 |
with TracerRun(self) as tracer: |
| 971 |
tracer.run(compile(textwrap.dedent(code), '<string>', 'exec')) |
| 972 |
|
| 973 |
def test_runeval_step(self): |
| 974 |
|
| 975 |
code = """ |
| 976 |
def main(): |
| 977 |
lno = 3 |
| 978 |
""" |
| 979 |
modules = { TEST_MODULE: code } |
| 980 |
with create_modules(modules): |
| 981 |
self.expect_set = [ |
| 982 |
('line', 1, '<module>'), ('step', ), |
| 983 |
('call', 2, 'main'), ('step', ), |
| 984 |
('line', 3, 'main'), ('step', ), |
| 985 |
('return', 3, 'main'), ('step', ), |
| 986 |
('return', 1, '<module>'), ('quit', ), |
| 987 |
] |
| 988 |
import test_module_for_bdb |
| 989 |
with TracerRun(self) as tracer: |
| 990 |
tracer.runeval('test_module_for_bdb.main()', globals(), locals()) |
| 991 |
|
| 992 |
class IssuesTestCase(BaseTestCase): |
| 993 |
"""Test fixed bdb issues.""" |
| 994 |
|
| 995 |
def test_step_at_return_with_no_trace_in_caller(self): |
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
code_1 = """ |
| 1000 |
from test_module_for_bdb_2 import func |
| 1001 |
def main(): |
| 1002 |
func() |
| 1003 |
lno = 5 |
| 1004 |
""" |
| 1005 |
code_2 = """ |
| 1006 |
def func(): |
| 1007 |
lno = 3 |
| 1008 |
""" |
| 1009 |
modules = { |
| 1010 |
TEST_MODULE: code_1, |
| 1011 |
'test_module_for_bdb_2': code_2, |
| 1012 |
} |
| 1013 |
with create_modules(modules): |
| 1014 |
self.expect_set = [ |
| 1015 |
('line', 2, 'tfunc_import'), |
| 1016 |
break_in_func('func', 'test_module_for_bdb_2.py'), |
| 1017 |
('None', 2, 'tfunc_import'), ('continue', ), |
| 1018 |
('line', 3, 'func', ({1:1}, [])), ('step', ), |
| 1019 |
('return', 3, 'func'), ('step', ), |
| 1020 |
('line', 5, 'main'), ('quit', ), |
| 1021 |
] |
| 1022 |
with TracerRun(self) as tracer: |
| 1023 |
tracer.runcall(tfunc_import) |
| 1024 |
|
| 1025 |
def test_main(): |
| 1026 |
test.support.run_unittest( |
| 1027 |
StateTestCase, |
| 1028 |
RunTestCase, |
| 1029 |
BreakpointTestCase, |
| 1030 |
IssuesTestCase, |
| 1031 |
) |
| 1032 |
|
| 1033 |
if __name__ == "__main__": |
| 1034 |
test_main() |