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

Commit949da49

Browse files
committed
Add more colors
1 parent9ae4c77 commit949da49

File tree

2 files changed

+47
-16
lines changed

2 files changed

+47
-16
lines changed

‎Lib/test/test_traceback.py

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

43014301
red=traceback._ANSIColors.RED
43024302
boldr=traceback._ANSIColors.BOLD_RED
4303+
magenta=traceback._ANSIColors.MAGENTA
43034304
reset=traceback._ANSIColors.RESET
43044305
lno_foo=foo.__code__.co_firstlineno
43054306
expected= ['Traceback (most recent call last):',
4306-
f' File "{__file__}", '
4307-
f'line{lno_foo+5}, in test_colorized_traceback_is_the_default',
4307+
f' File{magenta}"{__file__}"{reset}, '
4308+
f'line{magenta}{lno_foo+5}{reset}, in test_colorized_traceback_is_the_default',
43084309
f'{red}foo{reset+boldr}(){reset}',
43094310
f'{red}~~~{reset+boldr}^^{reset}',
4310-
f' File "{__file__}", '
4311-
f'line{lno_foo+1}, in foo',
4311+
f' File{magenta}"{__file__}"{reset}, '
4312+
f'line{magenta}{lno_foo+1}{reset}, in foo',
43124313
f'{red}1{reset+boldr}/{reset+red}0{reset}',
43134314
f'{red}~{reset+boldr}^{reset+red}~{reset}',
4314-
'ZeroDivisionError: division by zero']
4315+
f'{red}ZeroDivisionError{reset}: division by zero']
43154316
self.assertEqual(actual,expected)
43164317

43174318
deftest_colorized_detection_checks_for_environment_variables(self):

‎Lib/traceback.py

Lines changed: 41 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.RED}{etype}{_ANSIColors.RESET}{end_char}"
212+
else:
213+
line=f"{_ANSIColors.RED}{etype}{_ANSIColors.RESET}:{valuestr}{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,8 @@ 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+
GREY='\x1b[90m'
443451
RESET='\x1b[0m'
444452

445453
classStackSummary(list):
@@ -543,8 +551,20 @@ def format_frame_summary(self, frame_summary, **kwargs):
543551
filename=frame_summary.filename
544552
ifframe_summary.filename.startswith("<stdin>-"):
545553
filename="<stdin>"
546-
row.append(' File "{}", line {}, in {}\n'.format(
547-
filename,frame_summary.lineno,frame_summary.name))
554+
ifcolorize:
555+
row.append(' File {}"{}"{}, line {}{}{}, in {}\n'.format(
556+
_ANSIColors.MAGENTA,
557+
filename,
558+
_ANSIColors.RESET,
559+
_ANSIColors.MAGENTA,
560+
frame_summary.lineno,
561+
_ANSIColors.RESET,
562+
frame_summary.name,
563+
)
564+
)
565+
else:
566+
row.append(' File "{}", line {}, in {}\n'.format(
567+
filename,frame_summary.lineno,frame_summary.name))
548568
ifframe_summary._dedented_linesandframe_summary._dedented_lines.strip():
549569
if (
550570
frame_summary.colnoisNoneor
@@ -1201,22 +1221,22 @@ def format_exception_only(self, *, show_group=False, _depth=0, **kwargs):
12011221

12021222
indent=3*_depth*' '
12031223
ifnotself._have_exc_type:
1204-
yieldindent+_format_final_exc_line(None,self._str)
1224+
yieldindent+_format_final_exc_line(None,self._str,colorize=colorize)
12051225
return
12061226

12071227
stype=self.exc_type_str
12081228
ifnotself._is_syntax_error:
12091229
if_depth>0:
12101230
# Nested exceptions needs correct handling of multiline messages.
12111231
formatted=_format_final_exc_line(
1212-
stype,self._str,insert_final_newline=False,
1232+
stype,self._str,insert_final_newline=False,colorize=colorize
12131233
).split('\n')
12141234
yieldfrom [
12151235
indent+l+'\n'
12161236
forlinformatted
12171237
]
12181238
else:
1219-
yield_format_final_exc_line(stype,self._str)
1239+
yield_format_final_exc_line(stype,self._str,colorize=colorize)
12201240
else:
12211241
yieldfrom [indent+lforlinself._format_syntax_error(stype,colorize=colorize)]
12221242

@@ -1240,8 +1260,18 @@ def _format_syntax_error(self, stype, **kwargs):
12401260
colorize=kwargs.get("colorize",False)
12411261
filename_suffix=''
12421262
ifself.linenoisnotNone:
1243-
yield' File "{}", line {}\n'.format(
1244-
self.filenameor"<string>",self.lineno)
1263+
ifcolorize:
1264+
yield' File {}"{}"{}, line {}{}{}\n'.format(
1265+
_ANSIColors.MAGENTA,
1266+
self.filenameor"<string>",
1267+
_ANSIColors.RESET,
1268+
_ANSIColors.MAGENTA,
1269+
self.lineno,
1270+
_ANSIColors.RESET,
1271+
)
1272+
else:
1273+
yield' File "{}", line {}\n'.format(
1274+
self.filenameor"<string>",self.lineno)
12451275
elifself.filenameisnotNone:
12461276
filename_suffix=' ({})'.format(self.filename)
12471277

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp