Fixed showing exception message (#5394)

subj

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Andy Kipp 2024-05-06 16:18:43 +02:00 committed by GitHub
parent b19a89b432
commit ac7bc67406
Failed to generate hash of commit
3 changed files with 72 additions and 7 deletions

View file

@ -0,0 +1,23 @@
**Added:**
* <news item>
**Changed:**
* <news item>
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* Fixed showing exception message in some cases.
**Security:**
* <news item>

View file

@ -3,7 +3,9 @@
import datetime as dt
import os
import pathlib
import re
import stat
import subprocess
import warnings
from tempfile import TemporaryDirectory
@ -69,6 +71,7 @@ from xonsh.tools import (
pathsep_to_seq,
pathsep_to_set,
pathsep_to_upper_seq,
print_exception,
register_custom_style,
replace_logical_line,
seq_to_pathsep,
@ -2091,3 +2094,45 @@ from xonsh.style_tools import Token
)
def test_is_tok_color_dict(val, exp):
assert is_tok_color_dict(val) == exp
def test_print_exception_msg(xession, capsys):
xession.env["COLOR_INPUT"] = False
try:
a = 1 / 0
a += 1
except ZeroDivisionError:
print_exception(msg="MSG")
cap = capsys.readouterr()
assert cap.err.endswith("MSG\n"), f"captured_stderr = {cap.captured_stderr!r}"
def test_print_exception_error(xession, capsys):
xession.env["COLOR_INPUT"] = False
with xession.env.swap(XONSH_SHOW_TRACEBACK=False):
try:
raise subprocess.CalledProcessError(1, ["ls", "nofile"], output="nooutput")
except subprocess.CalledProcessError:
print_exception(msg="MSG")
cap = capsys.readouterr()
match = "subprocess.CalledProcessError: Command .* returned non-zero exit status .*\nMSG\n"
assert re.match(
match,
cap.err,
re.MULTILINE | re.DOTALL,
), f"Assert: {cap.err!r} not matched with {match!r}"
with xession.env.swap(XONSH_SHOW_TRACEBACK=True):
try:
raise subprocess.CalledProcessError(1, ["ls", "nofile"], output="nooutput")
except subprocess.CalledProcessError:
print_exception(msg="MSG")
cap = capsys.readouterr()
match = ".*Traceback.*subprocess.CalledProcessError: Command .* returned non-zero exit status .*\nMSG\n"
assert re.match(
match,
cap.err,
re.MULTILINE | re.DOTALL,
), f"Assert: {cap.err!r} not matched with {match!r}"

View file

@ -1098,8 +1098,8 @@ def print_exception(msg=None, exc_info=None, source_msg=None):
"RAISE_SUBPROC_ERROR"
):
display_colored_error_message(exc_info, limit=1)
return
display_error_message(exc_info)
else:
display_error_message(exc_info)
if msg:
msg = msg if msg.endswith("\n") else msg + "\n"
sys.stderr.write(msg)
@ -1115,10 +1115,7 @@ def display_colored_error_message(exc_info, strip_xonsh_error_types=True, limit=
content = traceback.format_exception(*exc_info, limit=limit)
if (
no_trace_and_raise_subproc_error
and "subprocess.CalledProcessError:" in content[-1]
):
if no_trace_and_raise_subproc_error and "Error:" in content[-1]:
content = [content[-1].rstrip()]
traceback_str = "".join([v for v in content])
@ -1136,7 +1133,7 @@ def display_colored_error_message(exc_info, strip_xonsh_error_types=True, limit=
lexer = pygments.lexers.python.PythonTracebackLexer()
tokens = list(pygments.lex(traceback_str, lexer=lexer))
# this goes to stdout, but since we are interactive it doesn't matter
print_color(tokens, end="", file=sys.stderr)
print_color(tokens, end="\n", file=sys.stderr)
return