ultratb.py 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  1. # -*- coding: utf-8 -*-
  2. """
  3. Verbose and colourful traceback formatting.
  4. **ColorTB**
  5. I've always found it a bit hard to visually parse tracebacks in Python. The
  6. ColorTB class is a solution to that problem. It colors the different parts of a
  7. traceback in a manner similar to what you would expect from a syntax-highlighting
  8. text editor.
  9. Installation instructions for ColorTB::
  10. import sys,ultratb
  11. sys.excepthook = ultratb.ColorTB()
  12. **VerboseTB**
  13. I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
  14. of useful info when a traceback occurs. Ping originally had it spit out HTML
  15. and intended it for CGI programmers, but why should they have all the fun? I
  16. altered it to spit out colored text to the terminal. It's a bit overwhelming,
  17. but kind of neat, and maybe useful for long-running programs that you believe
  18. are bug-free. If a crash *does* occur in that type of program you want details.
  19. Give it a shot--you'll love it or you'll hate it.
  20. .. note::
  21. The Verbose mode prints the variables currently visible where the exception
  22. happened (shortening their strings if too long). This can potentially be
  23. very slow, if you happen to have a huge data structure whose string
  24. representation is complex to compute. Your computer may appear to freeze for
  25. a while with cpu usage at 100%. If this occurs, you can cancel the traceback
  26. with Ctrl-C (maybe hitting it more than once).
  27. If you encounter this kind of situation often, you may want to use the
  28. Verbose_novars mode instead of the regular Verbose, which avoids formatting
  29. variables (but otherwise includes the information and context given by
  30. Verbose).
  31. .. note::
  32. The verbose mode print all variables in the stack, which means it can
  33. potentially leak sensitive information like access keys, or unencryted
  34. password.
  35. Installation instructions for VerboseTB::
  36. import sys,ultratb
  37. sys.excepthook = ultratb.VerboseTB()
  38. Note: Much of the code in this module was lifted verbatim from the standard
  39. library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
  40. Color schemes
  41. -------------
  42. The colors are defined in the class TBTools through the use of the
  43. ColorSchemeTable class. Currently the following exist:
  44. - NoColor: allows all of this module to be used in any terminal (the color
  45. escapes are just dummy blank strings).
  46. - Linux: is meant to look good in a terminal like the Linux console (black
  47. or very dark background).
  48. - LightBG: similar to Linux but swaps dark/light colors to be more readable
  49. in light background terminals.
  50. - Neutral: a neutral color scheme that should be readable on both light and
  51. dark background
  52. You can implement other color schemes easily, the syntax is fairly
  53. self-explanatory. Please send back new schemes you develop to the author for
  54. possible inclusion in future releases.
  55. Inheritance diagram:
  56. .. inheritance-diagram:: IPython.core.ultratb
  57. :parts: 3
  58. """
  59. #*****************************************************************************
  60. # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
  61. # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
  62. #
  63. # Distributed under the terms of the BSD License. The full license is in
  64. # the file COPYING, distributed as part of this software.
  65. #*****************************************************************************
  66. from __future__ import absolute_import
  67. from __future__ import unicode_literals
  68. from __future__ import print_function
  69. import dis
  70. import inspect
  71. import keyword
  72. import linecache
  73. import os
  74. import pydoc
  75. import re
  76. import sys
  77. import time
  78. import tokenize
  79. import traceback
  80. import types
  81. try: # Python 2
  82. generate_tokens = tokenize.generate_tokens
  83. except AttributeError: # Python 3
  84. generate_tokens = tokenize.tokenize
  85. # For purposes of monkeypatching inspect to fix a bug in it.
  86. from inspect import getsourcefile, getfile, getmodule, \
  87. ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode
  88. # IPython's own modules
  89. from IPython import get_ipython
  90. from IPython.core import debugger
  91. from IPython.core.display_trap import DisplayTrap
  92. from IPython.core.excolors import exception_colors
  93. from IPython.utils import PyColorize
  94. from IPython.utils import openpy
  95. from IPython.utils import path as util_path
  96. from IPython.utils import py3compat
  97. from IPython.utils import ulinecache
  98. from IPython.utils.data import uniq_stable
  99. from IPython.utils.terminal import get_terminal_size
  100. from logging import info, error
  101. import IPython.utils.colorable as colorable
  102. # Globals
  103. # amount of space to put line numbers before verbose tracebacks
  104. INDENT_SIZE = 8
  105. # Default color scheme. This is used, for example, by the traceback
  106. # formatter. When running in an actual IPython instance, the user's rc.colors
  107. # value is used, but having a module global makes this functionality available
  108. # to users of ultratb who are NOT running inside ipython.
  109. DEFAULT_SCHEME = 'NoColor'
  110. # ---------------------------------------------------------------------------
  111. # Code begins
  112. # Utility functions
  113. def inspect_error():
  114. """Print a message about internal inspect errors.
  115. These are unfortunately quite common."""
  116. error('Internal Python error in the inspect module.\n'
  117. 'Below is the traceback from this internal error.\n')
  118. # This function is a monkeypatch we apply to the Python inspect module. We have
  119. # now found when it's needed (see discussion on issue gh-1456), and we have a
  120. # test case (IPython.core.tests.test_ultratb.ChangedPyFileTest) that fails if
  121. # the monkeypatch is not applied. TK, Aug 2012.
  122. def findsource(object):
  123. """Return the entire source file and starting line number for an object.
  124. The argument may be a module, class, method, function, traceback, frame,
  125. or code object. The source code is returned as a list of all the lines
  126. in the file and the line number indexes a line in that list. An IOError
  127. is raised if the source code cannot be retrieved.
  128. FIXED version with which we monkeypatch the stdlib to work around a bug."""
  129. file = getsourcefile(object) or getfile(object)
  130. # If the object is a frame, then trying to get the globals dict from its
  131. # module won't work. Instead, the frame object itself has the globals
  132. # dictionary.
  133. globals_dict = None
  134. if inspect.isframe(object):
  135. # XXX: can this ever be false?
  136. globals_dict = object.f_globals
  137. else:
  138. module = getmodule(object, file)
  139. if module:
  140. globals_dict = module.__dict__
  141. lines = linecache.getlines(file, globals_dict)
  142. if not lines:
  143. raise IOError('could not get source code')
  144. if ismodule(object):
  145. return lines, 0
  146. if isclass(object):
  147. name = object.__name__
  148. pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
  149. # make some effort to find the best matching class definition:
  150. # use the one with the least indentation, which is the one
  151. # that's most probably not inside a function definition.
  152. candidates = []
  153. for i in range(len(lines)):
  154. match = pat.match(lines[i])
  155. if match:
  156. # if it's at toplevel, it's already the best one
  157. if lines[i][0] == 'c':
  158. return lines, i
  159. # else add whitespace to candidate list
  160. candidates.append((match.group(1), i))
  161. if candidates:
  162. # this will sort by whitespace, and by line number,
  163. # less whitespace first
  164. candidates.sort()
  165. return lines, candidates[0][1]
  166. else:
  167. raise IOError('could not find class definition')
  168. if ismethod(object):
  169. object = object.__func__
  170. if isfunction(object):
  171. object = object.__code__
  172. if istraceback(object):
  173. object = object.tb_frame
  174. if isframe(object):
  175. object = object.f_code
  176. if iscode(object):
  177. if not hasattr(object, 'co_firstlineno'):
  178. raise IOError('could not find function definition')
  179. pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
  180. pmatch = pat.match
  181. # fperez - fix: sometimes, co_firstlineno can give a number larger than
  182. # the length of lines, which causes an error. Safeguard against that.
  183. lnum = min(object.co_firstlineno, len(lines)) - 1
  184. while lnum > 0:
  185. if pmatch(lines[lnum]):
  186. break
  187. lnum -= 1
  188. return lines, lnum
  189. raise IOError('could not find code object')
  190. # This is a patched version of inspect.getargs that applies the (unmerged)
  191. # patch for http://bugs.python.org/issue14611 by Stefano Taschini. This fixes
  192. # https://github.com/ipython/ipython/issues/8205 and
  193. # https://github.com/ipython/ipython/issues/8293
  194. def getargs(co):
  195. """Get information about the arguments accepted by a code object.
  196. Three things are returned: (args, varargs, varkw), where 'args' is
  197. a list of argument names (possibly containing nested lists), and
  198. 'varargs' and 'varkw' are the names of the * and ** arguments or None."""
  199. if not iscode(co):
  200. raise TypeError('{!r} is not a code object'.format(co))
  201. nargs = co.co_argcount
  202. names = co.co_varnames
  203. args = list(names[:nargs])
  204. step = 0
  205. # The following acrobatics are for anonymous (tuple) arguments.
  206. for i in range(nargs):
  207. if args[i][:1] in ('', '.'):
  208. stack, remain, count = [], [], []
  209. while step < len(co.co_code):
  210. op = ord(co.co_code[step])
  211. step = step + 1
  212. if op >= dis.HAVE_ARGUMENT:
  213. opname = dis.opname[op]
  214. value = ord(co.co_code[step]) + ord(co.co_code[step+1])*256
  215. step = step + 2
  216. if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'):
  217. remain.append(value)
  218. count.append(value)
  219. elif opname in ('STORE_FAST', 'STORE_DEREF'):
  220. if op in dis.haslocal:
  221. stack.append(co.co_varnames[value])
  222. elif op in dis.hasfree:
  223. stack.append((co.co_cellvars + co.co_freevars)[value])
  224. # Special case for sublists of length 1: def foo((bar))
  225. # doesn't generate the UNPACK_TUPLE bytecode, so if
  226. # `remain` is empty here, we have such a sublist.
  227. if not remain:
  228. stack[0] = [stack[0]]
  229. break
  230. else:
  231. remain[-1] = remain[-1] - 1
  232. while remain[-1] == 0:
  233. remain.pop()
  234. size = count.pop()
  235. stack[-size:] = [stack[-size:]]
  236. if not remain:
  237. break
  238. remain[-1] = remain[-1] - 1
  239. if not remain:
  240. break
  241. args[i] = stack[0]
  242. varargs = None
  243. if co.co_flags & inspect.CO_VARARGS:
  244. varargs = co.co_varnames[nargs]
  245. nargs = nargs + 1
  246. varkw = None
  247. if co.co_flags & inspect.CO_VARKEYWORDS:
  248. varkw = co.co_varnames[nargs]
  249. return inspect.Arguments(args, varargs, varkw)
  250. # Monkeypatch inspect to apply our bugfix.
  251. def with_patch_inspect(f):
  252. """decorator for monkeypatching inspect.findsource"""
  253. def wrapped(*args, **kwargs):
  254. save_findsource = inspect.findsource
  255. save_getargs = inspect.getargs
  256. inspect.findsource = findsource
  257. inspect.getargs = getargs
  258. try:
  259. return f(*args, **kwargs)
  260. finally:
  261. inspect.findsource = save_findsource
  262. inspect.getargs = save_getargs
  263. return wrapped
  264. if py3compat.PY3:
  265. fixed_getargvalues = inspect.getargvalues
  266. else:
  267. # Fixes for https://github.com/ipython/ipython/issues/8293
  268. # and https://github.com/ipython/ipython/issues/8205.
  269. # The relevant bug is caused by failure to correctly handle anonymous tuple
  270. # unpacking, which only exists in Python 2.
  271. fixed_getargvalues = with_patch_inspect(inspect.getargvalues)
  272. def fix_frame_records_filenames(records):
  273. """Try to fix the filenames in each record from inspect.getinnerframes().
  274. Particularly, modules loaded from within zip files have useless filenames
  275. attached to their code object, and inspect.getinnerframes() just uses it.
  276. """
  277. fixed_records = []
  278. for frame, filename, line_no, func_name, lines, index in records:
  279. # Look inside the frame's globals dictionary for __file__,
  280. # which should be better. However, keep Cython filenames since
  281. # we prefer the source filenames over the compiled .so file.
  282. filename = py3compat.cast_unicode_py2(filename, "utf-8")
  283. if not filename.endswith(('.pyx', '.pxd', '.pxi')):
  284. better_fn = frame.f_globals.get('__file__', None)
  285. if isinstance(better_fn, str):
  286. # Check the type just in case someone did something weird with
  287. # __file__. It might also be None if the error occurred during
  288. # import.
  289. filename = better_fn
  290. fixed_records.append((frame, filename, line_no, func_name, lines, index))
  291. return fixed_records
  292. @with_patch_inspect
  293. def _fixed_getinnerframes(etb, context=1, tb_offset=0):
  294. LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5
  295. records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
  296. # If the error is at the console, don't build any context, since it would
  297. # otherwise produce 5 blank lines printed out (there is no file at the
  298. # console)
  299. rec_check = records[tb_offset:]
  300. try:
  301. rname = rec_check[0][1]
  302. if rname == '<ipython console>' or rname.endswith('<string>'):
  303. return rec_check
  304. except IndexError:
  305. pass
  306. aux = traceback.extract_tb(etb)
  307. assert len(records) == len(aux)
  308. for i, (file, lnum, _, _) in zip(range(len(records)), aux):
  309. maybeStart = lnum - 1 - context // 2
  310. start = max(maybeStart, 0)
  311. end = start + context
  312. lines = ulinecache.getlines(file)[start:end]
  313. buf = list(records[i])
  314. buf[LNUM_POS] = lnum
  315. buf[INDEX_POS] = lnum - 1 - start
  316. buf[LINES_POS] = lines
  317. records[i] = tuple(buf)
  318. return records[tb_offset:]
  319. # Helper function -- largely belongs to VerboseTB, but we need the same
  320. # functionality to produce a pseudo verbose TB for SyntaxErrors, so that they
  321. # can be recognized properly by ipython.el's py-traceback-line-re
  322. # (SyntaxErrors have to be treated specially because they have no traceback)
  323. _parser = PyColorize.Parser()
  324. def _format_traceback_lines(lnum, index, lines, Colors, lvals=None, scheme=None):
  325. numbers_width = INDENT_SIZE - 1
  326. res = []
  327. i = lnum - index
  328. # This lets us get fully syntax-highlighted tracebacks.
  329. if scheme is None:
  330. ipinst = get_ipython()
  331. if ipinst is not None:
  332. scheme = ipinst.colors
  333. else:
  334. scheme = DEFAULT_SCHEME
  335. _line_format = _parser.format2
  336. for line in lines:
  337. line = py3compat.cast_unicode(line)
  338. new_line, err = _line_format(line, 'str', scheme)
  339. if not err: line = new_line
  340. if i == lnum:
  341. # This is the line with the error
  342. pad = numbers_width - len(str(i))
  343. num = '%s%s' % (debugger.make_arrow(pad), str(lnum))
  344. line = '%s%s%s %s%s' % (Colors.linenoEm, num,
  345. Colors.line, line, Colors.Normal)
  346. else:
  347. num = '%*s' % (numbers_width, i)
  348. line = '%s%s%s %s' % (Colors.lineno, num,
  349. Colors.Normal, line)
  350. res.append(line)
  351. if lvals and i == lnum:
  352. res.append(lvals + '\n')
  353. i = i + 1
  354. return res
  355. def is_recursion_error(etype, value, records):
  356. try:
  357. # RecursionError is new in Python 3.5
  358. recursion_error_type = RecursionError
  359. except NameError:
  360. recursion_error_type = RuntimeError
  361. # The default recursion limit is 1000, but some of that will be taken up
  362. # by stack frames in IPython itself. >500 frames probably indicates
  363. # a recursion error.
  364. return (etype is recursion_error_type) \
  365. and str("recursion") in str(value).lower() \
  366. and len(records) > 500
  367. def find_recursion(etype, value, records):
  368. """Identify the repeating stack frames from a RecursionError traceback
  369. 'records' is a list as returned by VerboseTB.get_records()
  370. Returns (last_unique, repeat_length)
  371. """
  372. # This involves a bit of guesswork - we want to show enough of the traceback
  373. # to indicate where the recursion is occurring. We guess that the innermost
  374. # quarter of the traceback (250 frames by default) is repeats, and find the
  375. # first frame (from in to out) that looks different.
  376. if not is_recursion_error(etype, value, records):
  377. return len(records), 0
  378. # Select filename, lineno, func_name to track frames with
  379. records = [r[1:4] for r in records]
  380. inner_frames = records[-(len(records)//4):]
  381. frames_repeated = set(inner_frames)
  382. last_seen_at = {}
  383. longest_repeat = 0
  384. i = len(records)
  385. for frame in reversed(records):
  386. i -= 1
  387. if frame not in frames_repeated:
  388. last_unique = i
  389. break
  390. if frame in last_seen_at:
  391. distance = last_seen_at[frame] - i
  392. longest_repeat = max(longest_repeat, distance)
  393. last_seen_at[frame] = i
  394. else:
  395. last_unique = 0 # The whole traceback was recursion
  396. return last_unique, longest_repeat
  397. #---------------------------------------------------------------------------
  398. # Module classes
  399. class TBTools(colorable.Colorable):
  400. """Basic tools used by all traceback printer classes."""
  401. # Number of frames to skip when reporting tracebacks
  402. tb_offset = 0
  403. def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None):
  404. # Whether to call the interactive pdb debugger after printing
  405. # tracebacks or not
  406. super(TBTools, self).__init__(parent=parent, config=config)
  407. self.call_pdb = call_pdb
  408. # Output stream to write to. Note that we store the original value in
  409. # a private attribute and then make the public ostream a property, so
  410. # that we can delay accessing sys.stdout until runtime. The way
  411. # things are written now, the sys.stdout object is dynamically managed
  412. # so a reference to it should NEVER be stored statically. This
  413. # property approach confines this detail to a single location, and all
  414. # subclasses can simply access self.ostream for writing.
  415. self._ostream = ostream
  416. # Create color table
  417. self.color_scheme_table = exception_colors()
  418. self.set_colors(color_scheme)
  419. self.old_scheme = color_scheme # save initial value for toggles
  420. if call_pdb:
  421. self.pdb = debugger.Pdb()
  422. else:
  423. self.pdb = None
  424. def _get_ostream(self):
  425. """Output stream that exceptions are written to.
  426. Valid values are:
  427. - None: the default, which means that IPython will dynamically resolve
  428. to sys.stdout. This ensures compatibility with most tools, including
  429. Windows (where plain stdout doesn't recognize ANSI escapes).
  430. - Any object with 'write' and 'flush' attributes.
  431. """
  432. return sys.stdout if self._ostream is None else self._ostream
  433. def _set_ostream(self, val):
  434. assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush'))
  435. self._ostream = val
  436. ostream = property(_get_ostream, _set_ostream)
  437. def set_colors(self, *args, **kw):
  438. """Shorthand access to the color table scheme selector method."""
  439. # Set own color table
  440. self.color_scheme_table.set_active_scheme(*args, **kw)
  441. # for convenience, set Colors to the active scheme
  442. self.Colors = self.color_scheme_table.active_colors
  443. # Also set colors of debugger
  444. if hasattr(self, 'pdb') and self.pdb is not None:
  445. self.pdb.set_colors(*args, **kw)
  446. def color_toggle(self):
  447. """Toggle between the currently active color scheme and NoColor."""
  448. if self.color_scheme_table.active_scheme_name == 'NoColor':
  449. self.color_scheme_table.set_active_scheme(self.old_scheme)
  450. self.Colors = self.color_scheme_table.active_colors
  451. else:
  452. self.old_scheme = self.color_scheme_table.active_scheme_name
  453. self.color_scheme_table.set_active_scheme('NoColor')
  454. self.Colors = self.color_scheme_table.active_colors
  455. def stb2text(self, stb):
  456. """Convert a structured traceback (a list) to a string."""
  457. return '\n'.join(stb)
  458. def text(self, etype, value, tb, tb_offset=None, context=5):
  459. """Return formatted traceback.
  460. Subclasses may override this if they add extra arguments.
  461. """
  462. tb_list = self.structured_traceback(etype, value, tb,
  463. tb_offset, context)
  464. return self.stb2text(tb_list)
  465. def structured_traceback(self, etype, evalue, tb, tb_offset=None,
  466. context=5, mode=None):
  467. """Return a list of traceback frames.
  468. Must be implemented by each class.
  469. """
  470. raise NotImplementedError()
  471. #---------------------------------------------------------------------------
  472. class ListTB(TBTools):
  473. """Print traceback information from a traceback list, with optional color.
  474. Calling requires 3 arguments: (etype, evalue, elist)
  475. as would be obtained by::
  476. etype, evalue, tb = sys.exc_info()
  477. if tb:
  478. elist = traceback.extract_tb(tb)
  479. else:
  480. elist = None
  481. It can thus be used by programs which need to process the traceback before
  482. printing (such as console replacements based on the code module from the
  483. standard library).
  484. Because they are meant to be called without a full traceback (only a
  485. list), instances of this class can't call the interactive pdb debugger."""
  486. def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None):
  487. TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
  488. ostream=ostream, parent=parent)
  489. def __call__(self, etype, value, elist):
  490. self.ostream.flush()
  491. self.ostream.write(self.text(etype, value, elist))
  492. self.ostream.write('\n')
  493. def structured_traceback(self, etype, value, elist, tb_offset=None,
  494. context=5):
  495. """Return a color formatted string with the traceback info.
  496. Parameters
  497. ----------
  498. etype : exception type
  499. Type of the exception raised.
  500. value : object
  501. Data stored in the exception
  502. elist : list
  503. List of frames, see class docstring for details.
  504. tb_offset : int, optional
  505. Number of frames in the traceback to skip. If not given, the
  506. instance value is used (set in constructor).
  507. context : int, optional
  508. Number of lines of context information to print.
  509. Returns
  510. -------
  511. String with formatted exception.
  512. """
  513. tb_offset = self.tb_offset if tb_offset is None else tb_offset
  514. Colors = self.Colors
  515. out_list = []
  516. if elist:
  517. if tb_offset and len(elist) > tb_offset:
  518. elist = elist[tb_offset:]
  519. out_list.append('Traceback %s(most recent call last)%s:' %
  520. (Colors.normalEm, Colors.Normal) + '\n')
  521. out_list.extend(self._format_list(elist))
  522. # The exception info should be a single entry in the list.
  523. lines = ''.join(self._format_exception_only(etype, value))
  524. out_list.append(lines)
  525. # Note: this code originally read:
  526. ## for line in lines[:-1]:
  527. ## out_list.append(" "+line)
  528. ## out_list.append(lines[-1])
  529. # This means it was indenting everything but the last line by a little
  530. # bit. I've disabled this for now, but if we see ugliness somewhere we
  531. # can restore it.
  532. return out_list
  533. def _format_list(self, extracted_list):
  534. """Format a list of traceback entry tuples for printing.
  535. Given a list of tuples as returned by extract_tb() or
  536. extract_stack(), return a list of strings ready for printing.
  537. Each string in the resulting list corresponds to the item with the
  538. same index in the argument list. Each string ends in a newline;
  539. the strings may contain internal newlines as well, for those items
  540. whose source text line is not None.
  541. Lifted almost verbatim from traceback.py
  542. """
  543. Colors = self.Colors
  544. list = []
  545. for filename, lineno, name, line in extracted_list[:-1]:
  546. item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
  547. (Colors.filename, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.Normal,
  548. Colors.lineno, lineno, Colors.Normal,
  549. Colors.name, py3compat.cast_unicode_py2(name, "utf-8"), Colors.Normal)
  550. if line:
  551. item += ' %s\n' % line.strip()
  552. list.append(item)
  553. # Emphasize the last entry
  554. filename, lineno, name, line = extracted_list[-1]
  555. item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
  556. (Colors.normalEm,
  557. Colors.filenameEm, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.normalEm,
  558. Colors.linenoEm, lineno, Colors.normalEm,
  559. Colors.nameEm, py3compat.cast_unicode_py2(name, "utf-8"), Colors.normalEm,
  560. Colors.Normal)
  561. if line:
  562. item += '%s %s%s\n' % (Colors.line, line.strip(),
  563. Colors.Normal)
  564. list.append(item)
  565. return list
  566. def _format_exception_only(self, etype, value):
  567. """Format the exception part of a traceback.
  568. The arguments are the exception type and value such as given by
  569. sys.exc_info()[:2]. The return value is a list of strings, each ending
  570. in a newline. Normally, the list contains a single string; however,
  571. for SyntaxError exceptions, it contains several lines that (when
  572. printed) display detailed information about where the syntax error
  573. occurred. The message indicating which exception occurred is the
  574. always last string in the list.
  575. Also lifted nearly verbatim from traceback.py
  576. """
  577. have_filedata = False
  578. Colors = self.Colors
  579. list = []
  580. stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal)
  581. if value is None:
  582. # Not sure if this can still happen in Python 2.6 and above
  583. list.append(stype + '\n')
  584. else:
  585. if issubclass(etype, SyntaxError):
  586. have_filedata = True
  587. if not value.filename: value.filename = "<string>"
  588. if value.lineno:
  589. lineno = value.lineno
  590. textline = ulinecache.getline(value.filename, value.lineno)
  591. else:
  592. lineno = 'unknown'
  593. textline = ''
  594. list.append('%s File %s"%s"%s, line %s%s%s\n' % \
  595. (Colors.normalEm,
  596. Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm,
  597. Colors.linenoEm, lineno, Colors.Normal ))
  598. if textline == '':
  599. textline = py3compat.cast_unicode(value.text, "utf-8")
  600. if textline is not None:
  601. i = 0
  602. while i < len(textline) and textline[i].isspace():
  603. i += 1
  604. list.append('%s %s%s\n' % (Colors.line,
  605. textline.strip(),
  606. Colors.Normal))
  607. if value.offset is not None:
  608. s = ' '
  609. for c in textline[i:value.offset - 1]:
  610. if c.isspace():
  611. s += c
  612. else:
  613. s += ' '
  614. list.append('%s%s^%s\n' % (Colors.caret, s,
  615. Colors.Normal))
  616. try:
  617. s = value.msg
  618. except Exception:
  619. s = self._some_str(value)
  620. if s:
  621. list.append('%s%s:%s %s\n' % (stype, Colors.excName,
  622. Colors.Normal, s))
  623. else:
  624. list.append('%s\n' % stype)
  625. # sync with user hooks
  626. if have_filedata:
  627. ipinst = get_ipython()
  628. if ipinst is not None:
  629. ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0)
  630. return list
  631. def get_exception_only(self, etype, value):
  632. """Only print the exception type and message, without a traceback.
  633. Parameters
  634. ----------
  635. etype : exception type
  636. value : exception value
  637. """
  638. return ListTB.structured_traceback(self, etype, value, [])
  639. def show_exception_only(self, etype, evalue):
  640. """Only print the exception type and message, without a traceback.
  641. Parameters
  642. ----------
  643. etype : exception type
  644. value : exception value
  645. """
  646. # This method needs to use __call__ from *this* class, not the one from
  647. # a subclass whose signature or behavior may be different
  648. ostream = self.ostream
  649. ostream.flush()
  650. ostream.write('\n'.join(self.get_exception_only(etype, evalue)))
  651. ostream.flush()
  652. def _some_str(self, value):
  653. # Lifted from traceback.py
  654. try:
  655. return py3compat.cast_unicode(str(value))
  656. except:
  657. return u'<unprintable %s object>' % type(value).__name__
  658. #----------------------------------------------------------------------------
  659. class VerboseTB(TBTools):
  660. """A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
  661. of HTML. Requires inspect and pydoc. Crazy, man.
  662. Modified version which optionally strips the topmost entries from the
  663. traceback, to be used with alternate interpreters (because their own code
  664. would appear in the traceback)."""
  665. def __init__(self, color_scheme='Linux', call_pdb=False, ostream=None,
  666. tb_offset=0, long_header=False, include_vars=True,
  667. check_cache=None, debugger_cls = None):
  668. """Specify traceback offset, headers and color scheme.
  669. Define how many frames to drop from the tracebacks. Calling it with
  670. tb_offset=1 allows use of this handler in interpreters which will have
  671. their own code at the top of the traceback (VerboseTB will first
  672. remove that frame before printing the traceback info)."""
  673. TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
  674. ostream=ostream)
  675. self.tb_offset = tb_offset
  676. self.long_header = long_header
  677. self.include_vars = include_vars
  678. # By default we use linecache.checkcache, but the user can provide a
  679. # different check_cache implementation. This is used by the IPython
  680. # kernel to provide tracebacks for interactive code that is cached,
  681. # by a compiler instance that flushes the linecache but preserves its
  682. # own code cache.
  683. if check_cache is None:
  684. check_cache = linecache.checkcache
  685. self.check_cache = check_cache
  686. self.debugger_cls = debugger_cls or debugger.Pdb
  687. def format_records(self, records, last_unique, recursion_repeat):
  688. """Format the stack frames of the traceback"""
  689. frames = []
  690. for r in records[:last_unique+recursion_repeat+1]:
  691. #print '*** record:',file,lnum,func,lines,index # dbg
  692. frames.append(self.format_record(*r))
  693. if recursion_repeat:
  694. frames.append('... last %d frames repeated, from the frame below ...\n' % recursion_repeat)
  695. frames.append(self.format_record(*records[last_unique+recursion_repeat+1]))
  696. return frames
  697. def format_record(self, frame, file, lnum, func, lines, index):
  698. """Format a single stack frame"""
  699. Colors = self.Colors # just a shorthand + quicker name lookup
  700. ColorsNormal = Colors.Normal # used a lot
  701. col_scheme = self.color_scheme_table.active_scheme_name
  702. indent = ' ' * INDENT_SIZE
  703. em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal)
  704. undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
  705. tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
  706. tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
  707. ColorsNormal)
  708. tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
  709. (Colors.vName, Colors.valEm, ColorsNormal)
  710. tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
  711. tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
  712. Colors.vName, ColorsNormal)
  713. tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
  714. tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
  715. tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm, Colors.line,
  716. ColorsNormal)
  717. abspath = os.path.abspath
  718. if not file:
  719. file = '?'
  720. elif file.startswith(str("<")) and file.endswith(str(">")):
  721. # Not a real filename, no problem...
  722. pass
  723. elif not os.path.isabs(file):
  724. # Try to make the filename absolute by trying all
  725. # sys.path entries (which is also what linecache does)
  726. for dirname in sys.path:
  727. try:
  728. fullname = os.path.join(dirname, file)
  729. if os.path.isfile(fullname):
  730. file = os.path.abspath(fullname)
  731. break
  732. except Exception:
  733. # Just in case that sys.path contains very
  734. # strange entries...
  735. pass
  736. file = py3compat.cast_unicode(file, util_path.fs_encoding)
  737. link = tpl_link % file
  738. args, varargs, varkw, locals = fixed_getargvalues(frame)
  739. if func == '?':
  740. call = ''
  741. else:
  742. # Decide whether to include variable details or not
  743. var_repr = self.include_vars and eqrepr or nullrepr
  744. try:
  745. call = tpl_call % (func, inspect.formatargvalues(args,
  746. varargs, varkw,
  747. locals, formatvalue=var_repr))
  748. except KeyError:
  749. # This happens in situations like errors inside generator
  750. # expressions, where local variables are listed in the
  751. # line, but can't be extracted from the frame. I'm not
  752. # 100% sure this isn't actually a bug in inspect itself,
  753. # but since there's no info for us to compute with, the
  754. # best we can do is report the failure and move on. Here
  755. # we must *not* call any traceback construction again,
  756. # because that would mess up use of %debug later on. So we
  757. # simply report the failure and move on. The only
  758. # limitation will be that this frame won't have locals
  759. # listed in the call signature. Quite subtle problem...
  760. # I can't think of a good way to validate this in a unit
  761. # test, but running a script consisting of:
  762. # dict( (k,v.strip()) for (k,v) in range(10) )
  763. # will illustrate the error, if this exception catch is
  764. # disabled.
  765. call = tpl_call_fail % func
  766. # Don't attempt to tokenize binary files.
  767. if file.endswith(('.so', '.pyd', '.dll')):
  768. return '%s %s\n' % (link, call)
  769. elif file.endswith(('.pyc', '.pyo')):
  770. # Look up the corresponding source file.
  771. try:
  772. file = openpy.source_from_cache(file)
  773. except ValueError:
  774. # Failed to get the source file for some reason
  775. # E.g. https://github.com/ipython/ipython/issues/9486
  776. return '%s %s\n' % (link, call)
  777. def linereader(file=file, lnum=[lnum], getline=ulinecache.getline):
  778. line = getline(file, lnum[0])
  779. lnum[0] += 1
  780. return line
  781. # Build the list of names on this line of code where the exception
  782. # occurred.
  783. try:
  784. names = []
  785. name_cont = False
  786. for token_type, token, start, end, line in generate_tokens(linereader):
  787. # build composite names
  788. if token_type == tokenize.NAME and token not in keyword.kwlist:
  789. if name_cont:
  790. # Continuation of a dotted name
  791. try:
  792. names[-1].append(token)
  793. except IndexError:
  794. names.append([token])
  795. name_cont = False
  796. else:
  797. # Regular new names. We append everything, the caller
  798. # will be responsible for pruning the list later. It's
  799. # very tricky to try to prune as we go, b/c composite
  800. # names can fool us. The pruning at the end is easy
  801. # to do (or the caller can print a list with repeated
  802. # names if so desired.
  803. names.append([token])
  804. elif token == '.':
  805. name_cont = True
  806. elif token_type == tokenize.NEWLINE:
  807. break
  808. except (IndexError, UnicodeDecodeError, SyntaxError):
  809. # signals exit of tokenizer
  810. # SyntaxError can occur if the file is not actually Python
  811. # - see gh-6300
  812. pass
  813. except tokenize.TokenError as msg:
  814. _m = ("An unexpected error occurred while tokenizing input\n"
  815. "The following traceback may be corrupted or invalid\n"
  816. "The error message is: %s\n" % msg)
  817. error(_m)
  818. # Join composite names (e.g. "dict.fromkeys")
  819. names = ['.'.join(n) for n in names]
  820. # prune names list of duplicates, but keep the right order
  821. unique_names = uniq_stable(names)
  822. # Start loop over vars
  823. lvals = []
  824. if self.include_vars:
  825. for name_full in unique_names:
  826. name_base = name_full.split('.', 1)[0]
  827. if name_base in frame.f_code.co_varnames:
  828. if name_base in locals:
  829. try:
  830. value = repr(eval(name_full, locals))
  831. except:
  832. value = undefined
  833. else:
  834. value = undefined
  835. name = tpl_local_var % name_full
  836. else:
  837. if name_base in frame.f_globals:
  838. try:
  839. value = repr(eval(name_full, frame.f_globals))
  840. except:
  841. value = undefined
  842. else:
  843. value = undefined
  844. name = tpl_global_var % name_full
  845. lvals.append(tpl_name_val % (name, value))
  846. if lvals:
  847. lvals = '%s%s' % (indent, em_normal.join(lvals))
  848. else:
  849. lvals = ''
  850. level = '%s %s\n' % (link, call)
  851. if index is None:
  852. return level
  853. else:
  854. return '%s%s' % (level, ''.join(
  855. _format_traceback_lines(lnum, index, lines, Colors, lvals,
  856. col_scheme)))
  857. def prepare_chained_exception_message(self, cause):
  858. direct_cause = "\nThe above exception was the direct cause of the following exception:\n"
  859. exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n"
  860. if cause:
  861. message = [[direct_cause]]
  862. else:
  863. message = [[exception_during_handling]]
  864. return message
  865. def prepare_header(self, etype, long_version=False):
  866. colors = self.Colors # just a shorthand + quicker name lookup
  867. colorsnormal = colors.Normal # used a lot
  868. exc = '%s%s%s' % (colors.excName, etype, colorsnormal)
  869. width = min(75, get_terminal_size()[0])
  870. if long_version:
  871. # Header with the exception type, python version, and date
  872. pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
  873. date = time.ctime(time.time())
  874. head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal,
  875. exc, ' ' * (width - len(str(etype)) - len(pyver)),
  876. pyver, date.rjust(width) )
  877. head += "\nA problem occurred executing Python code. Here is the sequence of function" \
  878. "\ncalls leading up to the error, with the most recent (innermost) call last."
  879. else:
  880. # Simplified header
  881. head = '%s%s' % (exc, 'Traceback (most recent call last)'. \
  882. rjust(width - len(str(etype))) )
  883. return head
  884. def format_exception(self, etype, evalue):
  885. colors = self.Colors # just a shorthand + quicker name lookup
  886. colorsnormal = colors.Normal # used a lot
  887. indent = ' ' * INDENT_SIZE
  888. # Get (safely) a string form of the exception info
  889. try:
  890. etype_str, evalue_str = map(str, (etype, evalue))
  891. except:
  892. # User exception is improperly defined.
  893. etype, evalue = str, sys.exc_info()[:2]
  894. etype_str, evalue_str = map(str, (etype, evalue))
  895. # ... and format it
  896. exception = ['%s%s%s: %s' % (colors.excName, etype_str,
  897. colorsnormal, py3compat.cast_unicode(evalue_str))]
  898. if (not py3compat.PY3) and type(evalue) is types.InstanceType:
  899. try:
  900. names = [w for w in dir(evalue) if isinstance(w, py3compat.string_types)]
  901. except:
  902. # Every now and then, an object with funny internals blows up
  903. # when dir() is called on it. We do the best we can to report
  904. # the problem and continue
  905. _m = '%sException reporting error (object with broken dir())%s:'
  906. exception.append(_m % (colors.excName, colorsnormal))
  907. etype_str, evalue_str = map(str, sys.exc_info()[:2])
  908. exception.append('%s%s%s: %s' % (colors.excName, etype_str,
  909. colorsnormal, py3compat.cast_unicode(evalue_str)))
  910. names = []
  911. for name in names:
  912. value = text_repr(getattr(evalue, name))
  913. exception.append('\n%s%s = %s' % (indent, name, value))
  914. return exception
  915. def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset):
  916. """Formats the header, traceback and exception message for a single exception.
  917. This may be called multiple times by Python 3 exception chaining
  918. (PEP 3134).
  919. """
  920. # some locals
  921. orig_etype = etype
  922. try:
  923. etype = etype.__name__
  924. except AttributeError:
  925. pass
  926. tb_offset = self.tb_offset if tb_offset is None else tb_offset
  927. head = self.prepare_header(etype, self.long_header)
  928. records = self.get_records(etb, number_of_lines_of_context, tb_offset)
  929. if records is None:
  930. return ""
  931. last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records)
  932. frames = self.format_records(records, last_unique, recursion_repeat)
  933. formatted_exception = self.format_exception(etype, evalue)
  934. if records:
  935. filepath, lnum = records[-1][1:3]
  936. filepath = os.path.abspath(filepath)
  937. ipinst = get_ipython()
  938. if ipinst is not None:
  939. ipinst.hooks.synchronize_with_editor(filepath, lnum, 0)
  940. return [[head] + frames + [''.join(formatted_exception[0])]]
  941. def get_records(self, etb, number_of_lines_of_context, tb_offset):
  942. try:
  943. # Try the default getinnerframes and Alex's: Alex's fixes some
  944. # problems, but it generates empty tracebacks for console errors
  945. # (5 blanks lines) where none should be returned.
  946. return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
  947. except UnicodeDecodeError:
  948. # This can occur if a file's encoding magic comment is wrong.
  949. # I can't see a way to recover without duplicating a bunch of code
  950. # from the stdlib traceback module. --TK
  951. error('\nUnicodeDecodeError while processing traceback.\n')
  952. return None
  953. except:
  954. # FIXME: I've been getting many crash reports from python 2.3
  955. # users, traceable to inspect.py. If I can find a small test-case
  956. # to reproduce this, I should either write a better workaround or
  957. # file a bug report against inspect (if that's the real problem).
  958. # So far, I haven't been able to find an isolated example to
  959. # reproduce the problem.
  960. inspect_error()
  961. traceback.print_exc(file=self.ostream)
  962. info('\nUnfortunately, your original traceback can not be constructed.\n')
  963. return None
  964. def get_parts_of_chained_exception(self, evalue):
  965. def get_chained_exception(exception_value):
  966. cause = getattr(exception_value, '__cause__', None)
  967. if cause:
  968. return cause
  969. if getattr(exception_value, '__suppress_context__', False):
  970. return None
  971. return getattr(exception_value, '__context__', None)
  972. chained_evalue = get_chained_exception(evalue)
  973. if chained_evalue:
  974. return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__
  975. def structured_traceback(self, etype, evalue, etb, tb_offset=None,
  976. number_of_lines_of_context=5):
  977. """Return a nice text document describing the traceback."""
  978. formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
  979. tb_offset)
  980. colors = self.Colors # just a shorthand + quicker name lookup
  981. colorsnormal = colors.Normal # used a lot
  982. head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal)
  983. structured_traceback_parts = [head]
  984. if py3compat.PY3:
  985. chained_exceptions_tb_offset = 0
  986. lines_of_context = 3
  987. formatted_exceptions = formatted_exception
  988. exception = self.get_parts_of_chained_exception(evalue)
  989. if exception:
  990. formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
  991. etype, evalue, etb = exception
  992. else:
  993. evalue = None
  994. chained_exc_ids = set()
  995. while evalue:
  996. formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,
  997. chained_exceptions_tb_offset)
  998. exception = self.get_parts_of_chained_exception(evalue)
  999. if exception and not id(exception[1]) in chained_exc_ids:
  1000. chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop
  1001. formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
  1002. etype, evalue, etb = exception
  1003. else:
  1004. evalue = None
  1005. # we want to see exceptions in a reversed order:
  1006. # the first exception should be on top
  1007. for formatted_exception in reversed(formatted_exceptions):
  1008. structured_traceback_parts += formatted_exception
  1009. else:
  1010. structured_traceback_parts += formatted_exception[0]
  1011. return structured_traceback_parts
  1012. def debugger(self, force=False):
  1013. """Call up the pdb debugger if desired, always clean up the tb
  1014. reference.
  1015. Keywords:
  1016. - force(False): by default, this routine checks the instance call_pdb
  1017. flag and does not actually invoke the debugger if the flag is false.
  1018. The 'force' option forces the debugger to activate even if the flag
  1019. is false.
  1020. If the call_pdb flag is set, the pdb interactive debugger is
  1021. invoked. In all cases, the self.tb reference to the current traceback
  1022. is deleted to prevent lingering references which hamper memory
  1023. management.
  1024. Note that each call to pdb() does an 'import readline', so if your app
  1025. requires a special setup for the readline completers, you'll have to
  1026. fix that by hand after invoking the exception handler."""
  1027. if force or self.call_pdb:
  1028. if self.pdb is None:
  1029. self.pdb = self.debugger_cls()
  1030. # the system displayhook may have changed, restore the original
  1031. # for pdb
  1032. display_trap = DisplayTrap(hook=sys.__displayhook__)
  1033. with display_trap:
  1034. self.pdb.reset()
  1035. # Find the right frame so we don't pop up inside ipython itself
  1036. if hasattr(self, 'tb') and self.tb is not None:
  1037. etb = self.tb
  1038. else:
  1039. etb = self.tb = sys.last_traceback
  1040. while self.tb is not None and self.tb.tb_next is not None:
  1041. self.tb = self.tb.tb_next
  1042. if etb and etb.tb_next:
  1043. etb = etb.tb_next
  1044. self.pdb.botframe = etb.tb_frame
  1045. self.pdb.interaction(self.tb.tb_frame, self.tb)
  1046. if hasattr(self, 'tb'):
  1047. del self.tb
  1048. def handler(self, info=None):
  1049. (etype, evalue, etb) = info or sys.exc_info()
  1050. self.tb = etb
  1051. ostream = self.ostream
  1052. ostream.flush()
  1053. ostream.write(self.text(etype, evalue, etb))
  1054. ostream.write('\n')
  1055. ostream.flush()
  1056. # Changed so an instance can just be called as VerboseTB_inst() and print
  1057. # out the right info on its own.
  1058. def __call__(self, etype=None, evalue=None, etb=None):
  1059. """This hook can replace sys.excepthook (for Python 2.1 or higher)."""
  1060. if etb is None:
  1061. self.handler()
  1062. else:
  1063. self.handler((etype, evalue, etb))
  1064. try:
  1065. self.debugger()
  1066. except KeyboardInterrupt:
  1067. print("\nKeyboardInterrupt")
  1068. #----------------------------------------------------------------------------
  1069. class FormattedTB(VerboseTB, ListTB):
  1070. """Subclass ListTB but allow calling with a traceback.
  1071. It can thus be used as a sys.excepthook for Python > 2.1.
  1072. Also adds 'Context' and 'Verbose' modes, not available in ListTB.
  1073. Allows a tb_offset to be specified. This is useful for situations where
  1074. one needs to remove a number of topmost frames from the traceback (such as
  1075. occurs with python programs that themselves execute other python code,
  1076. like Python shells). """
  1077. def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False,
  1078. ostream=None,
  1079. tb_offset=0, long_header=False, include_vars=False,
  1080. check_cache=None, debugger_cls=None):
  1081. # NEVER change the order of this list. Put new modes at the end:
  1082. self.valid_modes = ['Plain', 'Context', 'Verbose']
  1083. self.verbose_modes = self.valid_modes[1:3]
  1084. VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
  1085. ostream=ostream, tb_offset=tb_offset,
  1086. long_header=long_header, include_vars=include_vars,
  1087. check_cache=check_cache, debugger_cls=debugger_cls)
  1088. # Different types of tracebacks are joined with different separators to
  1089. # form a single string. They are taken from this dict
  1090. self._join_chars = dict(Plain='', Context='\n', Verbose='\n')
  1091. # set_mode also sets the tb_join_char attribute
  1092. self.set_mode(mode)
  1093. def _extract_tb(self, tb):
  1094. if tb:
  1095. return traceback.extract_tb(tb)
  1096. else:
  1097. return None
  1098. def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5):
  1099. tb_offset = self.tb_offset if tb_offset is None else tb_offset
  1100. mode = self.mode
  1101. if mode in self.verbose_modes:
  1102. # Verbose modes need a full traceback
  1103. return VerboseTB.structured_traceback(
  1104. self, etype, value, tb, tb_offset, number_of_lines_of_context
  1105. )
  1106. else:
  1107. # We must check the source cache because otherwise we can print
  1108. # out-of-date source code.
  1109. self.check_cache()
  1110. # Now we can extract and format the exception
  1111. elist = self._extract_tb(tb)
  1112. return ListTB.structured_traceback(
  1113. self, etype, value, elist, tb_offset, number_of_lines_of_context
  1114. )
  1115. def stb2text(self, stb):
  1116. """Convert a structured traceback (a list) to a string."""
  1117. return self.tb_join_char.join(stb)
  1118. def set_mode(self, mode=None):
  1119. """Switch to the desired mode.
  1120. If mode is not specified, cycles through the available modes."""
  1121. if not mode:
  1122. new_idx = (self.valid_modes.index(self.mode) + 1 ) % \
  1123. len(self.valid_modes)
  1124. self.mode = self.valid_modes[new_idx]
  1125. elif mode not in self.valid_modes:
  1126. raise ValueError('Unrecognized mode in FormattedTB: <' + mode + '>\n'
  1127. 'Valid modes: ' + str(self.valid_modes))
  1128. else:
  1129. self.mode = mode
  1130. # include variable details only in 'Verbose' mode
  1131. self.include_vars = (self.mode == self.valid_modes[2])
  1132. # Set the join character for generating text tracebacks
  1133. self.tb_join_char = self._join_chars[self.mode]
  1134. # some convenient shortcuts
  1135. def plain(self):
  1136. self.set_mode(self.valid_modes[0])
  1137. def context(self):
  1138. self.set_mode(self.valid_modes[1])
  1139. def verbose(self):
  1140. self.set_mode(self.valid_modes[2])
  1141. #----------------------------------------------------------------------------
  1142. class AutoFormattedTB(FormattedTB):
  1143. """A traceback printer which can be called on the fly.
  1144. It will find out about exceptions by itself.
  1145. A brief example::
  1146. AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
  1147. try:
  1148. ...
  1149. except:
  1150. AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
  1151. """
  1152. def __call__(self, etype=None, evalue=None, etb=None,
  1153. out=None, tb_offset=None):
  1154. """Print out a formatted exception traceback.
  1155. Optional arguments:
  1156. - out: an open file-like object to direct output to.
  1157. - tb_offset: the number of frames to skip over in the stack, on a
  1158. per-call basis (this overrides temporarily the instance's tb_offset
  1159. given at initialization time. """
  1160. if out is None:
  1161. out = self.ostream
  1162. out.flush()
  1163. out.write(self.text(etype, evalue, etb, tb_offset))
  1164. out.write('\n')
  1165. out.flush()
  1166. # FIXME: we should remove the auto pdb behavior from here and leave
  1167. # that to the clients.
  1168. try:
  1169. self.debugger()
  1170. except KeyboardInterrupt:
  1171. print("\nKeyboardInterrupt")
  1172. def structured_traceback(self, etype=None, value=None, tb=None,
  1173. tb_offset=None, number_of_lines_of_context=5):
  1174. if etype is None:
  1175. etype, value, tb = sys.exc_info()
  1176. self.tb = tb
  1177. return FormattedTB.structured_traceback(
  1178. self, etype, value, tb, tb_offset, number_of_lines_of_context)
  1179. #---------------------------------------------------------------------------
  1180. # A simple class to preserve Nathan's original functionality.
  1181. class ColorTB(FormattedTB):
  1182. """Shorthand to initialize a FormattedTB in Linux colors mode."""
  1183. def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs):
  1184. FormattedTB.__init__(self, color_scheme=color_scheme,
  1185. call_pdb=call_pdb, **kwargs)
  1186. class SyntaxTB(ListTB):
  1187. """Extension which holds some state: the last exception value"""
  1188. def __init__(self, color_scheme='NoColor'):
  1189. ListTB.__init__(self, color_scheme)
  1190. self.last_syntax_error = None
  1191. def __call__(self, etype, value, elist):
  1192. self.last_syntax_error = value
  1193. ListTB.__call__(self, etype, value, elist)
  1194. def structured_traceback(self, etype, value, elist, tb_offset=None,
  1195. context=5):
  1196. # If the source file has been edited, the line in the syntax error can
  1197. # be wrong (retrieved from an outdated cache). This replaces it with
  1198. # the current value.
  1199. if isinstance(value, SyntaxError) \
  1200. and isinstance(value.filename, py3compat.string_types) \
  1201. and isinstance(value.lineno, int):
  1202. linecache.checkcache(value.filename)
  1203. newtext = ulinecache.getline(value.filename, value.lineno)
  1204. if newtext:
  1205. value.text = newtext
  1206. self.last_syntax_error = value
  1207. return super(SyntaxTB, self).structured_traceback(etype, value, elist,
  1208. tb_offset=tb_offset, context=context)
  1209. def clear_err_state(self):
  1210. """Return the current error state and clear it"""
  1211. e = self.last_syntax_error
  1212. self.last_syntax_error = None
  1213. return e
  1214. def stb2text(self, stb):
  1215. """Convert a structured traceback (a list) to a string."""
  1216. return ''.join(stb)
  1217. # some internal-use functions
  1218. def text_repr(value):
  1219. """Hopefully pretty robust repr equivalent."""
  1220. # this is pretty horrible but should always return *something*
  1221. try:
  1222. return pydoc.text.repr(value)
  1223. except KeyboardInterrupt:
  1224. raise
  1225. except:
  1226. try:
  1227. return repr(value)
  1228. except KeyboardInterrupt:
  1229. raise
  1230. except:
  1231. try:
  1232. # all still in an except block so we catch
  1233. # getattr raising
  1234. name = getattr(value, '__name__', None)
  1235. if name:
  1236. # ick, recursion
  1237. return text_repr(name)
  1238. klass = getattr(value, '__class__', None)
  1239. if klass:
  1240. return '%s instance' % text_repr(klass)
  1241. except KeyboardInterrupt:
  1242. raise
  1243. except:
  1244. return 'UNRECOVERABLE REPR FAILURE'
  1245. def eqrepr(value, repr=text_repr):
  1246. return '=%s' % repr(value)
  1247. def nullrepr(value, repr=text_repr):
  1248. return ''