diff options
author | Georg Brandl <georg@python.org> | 2023-02-03 07:47:18 +0100 |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2023-02-03 08:06:29 +0100 |
commit | fe42aaca7b000200ea9915c04c01f2fb3f4dbaf6 (patch) | |
tree | 17244df3f44d0978c40cd01a1406a41a74cc48c1 /pygments | |
parent | c9ca87f08531e2695543112589d4461e31d52546 (diff) | |
download | pygments-git-fe42aaca7b000200ea9915c04c01f2fb3f4dbaf6.tar.gz |
Python console: make traceback handling more robust
Fixes #2329
Fixes #2226
Diffstat (limited to 'pygments')
-rw-r--r-- | pygments/lexers/python.py | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/pygments/lexers/python.py b/pygments/lexers/python.py index 0a318a9e..6ba3d19e 100644 --- a/pygments/lexers/python.py +++ b/pygments/lexers/python.py @@ -679,15 +679,15 @@ class PythonConsoleLexer(Lexer): insertions = [] curtb = '' tbindex = 0 - tb = 0 + in_tb = False for match in line_re.finditer(text): line = match.group() if line.startswith('>>> ') or line.startswith('... '): - tb = 0 + in_tb = False insertions.append((len(curcode), [(0, Generic.Prompt, line[:4])])) curcode += line[4:] - elif line.rstrip() == '...' and not tb: + elif line.rstrip() == '...' and not in_tb: # only a new >>> prompt can end an exception block # otherwise an ellipsis in place of the traceback frames # will be mishandled @@ -700,20 +700,20 @@ class PythonConsoleLexer(Lexer): insertions, pylexer.get_tokens_unprocessed(curcode)) curcode = '' insertions = [] - if (line.startswith('Traceback (most recent call last):') or - re.match(' File "[^"]+", line \\d+\\n$', line)): - tb = 1 - curtb = line - tbindex = match.start() - elif line == 'KeyboardInterrupt\n': - yield match.start(), Name.Class, line - elif tb: + if in_tb: curtb += line if not (line.startswith(' ') or line.strip() == '...'): - tb = 0 + in_tb = False for i, t, v in tblexer.get_tokens_unprocessed(curtb): yield tbindex+i, t, v curtb = '' + elif (line.startswith('Traceback (most recent call last):') or + re.match(' File "[^"]+", line \\d+\\n$', line)): + in_tb = True + curtb = line + tbindex = match.start() + elif line == 'KeyboardInterrupt\n': + yield match.start(), Name.Class, line else: yield match.start(), Generic.Output, line if curcode: |