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

Commit1838f1c

Browse files
committed
Add test for syntax error (and fix colorization)
1 parente4a0717 commit1838f1c

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

‎Lib/test/test_traceback.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4284,6 +4284,27 @@ def bar():
42844284
self.assertIn(boldr+",4)"+reset,lines)
42854285
self.assertIn(red+"bar"+reset+boldr+"()"+reset,lines)
42864286

4287+
deftest_colorized_syntax_error(self):
4288+
try:
4289+
compile("a $ b","<string>","exec")
4290+
exceptSyntaxErrorase:
4291+
exc=traceback.TracebackException.from_exception(
4292+
e,capture_locals=True
4293+
)
4294+
actual="".join(exc.format(colorize=True))
4295+
red=traceback._ANSIColors.RED
4296+
magenta=traceback._ANSIColors.MAGENTA
4297+
boldm=traceback._ANSIColors.BOLD_MAGENTA
4298+
boldr=traceback._ANSIColors.BOLD_RED
4299+
reset=traceback._ANSIColors.RESET
4300+
expected="".join([
4301+
f' File{magenta}"<string>"{reset}, line{magenta}1{reset}\n',
4302+
f' a{boldr}${reset} b\n',
4303+
f'{boldr}^{reset}\n',
4304+
f'{boldm}SyntaxError{reset}:{magenta}invalid syntax{reset}\n']
4305+
)
4306+
self.assertIn(expected,actual)
4307+
42874308
deftest_colorized_traceback_is_the_default(self):
42884309
deffoo():
42894310
1/0

‎Lib/traceback.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,22 +1310,32 @@ def _format_syntax_error(self, stype, **kwargs):
13101310
# colorize from colno to end_colno
13111311
ltext= (
13121312
ltext[:colno]+
1313-
_ANSIColors.RED+ltext[colno:end_colno]+_ANSIColors.RESET+
1313+
_ANSIColors.BOLD_RED+ltext[colno:end_colno]+_ANSIColors.RESET+
13141314
ltext[end_colno:]
13151315
)
1316-
start_color=_ANSIColors.RED
1316+
start_color=_ANSIColors.BOLD_RED
13171317
end_color=_ANSIColors.RESET
13181318
yield' {}\n'.format(ltext)
1319-
yield' {}{}{}{}'.format(
1319+
yield' {}{}{}{}\n'.format(
13201320
"".join(caretspace),
13211321
start_color,
1322-
('^'* (end_colno-colno)+"\n"),
1322+
('^'* (end_colno-colno)),
13231323
end_color,
13241324
)
13251325
else:
13261326
yield' {}\n'.format(ltext)
13271327
msg=self.msgor"<no detail available>"
1328-
yield"{}: {}{}\n".format(stype,msg,filename_suffix)
1328+
ifcolorize:
1329+
yield"{}{}{}: {}{}{}{}\n".format(
1330+
_ANSIColors.BOLD_MAGENTA,
1331+
stype,
1332+
_ANSIColors.RESET,
1333+
_ANSIColors.MAGENTA,
1334+
msg,
1335+
_ANSIColors.RESET,
1336+
filename_suffix)
1337+
else:
1338+
yield"{}: {}{}\n".format(stype,msg,filename_suffix)
13291339

13301340
defformat(self,*,chain=True,_ctx=None,**kwargs):
13311341
"""Format the exception.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp