-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use code highlighting if pygments is installed
- Loading branch information
1 parent
39b25dd
commit 6e22bc1
Showing
5 changed files
with
51 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Code is now highlighted in tracebacks when ``pygments`` is installed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,22 @@ | ||
# Reexport TerminalWriter from here instead of py, to make it easier to | ||
# extend or swap our own implementation in the future. | ||
from py.io import TerminalWriter as TerminalWriter # noqa: F401 | ||
from py.io import TerminalWriter as BaseTerminalWriter # noqa: F401 | ||
|
||
|
||
class TerminalWriter(BaseTerminalWriter): | ||
def write_source(self, source: str) -> None: | ||
"""Write lines of source code possibly highlighted.""" | ||
self.write(self._highlight(source)) | ||
|
||
def _highlight(self, source): | ||
"""Highlight the given source code according to the "code_highlight" option""" | ||
if not self.hasmarkup: | ||
return source | ||
try: | ||
from pygments.formatters.terminal import TerminalFormatter | ||
from pygments.lexers.python import PythonLexer | ||
from pygments import highlight | ||
except ImportError: | ||
return source | ||
else: | ||
return highlight(source, PythonLexer(), TerminalFormatter()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters