MCPcopy Index your code
hub / github.com/python/cpython / print_exc

Method print_exc

Lib/timeit.py:140–171  ·  view source on GitHub ↗

Helper to print a traceback from the timed code. Typical use: t = Timer(...) # outside the try/except try: t.timeit(...) # or t.repeat(...) except: t.print_exc() The advantage over the standard traceback

(self, file=None, **kwargs)

Source from the content-addressed store, hash-verified

138 self.inner = local_ns["inner"]
139
140 def print_exc(self, file=None, **kwargs):
141 """Helper to print a traceback from the timed code.
142
143 Typical use:
144
145 t = Timer(...) # outside the try/except
146 try:
147 t.timeit(...) # or t.repeat(...)
148 except:
149 t.print_exc()
150
151 The advantage over the standard traceback is that source lines
152 in the compiled template will be displayed.
153
154 The optional file argument directs where the traceback is
155 sent; it defaults to sys.stderr.
156
157 The optional colorize keyword argument controls whether the
158 traceback is colorized; it defaults to False for programmatic
159 usage. When used from the command line, this is automatically
160 set based on terminal capabilities.
161 """
162 import linecache, traceback
163 if self.src is not None:
164 linecache.cache[dummy_src_name] = (len(self.src),
165 None,
166 self.src.split("\n"),
167 dummy_src_name)
168 # else the source is already stored somewhere else
169
170 kwargs['colorize'] = kwargs.get('colorize', False)
171 traceback.print_exc(file=file, **kwargs)
172
173 def timeit(self, number=default_number):
174 """Time 'number' executions of the main statement.

Callers 15

mainFunction · 0.95
test_print_excMethod · 0.95
__init__Method · 0.80
__init__Method · 0.80
handle_errorMethod · 0.80
handleMethod · 0.80
_run_childMethod · 0.80
collect_infoFunction · 0.80

Calls 2

splitMethod · 0.45
getMethod · 0.45

Tested by 6

test_print_excMethod · 0.76
_run_childMethod · 0.64
cleanupMethod · 0.64