ipython: IPython crash when use showtraceback with exception_only=True

Used IPython version: 7.20.0-py38hd4e2768_1

The following exception was raised:

Traceback (most recent call last):
  File "C:\Anaconda3\lib\site-packages\IPython\core\ultratb.py", line 1103, in get_records
    return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
  File "C:\Anaconda3\lib\site-packages\IPython\core\ultratb.py", line 248, in wrapped
    return f(*args, **kwargs)
  File "C:\Anaconda3\lib\site-packages\IPython\core\ultratb.py", line 281, in _fixed_getinnerframes
    records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))
  File "C:\Anaconda3\lib\inspect.py", line 1503, in getinnerframes
    frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)
AttributeError: 'tuple' object has no attribute 'tb_frame'

And when this exception is caught in IPython.core.ultratb.VerboseTB.get_records, the return value is None:

def get_records(self, etb, number_of_lines_of_context, tb_offset):
    try:
        # Try the default getinnerframes and Alex's: Alex's fixes some
        # problems, but it generates empty tracebacks for console errors
        # (5 blanks lines) where none should be returned.
        return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
    except UnicodeDecodeError:
        # This can occur if a file's encoding magic comment is wrong.
        # I can't see a way to recover without duplicating a bunch of code
        # from the stdlib traceback module. --TK
        error('\nUnicodeDecodeError while processing traceback.\n')
        return None
    **except:
        # FIXME: I've been getting many crash reports from python 2.3
        # users, traceable to inspect.py.  If I can find a small test-case
        # to reproduce this, I should either write a better workaround or
        # file a bug report against inspect (if that's the real problem).
        # So far, I haven't been able to find an isolated example to
        # reproduce the problem.
        inspect_error()
        traceback.print_exc(file=self.ostream)
        info('\nUnfortunately, your original traceback can not be constructed.\n')
        return None**

But the next operation with return value that it is called len() in IPython.core.ultratb.find_recursion, and this causes an exception and IPython crash.

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Comments: 21 (12 by maintainers)

Commits related to this issue

Most upvoted comments

Hi. I think the issue is still existing. On my side I manage to reproduce the issue with click library. The minimal code snippet is below.

import click

@click.command()
@click.option('--nb_files', type=int, help='number of files to load')
def execute_job(nb_files):
    print('hello world', nb_files)
    return nb_files

if __name__ == '__main__':
    execute_job()

Then executed in IPython with the following argugments:

--nb_files 20

I encounter the issue with IPython version 7.22.0 , on Databricks platform using a “Python” job (actions with them are also ongoing).

Should we create a different issue for this one in case this is specific to click? Let me know. Thanks.

It seems that click is calling a sys.exit(0) after your app has finished and ipython mistakes this as an error. We solved the problem by wrapping our application in a try except block:

try:
    app()
except SystemExit as e:
    if e.code != 0:
        raise

see also this Stackoverflow post: https://stackoverflow.com/questions/52740295/how-to-not-exit-a-click-cli

Same issue here with IPython version 7.27.0 on Databricks, are there any news on that?

Hello! I have been running into the same error, and tried to reproduce it with a minimal example. I only managed to get it the same error via a jupyter notebook, here is some info if it can help:

Setup

python: 3.9.18 Ubuntu 22.04 jammy

$ pip list  # kept only the interesting dependencies
Package                   Version      Editable project location
------------------------- ------------ -------------------------
ipykernel                 6.25.2
ipython                   8.18.1
ipywidgets                8.1.1
jupyter_client            8.6.0
jupyter_core              5.5.0
jupyter-events            0.9.0
jupyter-lsp               2.2.1
jupyter_server            2.11.1
jupyter_server_terminals  0.4.4
jupyterlab                4.0.9
jupyterlab_pygments       0.3.0
jupyterlab_server         2.24.0
jupyterlab-widgets        3.0.9

Code

In a notebook:

import traceback
from types import TracebackType
from typing import Optional, Type

from IPython import get_ipython
from IPython.core.interactiveshell import InteractiveShell


class FooExc(Exception):
    pass

def my_handler(
    self: InteractiveShell, etype: Type[BaseException], value: BaseException, tb: TracebackType, tb_offset: Optional[int] = None
):
    self.showtraceback(exc_tuple=(etype, value, tb), filename=None, tb_offset=tb_offset, exception_only=True)
    return traceback.format_tb(tb, tb_offset)

get_ipython().set_custom_exc((FooExc,), my_handler)
raise FooExc("outer exception") from Exception("outer exception")

The custom handler does not do much and simply prints the exception’s traceback, using exception_only=True

Logs

In short, this raises:


AttributeError: 'tuple' object has no attribute 'tb_frame'


During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
    [... skipping hidden 1 frame]

File venv/lib/python3.9/site-packages/IPython/core/interactiveshell.py:2011, in InteractiveShell.set_custom_exc.<locals>.wrapped(self, etype, value, tb, tb_offset)
   2009     print(self.InteractiveTB.stb2text(stb))
   2010     print("The original exception:")
-> 2011     stb = self.InteractiveTB.structured_traceback(
   2012                             (etype,value,tb), tb_offset=tb_offset
   2013     )
   2014 return stb

TypeError: structured_traceback() missing 1 required positional argument: 'evalue'
Click to see complete logs
Custom TB Handler failed, unregistering
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
Exception: outer exception

The above exception was the direct cause of the following exception:

FooExc                                    Traceback (most recent call last)
    [... skipping hidden 1 frame]

Cell In[7], line 1
----> 1 raise FooExc("outer exception") from Exception("outer exception")

FooExc: outer exception

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
Cell In[5], line 16, in my_handler(self, etype, value, tb, tb_offset)
     12 def my_handler(
     13     self: InteractiveShell, etype: Type[BaseException], value: BaseException, tb: TracebackType, tb_offset: Optional[int] = None
     14 ):  
     15     breakpoint()
---> 16     self.showtraceback(exc_tuple=(etype, value, tb), filename=None, tb_offset=tb_offset, exception_only=True)
     17     return traceback.format_tb(tb, tb_offset)

File venv/lib/python3.9/site-packages/IPython/core/interactiveshell.py:2123, in InteractiveShell.showtraceback(self, exc_tuple, filename, tb_offset, exception_only, running_compiled_code)
   2119     breakpoint()
   2121     stb = ['An exception has occurred, use %tb to see '
   2122            'the full traceback.\n']
-> 2123     stb.extend(self.InteractiveTB.get_exception_only(etype,
   2124                                                      value))
   2125 else:
   2127     def contains_exceptiongroup(val):

File venv/lib/python3.9/site-packages/IPython/core/ultratb.py:710, in ListTB.get_exception_only(self, etype, value)
    702 def get_exception_only(self, etype, value):
    703     """Only print the exception type and message, without a traceback.
    704 
    705     Parameters
   (...)
    708     value : exception value
    709     """
--> 710     return ListTB.structured_traceback(self, etype, value)

File venv/lib/python3.9/site-packages/IPython/core/ultratb.py:568, in ListTB.structured_traceback(self, etype, evalue, etb, tb_offset, context)
    565     chained_exc_ids.add(id(exception[1]))
    566     chained_exceptions_tb_offset = 0
    567     out_list = (
--> 568         self.structured_traceback(
    569             etype,
    570             evalue,
    571             (etb, chained_exc_ids),  # type: ignore
    572             chained_exceptions_tb_offset,
    573             context,
    574         )
    575         + chained_exception_message
    576         + out_list)
    578 return out_list

File venv/lib/python3.9/site-packages/IPython/core/ultratb.py:1435, in AutoFormattedTB.structured_traceback(self, etype, evalue, etb, tb_offset, number_of_lines_of_context)
   1433 else:
   1434     self.tb = etb
-> 1435 return FormattedTB.structured_traceback(
   1436     self, etype, evalue, etb, tb_offset, number_of_lines_of_context
   1437 )

File venv/lib/python3.9/site-packages/IPython/core/ultratb.py:1326, in FormattedTB.structured_traceback(self, etype, value, tb, tb_offset, number_of_lines_of_context)
   1323 mode = self.mode
   1324 if mode in self.verbose_modes:
   1325     # Verbose modes need a full traceback
-> 1326     return VerboseTB.structured_traceback(
   1327         self, etype, value, tb, tb_offset, number_of_lines_of_context
   1328     )
   1329 elif mode == 'Minimal':
   1330     return ListTB.get_exception_only(self, etype, value)

File venv/lib/python3.9/site-packages/IPython/core/ultratb.py:1173, in VerboseTB.structured_traceback(self, etype, evalue, etb, tb_offset, number_of_lines_of_context)
   1164 def structured_traceback(
   1165     self,
   1166     etype: type,
   (...)
   1170     number_of_lines_of_context: int = 5,
   1171 ):
   1172     """Return a nice text document describing the traceback."""
-> 1173     formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
   1174                                                            tb_offset)
   1176     colors = self.Colors  # just a shorthand + quicker name lookup
   1177     colorsnormal = colors.Normal  # used a lot

File venv/lib/python3.9/site-packages/IPython/core/ultratb.py:1063, in VerboseTB.format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset)
   1060 assert isinstance(tb_offset, int)
   1061 head = self.prepare_header(str(etype), self.long_header)
   1062 records = (
-> 1063     self.get_records(etb, number_of_lines_of_context, tb_offset) if etb else []
   1064 )
   1066 frames = []
   1067 skipped = 0

File venv/lib/python3.9/site-packages/IPython/core/ultratb.py:1131, in VerboseTB.get_records(self, etb, number_of_lines_of_context, tb_offset)
   1129 while cf is not None:
   1130     try:
-> 1131         mod = inspect.getmodule(cf.tb_frame)
   1132         if mod is not None:
   1133             mod_name = mod.__name__

AttributeError: 'tuple' object has no attribute 'tb_frame'
The original exception:
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
Exception: outer exception

The above exception was the direct cause of the following exception:

FooExc                                    Traceback (most recent call last)
    [... skipping hidden 1 frame]

Cell In[7], line 1
----> 1 raise FooExc("outer exception") from Exception("outer exception")

FooExc: outer exception

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
File venv/lib/python3.9/site-packages/IPython/core/interactiveshell.py:2001, in InteractiveShell.set_custom_exc.<locals>.wrapped(self, etype, value, tb, tb_offset)
   2000 try:
-> 2001     stb = handler(self,etype,value,tb,tb_offset=tb_offset)
   2002     return validate_stb(stb)

Cell In[5], line 16, in my_handler(self, etype, value, tb, tb_offset)
     15 breakpoint()
---> 16 self.showtraceback(exc_tuple=(etype, value, tb), filename=None, tb_offset=tb_offset, exception_only=True)
     17 return traceback.format_tb(tb, tb_offset)

File venv/lib/python3.9/site-packages/IPython/core/interactiveshell.py:2123, in InteractiveShell.showtraceback(self, exc_tuple, filename, tb_offset, exception_only, running_compiled_code)
   2121     stb = ['An exception has occurred, use %tb to see '
   2122            'the full traceback.\n']
-> 2123     stb.extend(self.InteractiveTB.get_exception_only(etype,
   2124                                                      value))
   2125 else:

File venv/lib/python3.9/site-packages/IPython/core/ultratb.py:710, in ListTB.get_exception_only(self, etype, value)
    703 """Only print the exception type and message, without a traceback.
    704 
    705 Parameters
   (...)
    708 value : exception value
    709 """
--> 710 return ListTB.structured_traceback(self, etype, value)

File venv/lib/python3.9/site-packages/IPython/core/ultratb.py:568, in ListTB.structured_traceback(self, etype, evalue, etb, tb_offset, context)
    566     chained_exceptions_tb_offset = 0
    567     out_list = (
--> 568         self.structured_traceback(
    569             etype,
    570             evalue,
    571             (etb, chained_exc_ids),  # type: ignore
    572             chained_exceptions_tb_offset,
    573             context,
    574         )
    575         + chained_exception_message
    576         + out_list)
    578 return out_list

File venv/lib/python3.9/site-packages/IPython/core/ultratb.py:1435, in AutoFormattedTB.structured_traceback(self, etype, evalue, etb, tb_offset, number_of_lines_of_context)
   1434     self.tb = etb
-> 1435 return FormattedTB.structured_traceback(
   1436     self, etype, evalue, etb, tb_offset, number_of_lines_of_context
   1437 )

File venv/lib/python3.9/site-packages/IPython/core/ultratb.py:1326, in FormattedTB.structured_traceback(self, etype, value, tb, tb_offset, number_of_lines_of_context)
   1324 if mode in self.verbose_modes:
   1325     # Verbose modes need a full traceback
-> 1326     return VerboseTB.structured_traceback(
   1327         self, etype, value, tb, tb_offset, number_of_lines_of_context
   1328     )
   1329 elif mode == 'Minimal':

File venv/lib/python3.9/site-packages/IPython/core/ultratb.py:1173, in VerboseTB.structured_traceback(self, etype, evalue, etb, tb_offset, number_of_lines_of_context)
   1172 """Return a nice text document describing the traceback."""
-> 1173 formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
   1174                                                        tb_offset)
   1176 colors = self.Colors  # just a shorthand + quicker name lookup

File venv/lib/python3.9/site-packages/IPython/core/ultratb.py:1063, in VerboseTB.format_exception_as_a_whole(self, etype, evalue, etb, number_of_lines_of_context, tb_offset)
   1061 head = self.prepare_header(str(etype), self.long_header)
   1062 records = (
-> 1063     self.get_records(etb, number_of_lines_of_context, tb_offset) if etb else []
   1064 )
   1066 frames = []

File venv/lib/python3.9/site-packages/IPython/core/ultratb.py:1131, in VerboseTB.get_records(self, etb, number_of_lines_of_context, tb_offset)
   1130 try:
-> 1131     mod = inspect.getmodule(cf.tb_frame)
   1132     if mod is not None:

AttributeError: 'tuple' object has no attribute 'tb_frame'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
    [... skipping hidden 1 frame]

File venv/lib/python3.9/site-packages/IPython/core/interactiveshell.py:2011, in InteractiveShell.set_custom_exc.<locals>.wrapped(self, etype, value, tb, tb_offset)
   2009     print(self.InteractiveTB.stb2text(stb))
   2010     print("The original exception:")
-> 2011     stb = self.InteractiveTB.structured_traceback(
   2012                             (etype,value,tb), tb_offset=tb_offset
   2013     )
   2014 return stb

TypeError: structured_traceback() missing 1 required positional argument: 'evalue'

Side notes

  • I could only get it in a notebook; I initially tried to reproduced by putting the code in a tmp.py file and calling ipython tmp.py but for some reasons it wouldn’t call the custom exception handler.
  • My intuition here is that it is linked to exception chaining. In the snippet if you only raise FooExc("outer exception") without from Exception("outer exception"), the code behave as expected i.e. logging the error.