Display the exception that just occurred. If nothing is known about the exception, this is the method which should be used throughout the code for presenting user tracebacks, rather than directly invoking the InteractiveTB object. A specific showsyntaxerror() also e
(
self,
exc_tuple: tuple[type[BaseException], BaseException, AnyType] | None = None,
filename: str | None = None,
tb_offset: int | None = None,
exception_only: bool = False,
running_compiled_code: bool = False,
)
| 2162 | return ''.join(msg) |
| 2163 | |
| 2164 | def showtraceback( |
| 2165 | self, |
| 2166 | exc_tuple: tuple[type[BaseException], BaseException, AnyType] | None = None, |
| 2167 | filename: str | None = None, |
| 2168 | tb_offset: int | None = None, |
| 2169 | exception_only: bool = False, |
| 2170 | running_compiled_code: bool = False, |
| 2171 | ) -> None: |
| 2172 | """Display the exception that just occurred. |
| 2173 | |
| 2174 | If nothing is known about the exception, this is the method which |
| 2175 | should be used throughout the code for presenting user tracebacks, |
| 2176 | rather than directly invoking the InteractiveTB object. |
| 2177 | |
| 2178 | A specific showsyntaxerror() also exists, but this method can take |
| 2179 | care of calling it if needed, so unless you are explicitly catching a |
| 2180 | SyntaxError exception, don't try to analyze the stack manually and |
| 2181 | simply call this method.""" |
| 2182 | |
| 2183 | try: |
| 2184 | try: |
| 2185 | etype, value, tb = self._get_exc_info(exc_tuple) |
| 2186 | except ValueError: |
| 2187 | print('No traceback available to show.', file=sys.stderr) |
| 2188 | return |
| 2189 | |
| 2190 | if issubclass(etype, SyntaxError): |
| 2191 | # Though this won't be called by syntax errors in the input |
| 2192 | # line, there may be SyntaxError cases with imported code. |
| 2193 | self.showsyntaxerror(filename, running_compiled_code) |
| 2194 | elif etype is UsageError: |
| 2195 | self.show_usage_error(value) |
| 2196 | else: |
| 2197 | if exception_only: |
| 2198 | stb = ['An exception has occurred, use %tb to see ' |
| 2199 | 'the full traceback.\n'] |
| 2200 | stb.extend(self.InteractiveTB.get_exception_only(etype, |
| 2201 | value)) |
| 2202 | else: |
| 2203 | |
| 2204 | def contains_exceptiongroup(val): |
| 2205 | if val is None: |
| 2206 | return False |
| 2207 | return isinstance( |
| 2208 | val, BaseExceptionGroup |
| 2209 | ) or contains_exceptiongroup(val.__context__) |
| 2210 | |
| 2211 | if contains_exceptiongroup(value): |
| 2212 | # fall back to native exception formatting until ultratb |
| 2213 | # supports exception groups |
| 2214 | traceback.print_exc() |
| 2215 | else: |
| 2216 | try: |
| 2217 | # Exception classes can customise their traceback - we |
| 2218 | # use this in IPython.parallel for exceptions occurring |
| 2219 | # in the engines. This should return a list of strings. |
| 2220 | if hasattr(value, "_render_traceback_"): |
| 2221 | stb = value._render_traceback_() |
no test coverage detected