##// END OF EJS Templates
Use Pygments instead of PyColorize to syntax highlight tracebacks
Alex Hall -
Show More
@@ -97,13 +97,13 b' import time'
97 import traceback
97 import traceback
98
98
99 import stack_data
99 import stack_data
100 from pygments.formatters.terminal import TerminalFormatter
100
101
101 # IPython's own modules
102 # IPython's own modules
102 from IPython import get_ipython
103 from IPython import get_ipython
103 from IPython.core import debugger
104 from IPython.core import debugger
104 from IPython.core.display_trap import DisplayTrap
105 from IPython.core.display_trap import DisplayTrap
105 from IPython.core.excolors import exception_colors
106 from IPython.core.excolors import exception_colors
106 from IPython.utils import PyColorize
107 from IPython.utils import path as util_path
107 from IPython.utils import path as util_path
108 from IPython.utils import py3compat
108 from IPython.utils import py3compat
109 from IPython.utils.terminal import get_terminal_size
109 from IPython.utils.terminal import get_terminal_size
@@ -129,7 +129,7 b" DEFAULT_SCHEME = 'NoColor'"
129 # (SyntaxErrors have to be treated specially because they have no traceback)
129 # (SyntaxErrors have to be treated specially because they have no traceback)
130
130
131
131
132 def _format_traceback_lines(lines, Colors, lvals, _line_format):
132 def _format_traceback_lines(lines, Colors, lvals):
133 """
133 """
134 Format tracebacks lines with pointing arrow, leading numbers...
134 Format tracebacks lines with pointing arrow, leading numbers...
135
135
@@ -141,8 +141,6 b' def _format_traceback_lines(lines, Colors, lvals, _line_format):'
141 ColorScheme used.
141 ColorScheme used.
142 lvals: str
142 lvals: str
143 Values of local variables, already colored, to inject just after the error line.
143 Values of local variables, already colored, to inject just after the error line.
144 _line_format: f (str) -> (str, bool)
145 return (colorized version of str, failure to do so)
146 """
144 """
147 numbers_width = INDENT_SIZE - 1
145 numbers_width = INDENT_SIZE - 1
148 res = []
146 res = []
@@ -152,23 +150,18 b' def _format_traceback_lines(lines, Colors, lvals, _line_format):'
152 res.append('%s (...)%s\n' % (Colors.linenoEm, Colors.Normal))
150 res.append('%s (...)%s\n' % (Colors.linenoEm, Colors.Normal))
153 continue
151 continue
154
152
155 line = stack_line.text.rstrip('\n') + '\n'
153 line = stack_line.render(pygmented=True).rstrip('\n') + '\n'
156
157 new_line, err = _line_format(line, 'str')
158 if not err:
159 line = new_line
160
161 lineno = stack_line.lineno
154 lineno = stack_line.lineno
162 if stack_line.is_current:
155 if stack_line.is_current:
163 # This is the line with the error
156 # This is the line with the error
164 pad = numbers_width - len(str(lineno))
157 pad = numbers_width - len(str(lineno))
165 num = '%s%s' % (debugger.make_arrow(pad), str(lineno))
158 num = '%s%s' % (debugger.make_arrow(pad), str(lineno))
166 line = '%s%s%s %s%s' % (Colors.linenoEm, num,
159 start_color = Colors.linenoEm
167 Colors.line, line, Colors.Normal)
168 else:
160 else:
169 num = '%*s' % (numbers_width, lineno)
161 num = '%*s' % (numbers_width, lineno)
170 line = '%s%s%s %s' % (Colors.lineno, num,
162 start_color = Colors.lineno
171 Colors.Normal, line)
163
164 line = '%s%s%s %s' % (start_color, num, Colors.Normal, line)
172
165
173 res.append(line)
166 res.append(line)
174 if lvals and stack_line.is_current:
167 if lvals and stack_line.is_current:
@@ -591,7 +584,6 b' class VerboseTB(TBTools):'
591 return ' %s[... skipping similar frames: %s]%s\n' % (
584 return ' %s[... skipping similar frames: %s]%s\n' % (
592 Colors.excName, frame_info.description, ColorsNormal)
585 Colors.excName, frame_info.description, ColorsNormal)
593
586
594 col_scheme = self.color_scheme_table.active_scheme_name
595 indent = ' ' * INDENT_SIZE
587 indent = ' ' * INDENT_SIZE
596 em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal)
588 em_normal = '%s\n%s%s' % (Colors.valEm, indent, ColorsNormal)
597 tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
589 tpl_link = '%s%%s%s' % (Colors.filenameEm, ColorsNormal)
@@ -646,8 +638,7 b' class VerboseTB(TBTools):'
646
638
647 result = '%s %s\n' % (link, call)
639 result = '%s %s\n' % (link, call)
648
640
649 _line_format = PyColorize.Parser(style=col_scheme, parent=self).format2
641 result += ''.join(_format_traceback_lines(frame_info.lines, Colors, lvals))
650 result += ''.join(_format_traceback_lines(frame_info.lines, Colors, lvals, _line_format))
651 return result
642 return result
652
643
653 def prepare_header(self, etype, long_version=False):
644 def prepare_header(self, etype, long_version=False):
@@ -718,7 +709,7 b' class VerboseTB(TBTools):'
718 context = number_of_lines_of_context - 1
709 context = number_of_lines_of_context - 1
719 after = context // 2
710 after = context // 2
720 before = context - after
711 before = context - after
721 options = stack_data.Options(before=before, after=after)
712 options = stack_data.Options(before=before, after=after, pygments_formatter=TerminalFormatter())
722 return list(stack_data.FrameInfo.stack_data(etb, options=options))[tb_offset:]
713 return list(stack_data.FrameInfo.stack_data(etb, options=options))[tb_offset:]
723
714
724 def structured_traceback(self, etype, evalue, etb, tb_offset=None,
715 def structured_traceback(self, etype, evalue, etb, tb_offset=None,
General Comments 0
You need to be logged in to leave comments. Login now