debugger.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. # -*- coding: utf-8 -*-
  2. """
  3. Pdb debugger class.
  4. Modified from the standard pdb.Pdb class to avoid including readline, so that
  5. the command line completion of other programs which include this isn't
  6. damaged.
  7. In the future, this class will be expanded with improvements over the standard
  8. pdb.
  9. The code in this file is mainly lifted out of cmd.py in Python 2.2, with minor
  10. changes. Licensing should therefore be under the standard Python terms. For
  11. details on the PSF (Python Software Foundation) standard license, see:
  12. https://docs.python.org/2/license.html
  13. """
  14. #*****************************************************************************
  15. #
  16. # This file is licensed under the PSF license.
  17. #
  18. # Copyright (C) 2001 Python Software Foundation, www.python.org
  19. # Copyright (C) 2005-2006 Fernando Perez. <fperez@colorado.edu>
  20. #
  21. #
  22. #*****************************************************************************
  23. from __future__ import print_function
  24. import bdb
  25. import functools
  26. import inspect
  27. import sys
  28. import warnings
  29. from IPython import get_ipython
  30. from IPython.utils import PyColorize, ulinecache
  31. from IPython.utils import coloransi, py3compat
  32. from IPython.core.excolors import exception_colors
  33. from IPython.testing.skipdoctest import skip_doctest
  34. prompt = 'ipdb> '
  35. #We have to check this directly from sys.argv, config struct not yet available
  36. from pdb import Pdb as OldPdb
  37. # Allow the set_trace code to operate outside of an ipython instance, even if
  38. # it does so with some limitations. The rest of this support is implemented in
  39. # the Tracer constructor.
  40. def make_arrow(pad):
  41. """generate the leading arrow in front of traceback or debugger"""
  42. if pad >= 2:
  43. return '-'*(pad-2) + '> '
  44. elif pad == 1:
  45. return '>'
  46. return ''
  47. def BdbQuit_excepthook(et, ev, tb, excepthook=None):
  48. """Exception hook which handles `BdbQuit` exceptions.
  49. All other exceptions are processed using the `excepthook`
  50. parameter.
  51. """
  52. warnings.warn("`BdbQuit_excepthook` is deprecated since version 5.1",
  53. DeprecationWarning, stacklevel=2)
  54. if et==bdb.BdbQuit:
  55. print('Exiting Debugger.')
  56. elif excepthook is not None:
  57. excepthook(et, ev, tb)
  58. else:
  59. # Backwards compatibility. Raise deprecation warning?
  60. BdbQuit_excepthook.excepthook_ori(et,ev,tb)
  61. def BdbQuit_IPython_excepthook(self,et,ev,tb,tb_offset=None):
  62. warnings.warn(
  63. "`BdbQuit_IPython_excepthook` is deprecated since version 5.1",
  64. DeprecationWarning, stacklevel=2)
  65. print('Exiting Debugger.')
  66. class Tracer(object):
  67. """
  68. DEPRECATED
  69. Class for local debugging, similar to pdb.set_trace.
  70. Instances of this class, when called, behave like pdb.set_trace, but
  71. providing IPython's enhanced capabilities.
  72. This is implemented as a class which must be initialized in your own code
  73. and not as a standalone function because we need to detect at runtime
  74. whether IPython is already active or not. That detection is done in the
  75. constructor, ensuring that this code plays nicely with a running IPython,
  76. while functioning acceptably (though with limitations) if outside of it.
  77. """
  78. @skip_doctest
  79. def __init__(self, colors=None):
  80. """
  81. DEPRECATED
  82. Create a local debugger instance.
  83. Parameters
  84. ----------
  85. colors : str, optional
  86. The name of the color scheme to use, it must be one of IPython's
  87. valid color schemes. If not given, the function will default to
  88. the current IPython scheme when running inside IPython, and to
  89. 'NoColor' otherwise.
  90. Examples
  91. --------
  92. ::
  93. from IPython.core.debugger import Tracer; debug_here = Tracer()
  94. Later in your code::
  95. debug_here() # -> will open up the debugger at that point.
  96. Once the debugger activates, you can use all of its regular commands to
  97. step through code, set breakpoints, etc. See the pdb documentation
  98. from the Python standard library for usage details.
  99. """
  100. warnings.warn("`Tracer` is deprecated since version 5.1, directly use "
  101. "`IPython.core.debugger.Pdb.set_trace()`",
  102. DeprecationWarning, stacklevel=2)
  103. ip = get_ipython()
  104. if ip is None:
  105. # Outside of ipython, we set our own exception hook manually
  106. sys.excepthook = functools.partial(BdbQuit_excepthook,
  107. excepthook=sys.excepthook)
  108. def_colors = 'NoColor'
  109. else:
  110. # In ipython, we use its custom exception handler mechanism
  111. def_colors = ip.colors
  112. ip.set_custom_exc((bdb.BdbQuit,), BdbQuit_IPython_excepthook)
  113. if colors is None:
  114. colors = def_colors
  115. # The stdlib debugger internally uses a modified repr from the `repr`
  116. # module, that limits the length of printed strings to a hardcoded
  117. # limit of 30 characters. That much trimming is too aggressive, let's
  118. # at least raise that limit to 80 chars, which should be enough for
  119. # most interactive uses.
  120. try:
  121. try:
  122. from reprlib import aRepr # Py 3
  123. except ImportError:
  124. from repr import aRepr # Py 2
  125. aRepr.maxstring = 80
  126. except:
  127. # This is only a user-facing convenience, so any error we encounter
  128. # here can be warned about but can be otherwise ignored. These
  129. # printouts will tell us about problems if this API changes
  130. import traceback
  131. traceback.print_exc()
  132. self.debugger = Pdb(colors)
  133. def __call__(self):
  134. """Starts an interactive debugger at the point where called.
  135. This is similar to the pdb.set_trace() function from the std lib, but
  136. using IPython's enhanced debugger."""
  137. self.debugger.set_trace(sys._getframe().f_back)
  138. def decorate_fn_with_doc(new_fn, old_fn, additional_text=""):
  139. """Make new_fn have old_fn's doc string. This is particularly useful
  140. for the ``do_...`` commands that hook into the help system.
  141. Adapted from from a comp.lang.python posting
  142. by Duncan Booth."""
  143. def wrapper(*args, **kw):
  144. return new_fn(*args, **kw)
  145. if old_fn.__doc__:
  146. wrapper.__doc__ = old_fn.__doc__ + additional_text
  147. return wrapper
  148. def _file_lines(fname):
  149. """Return the contents of a named file as a list of lines.
  150. This function never raises an IOError exception: if the file can't be
  151. read, it simply returns an empty list."""
  152. try:
  153. outfile = open(fname)
  154. except IOError:
  155. return []
  156. else:
  157. out = outfile.readlines()
  158. outfile.close()
  159. return out
  160. class Pdb(OldPdb):
  161. """Modified Pdb class, does not load readline.
  162. for a standalone version that uses prompt_toolkit, see
  163. `IPython.terminal.debugger.TerminalPdb` and
  164. `IPython.terminal.debugger.set_trace()`
  165. """
  166. def __init__(self, color_scheme=None, completekey=None,
  167. stdin=None, stdout=None, context=5):
  168. # Parent constructor:
  169. try:
  170. self.context = int(context)
  171. if self.context <= 0:
  172. raise ValueError("Context must be a positive integer")
  173. except (TypeError, ValueError):
  174. raise ValueError("Context must be a positive integer")
  175. OldPdb.__init__(self, completekey, stdin, stdout)
  176. # IPython changes...
  177. self.shell = get_ipython()
  178. if self.shell is None:
  179. save_main = sys.modules['__main__']
  180. # No IPython instance running, we must create one
  181. from IPython.terminal.interactiveshell import \
  182. TerminalInteractiveShell
  183. self.shell = TerminalInteractiveShell.instance()
  184. # needed by any code which calls __import__("__main__") after
  185. # the debugger was entered. See also #9941.
  186. sys.modules['__main__'] = save_main
  187. if color_scheme is not None:
  188. warnings.warn(
  189. "The `color_scheme` argument is deprecated since version 5.1",
  190. DeprecationWarning)
  191. else:
  192. color_scheme = self.shell.colors
  193. self.aliases = {}
  194. # Create color table: we copy the default one from the traceback
  195. # module and add a few attributes needed for debugging
  196. self.color_scheme_table = exception_colors()
  197. # shorthands
  198. C = coloransi.TermColors
  199. cst = self.color_scheme_table
  200. cst['NoColor'].colors.prompt = C.NoColor
  201. cst['NoColor'].colors.breakpoint_enabled = C.NoColor
  202. cst['NoColor'].colors.breakpoint_disabled = C.NoColor
  203. cst['Linux'].colors.prompt = C.Green
  204. cst['Linux'].colors.breakpoint_enabled = C.LightRed
  205. cst['Linux'].colors.breakpoint_disabled = C.Red
  206. cst['LightBG'].colors.prompt = C.Blue
  207. cst['LightBG'].colors.breakpoint_enabled = C.LightRed
  208. cst['LightBG'].colors.breakpoint_disabled = C.Red
  209. cst['Neutral'].colors.prompt = C.Blue
  210. cst['Neutral'].colors.breakpoint_enabled = C.LightRed
  211. cst['Neutral'].colors.breakpoint_disabled = C.Red
  212. self.set_colors(color_scheme)
  213. # Add a python parser so we can syntax highlight source while
  214. # debugging.
  215. self.parser = PyColorize.Parser()
  216. # Set the prompt - the default prompt is '(Pdb)'
  217. self.prompt = prompt
  218. def set_colors(self, scheme):
  219. """Shorthand access to the color table scheme selector method."""
  220. self.color_scheme_table.set_active_scheme(scheme)
  221. def interaction(self, frame, traceback):
  222. try:
  223. OldPdb.interaction(self, frame, traceback)
  224. except KeyboardInterrupt:
  225. sys.stdout.write('\n' + self.shell.get_exception_only())
  226. def new_do_up(self, arg):
  227. OldPdb.do_up(self, arg)
  228. do_u = do_up = decorate_fn_with_doc(new_do_up, OldPdb.do_up)
  229. def new_do_down(self, arg):
  230. OldPdb.do_down(self, arg)
  231. do_d = do_down = decorate_fn_with_doc(new_do_down, OldPdb.do_down)
  232. def new_do_frame(self, arg):
  233. OldPdb.do_frame(self, arg)
  234. def new_do_quit(self, arg):
  235. if hasattr(self, 'old_all_completions'):
  236. self.shell.Completer.all_completions=self.old_all_completions
  237. return OldPdb.do_quit(self, arg)
  238. do_q = do_quit = decorate_fn_with_doc(new_do_quit, OldPdb.do_quit)
  239. def new_do_restart(self, arg):
  240. """Restart command. In the context of ipython this is exactly the same
  241. thing as 'quit'."""
  242. self.msg("Restart doesn't make sense here. Using 'quit' instead.")
  243. return self.do_quit(arg)
  244. def print_stack_trace(self, context=None):
  245. if context is None:
  246. context = self.context
  247. try:
  248. context=int(context)
  249. if context <= 0:
  250. raise ValueError("Context must be a positive integer")
  251. except (TypeError, ValueError):
  252. raise ValueError("Context must be a positive integer")
  253. try:
  254. for frame_lineno in self.stack:
  255. self.print_stack_entry(frame_lineno, context=context)
  256. except KeyboardInterrupt:
  257. pass
  258. def print_stack_entry(self,frame_lineno, prompt_prefix='\n-> ',
  259. context=None):
  260. if context is None:
  261. context = self.context
  262. try:
  263. context=int(context)
  264. if context <= 0:
  265. raise ValueError("Context must be a positive integer")
  266. except (TypeError, ValueError):
  267. raise ValueError("Context must be a positive integer")
  268. print(self.format_stack_entry(frame_lineno, '', context))
  269. # vds: >>
  270. frame, lineno = frame_lineno
  271. filename = frame.f_code.co_filename
  272. self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
  273. # vds: <<
  274. def format_stack_entry(self, frame_lineno, lprefix=': ', context=None):
  275. if context is None:
  276. context = self.context
  277. try:
  278. context=int(context)
  279. if context <= 0:
  280. print("Context must be a positive integer")
  281. except (TypeError, ValueError):
  282. print("Context must be a positive integer")
  283. try:
  284. import reprlib # Py 3
  285. except ImportError:
  286. import repr as reprlib # Py 2
  287. ret = []
  288. Colors = self.color_scheme_table.active_colors
  289. ColorsNormal = Colors.Normal
  290. tpl_link = u'%s%%s%s' % (Colors.filenameEm, ColorsNormal)
  291. tpl_call = u'%s%%s%s%%s%s' % (Colors.vName, Colors.valEm, ColorsNormal)
  292. tpl_line = u'%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
  293. tpl_line_em = u'%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line,
  294. ColorsNormal)
  295. frame, lineno = frame_lineno
  296. return_value = ''
  297. if '__return__' in frame.f_locals:
  298. rv = frame.f_locals['__return__']
  299. #return_value += '->'
  300. return_value += reprlib.repr(rv) + '\n'
  301. ret.append(return_value)
  302. #s = filename + '(' + `lineno` + ')'
  303. filename = self.canonic(frame.f_code.co_filename)
  304. link = tpl_link % py3compat.cast_unicode(filename)
  305. if frame.f_code.co_name:
  306. func = frame.f_code.co_name
  307. else:
  308. func = "<lambda>"
  309. call = ''
  310. if func != '?':
  311. if '__args__' in frame.f_locals:
  312. args = reprlib.repr(frame.f_locals['__args__'])
  313. else:
  314. args = '()'
  315. call = tpl_call % (func, args)
  316. # The level info should be generated in the same format pdb uses, to
  317. # avoid breaking the pdbtrack functionality of python-mode in *emacs.
  318. if frame is self.curframe:
  319. ret.append('> ')
  320. else:
  321. ret.append(' ')
  322. ret.append(u'%s(%s)%s\n' % (link,lineno,call))
  323. start = lineno - 1 - context//2
  324. lines = ulinecache.getlines(filename, frame.f_globals)
  325. start = min(start, len(lines) - context)
  326. start = max(start, 0)
  327. lines = lines[start : start + context]
  328. for i,line in enumerate(lines):
  329. show_arrow = (start + 1 + i == lineno)
  330. linetpl = (frame is self.curframe or show_arrow) \
  331. and tpl_line_em \
  332. or tpl_line
  333. ret.append(self.__format_line(linetpl, filename,
  334. start + 1 + i, line,
  335. arrow = show_arrow) )
  336. return ''.join(ret)
  337. def __format_line(self, tpl_line, filename, lineno, line, arrow = False):
  338. bp_mark = ""
  339. bp_mark_color = ""
  340. scheme = self.color_scheme_table.active_scheme_name
  341. new_line, err = self.parser.format2(line, 'str', scheme)
  342. if not err: line = new_line
  343. bp = None
  344. if lineno in self.get_file_breaks(filename):
  345. bps = self.get_breaks(filename, lineno)
  346. bp = bps[-1]
  347. if bp:
  348. Colors = self.color_scheme_table.active_colors
  349. bp_mark = str(bp.number)
  350. bp_mark_color = Colors.breakpoint_enabled
  351. if not bp.enabled:
  352. bp_mark_color = Colors.breakpoint_disabled
  353. numbers_width = 7
  354. if arrow:
  355. # This is the line with the error
  356. pad = numbers_width - len(str(lineno)) - len(bp_mark)
  357. num = '%s%s' % (make_arrow(pad), str(lineno))
  358. else:
  359. num = '%*s' % (numbers_width - len(bp_mark), str(lineno))
  360. return tpl_line % (bp_mark_color + bp_mark, num, line)
  361. def print_list_lines(self, filename, first, last):
  362. """The printing (as opposed to the parsing part of a 'list'
  363. command."""
  364. try:
  365. Colors = self.color_scheme_table.active_colors
  366. ColorsNormal = Colors.Normal
  367. tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
  368. tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal)
  369. src = []
  370. if filename == "<string>" and hasattr(self, "_exec_filename"):
  371. filename = self._exec_filename
  372. for lineno in range(first, last+1):
  373. line = ulinecache.getline(filename, lineno)
  374. if not line:
  375. break
  376. if lineno == self.curframe.f_lineno:
  377. line = self.__format_line(tpl_line_em, filename, lineno, line, arrow = True)
  378. else:
  379. line = self.__format_line(tpl_line, filename, lineno, line, arrow = False)
  380. src.append(line)
  381. self.lineno = lineno
  382. print(''.join(src))
  383. except KeyboardInterrupt:
  384. pass
  385. def do_list(self, arg):
  386. """Print lines of code from the current stack frame
  387. """
  388. self.lastcmd = 'list'
  389. last = None
  390. if arg:
  391. try:
  392. x = eval(arg, {}, {})
  393. if type(x) == type(()):
  394. first, last = x
  395. first = int(first)
  396. last = int(last)
  397. if last < first:
  398. # Assume it's a count
  399. last = first + last
  400. else:
  401. first = max(1, int(x) - 5)
  402. except:
  403. print('*** Error in argument:', repr(arg))
  404. return
  405. elif self.lineno is None:
  406. first = max(1, self.curframe.f_lineno - 5)
  407. else:
  408. first = self.lineno + 1
  409. if last is None:
  410. last = first + 10
  411. self.print_list_lines(self.curframe.f_code.co_filename, first, last)
  412. # vds: >>
  413. lineno = first
  414. filename = self.curframe.f_code.co_filename
  415. self.shell.hooks.synchronize_with_editor(filename, lineno, 0)
  416. # vds: <<
  417. do_l = do_list
  418. def getsourcelines(self, obj):
  419. lines, lineno = inspect.findsource(obj)
  420. if inspect.isframe(obj) and obj.f_globals is obj.f_locals:
  421. # must be a module frame: do not try to cut a block out of it
  422. return lines, 1
  423. elif inspect.ismodule(obj):
  424. return lines, 1
  425. return inspect.getblock(lines[lineno:]), lineno+1
  426. def do_longlist(self, arg):
  427. """Print lines of code from the current stack frame.
  428. Shows more lines than 'list' does.
  429. """
  430. self.lastcmd = 'longlist'
  431. try:
  432. lines, lineno = self.getsourcelines(self.curframe)
  433. except OSError as err:
  434. self.error(err)
  435. return
  436. last = lineno + len(lines)
  437. self.print_list_lines(self.curframe.f_code.co_filename, lineno, last)
  438. do_ll = do_longlist
  439. def do_pdef(self, arg):
  440. """Print the call signature for any callable object.
  441. The debugger interface to %pdef"""
  442. namespaces = [('Locals', self.curframe.f_locals),
  443. ('Globals', self.curframe.f_globals)]
  444. self.shell.find_line_magic('pdef')(arg, namespaces=namespaces)
  445. def do_pdoc(self, arg):
  446. """Print the docstring for an object.
  447. The debugger interface to %pdoc."""
  448. namespaces = [('Locals', self.curframe.f_locals),
  449. ('Globals', self.curframe.f_globals)]
  450. self.shell.find_line_magic('pdoc')(arg, namespaces=namespaces)
  451. def do_pfile(self, arg):
  452. """Print (or run through pager) the file where an object is defined.
  453. The debugger interface to %pfile.
  454. """
  455. namespaces = [('Locals', self.curframe.f_locals),
  456. ('Globals', self.curframe.f_globals)]
  457. self.shell.find_line_magic('pfile')(arg, namespaces=namespaces)
  458. def do_pinfo(self, arg):
  459. """Provide detailed information about an object.
  460. The debugger interface to %pinfo, i.e., obj?."""
  461. namespaces = [('Locals', self.curframe.f_locals),
  462. ('Globals', self.curframe.f_globals)]
  463. self.shell.find_line_magic('pinfo')(arg, namespaces=namespaces)
  464. def do_pinfo2(self, arg):
  465. """Provide extra detailed information about an object.
  466. The debugger interface to %pinfo2, i.e., obj??."""
  467. namespaces = [('Locals', self.curframe.f_locals),
  468. ('Globals', self.curframe.f_globals)]
  469. self.shell.find_line_magic('pinfo2')(arg, namespaces=namespaces)
  470. def do_psource(self, arg):
  471. """Print (or run through pager) the source code for an object."""
  472. namespaces = [('Locals', self.curframe.f_locals),
  473. ('Globals', self.curframe.f_globals)]
  474. self.shell.find_line_magic('psource')(arg, namespaces=namespaces)
  475. if sys.version_info > (3, ):
  476. def do_where(self, arg):
  477. """w(here)
  478. Print a stack trace, with the most recent frame at the bottom.
  479. An arrow indicates the "current frame", which determines the
  480. context of most commands. 'bt' is an alias for this command.
  481. Take a number as argument as an (optional) number of context line to
  482. print"""
  483. if arg:
  484. context = int(arg)
  485. self.print_stack_trace(context)
  486. else:
  487. self.print_stack_trace()
  488. do_w = do_where
  489. def set_trace(frame=None):
  490. """
  491. Start debugging from `frame`.
  492. If frame is not specified, debugging starts from caller's frame.
  493. """
  494. Pdb().set_trace(frame or sys._getframe().f_back)