##// END OF EJS Templates
remove another py2 only test
remove another py2 only test

File last commit:

r22943:3e97b06a
r22962:c05c1799
Show More
ultratb.py
1491 lines | 59.2 KiB | text/x-python | PythonLexer
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 # -*- coding: utf-8 -*-
"""
Thomas Kluyver
Standardise some module docstring first lines
r13888 Verbose and colourful traceback formatting.
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
Thomas Kluyver
Miscellaneous docs fixes
r13597 **ColorTB**
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 I've always found it a bit hard to visually parse tracebacks in Python. The
ColorTB class is a solution to that problem. It colors the different parts of a
traceback in a manner similar to what you would expect from a syntax-highlighting
text editor.
Thomas Kluyver
Improvements to docs formatting.
r12553 Installation instructions for ColorTB::
Brian Granger
Merging -r 1177 from lp:ipython with fixes and resolutions....
r2124 import sys,ultratb
sys.excepthook = ultratb.ColorTB()
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
Thomas Kluyver
Miscellaneous docs fixes
r13597 **VerboseTB**
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 I've also included a port of Ka-Ping Yee's "cgitb.py" that produces all kinds
of useful info when a traceback occurs. Ping originally had it spit out HTML
and intended it for CGI programmers, but why should they have all the fun? I
altered it to spit out colored text to the terminal. It's a bit overwhelming,
but kind of neat, and maybe useful for long-running programs that you believe
are bug-free. If a crash *does* occur in that type of program you want details.
Give it a shot--you'll love it or you'll hate it.
Thomas Kluyver
Improvements to docs formatting.
r12553 .. note::
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
The Verbose mode prints the variables currently visible where the exception
happened (shortening their strings if too long). This can potentially be
very slow, if you happen to have a huge data structure whose string
representation is complex to compute. Your computer may appear to freeze for
a while with cpu usage at 100%. If this occurs, you can cancel the traceback
with Ctrl-C (maybe hitting it more than once).
If you encounter this kind of situation often, you may want to use the
Verbose_novars mode instead of the regular Verbose, which avoids formatting
variables (but otherwise includes the information and context given by
Verbose).
Bernardo B. Marques
remove all trailling spaces
r4872
Matthias Bussonnier
add docs and cleanup bad code style
r22096 .. note::
The verbose mode print all variables in the stack, which means it can
potentially leak sensitive information like access keys, or unencryted
password.
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 Installation instructions for VerboseTB::
Thomas Kluyver
Improvements to docs formatting.
r12553
Brian Granger
Merging -r 1177 from lp:ipython with fixes and resolutions....
r2124 import sys,ultratb
sys.excepthook = ultratb.VerboseTB()
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
Note: Much of the code in this module was lifted verbatim from the standard
library module 'traceback.py' and Ka-Ping Yee's 'cgitb.py'.
Thomas Kluyver
Improvements to docs formatting.
r12553 Color schemes
-------------
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 The colors are defined in the class TBTools through the use of the
ColorSchemeTable class. Currently the following exist:
- NoColor: allows all of this module to be used in any terminal (the color
Thomas Kluyver
Improvements to docs formatting.
r12553 escapes are just dummy blank strings).
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
- Linux: is meant to look good in a terminal like the Linux console (black
Thomas Kluyver
Improvements to docs formatting.
r12553 or very dark background).
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
- LightBG: similar to Linux but swaps dark/light colors to be more readable
Thomas Kluyver
Improvements to docs formatting.
r12553 in light background terminals.
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
Matthias Bussonnier
Make everyone happy with a neutral colortheme by default.
r22609 - Neutral: a neutral color scheme that should be readable on both light and
dark background
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 You can implement other color schemes easily, the syntax is fairly
self-explanatory. Please send back new schemes you develop to the author for
possible inclusion in future releases.
Thomas Kluyver
Only include inheritance diagram where it's useful.
r8795
Inheritance diagram:
.. inheritance-diagram:: IPython.core.ultratb
:parts: 3
Fernando Perez
Remove svn-style $Id marks from docstrings and Release imports....
r1853 """
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
Justyna Ilczuk
i1673 some DRY refactoring
r17158 #*****************************************************************************
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 # Copyright (C) 2001 Nathaniel Gray <n8gray@caltech.edu>
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 # Copyright (C) 2001-2004 Fernando Perez <fperez@colorado.edu>
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 #
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 # Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 #*****************************************************************************
Matthias Bussonnier
Introduce some classes necessary fro Pygments refactor.
r22109 from __future__ import absolute_import
Jörgen Stenarson
Quick fix for #1688, traceback-unicode issue...
r8299 from __future__ import unicode_literals
Thomas Kluyver
Convert print statements to print function calls...
r13348 from __future__ import print_function
Brian Granger
sys.displayhook is now managed dynamically by display_trap.
r2231
Scott Sanderson
BUG: Fix getargvalues call with nested genexprs....
r21715 import dis
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 import inspect
import keyword
import linecache
import os
import pydoc
import re
import sys
import time
import tokenize
import traceback
import types
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 try: # Python 2
Thomas Kluyver
Fixes for UltraTB and PyColorize with Python 3
r4758 generate_tokens = tokenize.generate_tokens
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 except AttributeError: # Python 3
Thomas Kluyver
Fixes for UltraTB and PyColorize with Python 3
r4758 generate_tokens = tokenize.tokenize
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 # For purposes of monkeypatching inspect to fix a bug in it.
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 from inspect import getsourcefile, getfile, getmodule, \
ismodule, isclass, ismethod, isfunction, istraceback, isframe, iscode
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
# IPython's own modules
MinRK
don't use deprecated ipapi.get...
r10581 from IPython import get_ipython
from IPython.core import debugger
Brian Granger
sys.displayhook is now managed dynamically by display_trap.
r2231 from IPython.core.display_trap import DisplayTrap
Brian Granger
excolors.py => core/excolors.py and updated import statements.
r2021 from IPython.core.excolors import exception_colors
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 from IPython.utils import PyColorize
Thomas Kluyver
Consolidate utils.pyfile into utils.openpy
r9473 from IPython.utils import openpy
Jörgen Stenarson
Quick fix for #1688, traceback-unicode issue...
r8299 from IPython.utils import path as util_path
Thomas Kluyver
Fixes for UltraTB and PyColorize with Python 3
r4758 from IPython.utils import py3compat
Thomas Kluyver
Fixes for producing unicode tracebacks.
r8325 from IPython.utils import ulinecache
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 from IPython.utils.data import uniq_stable
Vincent Woo
Use term width to determine header length
r22150 from IPython.utils.terminal import get_terminal_size
Pierre Gerold
Replace all import of IPython.utils.warn module
r22092 from logging import info, error
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
Matthias Bussonnier
Introduce some classes necessary fro Pygments refactor.
r22109 import IPython.utils.colorable as colorable
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 # Globals
# amount of space to put line numbers before verbose tracebacks
INDENT_SIZE = 8
# Default color scheme. This is used, for example, by the traceback
# formatter. When running in an actual IPython instance, the user's rc.colors
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 # value is used, but having a module global makes this functionality available
Brian Granger
Merging -r 1177 from lp:ipython with fixes and resolutions....
r2124 # to users of ultratb who are NOT running inside ipython.
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 DEFAULT_SCHEME = 'NoColor'
Justyna Ilczuk
i1673 some DRY refactoring
r17158 # ---------------------------------------------------------------------------
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 # Code begins
# Utility functions
def inspect_error():
"""Print a message about internal inspect errors.
These are unfortunately quite common."""
Bernardo B. Marques
remove all trailling spaces
r4872
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 error('Internal Python error in the inspect module.\n'
'Below is the traceback from this internal error.\n')
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156
Thomas Kluyver
Reapply our monkeypatch to inspect.findsource()....
r8100 # This function is a monkeypatch we apply to the Python inspect module. We have
# now found when it's needed (see discussion on issue gh-1456), and we have a
# test case (IPython.core.tests.test_ultratb.ChangedPyFileTest) that fails if
# the monkeypatch is not applied. TK, Aug 2012.
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 def findsource(object):
"""Return the entire source file and starting line number for an object.
The argument may be a module, class, method, function, traceback, frame,
or code object. The source code is returned as a list of all the lines
in the file and the line number indexes a line in that list. An IOError
is raised if the source code cannot be retrieved.
FIXED version with which we monkeypatch the stdlib to work around a bug."""
file = getsourcefile(object) or getfile(object)
# If the object is a frame, then trying to get the globals dict from its
# module won't work. Instead, the frame object itself has the globals
# dictionary.
globals_dict = None
if inspect.isframe(object):
# XXX: can this ever be false?
globals_dict = object.f_globals
else:
module = getmodule(object, file)
if module:
globals_dict = module.__dict__
lines = linecache.getlines(file, globals_dict)
if not lines:
raise IOError('could not get source code')
if ismodule(object):
return lines, 0
if isclass(object):
name = object.__name__
pat = re.compile(r'^(\s*)class\s*' + name + r'\b')
# make some effort to find the best matching class definition:
# use the one with the least indentation, which is the one
# that's most probably not inside a function definition.
candidates = []
for i in range(len(lines)):
match = pat.match(lines[i])
if match:
# if it's at toplevel, it's already the best one
if lines[i][0] == 'c':
return lines, i
# else add whitespace to candidate list
candidates.append((match.group(1), i))
if candidates:
# this will sort by whitespace, and by line number,
# less whitespace first
candidates.sort()
return lines, candidates[0][1]
else:
raise IOError('could not find class definition')
if ismethod(object):
Thomas Kluyver
Fix method special attributes...
r13370 object = object.__func__
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 if isfunction(object):
Thomas Kluyver
Update function attribute names...
r13362 object = object.__code__
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 if istraceback(object):
object = object.tb_frame
if isframe(object):
object = object.f_code
if iscode(object):
if not hasattr(object, 'co_firstlineno'):
raise IOError('could not find function definition')
pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
pmatch = pat.match
# fperez - fix: sometimes, co_firstlineno can give a number larger than
# the length of lines, which causes an error. Safeguard against that.
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 lnum = min(object.co_firstlineno, len(lines)) - 1
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 while lnum > 0:
Matthias Bussonnier
add docs and cleanup bad code style
r22096 if pmatch(lines[lnum]):
break
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 lnum -= 1
Bernardo B. Marques
remove all trailling spaces
r4872
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 return lines, lnum
raise IOError('could not find code object')
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156
Scott Sanderson
BUG: Fix getargvalues call with nested genexprs....
r21715 # This is a patched version of inspect.getargs that applies the (unmerged)
Scott Sanderson
DOC: Better attribution to bugfix author.
r21716 # patch for http://bugs.python.org/issue14611 by Stefano Taschini. This fixes
Scott Sanderson
BUG: Fix getargvalues call with nested genexprs....
r21715 # https://github.com/ipython/ipython/issues/8205 and
# https://github.com/ipython/ipython/issues/8293
def getargs(co):
"""Get information about the arguments accepted by a code object.
Three things are returned: (args, varargs, varkw), where 'args' is
a list of argument names (possibly containing nested lists), and
'varargs' and 'varkw' are the names of the * and ** arguments or None."""
if not iscode(co):
raise TypeError('{!r} is not a code object'.format(co))
nargs = co.co_argcount
names = co.co_varnames
args = list(names[:nargs])
step = 0
# The following acrobatics are for anonymous (tuple) arguments.
for i in range(nargs):
if args[i][:1] in ('', '.'):
stack, remain, count = [], [], []
while step < len(co.co_code):
op = ord(co.co_code[step])
step = step + 1
if op >= dis.HAVE_ARGUMENT:
opname = dis.opname[op]
value = ord(co.co_code[step]) + ord(co.co_code[step+1])*256
step = step + 2
if opname in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'):
remain.append(value)
count.append(value)
elif opname in ('STORE_FAST', 'STORE_DEREF'):
if op in dis.haslocal:
stack.append(co.co_varnames[value])
elif op in dis.hasfree:
stack.append((co.co_cellvars + co.co_freevars)[value])
# Special case for sublists of length 1: def foo((bar))
# doesn't generate the UNPACK_TUPLE bytecode, so if
# `remain` is empty here, we have such a sublist.
if not remain:
stack[0] = [stack[0]]
break
else:
remain[-1] = remain[-1] - 1
while remain[-1] == 0:
remain.pop()
size = count.pop()
stack[-size:] = [stack[-size:]]
Matthias Bussonnier
add docs and cleanup bad code style
r22096 if not remain:
break
Scott Sanderson
BUG: Fix getargvalues call with nested genexprs....
r21715 remain[-1] = remain[-1] - 1
Matthias Bussonnier
add docs and cleanup bad code style
r22096 if not remain:
break
Scott Sanderson
BUG: Fix getargvalues call with nested genexprs....
r21715 args[i] = stack[0]
varargs = None
if co.co_flags & inspect.CO_VARARGS:
varargs = co.co_varnames[nargs]
nargs = nargs + 1
varkw = None
if co.co_flags & inspect.CO_VARKEYWORDS:
varkw = co.co_varnames[nargs]
return inspect.Arguments(args, varargs, varkw)
MinRK
monkeypatch inspect.findsource in a context manager...
r16441 # Monkeypatch inspect to apply our bugfix.
def with_patch_inspect(f):
"""decorator for monkeypatching inspect.findsource"""
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156
MinRK
monkeypatch inspect.findsource in a context manager...
r16441 def wrapped(*args, **kwargs):
MinRK
get rid of redundant context manager
r16461 save_findsource = inspect.findsource
Scott Sanderson
BUG: Fix getargvalues call with nested genexprs....
r21715 save_getargs = inspect.getargs
MinRK
get rid of redundant context manager
r16461 inspect.findsource = findsource
Scott Sanderson
BUG: Fix getargvalues call with nested genexprs....
r21715 inspect.getargs = getargs
MinRK
get rid of redundant context manager
r16461 try:
MinRK
monkeypatch inspect.findsource in a context manager...
r16441 return f(*args, **kwargs)
MinRK
get rid of redundant context manager
r16461 finally:
inspect.findsource = save_findsource
Scott Sanderson
BUG: Fix getargvalues call with nested genexprs....
r21715 inspect.getargs = save_getargs
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156
MinRK
monkeypatch inspect.findsource in a context manager...
r16441 return wrapped
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156
Scott Sanderson
BUG: Fix getargvalues call with nested genexprs....
r21715 if py3compat.PY3:
fixed_getargvalues = inspect.getargvalues
else:
# Fixes for https://github.com/ipython/ipython/issues/8293
# and https://github.com/ipython/ipython/issues/8205.
# The relevant bug is caused by failure to correctly handle anonymous tuple
# unpacking, which only exists in Python 2.
fixed_getargvalues = with_patch_inspect(inspect.getargvalues)
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 def fix_frame_records_filenames(records):
"""Try to fix the filenames in each record from inspect.getinnerframes().
Particularly, modules loaded from within zip files have useless filenames
attached to their code object, and inspect.getinnerframes() just uses it.
"""
fixed_records = []
for frame, filename, line_no, func_name, lines, index in records:
Jeroen Demeyer
Keep Cython filenames in tracebacks
r19239 # Look inside the frame's globals dictionary for __file__,
# which should be better. However, keep Cython filenames since
# we prefer the source filenames over the compiled .so file.
Carlos Cordoba
Replace cast_unicode calls with cast_unicode_py2 ones...
r19866 filename = py3compat.cast_unicode_py2(filename, "utf-8")
Jeroen Demeyer
Keep Cython filenames in tracebacks
r19239 if not filename.endswith(('.pyx', '.pxd', '.pxi')):
better_fn = frame.f_globals.get('__file__', None)
if isinstance(better_fn, str):
# Check the type just in case someone did something weird with
# __file__. It might also be None if the error occurred during
# import.
filename = better_fn
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 fixed_records.append((frame, filename, line_no, func_name, lines, index))
return fixed_records
MinRK
monkeypatch inspect.findsource in a context manager...
r16441 @with_patch_inspect
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 def _fixed_getinnerframes(etb, context=1, tb_offset=0):
LNUM_POS, LINES_POS, INDEX_POS = 2, 4, 5
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 # If the error is at the console, don't build any context, since it would
# otherwise produce 5 blank lines printed out (there is no file at the
# console)
rec_check = records[tb_offset:]
try:
rname = rec_check[0][1]
if rname == '<ipython console>' or rname.endswith('<string>'):
return rec_check
except IndexError:
pass
aux = traceback.extract_tb(etb)
assert len(records) == len(aux)
for i, (file, lnum, _, _) in zip(range(len(records)), aux):
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 maybeStart = lnum - 1 - context // 2
start = max(maybeStart, 0)
end = start + context
Thomas Kluyver
Fixes for producing unicode tracebacks.
r8325 lines = ulinecache.getlines(file)[start:end]
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 buf = list(records[i])
buf[LNUM_POS] = lnum
buf[INDEX_POS] = lnum - 1 - start
buf[LINES_POS] = lines
records[i] = tuple(buf)
return records[tb_offset:]
# Helper function -- largely belongs to VerboseTB, but we need the same
# functionality to produce a pseudo verbose TB for SyntaxErrors, so that they
# can be recognized properly by ipython.el's py-traceback-line-re
# (SyntaxErrors have to be treated specially because they have no traceback)
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156
Matthias Bussonnier
Pass parent to child for configuration to propagate
r22943 def _format_traceback_lines(lnum, index, lines, Colors, lvals=None, _line_format=(lambda x,_:x,None)):
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 numbers_width = INDENT_SIZE - 1
res = []
i = lnum - index
for line in lines:
Jörgen Stenarson
Quick fix for #1688, traceback-unicode issue...
r8299 line = py3compat.cast_unicode(line)
Bernardo B. Marques
remove all trailling spaces
r4872
Matthias Bussonnier
Start refactoring handling of color....
r22911 new_line, err = _line_format(line, 'str')
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 if not err: line = new_line
Bernardo B. Marques
remove all trailling spaces
r4872
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 if i == lnum:
# This is the line with the error
pad = numbers_width - len(str(i))
Matthias Bussonnier
Refactor simplify and share common function.
r22083 num = '%s%s' % (debugger.make_arrow(pad), str(lnum))
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 line = '%s%s%s %s%s' % (Colors.linenoEm, num,
Colors.line, line, Colors.Normal)
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 else:
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 num = '%*s' % (numbers_width, i)
line = '%s%s%s %s' % (Colors.lineno, num,
Colors.Normal, line)
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
res.append(line)
if lvals and i == lnum:
res.append(lvals + '\n')
i = i + 1
return res
Thomas Kluyver
Truncate tracebacks on recursion error...
r21983 def is_recursion_error(etype, value, records):
try:
# RecursionError is new in Python 3.5
recursion_error_type = RecursionError
except NameError:
recursion_error_type = RuntimeError
# The default recursion limit is 1000, but some of that will be taken up
# by stack frames in IPython itself. >500 frames probably indicates
# a recursion error.
return (etype is recursion_error_type) \
and "recursion" in str(value).lower() \
and len(records) > 500
def find_recursion(etype, value, records):
"""Identify the repeating stack frames from a RecursionError traceback
'records' is a list as returned by VerboseTB.get_records()
Returns (last_unique, repeat_length)
"""
# This involves a bit of guesswork - we want to show enough of the traceback
# to indicate where the recursion is occurring. We guess that the innermost
# quarter of the traceback (250 frames by default) is repeats, and find the
# first frame (from in to out) that looks different.
if not is_recursion_error(etype, value, records):
return len(records), 0
# Select filename, lineno, func_name to track frames with
records = [r[1:4] for r in records]
inner_frames = records[-(len(records)//4):]
frames_repeated = set(inner_frames)
last_seen_at = {}
longest_repeat = 0
i = len(records)
for frame in reversed(records):
i -= 1
if frame not in frames_repeated:
last_unique = i
break
if frame in last_seen_at:
distance = last_seen_at[frame] - i
longest_repeat = max(longest_repeat, distance)
last_seen_at[frame] = i
else:
last_unique = 0 # The whole traceback was recursion
return last_unique, longest_repeat
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
#---------------------------------------------------------------------------
# Module classes
Matthias Bussonnier
Introduce some classes necessary fro Pygments refactor.
r22109 class TBTools(colorable.Colorable):
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 """Basic tools used by all traceback printer classes."""
Fernando Perez
Fix exception color problems in win32....
r2459
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 # Number of frames to skip when reporting tracebacks
tb_offset = 0
Matthias Bussonnier
Introduce some classes necessary fro Pygments refactor.
r22109 def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None):
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 # Whether to call the interactive pdb debugger after printing
# tracebacks or not
Matthias Bussonnier
Introduce some classes necessary fro Pygments refactor.
r22109 super(TBTools, self).__init__(parent=parent, config=config)
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 self.call_pdb = call_pdb
Fernando Perez
Simplified output stream management in ultratb....
r2852 # Output stream to write to. Note that we store the original value in
# a private attribute and then make the public ostream a property, so
Min RK
fix some deprecations...
r22742 # that we can delay accessing sys.stdout until runtime. The way
# things are written now, the sys.stdout object is dynamically managed
Fernando Perez
Simplified output stream management in ultratb....
r2852 # so a reference to it should NEVER be stored statically. This
# property approach confines this detail to a single location, and all
# subclasses can simply access self.ostream for writing.
self._ostream = ostream
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 # Create color table
Fernando Perez
Update exception colors API to PEP8, various small fixes in related files....
r1845 self.color_scheme_table = exception_colors()
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
self.set_colors(color_scheme)
self.old_scheme = color_scheme # save initial value for toggles
if call_pdb:
Min RK
fix some deprecations...
r22742 self.pdb = debugger.Pdb()
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 else:
self.pdb = None
Fernando Perez
Simplified output stream management in ultratb....
r2852 def _get_ostream(self):
"""Output stream that exceptions are written to.
Valid values are:
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Simplified output stream management in ultratb....
r2852 - None: the default, which means that IPython will dynamically resolve
Min RK
fix some deprecations...
r22742 to sys.stdout. This ensures compatibility with most tools, including
Thomas Kluyver
Improvements to docs formatting.
r12553 Windows (where plain stdout doesn't recognize ANSI escapes).
Fernando Perez
Simplified output stream management in ultratb....
r2852
- Any object with 'write' and 'flush' attributes.
"""
Thomas Kluyver
Deprecate io.{stdout,stderr} and shell.{write,write_err}...
r22192 return sys.stdout if self._ostream is None else self._ostream
Fernando Perez
Simplified output stream management in ultratb....
r2852
def _set_ostream(self, val):
assert val is None or (hasattr(val, 'write') and hasattr(val, 'flush'))
self._ostream = val
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Simplified output stream management in ultratb....
r2852 ostream = property(_get_ostream, _set_ostream)
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 def set_colors(self, *args, **kw):
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 """Shorthand access to the color table scheme selector method."""
# Set own color table
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 self.color_scheme_table.set_active_scheme(*args, **kw)
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 # for convenience, set Colors to the active scheme
self.Colors = self.color_scheme_table.active_colors
# Also set colors of debugger
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 if hasattr(self, 'pdb') and self.pdb is not None:
self.pdb.set_colors(*args, **kw)
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
def color_toggle(self):
"""Toggle between the currently active color scheme and NoColor."""
Bernardo B. Marques
remove all trailling spaces
r4872
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 if self.color_scheme_table.active_scheme_name == 'NoColor':
self.color_scheme_table.set_active_scheme(self.old_scheme)
self.Colors = self.color_scheme_table.active_colors
else:
self.old_scheme = self.color_scheme_table.active_scheme_name
self.color_scheme_table.set_active_scheme('NoColor')
self.Colors = self.color_scheme_table.active_colors
Fernando Perez
Multiple improvements to tab completion....
r2839 def stb2text(self, stb):
"""Convert a structured traceback (a list) to a string."""
return '\n'.join(stb)
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 def text(self, etype, value, tb, tb_offset=None, context=5):
"""Return formatted traceback.
Subclasses may override this if they add extra arguments.
"""
tb_list = self.structured_traceback(etype, value, tb,
tb_offset, context)
Fernando Perez
Multiple improvements to tab completion....
r2839 return self.stb2text(tb_list)
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838
def structured_traceback(self, etype, evalue, tb, tb_offset=None,
context=5, mode=None):
"""Return a list of traceback frames.
Must be implemented by each class.
"""
raise NotImplementedError()
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 #---------------------------------------------------------------------------
class ListTB(TBTools):
"""Print traceback information from a traceback list, with optional color.
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Miscellaneous docs fixes
r9244 Calling requires 3 arguments: (etype, evalue, elist)
as would be obtained by::
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 etype, evalue, tb = sys.exc_info()
if tb:
elist = traceback.extract_tb(tb)
else:
elist = None
It can thus be used by programs which need to process the traceback before
printing (such as console replacements based on the code module from the
standard library).
Because they are meant to be called without a full traceback (only a
list), instances of this class can't call the interactive pdb debugger."""
Matthias Bussonnier
Pass parent to child for configuration to propagate
r22943 def __init__(self, color_scheme='NoColor', call_pdb=False, ostream=None, parent=None, config=None):
Fernando Perez
Simplified output stream management in ultratb....
r2852 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
Matthias Bussonnier
Pass parent to child for configuration to propagate
r22943 ostream=ostream, parent=parent,config=config)
Bernardo B. Marques
remove all trailling spaces
r4872
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 def __call__(self, etype, value, elist):
Fernando Perez
Clean up small leftover uses of raw streams in exception handling.
r2860 self.ostream.flush()
self.ostream.write(self.text(etype, value, elist))
self.ostream.write('\n')
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 def structured_traceback(self, etype, value, elist, tb_offset=None,
context=5):
Fernando Perez
Lots of work on exception handling, including tests for traceback printing....
r2440 """Return a color formatted string with the traceback info.
Parameters
----------
etype : exception type
Type of the exception raised.
value : object
Data stored in the exception
elist : list
List of frames, see class docstring for details.
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 tb_offset : int, optional
Number of frames in the traceback to skip. If not given, the
instance value is used (set in constructor).
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 context : int, optional
Number of lines of context information to print.
Fernando Perez
Lots of work on exception handling, including tests for traceback printing....
r2440 Returns
-------
String with formatted exception.
"""
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 tb_offset = self.tb_offset if tb_offset is None else tb_offset
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 Colors = self.Colors
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 out_list = []
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 if elist:
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838
if tb_offset and len(elist) > tb_offset:
elist = elist[tb_offset:]
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 out_list.append('Traceback %s(most recent call last)%s:' %
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 (Colors.normalEm, Colors.Normal) + '\n')
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 out_list.extend(self._format_list(elist))
# The exception info should be a single entry in the list.
lines = ''.join(self._format_exception_only(etype, value))
out_list.append(lines)
# Note: this code originally read:
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 ## for line in lines[:-1]:
## out_list.append(" "+line)
## out_list.append(lines[-1])
# This means it was indenting everything but the last line by a little
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 # bit. I've disabled this for now, but if we see ugliness somewhere we
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 # can restore it.
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 return out_list
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
def _format_list(self, extracted_list):
"""Format a list of traceback entry tuples for printing.
Given a list of tuples as returned by extract_tb() or
extract_stack(), return a list of strings ready for printing.
Each string in the resulting list corresponds to the item with the
same index in the argument list. Each string ends in a newline;
the strings may contain internal newlines as well, for those items
whose source text line is not None.
Bernardo B. Marques
remove all trailling spaces
r4872
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 Lifted almost verbatim from traceback.py
"""
Colors = self.Colors
list = []
for filename, lineno, name, line in extracted_list[:-1]:
item = ' File %s"%s"%s, line %s%d%s, in %s%s%s\n' % \
Carlos Cordoba
Replace cast_unicode calls with cast_unicode_py2 ones...
r19866 (Colors.filename, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.Normal,
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 Colors.lineno, lineno, Colors.Normal,
Carlos Cordoba
Replace cast_unicode calls with cast_unicode_py2 ones...
r19866 Colors.name, py3compat.cast_unicode_py2(name, "utf-8"), Colors.Normal)
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 if line:
Thomas Kluyver
Use inplace addition in ultratb.
r5831 item += ' %s\n' % line.strip()
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 list.append(item)
# Emphasize the last entry
filename, lineno, name, line = extracted_list[-1]
item = '%s File %s"%s"%s, line %s%d%s, in %s%s%s%s\n' % \
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 (Colors.normalEm,
Carlos Cordoba
Replace cast_unicode calls with cast_unicode_py2 ones...
r19866 Colors.filenameEm, py3compat.cast_unicode_py2(filename, "utf-8"), Colors.normalEm,
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 Colors.linenoEm, lineno, Colors.normalEm,
Carlos Cordoba
Replace cast_unicode calls with cast_unicode_py2 ones...
r19866 Colors.nameEm, py3compat.cast_unicode_py2(name, "utf-8"), Colors.normalEm,
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 Colors.Normal)
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 if line:
Thomas Kluyver
Use inplace addition in ultratb.
r5831 item += '%s %s%s\n' % (Colors.line, line.strip(),
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 Colors.Normal)
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 list.append(item)
return list
Bernardo B. Marques
remove all trailling spaces
r4872
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 def _format_exception_only(self, etype, value):
"""Format the exception part of a traceback.
The arguments are the exception type and value such as given by
sys.exc_info()[:2]. The return value is a list of strings, each ending
in a newline. Normally, the list contains a single string; however,
for SyntaxError exceptions, it contains several lines that (when
printed) display detailed information about where the syntax error
occurred. The message indicating which exception occurred is the
always last string in the list.
Bernardo B. Marques
remove all trailling spaces
r4872
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 Also lifted nearly verbatim from traceback.py
"""
Fernando Perez
Checkpoint before merging with upstream
r1568 have_filedata = False
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 Colors = self.Colors
list = []
Min RK
handle unicode exception messages in xmode plain
r22269 stype = py3compat.cast_unicode(Colors.excName + etype.__name__ + Colors.Normal)
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 if value is None:
Thomas Kluyver
Fix display of SyntaxError in Python 3....
r5827 # Not sure if this can still happen in Python 2.6 and above
Min RK
handle unicode exception messages in xmode plain
r22269 list.append(stype + '\n')
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 else:
Thomas Kluyver
Make IndentationErrors display like other SyntaxErrors...
r8417 if issubclass(etype, SyntaxError):
Thomas Kluyver
Fix display of SyntaxError in Python 3....
r5827 have_filedata = True
if not value.filename: value.filename = "<string>"
Jez Ng
Avoid calling getline() if lineno is None.
r8590 if value.lineno:
lineno = value.lineno
textline = ulinecache.getline(value.filename, value.lineno)
else:
lineno = 'unknown'
textline = ''
Jez Ng
Fix traceback handling of SyntaxErrors without linenos.
r8589 list.append('%s File %s"%s"%s, line %s%s%s\n' % \
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 (Colors.normalEm,
Colors.filenameEm, py3compat.cast_unicode(value.filename), Colors.normalEm,
Colors.linenoEm, lineno, Colors.Normal ))
Jörgen Stenarson
Adding value.text fallback if ulinecache returns ''
r8330 if textline == '':
textline = py3compat.cast_unicode(value.text, "utf-8")
Jörgen Stenarson
Fix unicode problem with SyntaxError traceback
r8328
if textline is not None:
Thomas Kluyver
Fix display of SyntaxError in Python 3....
r5827 i = 0
Jörgen Stenarson
Fix unicode problem with SyntaxError traceback
r8328 while i < len(textline) and textline[i].isspace():
Thomas Kluyver
Use inplace addition in ultratb.
r5831 i += 1
Thomas Kluyver
Fix display of SyntaxError in Python 3....
r5827 list.append('%s %s%s\n' % (Colors.line,
Jörgen Stenarson
Fix unicode problem with SyntaxError traceback
r8328 textline.strip(),
Thomas Kluyver
Fix display of SyntaxError in Python 3....
r5827 Colors.Normal))
if value.offset is not None:
s = ' '
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 for c in textline[i:value.offset - 1]:
Thomas Kluyver
Fix display of SyntaxError in Python 3....
r5827 if c.isspace():
Thomas Kluyver
Use inplace addition in ultratb.
r5831 s += c
Thomas Kluyver
Fix display of SyntaxError in Python 3....
r5827 else:
Thomas Kluyver
Use inplace addition in ultratb.
r5831 s += ' '
Thomas Kluyver
Fix display of SyntaxError in Python 3....
r5827 list.append('%s%s^%s\n' % (Colors.caret, s,
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 Colors.Normal))
Thomas Kluyver
Fix display of SyntaxError in Python 3....
r5827
try:
s = value.msg
except Exception:
s = self._some_str(value)
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 if s:
Min RK
handle unicode exception messages in xmode plain
r22269 list.append('%s%s:%s %s\n' % (stype, Colors.excName,
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 Colors.Normal, s))
else:
Min RK
handle unicode exception messages in xmode plain
r22269 list.append('%s\n' % stype)
Fernando Perez
Merge Vivan De Smedt's branch on editor synchronization.
r1532
Fernando Perez
Lots of work on exception handling, including tests for traceback printing....
r2440 # sync with user hooks
Fernando Perez
Fix https://bugs.launchpad.net/ipython/+bug/362137...
r2088 if have_filedata:
MinRK
don't use deprecated ipapi.get...
r10581 ipinst = get_ipython()
Brian Granger
Merging -r 1177 from lp:ipython with fixes and resolutions....
r2124 if ipinst is not None:
Thomas Kluyver
Fix display of SyntaxError in Python 3....
r5827 ipinst.hooks.synchronize_with_editor(value.filename, value.lineno, 0)
Fernando Perez
Merge Vivan De Smedt's branch on editor synchronization.
r1532
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 return list
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 def get_exception_only(self, etype, value):
"""Only print the exception type and message, without a traceback.
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 Parameters
----------
etype : exception type
value : exception value
"""
return ListTB.structured_traceback(self, etype, value, [])
Brian Granger
Merge branch 'newkernel' into upstream-newkernel...
r2873 def show_exception_only(self, etype, evalue):
Fernando Perez
Lots of work on exception handling, including tests for traceback printing....
r2440 """Only print the exception type and message, without a traceback.
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Lots of work on exception handling, including tests for traceback printing....
r2440 Parameters
----------
etype : exception type
value : exception value
"""
# This method needs to use __call__ from *this* class, not the one from
# a subclass whose signature or behavior may be different
Fernando Perez
Simplified output stream management in ultratb....
r2852 ostream = self.ostream
Brian Granger
Changing how IPython.utils.io.Term is handled....
r2775 ostream.flush()
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 ostream.write('\n'.join(self.get_exception_only(etype, evalue)))
Brian Granger
Changing how IPython.utils.io.Term is handled....
r2775 ostream.flush()
Fernando Perez
Lots of work on exception handling, including tests for traceback printing....
r2440
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 def _some_str(self, value):
# Lifted from traceback.py
try:
Min RK
handle unicode exception messages in xmode plain
r22269 return py3compat.cast_unicode(str(value))
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 except:
Min RK
handle unicode exception messages in xmode plain
r22269 return u'<unprintable %s object>' % type(value).__name__
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 #----------------------------------------------------------------------------
class VerboseTB(TBTools):
"""A port of Ka-Ping Yee's cgitb.py module that outputs color text instead
of HTML. Requires inspect and pydoc. Crazy, man.
Modified version which optionally strips the topmost entries from the
traceback, to be used with alternate interpreters (because their own code
would appear in the traceback)."""
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 def __init__(self, color_scheme='Linux', call_pdb=False, ostream=None,
Fernando Perez
Complete implementation of interactive traceback support....
r3175 tb_offset=0, long_header=False, include_vars=True,
Matthias Bussonnier
Pass parent to child for configuration to propagate
r22943 check_cache=None, debugger_cls = None,
parent=None, config=None):
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 """Specify traceback offset, headers and color scheme.
Define how many frames to drop from the tracebacks. Calling it with
tb_offset=1 allows use of this handler in interpreters which will have
their own code at the top of the traceback (VerboseTB will first
remove that frame before printing the traceback info)."""
Fernando Perez
Simplified output stream management in ultratb....
r2852 TBTools.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
Matthias Bussonnier
Pass parent to child for configuration to propagate
r22943 ostream=ostream, parent=parent, config=config)
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 self.tb_offset = tb_offset
self.long_header = long_header
self.include_vars = include_vars
Fernando Perez
Complete implementation of interactive traceback support....
r3175 # By default we use linecache.checkcache, but the user can provide a
# different check_cache implementation. This is used by the IPython
# kernel to provide tracebacks for interactive code that is cached,
# by a compiler instance that flushes the linecache but preserves its
# own code cache.
if check_cache is None:
check_cache = linecache.checkcache
self.check_cache = check_cache
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
Thomas Kluyver
Create separate class for debugger using prompt_toolkit
r22391 self.debugger_cls = debugger_cls or debugger.Pdb
Thomas Kluyver
Truncate tracebacks on recursion error...
r21983 def format_records(self, records, last_unique, recursion_repeat):
"""Format the stack frames of the traceback"""
frames = []
for r in records[:last_unique+recursion_repeat+1]:
#print '*** record:',file,lnum,func,lines,index # dbg
frames.append(self.format_record(*r))
if recursion_repeat:
Thomas Kluyver
Tweak elision message
r21996 frames.append('... last %d frames repeated, from the frame below ...\n' % recursion_repeat)
Thomas Kluyver
Truncate tracebacks on recursion error...
r21983 frames.append(self.format_record(*records[last_unique+recursion_repeat+1]))
return frames
def format_record(self, frame, file, lnum, func, lines, index):
"""Format a single stack frame"""
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 Colors = self.Colors # just a shorthand + quicker name lookup
ColorsNormal = Colors.Normal # used a lot
col_scheme = self.color_scheme_table.active_scheme_name
indent = ' ' * INDENT_SIZE
em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal)
undefined = '%sundefined%s' % (Colors.em, ColorsNormal)
tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
tpl_call = 'in %s%%s%s%%s%s' % (Colors.vName, Colors.valEm,
ColorsNormal)
tpl_call_fail = 'in %s%%s%s(***failed resolving arguments***)%s' % \
(Colors.vName, Colors.valEm, ColorsNormal)
tpl_local_var = '%s%%s%s' % (Colors.vName, ColorsNormal)
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 tpl_global_var = '%sglobal%s %s%%s%s' % (Colors.em, ColorsNormal,
Colors.vName, ColorsNormal)
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 tpl_name_val = '%%s %s= %%s%s' % (Colors.valEm, ColorsNormal)
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 tpl_line = '%s%%s%s %%s' % (Colors.lineno, ColorsNormal)
tpl_line_em = '%s%%s%s %%s%s' % (Colors.linenoEm, Colors.line,
ColorsNormal)
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
abspath = os.path.abspath
Jeroen Demeyer
Use sys.path to make filename in traceback absolute
r19120
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Truncate tracebacks on recursion error...
r21983 if not file:
file = '?'
elif file.startswith(str("<")) and file.endswith(str(">")):
# Not a real filename, no problem...
pass
elif not os.path.isabs(file):
# Try to make the filename absolute by trying all
# sys.path entries (which is also what linecache does)
for dirname in sys.path:
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 try:
Thomas Kluyver
Truncate tracebacks on recursion error...
r21983 fullname = os.path.join(dirname, file)
if os.path.isfile(fullname):
file = os.path.abspath(fullname)
Thomas Kluyver
Simplify tokenizing code for VerboseTB.
r5853 break
Thomas Kluyver
Truncate tracebacks on recursion error...
r21983 except Exception:
# Just in case that sys.path contains very
# strange entries...
pass
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156
Thomas Kluyver
Truncate tracebacks on recursion error...
r21983 file = py3compat.cast_unicode(file, util_path.fs_encoding)
link = tpl_link % file
args, varargs, varkw, locals = fixed_getargvalues(frame)
if func == '?':
call = ''
else:
# Decide whether to include variable details or not
var_repr = self.include_vars and eqrepr or nullrepr
try:
call = tpl_call % (func, inspect.formatargvalues(args,
varargs, varkw,
locals, formatvalue=var_repr))
except KeyError:
# This happens in situations like errors inside generator
# expressions, where local variables are listed in the
# line, but can't be extracted from the frame. I'm not
# 100% sure this isn't actually a bug in inspect itself,
# but since there's no info for us to compute with, the
# best we can do is report the failure and move on. Here
# we must *not* call any traceback construction again,
# because that would mess up use of %debug later on. So we
# simply report the failure and move on. The only
# limitation will be that this frame won't have locals
# listed in the call signature. Quite subtle problem...
# I can't think of a good way to validate this in a unit
# test, but running a script consisting of:
# dict( (k,v.strip()) for (k,v) in range(10) )
# will illustrate the error, if this exception catch is
# disabled.
call = tpl_call_fail % func
# Don't attempt to tokenize binary files.
if file.endswith(('.so', '.pyd', '.dll')):
return '%s %s\n' % (link, call)
elif file.endswith(('.pyc', '.pyo')):
# Look up the corresponding source file.
Thomas Kluyver
Catch failure to find source file for traceback...
r22357 try:
file = openpy.source_from_cache(file)
except ValueError:
# Failed to get the source file for some reason
# E.g. https://github.com/ipython/ipython/issues/9486
return '%s %s\n' % (link, call)
Thomas Kluyver
Truncate tracebacks on recursion error...
r21983
def linereader(file=file, lnum=[lnum], getline=ulinecache.getline):
line = getline(file, lnum[0])
lnum[0] += 1
return line
# Build the list of names on this line of code where the exception
# occurred.
try:
names = []
name_cont = False
for token_type, token, start, end, line in generate_tokens(linereader):
# build composite names
if token_type == tokenize.NAME and token not in keyword.kwlist:
if name_cont:
# Continuation of a dotted name
try:
names[-1].append(token)
except IndexError:
names.append([token])
name_cont = False
else:
# Regular new names. We append everything, the caller
# will be responsible for pruning the list later. It's
# very tricky to try to prune as we go, b/c composite
# names can fool us. The pruning at the end is easy
# to do (or the caller can print a list with repeated
# names if so desired.
names.append([token])
elif token == '.':
name_cont = True
elif token_type == tokenize.NEWLINE:
break
except (IndexError, UnicodeDecodeError, SyntaxError):
# signals exit of tokenizer
# SyntaxError can occur if the file is not actually Python
# - see gh-6300
pass
except tokenize.TokenError as msg:
_m = ("An unexpected error occurred while tokenizing input\n"
"The following traceback may be corrupted or invalid\n"
"The error message is: %s\n" % msg)
error(_m)
# Join composite names (e.g. "dict.fromkeys")
names = ['.'.join(n) for n in names]
# prune names list of duplicates, but keep the right order
unique_names = uniq_stable(names)
# Start loop over vars
lvals = []
if self.include_vars:
for name_full in unique_names:
name_base = name_full.split('.', 1)[0]
if name_base in frame.f_code.co_varnames:
if name_base in locals:
try:
value = repr(eval(name_full, locals))
except:
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 value = undefined
else:
Thomas Kluyver
Truncate tracebacks on recursion error...
r21983 value = undefined
name = tpl_local_var % name_full
else:
if name_base in frame.f_globals:
try:
value = repr(eval(name_full, frame.f_globals))
except:
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 value = undefined
Thomas Kluyver
Truncate tracebacks on recursion error...
r21983 else:
value = undefined
name = tpl_global_var % name_full
lvals.append(tpl_name_val % (name, value))
if lvals:
lvals = '%s%s' % (indent, em_normal.join(lvals))
else:
lvals = ''
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
Thomas Kluyver
Truncate tracebacks on recursion error...
r21983 level = '%s %s\n' % (link, call)
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
Thomas Kluyver
Truncate tracebacks on recursion error...
r21983 if index is None:
return level
else:
Matthias Bussonnier
Pass parent to child for configuration to propagate
r22943 _line_format = PyColorize.Parser(style=col_scheme, parent=self).format2
Thomas Kluyver
Truncate tracebacks on recursion error...
r21983 return '%s%s' % (level, ''.join(
_format_traceback_lines(lnum, index, lines, Colors, lvals,
Matthias Bussonnier
Pass parent to child for configuration to propagate
r22943 _line_format)))
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157
def prepare_chained_exception_message(self, cause):
direct_cause = "\nThe above exception was the direct cause of the following exception:\n"
exception_during_handling = "\nDuring handling of the above exception, another exception occurred:\n"
if cause:
Justyna Ilczuk
i1673 change formatting (dashed line only on top)
r17163 message = [[direct_cause]]
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 else:
Justyna Ilczuk
i1673 change formatting (dashed line only on top)
r17163 message = [[exception_during_handling]]
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 return message
def prepare_header(self, etype, long_version=False):
colors = self.Colors # just a shorthand + quicker name lookup
colorsnormal = colors.Normal # used a lot
exc = '%s%s%s' % (colors.excName, etype, colorsnormal)
Vincent Woo
Use term width to determine header length
r22150 width = min(75, get_terminal_size()[0])
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 if long_version:
# Header with the exception type, python version, and date
pyver = 'Python ' + sys.version.split()[0] + ': ' + sys.executable
date = time.ctime(time.time())
Vincent Woo
Use term width to determine header length
r22150 head = '%s%s%s\n%s%s%s\n%s' % (colors.topline, '-' * width, colorsnormal,
exc, ' ' * (width - len(str(etype)) - len(pyver)),
pyver, date.rjust(width) )
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 head += "\nA problem occurred executing Python code. Here is the sequence of function" \
"\ncalls leading up to the error, with the most recent (innermost) call last."
else:
# Simplified header
Justyna Ilczuk
i1673 change formatting (dashed line only on top)
r17163 head = '%s%s' % (exc, 'Traceback (most recent call last)'. \
Vincent Woo
Use term width to determine header length
r22150 rjust(width - len(str(etype))) )
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157
return head
def format_exception(self, etype, evalue):
colors = self.Colors # just a shorthand + quicker name lookup
colorsnormal = colors.Normal # used a lot
indent = ' ' * INDENT_SIZE
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 # Get (safely) a string form of the exception info
try:
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 etype_str, evalue_str = map(str, (etype, evalue))
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 except:
# User exception is improperly defined.
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 etype, evalue = str, sys.exc_info()[:2]
etype_str, evalue_str = map(str, (etype, evalue))
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 # ... and format it
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 exception = ['%s%s%s: %s' % (colors.excName, etype_str,
colorsnormal, py3compat.cast_unicode(evalue_str))]
Thomas Kluyver
Fixes for UltraTB and PyColorize with Python 3
r4758 if (not py3compat.PY3) and type(evalue) is types.InstanceType:
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 try:
Thomas Kluyver
Replace references to unicode and basestring
r13353 names = [w for w in dir(evalue) if isinstance(w, py3compat.string_types)]
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 except:
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 # Every now and then, an object with funny internals blows up
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 # when dir() is called on it. We do the best we can to report
# the problem and continue
_m = '%sException reporting error (object with broken dir())%s:'
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 exception.append(_m % (colors.excName, colorsnormal))
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 etype_str, evalue_str = map(str, sys.exc_info()[:2])
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 exception.append('%s%s%s: %s' % (colors.excName, etype_str,
colorsnormal, py3compat.cast_unicode(evalue_str)))
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 names = []
for name in names:
value = text_repr(getattr(evalue, name))
exception.append('\n%s%s = %s' % (indent, name, value))
Justyna Ilczuk
i1673 fix py2 bug in ultratb
r17160
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 return exception
Fernando Perez
Merge Vivan De Smedt's branch on editor synchronization.
r1532
Justyna Ilczuk
i1673 some DRY refactoring
r17158 def format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset):
Thomas Kluyver
Truncate tracebacks on recursion error...
r21983 """Formats the header, traceback and exception message for a single exception.
This may be called multiple times by Python 3 exception chaining
(PEP 3134).
"""
Justyna Ilczuk
i1673 some DRY refactoring
r17158 # some locals
Thomas Kluyver
Truncate tracebacks on recursion error...
r21983 orig_etype = etype
Justyna Ilczuk
i1673 some DRY refactoring
r17158 try:
etype = etype.__name__
except AttributeError:
pass
tb_offset = self.tb_offset if tb_offset is None else tb_offset
head = self.prepare_header(etype, self.long_header)
records = self.get_records(etb, number_of_lines_of_context, tb_offset)
if records is None:
return ""
Thomas Kluyver
Truncate tracebacks on recursion error...
r21983 last_unique, recursion_repeat = find_recursion(orig_etype, evalue, records)
frames = self.format_records(records, last_unique, recursion_repeat)
Hoyt Koepke
BUGFIX: check for None moved earlier....
r21751
Justyna Ilczuk
i1673 some DRY refactoring
r17158 formatted_exception = self.format_exception(etype, evalue)
if records:
filepath, lnum = records[-1][1:3]
filepath = os.path.abspath(filepath)
ipinst = get_ipython()
if ipinst is not None:
ipinst.hooks.synchronize_with_editor(filepath, lnum, 0)
return [[head] + frames + [''.join(formatted_exception[0])]]
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 def get_records(self, etb, number_of_lines_of_context, tb_offset):
try:
# Try the default getinnerframes and Alex's: Alex's fixes some
# problems, but it generates empty tracebacks for console errors
# (5 blanks lines) where none should be returned.
return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
Thomas Kluyver
Catch UnicodeDecodeError from file lying about its encoding...
r22880 except UnicodeDecodeError:
# This can occur if a file's encoding magic comment is wrong.
# I can't see a way to recover without duplicating a bunch of code
# from the stdlib traceback module. --TK
error('\nUnicodeDecodeError while processing traceback.\n')
return None
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 except:
# FIXME: I've been getting many crash reports from python 2.3
# users, traceable to inspect.py. If I can find a small test-case
# to reproduce this, I should either write a better workaround or
# file a bug report against inspect (if that's the real problem).
# So far, I haven't been able to find an isolated example to
# reproduce the problem.
inspect_error()
traceback.print_exc(file=self.ostream)
info('\nUnfortunately, your original traceback can not be constructed.\n')
return None
Bernardo B. Marques
remove all trailling spaces
r4872
Justyna Ilczuk
i1673 use __cause__ first in chained exceptions
r17164 def get_parts_of_chained_exception(self, evalue):
def get_chained_exception(exception_value):
cause = getattr(exception_value, '__cause__', None)
if cause:
return cause
Thomas Kluyver
Fix suppression of exception chaining with 'from None'...
r21520 if getattr(exception_value, '__suppress_context__', False):
return None
Justyna Ilczuk
i1673 use __cause__ first in chained exceptions
r17164 return getattr(exception_value, '__context__', None)
chained_evalue = get_chained_exception(evalue)
if chained_evalue:
return chained_evalue.__class__, chained_evalue, chained_evalue.__traceback__
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157
def structured_traceback(self, etype, evalue, etb, tb_offset=None,
number_of_lines_of_context=5):
"""Return a nice text document describing the traceback."""
Justyna Ilczuk
i1673 remove confusing traceback truncating
r17162
formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
tb_offset)
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157
Justyna Ilczuk
i1673 change formatting (dashed line only on top)
r17163 colors = self.Colors # just a shorthand + quicker name lookup
colorsnormal = colors.Normal # used a lot
Vincent Woo
Use term width to determine header length
r22150 head = '%s%s%s' % (colors.topline, '-' * min(75, get_terminal_size()[0]), colorsnormal)
Justyna Ilczuk
i1673 change formatting (dashed line only on top)
r17163 structured_traceback_parts = [head]
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 if py3compat.PY3:
Justyna Ilczuk
i1673 remove confusing traceback truncating
r17162 chained_exceptions_tb_offset = 0
lines_of_context = 3
formatted_exceptions = formatted_exception
Justyna Ilczuk
i1673 use __cause__ first in chained exceptions
r17164 exception = self.get_parts_of_chained_exception(evalue)
Justyna Ilczuk
i1673 remove confusing traceback truncating
r17162 if exception:
formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
etype, evalue, etb = exception
else:
evalue = None
Guillaume DOUMENC
Update ultratb.py
r21901 chained_exc_ids = set()
Justyna Ilczuk
i1673 some DRY refactoring
r17158 while evalue:
Justyna Ilczuk
i1673 remove confusing traceback truncating
r17162 formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,
chained_exceptions_tb_offset)
Justyna Ilczuk
i1673 use __cause__ first in chained exceptions
r17164 exception = self.get_parts_of_chained_exception(evalue)
Guillaume DOUMENC
Update ultratb.py
r21901 if exception and not id(exception[1]) in chained_exc_ids:
chained_exc_ids.add(id(exception[1])) # trace exception to avoid infinite 'cause' loop
Justyna Ilczuk
i1673 some DRY refactoring
r17158 formatted_exceptions += self.prepare_chained_exception_message(evalue.__cause__)
etype, evalue, etb = exception
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 else:
Justyna Ilczuk
i1673 some DRY refactoring
r17158 evalue = None
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157
Justyna Ilczuk
i1673 some DRY refactoring
r17158 # we want to see exceptions in a reversed order:
# the first exception should be on top
for formatted_exception in reversed(formatted_exceptions):
structured_traceback_parts += formatted_exception
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 else:
Justyna Ilczuk
i1673 change formatting (dashed line only on top)
r17163 structured_traceback_parts += formatted_exception[0]
Justyna Ilczuk
i1673 remove confusing traceback truncating
r17162
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 return structured_traceback_parts
Bernardo B. Marques
remove all trailling spaces
r4872
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 def debugger(self, force=False):
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 """Call up the pdb debugger if desired, always clean up the tb
reference.
Keywords:
- force(False): by default, this routine checks the instance call_pdb
Thomas Kluyver
Improvements to docs formatting.
r12553 flag and does not actually invoke the debugger if the flag is false.
The 'force' option forces the debugger to activate even if the flag
is false.
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
If the call_pdb flag is set, the pdb interactive debugger is
invoked. In all cases, the self.tb reference to the current traceback
is deleted to prevent lingering references which hamper memory
management.
Note that each call to pdb() does an 'import readline', so if your app
requires a special setup for the readline completers, you'll have to
fix that by hand after invoking the exception handler."""
if force or self.call_pdb:
if self.pdb is None:
Matthias Bussonnier
Start refactoring handling of color....
r22911 self.pdb = self.debugger_cls()
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 # the system displayhook may have changed, restore the original
# for pdb
Brian Granger
Fixing two minor bugs....
r2816 display_trap = DisplayTrap(hook=sys.__displayhook__)
Brian Granger
sys.displayhook is now managed dynamically by display_trap.
r2231 with display_trap:
self.pdb.reset()
# Find the right frame so we don't pop up inside ipython itself
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 if hasattr(self, 'tb') and self.tb is not None:
Brian Granger
sys.displayhook is now managed dynamically by display_trap.
r2231 etb = self.tb
else:
etb = self.tb = sys.last_traceback
Fernando Perez
Fix minor error that I saw in some odd cases, not sure how to test for it....
r2438 while self.tb is not None and self.tb.tb_next is not None:
Brian Granger
sys.displayhook is now managed dynamically by display_trap.
r2231 self.tb = self.tb.tb_next
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 if etb and etb.tb_next:
etb = etb.tb_next
self.pdb.botframe = etb.tb_frame
self.pdb.interaction(self.tb.tb_frame, self.tb)
Brian Granger
sys.displayhook is now managed dynamically by display_trap.
r2231
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 if hasattr(self, 'tb'):
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 del self.tb
def handler(self, info=None):
(etype, evalue, etb) = info or sys.exc_info()
self.tb = etb
Fernando Perez
Simplified output stream management in ultratb....
r2852 ostream = self.ostream
ostream.flush()
ostream.write(self.text(etype, evalue, etb))
ostream.write('\n')
ostream.flush()
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
# Changed so an instance can just be called as VerboseTB_inst() and print
# out the right info on its own.
def __call__(self, etype=None, evalue=None, etb=None):
"""This hook can replace sys.excepthook (for Python 2.1 or higher)."""
if etb is None:
self.handler()
else:
self.handler((etype, evalue, etb))
try:
self.debugger()
except KeyboardInterrupt:
Thomas Kluyver
Convert print statements to print function calls...
r13348 print("\nKeyboardInterrupt")
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 #----------------------------------------------------------------------------
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 class FormattedTB(VerboseTB, ListTB):
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 """Subclass ListTB but allow calling with a traceback.
It can thus be used as a sys.excepthook for Python > 2.1.
Also adds 'Context' and 'Verbose' modes, not available in ListTB.
Allows a tb_offset to be specified. This is useful for situations where
one needs to remove a number of topmost frames from the traceback (such as
occurs with python programs that themselves execute other python code,
like Python shells). """
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Simplified output stream management in ultratb....
r2852 def __init__(self, mode='Plain', color_scheme='Linux', call_pdb=False,
Bernardo B. Marques
remove all trailling spaces
r4872 ostream=None,
Fernando Perez
Complete implementation of interactive traceback support....
r3175 tb_offset=0, long_header=False, include_vars=False,
Matthias Bussonnier
Pass parent to child for configuration to propagate
r22943 check_cache=None, debugger_cls=None,
parent=None, config=None):
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
# NEVER change the order of this list. Put new modes at the end:
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 self.valid_modes = ['Plain', 'Context', 'Verbose']
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 self.verbose_modes = self.valid_modes[1:3]
Fernando Perez
Simplified output stream management in ultratb....
r2852 VerboseTB.__init__(self, color_scheme=color_scheme, call_pdb=call_pdb,
ostream=ostream, tb_offset=tb_offset,
Fernando Perez
Complete implementation of interactive traceback support....
r3175 long_header=long_header, include_vars=include_vars,
Matthias Bussonnier
Pass parent to child for configuration to propagate
r22943 check_cache=check_cache, debugger_cls=debugger_cls,
parent=parent, config=config)
Fernando Perez
Multiple improvements to tab completion....
r2839
# Different types of tracebacks are joined with different separators to
# form a single string. They are taken from this dict
self._join_chars = dict(Plain='', Context='\n', Verbose='\n')
# set_mode also sets the tb_join_char attribute
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 self.set_mode(mode)
Bernardo B. Marques
remove all trailling spaces
r4872
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 def _extract_tb(self, tb):
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 if tb:
return traceback.extract_tb(tb)
else:
return None
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 def structured_traceback(self, etype, value, tb, tb_offset=None, number_of_lines_of_context=5):
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 tb_offset = self.tb_offset if tb_offset is None else tb_offset
Fernando Perez
Multiple improvements to tab completion....
r2839 mode = self.mode
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 if mode in self.verbose_modes:
Brian Granger
Initial refactoring to support structured traceback information.
r2792 # Verbose modes need a full traceback
return VerboseTB.structured_traceback(
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 self, etype, value, tb, tb_offset, number_of_lines_of_context
Brian Granger
Initial refactoring to support structured traceback information.
r2792 )
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 else:
# We must check the source cache because otherwise we can print
# out-of-date source code.
Fernando Perez
Complete implementation of interactive traceback support....
r3175 self.check_cache()
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 # Now we can extract and format the exception
elist = self._extract_tb(tb)
Brian Granger
Initial refactoring to support structured traceback information.
r2792 return ListTB.structured_traceback(
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 self, etype, value, elist, tb_offset, number_of_lines_of_context
Brian Granger
Initial refactoring to support structured traceback information.
r2792 )
Fernando Perez
Multiple improvements to tab completion....
r2839 def stb2text(self, stb):
"""Convert a structured traceback (a list) to a string."""
return self.tb_join_char.join(stb)
Bernardo B. Marques
remove all trailling spaces
r4872
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 def set_mode(self, mode=None):
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 """Switch to the desired mode.
If mode is not specified, cycles through the available modes."""
if not mode:
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 new_idx = (self.valid_modes.index(self.mode) + 1 ) % \
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 len(self.valid_modes)
self.mode = self.valid_modes[new_idx]
elif mode not in self.valid_modes:
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 raise ValueError('Unrecognized mode in FormattedTB: <' + mode + '>\n'
'Valid modes: ' + str(self.valid_modes))
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 else:
self.mode = mode
# include variable details only in 'Verbose' mode
self.include_vars = (self.mode == self.valid_modes[2])
Fernando Perez
Multiple improvements to tab completion....
r2839 # Set the join character for generating text tracebacks
Fernando Perez
Fix small bug where %xmode without arguments failed to run....
r3146 self.tb_join_char = self._join_chars[self.mode]
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 # some convenient shortcuts
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 def plain(self):
self.set_mode(self.valid_modes[0])
def context(self):
self.set_mode(self.valid_modes[1])
def verbose(self):
self.set_mode(self.valid_modes[2])
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 #----------------------------------------------------------------------------
class AutoFormattedTB(FormattedTB):
"""A traceback printer which can be called on the fly.
It will find out about exceptions by itself.
Thomas Kluyver
Miscellaneous docs fixes
r9244 A brief example::
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Miscellaneous docs fixes
r9244 AutoTB = AutoFormattedTB(mode = 'Verbose',color_scheme='Linux')
try:
...
except:
AutoTB() # or AutoTB(out=logfile) where logfile is an open file object
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 """
Bernardo B. Marques
remove all trailling spaces
r4872
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 def __call__(self, etype=None, evalue=None, etb=None,
out=None, tb_offset=None):
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 """Print out a formatted exception traceback.
Optional arguments:
- out: an open file-like object to direct output to.
- tb_offset: the number of frames to skip over in the stack, on a
per-call basis (this overrides temporarily the instance's tb_offset
given at initialization time. """
Fernando Perez
Lots of work on exception handling, including tests for traceback printing....
r2440
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 if out is None:
Fernando Perez
Simplified output stream management in ultratb....
r2852 out = self.ostream
Brian Granger
Changing how IPython.utils.io.Term is handled....
r2775 out.flush()
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 out.write(self.text(etype, evalue, etb, tb_offset))
out.write('\n')
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 out.flush()
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838 # FIXME: we should remove the auto pdb behavior from here and leave
# that to the clients.
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 try:
self.debugger()
except KeyboardInterrupt:
Thomas Kluyver
Convert print statements to print function calls...
r13348 print("\nKeyboardInterrupt")
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
Brian Granger
Initial refactoring to support structured traceback information.
r2792 def structured_traceback(self, etype=None, value=None, tb=None,
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 tb_offset=None, number_of_lines_of_context=5):
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 if etype is None:
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 etype, value, tb = sys.exc_info()
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 self.tb = tb
Brian Granger
Initial refactoring to support structured traceback information.
r2792 return FormattedTB.structured_traceback(
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 self, etype, value, tb, tb_offset, number_of_lines_of_context)
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 #---------------------------------------------------------------------------
Brian Granger
Initial refactoring to support structured traceback information.
r2792
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234 # A simple class to preserve Nathan's original functionality.
class ColorTB(FormattedTB):
"""Shorthand to initialize a FormattedTB in Linux colors mode."""
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156
Matthias Bussonnier
Move ultratb test to be run on CI.
r21789 def __init__(self, color_scheme='Linux', call_pdb=0, **kwargs):
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 FormattedTB.__init__(self, color_scheme=color_scheme,
Matthias Bussonnier
Move ultratb test to be run on CI.
r21789 call_pdb=call_pdb, **kwargs)
Brian E Granger
This is a manual merge of certain things in the ipython1-dev branch, revision 46, into the main ...
r1234
Brian Granger
Initial refactoring to support structured traceback information.
r2792
class SyntaxTB(ListTB):
"""Extension which holds some state: the last exception value"""
Matthias Bussonnier
Pass parent to child for configuration to propagate
r22943 def __init__(self, color_scheme='NoColor', parent=None, config=None):
ListTB.__init__(self, color_scheme, parent=parent, config=config)
Brian Granger
Initial refactoring to support structured traceback information.
r2792 self.last_syntax_error = None
def __call__(self, etype, value, elist):
self.last_syntax_error = value
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 ListTB.__call__(self, etype, value, elist)
Brian Granger
Initial refactoring to support structured traceback information.
r2792
Thomas Kluyver
Fix display of SyntaxError when .py file is modified.
r12544 def structured_traceback(self, etype, value, elist, tb_offset=None,
context=5):
# If the source file has been edited, the line in the syntax error can
# be wrong (retrieved from an outdated cache). This replaces it with
# the current value.
Thomas Kluyver
Don't assume that SyntaxTB is always called with a SyntaxError...
r12951 if isinstance(value, SyntaxError) \
and isinstance(value.filename, py3compat.string_types) \
Thomas Kluyver
Check for string_types instead of str
r12545 and isinstance(value.lineno, int):
Thomas Kluyver
Fix display of SyntaxError when .py file is modified.
r12544 linecache.checkcache(value.filename)
newtext = ulinecache.getline(value.filename, value.lineno)
if newtext:
value.text = newtext
Thomas Kluyver
Restore option to auto-edit files on syntax error
r22204 self.last_syntax_error = value
Thomas Kluyver
Fix display of SyntaxError when .py file is modified.
r12544 return super(SyntaxTB, self).structured_traceback(etype, value, elist,
Justyna Ilczuk
i1673 PEP8 formatting in `ultratab.py`
r17156 tb_offset=tb_offset, context=context)
Thomas Kluyver
Fix display of SyntaxError when .py file is modified.
r12544
Brian Granger
Initial refactoring to support structured traceback information.
r2792 def clear_err_state(self):
"""Return the current error state and clear it"""
e = self.last_syntax_error
self.last_syntax_error = None
return e
Fernando Perez
Multiple improvements to tab completion....
r2839 def stb2text(self, stb):
"""Convert a structured traceback (a list) to a string."""
return ''.join(stb)
Fernando Perez
Improvements to exception handling to transport structured tracebacks....
r2838
Justyna Ilczuk
i1673 implementation of py3 proper error handling...
r17157 # some internal-use functions
def text_repr(value):
"""Hopefully pretty robust repr equivalent."""
# this is pretty horrible but should always return *something*
try:
return pydoc.text.repr(value)
except KeyboardInterrupt:
raise
except:
try:
return repr(value)
except KeyboardInterrupt:
raise
except:
try:
# all still in an except block so we catch
# getattr raising
name = getattr(value, '__name__', None)
if name:
# ick, recursion
return text_repr(name)
klass = getattr(value, '__class__', None)
if klass:
return '%s instance' % text_repr(klass)
except KeyboardInterrupt:
raise
except:
return 'UNRECOVERABLE REPR FAILURE'
def eqrepr(value, repr=text_repr):
return '=%s' % repr(value)
def nullrepr(value, repr=text_repr):
return ''