Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite4a0717

Browse files
committed
Add more colors
Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
1 parent9ae4c77 commite4a0717

File tree

2 files changed

+51
-16
lines changed

2 files changed

+51
-16
lines changed

‎Lib/test/test_traceback.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4300,18 +4300,20 @@ def foo():
43004300

43014301
red=traceback._ANSIColors.RED
43024302
boldr=traceback._ANSIColors.BOLD_RED
4303+
magenta=traceback._ANSIColors.MAGENTA
4304+
boldm=traceback._ANSIColors.BOLD_MAGENTA
43034305
reset=traceback._ANSIColors.RESET
43044306
lno_foo=foo.__code__.co_firstlineno
43054307
expected= ['Traceback (most recent call last):',
4306-
f' File "{__file__}", '
4307-
f'line{lno_foo+5}, in test_colorized_traceback_is_the_default',
4308+
f' File{magenta}"{__file__}"{reset}, '
4309+
f'line{magenta}{lno_foo+5}{reset}, in{magenta}test_colorized_traceback_is_the_default{reset}',
43084310
f'{red}foo{reset+boldr}(){reset}',
43094311
f'{red}~~~{reset+boldr}^^{reset}',
4310-
f' File "{__file__}", '
4311-
f'line{lno_foo+1}, in foo',
4312+
f' File{magenta}"{__file__}"{reset}, '
4313+
f'line{magenta}{lno_foo+1}{reset}, in{magenta}foo{reset}',
43124314
f'{red}1{reset+boldr}/{reset+red}0{reset}',
43134315
f'{red}~{reset+boldr}^{reset+red}~{reset}',
4314-
'ZeroDivisionError:division by zero']
4316+
f'{boldm}ZeroDivisionError{reset}:{magenta}division by zero{reset}']
43154317
self.assertEqual(actual,expected)
43164318

43174319
deftest_colorized_detection_checks_for_environment_variables(self):

‎Lib/traceback.py

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,19 @@ def format_exception_only(exc, /, value=_sentinel, *, show_group=False):
203203

204204
# -- not official API but folk probably use these two functions.
205205

206-
def_format_final_exc_line(etype,value,*,insert_final_newline=True):
206+
def_format_final_exc_line(etype,value,*,insert_final_newline=True,colorize=False):
207207
valuestr=_safe_string(value,'exception')
208208
end_char="\n"ifinsert_final_newlineelse""
209-
ifvalueisNoneornotvaluestr:
210-
line=f"{etype}{end_char}"
209+
ifcolorize:
210+
ifvalueisNoneornotvaluestr:
211+
line=f"{_ANSIColors.BOLD_MAGENTA}{etype}{_ANSIColors.RESET}{end_char}"
212+
else:
213+
line=f"{_ANSIColors.BOLD_MAGENTA}{etype}{_ANSIColors.RESET}:{_ANSIColors.MAGENTA}{valuestr}{_ANSIColors.RESET}{end_char}"
211214
else:
212-
line=f"{etype}:{valuestr}{end_char}"
215+
ifvalueisNoneornotvaluestr:
216+
line=f"{etype}{end_char}"
217+
else:
218+
line=f"{etype}:{valuestr}{end_char}"
213219
returnline
214220

215221
def_safe_string(value,what,func=str):
@@ -440,6 +446,9 @@ def _get_code_position(code, instruction_index):
440446
class_ANSIColors:
441447
RED='\x1b[31m'
442448
BOLD_RED='\x1b[1;31m'
449+
MAGENTA='\x1b[35m'
450+
BOLD_MAGENTA='\x1b[1;35m'
451+
GREY='\x1b[90m'
443452
RESET='\x1b[0m'
444453

445454
classStackSummary(list):
@@ -543,8 +552,22 @@ def format_frame_summary(self, frame_summary, **kwargs):
543552
filename=frame_summary.filename
544553
ifframe_summary.filename.startswith("<stdin>-"):
545554
filename="<stdin>"
546-
row.append(' File "{}", line {}, in {}\n'.format(
547-
filename,frame_summary.lineno,frame_summary.name))
555+
ifcolorize:
556+
row.append(' File {}"{}"{}, line {}{}{}, in {}{}{}\n'.format(
557+
_ANSIColors.MAGENTA,
558+
filename,
559+
_ANSIColors.RESET,
560+
_ANSIColors.MAGENTA,
561+
frame_summary.lineno,
562+
_ANSIColors.RESET,
563+
_ANSIColors.MAGENTA,
564+
frame_summary.name,
565+
_ANSIColors.RESET,
566+
)
567+
)
568+
else:
569+
row.append(' File "{}", line {}, in {}\n'.format(
570+
filename,frame_summary.lineno,frame_summary.name))
548571
ifframe_summary._dedented_linesandframe_summary._dedented_lines.strip():
549572
if (
550573
frame_summary.colnoisNoneor
@@ -1201,22 +1224,22 @@ def format_exception_only(self, *, show_group=False, _depth=0, **kwargs):
12011224

12021225
indent=3*_depth*' '
12031226
ifnotself._have_exc_type:
1204-
yieldindent+_format_final_exc_line(None,self._str)
1227+
yieldindent+_format_final_exc_line(None,self._str,colorize=colorize)
12051228
return
12061229

12071230
stype=self.exc_type_str
12081231
ifnotself._is_syntax_error:
12091232
if_depth>0:
12101233
# Nested exceptions needs correct handling of multiline messages.
12111234
formatted=_format_final_exc_line(
1212-
stype,self._str,insert_final_newline=False,
1235+
stype,self._str,insert_final_newline=False,colorize=colorize
12131236
).split('\n')
12141237
yieldfrom [
12151238
indent+l+'\n'
12161239
forlinformatted
12171240
]
12181241
else:
1219-
yield_format_final_exc_line(stype,self._str)
1242+
yield_format_final_exc_line(stype,self._str,colorize=colorize)
12201243
else:
12211244
yieldfrom [indent+lforlinself._format_syntax_error(stype,colorize=colorize)]
12221245

@@ -1240,8 +1263,18 @@ def _format_syntax_error(self, stype, **kwargs):
12401263
colorize=kwargs.get("colorize",False)
12411264
filename_suffix=''
12421265
ifself.linenoisnotNone:
1243-
yield' File "{}", line {}\n'.format(
1244-
self.filenameor"<string>",self.lineno)
1266+
ifcolorize:
1267+
yield' File {}"{}"{}, line {}{}{}\n'.format(
1268+
_ANSIColors.MAGENTA,
1269+
self.filenameor"<string>",
1270+
_ANSIColors.RESET,
1271+
_ANSIColors.MAGENTA,
1272+
self.lineno,
1273+
_ANSIColors.RESET,
1274+
)
1275+
else:
1276+
yield' File "{}", line {}\n'.format(
1277+
self.filenameor"<string>",self.lineno)
12451278
elifself.filenameisnotNone:
12461279
filename_suffix=' ({})'.format(self.filename)
12471280

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp