Skip to content

gh-112730: use keyword only argument for colorize #118086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ def test_signatures(self):
self.assertEqual(
str(inspect.signature(traceback.print_exception)),
('(exc, /, value=<implicit>, tb=<implicit>, '
'limit=None, file=None, chain=True, **kwargs)'))
'limit=None, file=None, chain=True, *, colorize=False)'))

self.assertEqual(
str(inspect.signature(traceback.format_exception)),
Expand Down
26 changes: 9 additions & 17 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ def _parse_value_tb(exc, value, tb):
return value, tb


def print_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, \
file=None, chain=True, **kwargs):
def print_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None,
file=None, chain=True, *, colorize=False):
"""Print exception up to 'limit' stack trace entries from 'tb' to 'file'.

This differs from print_tb() in the following ways: (1) if
Expand All @@ -125,7 +125,6 @@ def print_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, \
occurred with a caret on the next line indicating the approximate
position of the error.
"""
colorize = kwargs.get("colorize", False)
value, tb = _parse_value_tb(exc, value, tb)
te = TracebackException(type(value), value, tb, limit=limit, compact=True)
te.print(file=file, chain=chain, colorize=colorize)
Expand Down Expand Up @@ -165,7 +164,7 @@ def _print_exception_bltin(exc, /):
return print_exception(exc, limit=BUILTIN_EXCEPTION_LIMIT, file=file, colorize=colorize)


def format_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, \
def format_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None,
chain=True):
"""Format a stack trace and the exception information.

Expand Down Expand Up @@ -541,13 +540,12 @@ def from_list(klass, a_list):
result.append(FrameSummary(filename, lineno, name, line=line))
return result

def format_frame_summary(self, frame_summary, **kwargs):
def format_frame_summary(self, frame_summary, *, colorize=False):
"""Format the lines for a single FrameSummary.

Returns a string representing one frame involved in the stack. This
gets called for every frame to be printed in the stack summary.
"""
colorize = kwargs.get("colorize", False)
row = []
filename = frame_summary.filename
if frame_summary.filename.startswith("<stdin>-"):
Expand Down Expand Up @@ -727,7 +725,7 @@ def output_line(lineno):

return ''.join(row)

def format(self, **kwargs):
def format(self, *, colorize=False):
"""Format the stack ready for printing.

Returns a list of strings ready for printing. Each string in the
Expand All @@ -739,7 +737,6 @@ def format(self, **kwargs):
repetitions are shown, followed by a summary line stating the exact
number of further repetitions.
"""
colorize = kwargs.get("colorize", False)
result = []
last_file = None
last_line = None
Expand Down Expand Up @@ -1207,7 +1204,7 @@ def __eq__(self, other):
def __str__(self):
return self._str

def format_exception_only(self, *, show_group=False, _depth=0, **kwargs):
def format_exception_only(self, *, show_group=False, _depth=0, colorize=False):
"""Format the exception part of the traceback.

The return value is a generator of strings, each ending in a newline.
Expand All @@ -1224,8 +1221,6 @@ def format_exception_only(self, *, show_group=False, _depth=0, **kwargs):
:exc:`BaseExceptionGroup`, the nested exceptions are included as
well, recursively, with indentation relative to their nesting depth.
"""
colorize = kwargs.get("colorize", False)

indent = 3 * _depth * ' '
if not self._have_exc_type:
yield indent + _format_final_exc_line(None, self._str, colorize=colorize)
Expand Down Expand Up @@ -1261,10 +1256,9 @@ def format_exception_only(self, *, show_group=False, _depth=0, **kwargs):
for ex in self.exceptions:
yield from ex.format_exception_only(show_group=show_group, _depth=_depth+1, colorize=colorize)

def _format_syntax_error(self, stype, **kwargs):
def _format_syntax_error(self, stype, *, colorize=False):
"""Format SyntaxError exceptions (internal helper)."""
# Show exactly where the problem was found.
colorize = kwargs.get("colorize", False)
filename_suffix = ''
if self.lineno is not None:
if colorize:
Expand Down Expand Up @@ -1341,7 +1335,7 @@ def _format_syntax_error(self, stype, **kwargs):
else:
yield "{}: {}{}\n".format(stype, msg, filename_suffix)

def format(self, *, chain=True, _ctx=None, **kwargs):
def format(self, *, chain=True, _ctx=None, colorize=False):
"""Format the exception.

If chain is not *True*, *__cause__* and *__context__* will not be formatted.
Expand All @@ -1353,7 +1347,6 @@ def format(self, *, chain=True, _ctx=None, **kwargs):
The message indicating which exception occurred is always the last
string in the output.
"""
colorize = kwargs.get("colorize", False)
if _ctx is None:
_ctx = _ExceptionPrintContext()

Expand Down Expand Up @@ -1442,9 +1435,8 @@ def format(self, *, chain=True, _ctx=None, **kwargs):
_ctx.exception_group_depth = 0


def print(self, *, file=None, chain=True, **kwargs):
def print(self, *, file=None, chain=True, colorize=False):
"""Print the result of self.format(chain=chain) to 'file'."""
colorize = kwargs.get("colorize", False)
if file is None:
file = sys.stderr
for line in self.format(chain=chain, colorize=colorize):
Expand Down
Loading