Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
5 changes: 4 additions & 1 deletion Lib/test/test_timeit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io
from textwrap import dedent

from test.support import captured_stdout
from test.support import captured_stdout, force_not_colorized
from test.support import captured_stderr

# timeit's default number of iterations.
Expand Down Expand Up @@ -225,6 +225,7 @@ def assert_exc_string(self, exc_string, expected_exc_name):
self.assertStartsWith(exc_lines[0], 'Traceback')
self.assertStartsWith(exc_lines[-1], expected_exc_name)

@force_not_colorized
def test_print_exc(self):
s = io.StringIO()
t = timeit.Timer("1/0")
Expand Down Expand Up @@ -351,11 +352,13 @@ def test_main_with_time_unit(self):
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'])
Expand Down
9 changes: 6 additions & 3 deletions Lib/timeit.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def __init__(self, stmt="pass", setup="pass", timer=default_timer,
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:
Expand All @@ -150,15 +150,18 @@ def print_exc(self, file=None):
The optional file argument directs where the traceback is
sent; it defaults to sys.stderr.
"""
import linecache, traceback
import _colorize, linecache, traceback, sys
if self.src is not None:
linecache.cache[dummy_src_name] = (len(self.src),
None,
self.src.split("\n"),
dummy_src_name)
# else the source is already stored somewhere else

traceback.print_exc(file=file)
if 'colorize' not in kwargs:
kwargs['colorize'] = _colorize.can_colorize(file=file)

traceback.print_exc(file=file, **kwargs)

def timeit(self, number=default_number):
"""Time 'number' executions of the main statement.
Expand Down
4 changes: 2 additions & 2 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ def _safe_string(value, what, func=str):

# --

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."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:mod:`timeit`: Add color to error tracebacks.
Loading