Skip to content

Commit

Permalink
Python console: do not require output that looks like a traceback to …
Browse files Browse the repository at this point in the history
…be valid

which causes Error to be flagged with output that just happens
to look like a traceback

Fixes #2407
  • Loading branch information
birkenfeld committed Apr 12, 2023
1 parent 96a0cdf commit 13c1c73
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ Pygments changelog

Pull request numbers before 2.4.2 are not linked as they refer to the now defunct Bitbucket project.

Version 2.16.0
Version 2.15.1
--------------
(unreleased)

- Fix Python console traceback lexing being too strict (#2407)


Version 2.15.0
--------------
(released April 10th, 2023)
Expand Down Expand Up @@ -68,6 +71,7 @@ Version 2.15.0
for developing are now defined and run through tox. The ``doc`` folder
still contains a ``Makefile`` as an alternative to ``tox -e doc``.


Version 2.14.0
--------------
(released January 1st, 2023)
Expand Down
6 changes: 6 additions & 0 deletions pygments/lexers/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,10 @@ def get_tokens_unprocessed(self, text):
if not (line.startswith(' ') or line.strip() == '...'):
in_tb = False
for i, t, v in tblexer.get_tokens_unprocessed(curtb):
# Do not require the output to be a valid traceback
# (after all it could be print("Traceback ..."))
if t is Error:
t = Text
yield tbindex+i, t, v
curtb = ''
elif (line.startswith('Traceback (most recent call last):') or
Expand All @@ -721,6 +725,8 @@ def get_tokens_unprocessed(self, text):
pylexer.get_tokens_unprocessed(curcode))
if curtb:
for i, t, v in tblexer.get_tokens_unprocessed(curtb):
if t is Error: # see above
t = Text
yield tbindex+i, t, v


Expand Down
142 changes: 142 additions & 0 deletions tests/snippets/pycon/multiple_tb.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
---input---
>>> from multiprocessing import Pool
>>> p = Pool(5)
>>> def f(x):
... return x*x
...
>>> with p:
... p.map(f, [1,2,3])
Process PoolWorker-1:
Process PoolWorker-2:
Process PoolWorker-3:
Traceback (most recent call last):
Traceback (most recent call last):
Traceback (most recent call last):
AttributeError: Can't get attribute 'f' on <module '__main__' (<class '_frozen_importlib.BuiltinImporter'>)>
AttributeError: Can't get attribute 'f' on <module '__main__' (<class '_frozen_importlib.BuiltinImporter'>)>
AttributeError: Can't get attribute 'f' on <module '__main__' (<class '_frozen_importlib.BuiltinImporter'>)>

---tokens---
'>>> ' Generic.Prompt
'from' Keyword.Namespace
' ' Text
'multiprocessing' Name.Namespace
' ' Text
'import' Keyword.Namespace
' ' Text
'Pool' Name
'\n' Text.Whitespace

'>>> ' Generic.Prompt
'p' Name
' ' Text
'=' Operator
' ' Text
'Pool' Name
'(' Punctuation
'5' Literal.Number.Integer
')' Punctuation
'\n' Text.Whitespace

'>>> ' Generic.Prompt
'def' Keyword
' ' Text
'f' Name.Function
'(' Punctuation
'x' Name
')' Punctuation
':' Punctuation
'\n' Text.Whitespace

'... ' Generic.Prompt
' ' Text
'return' Keyword
' ' Text
'x' Name
'*' Operator
'x' Name
'\n' Text.Whitespace

'...' Generic.Prompt
'\n' Text.Whitespace

'>>> ' Generic.Prompt
'with' Keyword
' ' Text
'p' Name
':' Punctuation
'\n' Text.Whitespace

'... ' Generic.Prompt
' ' Text
'p' Name
'.' Operator
'map' Name
'(' Punctuation
'f' Name
',' Punctuation
' ' Text
'[' Punctuation
'1' Literal.Number.Integer
',' Punctuation
'2' Literal.Number.Integer
',' Punctuation
'3' Literal.Number.Integer
']' Punctuation
')' Punctuation
'\n' Text.Whitespace

'Process PoolWorker-1:\n' Generic.Output

'Process PoolWorker-2:\n' Generic.Output

'Process PoolWorker-3:\n' Generic.Output

'Traceback (most recent call last):\n' Generic.Traceback

'T' Text
'r' Text
'a' Text
'c' Text
'e' Text
'b' Text
'a' Text
'c' Text
'k' Text
' ' Text
'(' Text
'm' Text
'o' Text
's' Text
't' Text
' ' Text
'r' Text
'e' Text
'c' Text
'e' Text
'n' Text
't' Text
' ' Text
'c' Text
'a' Text
'l' Text
'l' Text
' ' Text
'l' Text
'a' Text
's' Text
't' Text
')' Text
':' Text
'\n' Text.Whitespace

'Traceback (most recent call last):\n' Generic.Traceback

'AttributeError' Generic.Error
': ' Text
"Can't get attribute 'f' on <module '__main__' (<class '_frozen_importlib.BuiltinImporter'>)>" Name
'\n' Text.Whitespace

"AttributeError: Can't get attribute 'f' on <module '__main__' (<class '_frozen_importlib.BuiltinImporter'>)>\n" Generic.Output

"AttributeError: Can't get attribute 'f' on <module '__main__' (<class '_frozen_importlib.BuiltinImporter'>)>\n" Generic.Output

0 comments on commit 13c1c73

Please sign in to comment.