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)
| 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. |