(Contributed by Christoph Walcher in :gh:`57911`.)
+timeit
+------
+
+* The command-line interface now colorizes error tracebacks
+ by default. This can be controlled with
+ :ref:`environment variables <using-on-controlling-color>`.
+ (Contributed by Yi Hong in :gh:`139374`.)
+
+
types
------
import io
from textwrap import dedent
-from test.support import captured_stdout
-from test.support import captured_stderr
+from test.support import (
+ captured_stdout, captured_stderr, force_not_colorized,
+)
# timeit's default number of iterations.
DEFAULT_NUMBER = 1000000
self.assertEqual(error_stringio.getvalue(),
"Unrecognized unit. Please select nsec, usec, msec, or sec.\n")
+ @force_not_colorized
def test_main_exception(self):
with captured_stderr() as error_stringio:
s = self.run_main(switches=['1/0'])
self.assert_exc_string(error_stringio.getvalue(), 'ZeroDivisionError')
+ @force_not_colorized
def test_main_exception_fixed_reps(self):
with captured_stderr() as error_stringio:
s = self.run_main(switches=['-n1', '1/0'])
exec(code, global_ns, local_ns)
self.inner = local_ns["inner"]
- def print_exc(self, file=None):
+ def print_exc(self, file=None, **kwargs):
"""Helper to print a traceback from the timed code.
Typical use:
The optional file argument directs where the traceback is
sent; it defaults to sys.stderr.
+
+ The optional colorize keyword argument controls whether the
+ traceback is colorized; it defaults to False for programmatic
+ usage. When used from the command line, this is automatically
+ set based on terminal capabilities.
"""
import linecache, traceback
if self.src is not None:
dummy_src_name)
# else the source is already stored somewhere else
- traceback.print_exc(file=file)
+ kwargs['colorize'] = kwargs.get('colorize', False)
+ traceback.print_exc(file=file, **kwargs)
def timeit(self, number=default_number):
"""Time 'number' executions of the main statement.
is not None, it must be a callable that accepts a timer function
and returns another timer function (used for unit testing).
"""
+ import getopt
if args is None:
args = sys.argv[1:]
- import getopt
+ import _colorize
+ colorize = _colorize.can_colorize()
+
try:
opts, args = getopt.getopt(args, "n:u:s:r:pvh",
["number=", "setup=", "repeat=",
try:
number, _ = t.autorange(callback)
except:
- t.print_exc()
+ t.print_exc(colorize=colorize)
return 1
if verbose:
try:
raw_timings = t.repeat(repeat, number)
except:
- t.print_exc()
+ t.print_exc(colorize=colorize)
return 1
def format_time(dt):
# --
-def print_exc(limit=None, file=None, chain=True):
+def print_exc(limit=None, file=None, chain=True, **kwargs):
"""Shorthand for 'print_exception(sys.exception(), limit=limit, file=file, chain=chain)'."""
- print_exception(sys.exception(), limit=limit, file=file, chain=chain)
+ print_exception(sys.exception(), limit=limit, file=file, chain=chain, **kwargs)
def format_exc(limit=None, chain=True):
"""Like print_exc() but return a string."""
--- /dev/null
+:mod:`timeit`: Add color to error tracebacks.