diff --git a/IPython/utils/PyColorize.py b/IPython/utils/PyColorize.py index b94aea9..86bb9af 100644 --- a/IPython/utils/PyColorize.py +++ b/IPython/utils/PyColorize.py @@ -29,7 +29,7 @@ scan Python source code and re-emit it with no changes to its original formatting (which is the hard part). """ -__all__ = ['ANSICodeColors','Parser'] +__all__ = ['ANSICodeColors', 'Parser'] _scheme_default = 'Linux' @@ -43,7 +43,7 @@ import tokenize generate_tokens = tokenize.generate_tokens -from IPython.utils.coloransi import TermColors, InputTermColors ,ColorScheme, ColorSchemeTable +from IPython.utils.coloransi import TermColors, InputTermColors,ColorScheme, ColorSchemeTable from .colorable import Colorable from io import StringIO @@ -185,8 +185,11 @@ class Parser(Colorable): super(Parser, self).__init__(parent=parent) - self.color_table = color_table and color_table or ANSICodeColors + self.color_table = color_table if color_table else ANSICodeColors self.out = out + self.pos = None + self.lines = None + self.raw = None if not style: self.style = self.default_style else: @@ -231,9 +234,8 @@ class Parser(Colorable): error = False self.out.write(raw) if string_output: - return raw,error - else: - return None,error + return raw, error + return None, error # local shorthands colors = self.color_table[self.style].colors @@ -247,9 +249,10 @@ class Parser(Colorable): pos = 0 raw_find = self.raw.find lines_append = self.lines.append - while 1: + while True: pos = raw_find('\n', pos) + 1 - if not pos: break + if not pos: + break lines_append(pos) lines_append(len(self.raw)) @@ -277,11 +280,11 @@ class Parser(Colorable): return (output, error) return (None, error) - def _inner_call_(self, toktype, toktext, start_pos, end_pos, line): + + def _inner_call_(self, toktype, toktext, start_pos): """like call but write to a temporary buffer""" buff = StringIO() - (srow,scol) = start_pos - (erow,ecol) = end_pos + srow, scol = start_pos colors = self.colors owrite = buff.write @@ -310,8 +313,6 @@ class Parser(Colorable): toktype = _KEYWORD color = colors.get(toktype, colors[_TEXT]) - #print '<%s>' % toktext, # dbg - # Triple quoted strings must be handled carefully so that backtracking # in pagers works correctly. We need color terminators on _each_ line. if linesep in toktext: @@ -327,5 +328,4 @@ class Parser(Colorable): def __call__(self, toktype, toktext, start_pos, end_pos, line): """ Token handler, with syntax highlighting.""" self.out.write( - self._inner_call_(toktype, toktext, start_pos, end_pos, line)) - + self._inner_call_(toktype, toktext, start_pos))