Skip to content

Commit

Permalink
executors/python: add coloring for compileall error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Xyene committed Jun 3, 2024
1 parent de1cb6f commit 1b43584
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions dmoj/executors/PY2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class Executor(PythonExecutor):
command = 'python'
command_paths = ['python2.7', 'python2', 'python']
pygments_traceback_lexer = 'pytb'
test_program = """
import sys
if sys.version_info.major == 2:
Expand Down
1 change: 1 addition & 0 deletions dmoj/executors/PY3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class Executor(PythonExecutor):
command = 'python3'
command_paths = [f'python{i}' for i in ['3.6', '3.5', '3.4', '3.3', '3.2', '3.1', '3']]
pygments_traceback_lexer = 'py3tb'
test_program = """
import sys
if sys.version_info.major == 3:
Expand Down
1 change: 1 addition & 0 deletions dmoj/executors/PYPY.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class Executor(PythonExecutor):
command = 'pypy'
pygments_traceback_lexer = 'pytb'
test_program = """
import sys
if sys.version_info.major == 2:
Expand Down
1 change: 1 addition & 0 deletions dmoj/executors/PYPY3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

class Executor(PYPYExecutor):
command = 'pypy3'
pygments_traceback_lexer = 'py3tb'
test_program = """
import sys
if sys.version_info.major == 3:
Expand Down
13 changes: 12 additions & 1 deletion dmoj/executors/python_executor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import builtins
import re
from collections import deque
from typing import List
from typing import List, Optional

from pygments import highlight
from pygments.formatters import Terminal256Formatter
from pygments.lexers import get_lexer_by_name

from dmoj.cptbox import TracedPopen
from dmoj.executors.base_executor import VersionFlags
Expand All @@ -28,6 +32,7 @@ class PythonExecutor(CompiledExecutor):
address_grace = 131072
data_grace = 2048
ext = 'py'
pygments_traceback_lexer: Optional[str] = None

def get_compile_args(self) -> List[str]:
command = self.get_command()
Expand All @@ -48,6 +53,12 @@ def get_executable(self) -> str:
assert command is not None
return command

def handle_compile_error(self, output: bytes) -> None:
if self.pygments_traceback_lexer:
lexer = get_lexer_by_name(self.pygments_traceback_lexer)
output = utf8bytes(highlight(utf8text(output), lexer, Terminal256Formatter()))
super().handle_compile_error(output)

def create_files(self, problem_id: str, source_code: bytes, *args, **kwargs) -> None:
super().create_files(problem_id, source_code, **kwargs)
self._loader = self._file('-loader.py')
Expand Down

0 comments on commit 1b43584

Please sign in to comment.