(ip: Any)
| 164 | traceback_console.print(exception_traceback) |
| 165 | |
| 166 | def ipy_excepthook_closure(ip: Any) -> None: # pragma: no cover |
| 167 | tb_data = {} # store information about showtraceback call |
| 168 | default_showtraceback = ip.showtraceback # keep reference of default traceback |
| 169 | |
| 170 | def ipy_show_traceback(*args: Any, **kwargs: Any) -> None: |
| 171 | """wrap the default ip.showtraceback to store info for ip._showtraceback""" |
| 172 | nonlocal tb_data |
| 173 | tb_data = kwargs |
| 174 | default_showtraceback(*args, **kwargs) |
| 175 | |
| 176 | def ipy_display_traceback( |
| 177 | *args: Any, is_syntax: bool = False, **kwargs: Any |
| 178 | ) -> None: |
| 179 | """Internally called traceback from ip._showtraceback""" |
| 180 | nonlocal tb_data |
| 181 | exc_tuple = ip._get_exc_info() |
| 182 | |
| 183 | # do not display trace on syntax error |
| 184 | tb: Optional[TracebackType] = None if is_syntax else exc_tuple[2] |
| 185 | |
| 186 | # determine correct tb_offset |
| 187 | compiled = tb_data.get("running_compiled_code", False) |
| 188 | tb_offset = tb_data.get("tb_offset") |
| 189 | if tb_offset is None: |
| 190 | tb_offset = 1 if compiled else 0 |
| 191 | # remove ipython internal frames from trace with tb_offset |
| 192 | for _ in range(tb_offset): |
| 193 | if tb is None: |
| 194 | break |
| 195 | tb = tb.tb_next |
| 196 | |
| 197 | excepthook(exc_tuple[0], exc_tuple[1], tb) |
| 198 | tb_data = {} # clear data upon usage |
| 199 | |
| 200 | # replace _showtraceback instead of showtraceback to allow ipython features such as debugging to work |
| 201 | # this is also what the ipython docs recommends to modify when subclassing InteractiveShell |
| 202 | ip._showtraceback = ipy_display_traceback |
| 203 | # add wrapper to capture tb_data |
| 204 | ip.showtraceback = ipy_show_traceback |
| 205 | ip.showsyntaxerror = lambda *args, **kwargs: ipy_display_traceback( |
| 206 | *args, is_syntax=True, **kwargs |
| 207 | ) |
| 208 | |
| 209 | try: # pragma: no cover |
| 210 | # if within ipython, use customized traceback |
no test coverage detected