test_bdb.py 43 KB

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