Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ipython 8+ python crash on exception #13598

Open
2sn opened this issue Mar 20, 2022 · 42 comments
Open

ipython 8+ python crash on exception #13598

2sn opened this issue Mar 20, 2022 · 42 comments

Comments

@2sn
Copy link

2sn commented Mar 20, 2022

Since version 8 in Ipython I frequently - very frequently - get IPython crashes (termination) of the kind below.

I think the specific situation is that I edit code, I use autoreload(3), there is a bug, but when IPython tries traceback it crashes and terminates. Since a main point of using IPython is the auto-reload, this makes it almost unusable for development compared to Version 7.x.x. (Certainly distracting enough to spend the time to finally post it here.)

I have seen there is a few other bugs of that kind reported, but I was not sure which would be similar enough to any of them, hence I post this separately. My apologies for any duplications.

I am not sure whether the error is strictly reproducible as it happens in situations when code is edited.

I use Python 3.10.2 built from source on Linux (Fedora 34).

I am using pylab mode which loads matplotlib, I am not sure whether their event loop may interfere with IPython.
Just a thought.

In [15]: m.plot1()
Error in sys.excepthook:
Traceback (most recent call last):
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 1979, in showtraceback
    stb = value._render_traceback_()
AttributeError: 'NotOneValueFound' object has no attribute '_render_traceback_'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 1981, in showtraceback
    stb = self.InteractiveTB.structured_traceback(etype,
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 1105, in structured_traceback
    return FormattedTB.structured_traceback(
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 999, in structured_traceback
    return VerboseTB.structured_traceback(
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 871, in structured_traceback
    formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 804, in format_exception_as_a_whole
    frames.append(self.format_record(r))
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 723, in format_record
    result += ''.join(_format_traceback_lines(frame_info.lines, Colors, self.has_colors, lvals))
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 698, in lines
    pieces = self.included_pieces
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 649, in included_pieces
    pos = scope_pieces.index(self.executing_piece)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 628, in executing_piece
    return only(
  File "/home/alex/Python/lib/python3.10/site-packages/executing/executing.py", line 164, in only
    raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0

Original exception was:
Traceback (most recent call last):
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3361, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-15-0de954ff7b19>", line 1, in <cell line: 1>
  File "/home/alex/python/source/multistar/base.py", line 586, in plot1
    rr = norm(self.y[self.y_rad])
AttributeError: 'Direct' object has no attribute 'y_rad'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 1979, in showtraceback
    stb = value._render_traceback_()
AttributeError: 'AttributeError' object has no attribute '_render_traceback_'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3301, in run_ast_nodes
    if await self.run_code(code, result, async_=asy):
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3378, in run_code
    self.showtraceback(running_compiled_code=True)
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 1981, in showtraceback
    stb = self.InteractiveTB.structured_traceback(etype,
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 1105, in structured_traceback
    return FormattedTB.structured_traceback(
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 999, in structured_traceback
    return VerboseTB.structured_traceback(
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 852, in structured_traceback
    formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 804, in format_exception_as_a_whole
    frames.append(self.format_record(r))
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 723, in format_record
    result += ''.join(_format_traceback_lines(frame_info.lines, Colors, self.has_colors, lvals))
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 698, in lines
    pieces = self.included_pieces
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 649, in included_pieces
    pos = scope_pieces.index(self.executing_piece)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 628, in executing_piece
    return only(
  File "/home/alex/Python/lib/python3.10/site-packages/executing/executing.py", line 164, in only
    raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 1979, in showtraceback
    stb = value._render_traceback_()
AttributeError: 'NotOneValueFound' object has no attribute '_render_traceback_'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 2900, in _run_cell
    return runner(coro)
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner
    coro.send(None)
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3098, in run_cell_async
    has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3320, in run_ast_nodes
    self.showtraceback()
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 1981, in showtraceback
    stb = self.InteractiveTB.structured_traceback(etype,
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 1105, in structured_traceback
    return FormattedTB.structured_traceback(
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 999, in structured_traceback
    return VerboseTB.structured_traceback(
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 871, in structured_traceback
    formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 804, in format_exception_as_a_whole
    frames.append(self.format_record(r))
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 723, in format_record
    result += ''.join(_format_traceback_lines(frame_info.lines, Colors, self.has_colors, lvals))
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 698, in lines
    pieces = self.included_pieces
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 649, in included_pieces
    pos = scope_pieces.index(self.executing_piece)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 628, in executing_piece
    return only(
  File "/home/alex/Python/lib/python3.10/site-packages/executing/executing.py", line 164, in only
    raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 1979, in showtraceback
    stb = value._render_traceback_()
AttributeError: 'NotOneValueFound' object has no attribute '_render_traceback_'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/alex/Python/bin/ipython3", line 8, in <module>
    sys.exit(start_ipython())
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/__init__.py", line 123, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
  File "/home/alex/Python/lib/python3.10/site-packages/traitlets/config/application.py", line 846, in launch_instance
    app.start()
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/terminal/ipapp.py", line 318, in start
    self.shell.mainloop()
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/terminal/interactiveshell.py", line 653, in mainloop
    self.interact()
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/terminal/interactiveshell.py", line 646, in interact
    self.run_cell(code, store_history=True)
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 2854, in run_cell
    result = self._run_cell(
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 2905, in _run_cell
    self.showtraceback(running_compiled_code=True)
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 1981, in showtraceback
    stb = self.InteractiveTB.structured_traceback(etype,
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 1105, in structured_traceback
    return FormattedTB.structured_traceback(
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 999, in structured_traceback
    return VerboseTB.structured_traceback(
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 871, in structured_traceback
    formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 804, in format_exception_as_a_whole
    frames.append(self.format_record(r))
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 723, in format_record
    result += ''.join(_format_traceback_lines(frame_info.lines, Colors, self.has_colors, lvals))
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 698, in lines
    pieces = self.included_pieces
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 649, in included_pieces
    pos = scope_pieces.index(self.executing_piece)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 628, in executing_piece
    return only(
  File "/home/alex/Python/lib/python3.10/site-packages/executing/executing.py", line 164, in only
    raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0
@MrMino
Copy link
Member

MrMino commented Mar 20, 2022

Which version of IPython is this? There were at least 4 releases in 8.. series, IIRC some of them aimed at patching similar issues.

@2sn
Copy link
Author

2sn commented Mar 20, 2022

IPython 8.1.1 - the latest I can get on pip.

@2sn
Copy link
Author

2sn commented Mar 20, 2022

The previous 8-series versions had the same issue.

@MrMino
Copy link
Member

MrMino commented Mar 20, 2022

Do you have a minimal set of steps to reproduce this? If you're using autoreload it might be specific to the code you are reloading.

@2sn
Copy link
Author

2sn commented Mar 20, 2022

Unfortunately all I can show is the tracebacks. I seems to be related to autoreload that involves data current objects. Usually of rather complex data objects,

@2sn
Copy link
Author

2sn commented Mar 20, 2022

another example:

Error in sys.excepthook:
Traceback (most recent call last):
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 1979, in showtraceback
    stb = value._render_traceback_()
AttributeError: 'NotOneValueFound' object has no attribute '_render_traceback_'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 1981, in showtraceback
    stb = self.InteractiveTB.structured_traceback(etype,
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 1105, in structured_traceback
    return FormattedTB.structured_traceback(
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 999, in structured_traceback
    return VerboseTB.structured_traceback(
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 871, in structured_traceback
    formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 804, in format_exception_as_a_whole
    frames.append(self.format_record(r))
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 723, in format_record
    result += ''.join(_format_traceback_lines(frame_info.lines, Colors, self.has_colors, lvals))
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 698, in lines
    pieces = self.included_pieces
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 649, in included_pieces
    pos = scope_pieces.index(self.executing_piece)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 628, in executing_piece
    return only(
  File "/home/alex/Python/lib/python3.10/site-packages/executing/executing.py", line 164, in only
    raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0

Original exception was:
Traceback (most recent call last):
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3361, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-6-825396d5c1d6>", line 1, in <cell line: 1>
  File "/home/alex/python/source/multistar/base.py", line 532, in plot
    ax.plot(*(r * f), 'o', color=c)
NameError: name 'c' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 1979, in showtraceback
    stb = value._render_traceback_()
AttributeError: 'NameError' object has no attribute '_render_traceback_'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3301, in run_ast_nodes
    if await self.run_code(code, result, async_=asy):
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3378, in run_code
    self.showtraceback(running_compiled_code=True)
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 1981, in showtraceback
    stb = self.InteractiveTB.structured_traceback(etype,
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 1105, in structured_traceback
    return FormattedTB.structured_traceback(
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 999, in structured_traceback
    return VerboseTB.structured_traceback(
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 852, in structured_traceback
    formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 804, in format_exception_as_a_whole
    frames.append(self.format_record(r))
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 723, in format_record
    result += ''.join(_format_traceback_lines(frame_info.lines, Colors, self.has_colors, lvals))
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 698, in lines
    pieces = self.included_pieces
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 649, in included_pieces
    pos = scope_pieces.index(self.executing_piece)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 628, in executing_piece
    return only(
  File "/home/alex/Python/lib/python3.10/site-packages/executing/executing.py", line 164, in only
    raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 1979, in showtraceback
    stb = value._render_traceback_()
AttributeError: 'NotOneValueFound' object has no attribute '_render_traceback_'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 2900, in _run_cell
    return runner(coro)
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/async_helpers.py", line 129, in _pseudo_sync_runner
    coro.send(None)
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3098, in run_cell_async
    has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3320, in run_ast_nodes
    self.showtraceback()
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 1981, in showtraceback
    stb = self.InteractiveTB.structured_traceback(etype,
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 1105, in structured_traceback
    return FormattedTB.structured_traceback(
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 999, in structured_traceback
    return VerboseTB.structured_traceback(
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 871, in structured_traceback
    formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 804, in format_exception_as_a_whole
    frames.append(self.format_record(r))
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 723, in format_record
    result += ''.join(_format_traceback_lines(frame_info.lines, Colors, self.has_colors, lvals))
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 698, in lines
    pieces = self.included_pieces
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 649, in included_pieces
    pos = scope_pieces.index(self.executing_piece)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 628, in executing_piece
    return only(
  File "/home/alex/Python/lib/python3.10/site-packages/executing/executing.py", line 164, in only
    raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 1979, in showtraceback
    stb = value._render_traceback_()
AttributeError: 'NotOneValueFound' object has no attribute '_render_traceback_'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/alex/Python/bin/ipython3", line 8, in <module>
    sys.exit(start_ipython())
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/__init__.py", line 123, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
  File "/home/alex/Python/lib/python3.10/site-packages/traitlets/config/application.py", line 846, in launch_instance
    app.start()
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/terminal/ipapp.py", line 318, in start
    self.shell.mainloop()
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/terminal/interactiveshell.py", line 653, in mainloop
    self.interact()
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/terminal/interactiveshell.py", line 646, in interact
    self.run_cell(code, store_history=True)
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 2854, in run_cell
    result = self._run_cell(
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 2905, in _run_cell
    self.showtraceback(running_compiled_code=True)
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 1981, in showtraceback
    stb = self.InteractiveTB.structured_traceback(etype,
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 1105, in structured_traceback
    return FormattedTB.structured_traceback(
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 999, in structured_traceback
    return VerboseTB.structured_traceback(
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 871, in structured_traceback
    formatted_exceptions += self.format_exception_as_a_whole(etype, evalue, etb, lines_of_context,
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 804, in format_exception_as_a_whole
    frames.append(self.format_record(r))
  File "/home/alex/Python/lib/python3.10/site-packages/IPython/core/ultratb.py", line 723, in format_record
    result += ''.join(_format_traceback_lines(frame_info.lines, Colors, self.has_colors, lvals))
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 698, in lines
    pieces = self.included_pieces
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 649, in included_pieces
    pos = scope_pieces.index(self.executing_piece)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 628, in executing_piece
    return only(
  File "/home/alex/Python/lib/python3.10/site-packages/executing/executing.py", line 164, in only
    raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0

Then Ipython terminates and I have to start over again.

@2sn
Copy link
Author

2sn commented Mar 20, 2022

It seems to be related to render_traceback. I wonder whether the new new util the Ipython 8.x uses to render output should be rolled back to the previous - working - version. What is currently being used does not yet seem ready for prime time.

@Carreau
Copy link
Member

Carreau commented Mar 21, 2022

which version of executing and stack_data ?

@2sn
Copy link
Author

2sn commented Mar 21, 2022

I use the current versions from pip

  • executing 0.8.3
  • stack-data 0.2.0

@2sn
Copy link
Author

2sn commented Mar 21, 2022

the last error always seems to come from executing - maybe this needs to be replaced by a properly debugged package?

  File "/home/alex/Python/lib/python3.10/site-packages/executing/executing.py", line 164, in only
    raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0

@Carreau
Copy link
Member

Carreau commented Mar 21, 2022 via email

@2sn
Copy link
Author

2sn commented Mar 22, 2022

Hmm, it seems it was not successful then.

I see many dozens of these per day:

  File "/home/alex/Python/lib/python3.10/site-packages/executing/executing.py", line 164, in only
    raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0

Should I switch to an earlier version? Can it be disabled?

@2sn
Copy link
Author

2sn commented Mar 22, 2022

I installed Version 0.7.0, still fails.

  File "/home/alex/Python/lib/python3.10/site-packages/executing/executing.py", line 138, in only
    raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0

Maybe to replace the raise statement by a pass?

@2sn
Copy link
Author

2sn commented Mar 22, 2022

that did not help, now I end up with

  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/home/alex/Python/lib/python3.10/site-packages/stack_data/core.py", line 649, in included_pieces
    pos = scope_pieces.index(self.executing_piece)
ValueError: None is not in list

@brandondube

This comment was marked as off-topic.

@nedlrichards
Copy link

I have this issue too with IPython 8.1.1, on a Pop OS machine. It seems like the error message is essentially identical to #13586. For the moment, I just reset ipython every few mins during debugging.

Carreau added a commit to Carreau/ipython that referenced this issue Mar 26, 2022
Carreau added a commit to Carreau/ipython that referenced this issue Mar 26, 2022
Carreau added a commit that referenced this issue Mar 27, 2022
jdtsmith pushed a commit to jdtsmith/ipython that referenced this issue Mar 27, 2022
@brandondube
Copy link

@Carreau did you mark my comment as off-topic with no other engagement? Why? I tried to provide some detailed information that ipython v8 is not compatible with jupyter.

@yonatanp
Copy link

I want to +1 the relevance of @brandondube 's report to this issue.
The error being reported there is the same one that essentially got me here: when a cell raises an error, and the internal error handling of IPython fails to handle it (with the executing.executing.NotOneValueFound: Expected one value, found 0 error and the AttributeError: 'NotOneValueFound' object has no attribute '_render_traceback_' error), it ultimately results in the cell's number not being properly stored in Jupyter.
This in turn results in a dissociation between the counters of Jupyter and those of its history sqlite database.
And when you run another cell, you get the error that @brandondube reports:
ERROR! Session/line number was not unique in database. History logging moved to new session ###

So at the very least this gives scope to the impact of the bug, and allows searchers like myself to find this issue and figure out that's the underlying cause.

@ndemou
Copy link

ndemou commented Jun 26, 2022

I get the same long stream of errors that end with "executing.executing.NotOneValueFound: Expected one value, found 0" on windows with ipython 8.4.0 (python 3.9.2). I just start ipython, import, edit, re-import and at some point when my script raises an error I get it.

@jaseg
Copy link

jaseg commented Jul 25, 2022

Here's a repro for this. On my system I was able to consistently trigger this type of exception by importlib.reload'ing a module. It seems ipython gets confused when the module changes on disk and the old source location of the error does not align with the new source location. I can imagine that running conda/pip/other package managers while ipython is running may also cause this issue.

I'm running jupyterlab 3.4.3, jupyter-nbclassic 0.3.7, jupyter-notebook 6.4.12, and python 3.10.5 on Arch Linux x86_64.

To reproduce, run these three ipython cells in order:

Cell 1

import importlib
import sys
from pathlib import Path

tmpdir = Path('/tmp/traceback_exc_test')
tmpdir.mkdir(exist_ok=True)
sys.path.append(str(tmpdir))

(tmpdir / 'ipy_err_test_module.py').write_text('print("imported version 1")\ndef crash_and_burn():\n    raise ValueError()')

import ipy_err_test_module
try:
    ipy_err_test_module.crash_and_burn()
except:
    pass

Output:

imported version 1

Cell 2

(tmpdir / 'ipy_err_test_module.py').write_text('print("imported version 2")\n\n\ndef crash_and_burn():\n    raise SystemError()')

importlib.reload(ipy_err_test_module)
ipy_err_test_module.crash_and_burn()

Output:

---------------------------------------------------------------------------
SystemError                               Traceback (most recent call last)
Input In [2], in <cell line: 4>()
      1 (tmpdir / 'ipy_err_test_module.py').write_text('print("imported version 2")\n\n\ndef crash_and_burn():\n    raise SystemError()')
      3 importlib.reload(ipy_err_test_module)
----> 4 ipy_err_test_module.crash_and_burn()

File /tmp/traceback_exc_test/ipy_err_test_module.py:5, in crash_and_burn()
      4 def crash_and_burn():
----> 5     raise SystemError()

SystemError: 

Cell 3

(tmpdir / 'ipy_err_test_module.py').write_text('print("imported version 2")\n\n\n\n\n\n\ndef crash_and_burn():\n    raise SystemError()')

importlib.reload(ipy_err_test_module)
ipy_err_test_module.crash_and_burn()

Output:

(tmpdir / 'ipy_err_test_module.py').write_text('print("imported version 2")\n\n\n\n\n\n\ndef crash_and_burn():\n    raise SystemError()')

​

importlib.reload(ipy_err_test_module)

ipy_err_test_module.crash_and_burn()

imported version 2
Unexpected exception formatting exception. Falling back to standard exception

Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3398, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "/tmp/ipykernel_5390/429422080.py", line 4, in <cell line: 4>
    ipy_err_test_module.crash_and_burn()
  File "/tmp/traceback_exc_test/ipy_err_test_module.py", line 9, in crash_and_burn
    raise SystemError()
SystemError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 1993, in showtraceback
    stb = self.InteractiveTB.structured_traceback(
  File "/usr/lib/python3.10/site-packages/IPython/core/ultratb.py", line 1118, in structured_traceback
    return FormattedTB.structured_traceback(
  File "/usr/lib/python3.10/site-packages/IPython/core/ultratb.py", line 1012, in structured_traceback
    return VerboseTB.structured_traceback(
  File "/usr/lib/python3.10/site-packages/IPython/core/ultratb.py", line 865, in structured_traceback
    formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
  File "/usr/lib/python3.10/site-packages/IPython/core/ultratb.py", line 818, in format_exception_as_a_whole
    frames.append(self.format_record(r))
  File "/usr/lib/python3.10/site-packages/IPython/core/ultratb.py", line 736, in format_record
    result += ''.join(_format_traceback_lines(frame_info.lines, Colors, self.has_colors, lvals))
  File "/usr/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/usr/lib/python3.10/site-packages/stack_data/core.py", line 698, in lines
    pieces = self.included_pieces
  File "/usr/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/usr/lib/python3.10/site-packages/stack_data/core.py", line 649, in included_pieces
    pos = scope_pieces.index(self.executing_piece)
  File "/usr/lib/python3.10/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/usr/lib/python3.10/site-packages/stack_data/core.py", line 628, in executing_piece
    return only(
  File "/usr/lib/python3.10/site-packages/executing/executing.py", line 164, in only
    raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0

@mlucool
Copy link

mlucool commented Jul 27, 2022

A simple reproducer we came across for this type of error:

def enhanced_foo(*args, **kwargs):
    pass
    pass
    pass
    raise ValueError()


def enhance_method(function):
    f = enhanced_foo
    c0 = function.__code__
    f.__code__ = f.__code__.replace(co_filename=c0.co_filename, co_firstlineno=c0.co_firstlineno)
    return f


@enhance_method
def foo():
    pass
    pass


foo()

@alexmojaki
Copy link
Contributor

Hi all. Author of executing and stack_data here.

First of all, please @ me or open an issue on stack_data in cases like this. It's a bit weird seeing so many people clearly very annoyed about this but no one telling me about it. I'm not watching IPython's issue tracker so I have no way of knowing, I just happened to stumble across this now. This isn't even the first time I've discovered an issue in this way. @Carreau especially please ping me sooner.

I see the traceback always ends like this:

  File "/usr/lib/python3.10/site-packages/stack_data/core.py", line 628, in executing_piece
    return only(
  File "/usr/lib/python3.10/site-packages/executing/executing.py", line 164, in only
    raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0

only is a simple generic utility function for extracting one item from an iterable. only(lst) is a lot like lst[0]. When it says "found 0" that's similar to lst[0] raising an IndexError because lst is empty. So the problem here is not in executing but in stack_data and the executing_piece method. If executing_piece is not the second last entry in the traceback, it's probably a different issue.

stack_data maintains an internal cache of metadata parsed from source code. When the source code changes and the module is reloaded, it has no way of knowing that. Reloading just isn't something I considered originally, and never using the cache in case there was a reload could have a noticeable effect on performance. I will investigate this more closely and see if I can avoid this kind of error in stack_data without harming performance.

In the meantime, I suggest running this code:

import stack_data

stack_data.Source.for_filename(filename, use_cache=False)

for all reloaded files/modules immediately after reloading them.

@jaseg
Copy link

jaseg commented Aug 8, 2022

@alexmojaki A way to catch reloading modules would be to insert a MetaPathFinder at index 0 of sys.meta_path. When any module is reloaded, that finder will get its find_spec method called with the original path of the modules __init__.py as the target argument (which otherwise would be None). Thus, this finder can catch when any module is re-imported and you could use it to just drop all caches when that happens.

IMHO this solution is pretty minimally invasive, as it does not mess around with python's actual importing machinery.

Here's a demo:

import importlib
import importlib.abc
import sys

class PrintFinder(importlib.abc.MetaPathFinder):
    def find_spec(fullname, path, target=None):
        if target:
            print('    reloading', fullname, path, target)
            # drop caches here

sys.meta_path.insert(0, PrintFinder)

print('=== Initial import ===')
import requests
print('=== Reload ===')
importlib.reload(requests)

Output:

=== Initial import ===
=== Reload ===
    reloading requests None <module 'requests' from '/usr/lib/python3.10/site-packages/requests/__init__.py'>

@alexmojaki
Copy link
Contributor

Thanks @jaseg for the great suggestion. I've implemented and released it. Everyone: pip install -U executing to get the fix in version 0.10.0.

@yonatanp
Copy link

We should be mindful that if someone installs another MetaPathFinder at index zero after executing has done its thing, then this workaround would not work when reloading modules provided by that Finder.

It’s probably a small minority of cases but it makes this a workaround, not a complete solution.

I was wondering if there is any place where we can check the identity of the cached module compared to the item in sys.modules, and flush the cache when the identity is not the same?

@jaseg
Copy link

jaseg commented Aug 15, 2022

I agree that it is still possible to break this solution, but there are some mitigating factors at play and I think the practical risk is very low.

  • A quick github search does not (yet, as of 2022-08) produce many results of code that messes with sys.meta_path in the first place.
  • Another finder just being present at index 0 is not a problem by itself if the crash is not in a module actually handled by that finder. Since the common cases are users either importing their own files or pip' updating modules while jupyter is running, those likely would not be handled by a nonstandard meta_path finder anyway. The only example of such a finder that I found was some plugin system of some sort, which would not affect user code or regular pip' install'ed python modules.
  • Finally, this case would really be the fault of the later module inserting itself at index 0 b/c executing would already have been loaded at this point by jupyter. So if someone ever actually manages to trigger this corner case, some coordination has to happen so that the offending module checks sys.meta_path[0] and inserts itself at index 1 if it finds executing there.

@ChrisBarker-NOAA
Copy link
Contributor

Just another data point:

I'm seeing this with regular old iPython command line, outside Jupyter.

ipython                   8.4.0            py39h6e9494a_0    conda-forge
executing                 0.10.0             pyhd8ed1ab_0    conda-forge

I am not doing any reloading magic by hand, but get this from the run command.

Curiously, after starting up iPython, run the_python_file.py captures the exception just fine, but the second time I run it, it fails in this same way.

Unexpected exception formatting exception. Falling back to standard exception
Traceback (most recent call last):
  File "/Users/chris.barker/miniconda3/envs/gnome/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 2738, in safe_execfile
    py3compat.execfile(
  File "/Users/chris.barker/miniconda3/envs/gnome/lib/python3.9/site-packages/IPython/utils/py3compat.py", line 55, in execfile
    exec(compiler(f.read(), fname, "exec"), glob, loc)
  File "/Users/chris.barker/Hazmat/Response/2022/F:V Aleutian Isle/tidal forecasts/tide_interpolation.py", line 140, in <module>
    interp_tides(data1, data2)
  File "/Users/chris.barker/Hazmat/Response/2022/F:V Aleutian Isle/tidal forecasts/tide_interpolation.py", line 85, in interp_tides
    raise ValueError("tide stations seem to be out of sync")
ValueError: tide stations seem to be out of sync

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/chris.barker/miniconda3/envs/gnome/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 1993, in showtraceback
    stb = self.InteractiveTB.structured_traceback(
  File "/Users/chris.barker/miniconda3/envs/gnome/lib/python3.9/site-packages/IPython/core/ultratb.py", line 1118, in structured_traceback
    return FormattedTB.structured_traceback(
  File "/Users/chris.barker/miniconda3/envs/gnome/lib/python3.9/site-packages/IPython/core/ultratb.py", line 1012, in structured_traceback
    return VerboseTB.structured_traceback(
  File "/Users/chris.barker/miniconda3/envs/gnome/lib/python3.9/site-packages/IPython/core/ultratb.py", line 865, in structured_traceback
    formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
  File "/Users/chris.barker/miniconda3/envs/gnome/lib/python3.9/site-packages/IPython/core/ultratb.py", line 818, in format_exception_as_a_whole
    frames.append(self.format_record(r))
  File "/Users/chris.barker/miniconda3/envs/gnome/lib/python3.9/site-packages/IPython/core/ultratb.py", line 736, in format_record
    result += ''.join(_format_traceback_lines(frame_info.lines, Colors, self.has_colors, lvals))
  File "/Users/chris.barker/miniconda3/envs/gnome/lib/python3.9/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/Users/chris.barker/miniconda3/envs/gnome/lib/python3.9/site-packages/stack_data/core.py", line 699, in lines
    pieces = self.included_pieces
  File "/Users/chris.barker/miniconda3/envs/gnome/lib/python3.9/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/Users/chris.barker/miniconda3/envs/gnome/lib/python3.9/site-packages/stack_data/core.py", line 647, in included_pieces
    pos = scope_pieces.index(self.executing_piece)
  File "/Users/chris.barker/miniconda3/envs/gnome/lib/python3.9/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/Users/chris.barker/miniconda3/envs/gnome/lib/python3.9/site-packages/stack_data/core.py", line 626, in executing_piece
    return only(
  File "/Users/chris.barker/miniconda3/envs/gnome/lib/python3.9/site-packages/executing/executing.py", line 164, in only
    raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0

@alexmojaki
Copy link
Contributor

Does this happen even if you don't change the python file between runs?

@ChrisBarker-NOAA
Copy link
Contributor

I actually hadn't noticed -- the whole point of re-running was that. I changed it :-)

But testing now, you're insight is right -- it only happens if I change the file, and sometimes not then, depending on the change. I haven't tested carefully, but I think it happens when I add or remove lines before where the exception occurs.

@jaymegordo
Copy link

Nothing to add other than a +1, this issue happens very frequently working in vscode python interactive terminal with

"%load_ext autoreload",
"%autoreload 2",

executing = 0.10.0
stack-data = 0.4.0
IPython = 8.4.0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/jayme/Library/Caches/pypoetry/virtualenvs/jambot-B_1U8XU9-py3.9/lib/python3.9/site-packages/IPython/core/interactiveshell.py", line 1993, in showtraceback
    stb = self.InteractiveTB.structured_traceback(
  File "/Users/jayme/Library/Caches/pypoetry/virtualenvs/jambot-B_1U8XU9-py3.9/lib/python3.9/site-packages/IPython/core/ultratb.py", line 1118, in structured_traceback
    return FormattedTB.structured_traceback(
  File "/Users/jayme/Library/Caches/pypoetry/virtualenvs/jambot-B_1U8XU9-py3.9/lib/python3.9/site-packages/IPython/core/ultratb.py", line 1012, in structured_traceback
    return VerboseTB.structured_traceback(
  File "/Users/jayme/Library/Caches/pypoetry/virtualenvs/jambot-B_1U8XU9-py3.9/lib/python3.9/site-packages/IPython/core/ultratb.py", line 865, in structured_traceback
    formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
  File "/Users/jayme/Library/Caches/pypoetry/virtualenvs/jambot-B_1U8XU9-py3.9/lib/python3.9/site-packages/IPython/core/ultratb.py", line 818, in format_exception_as_a_whole
    frames.append(self.format_record(r))
  File "/Users/jayme/Library/Caches/pypoetry/virtualenvs/jambot-B_1U8XU9-py3.9/lib/python3.9/site-packages/IPython/core/ultratb.py", line 736, in format_record
    result += ''.join(_format_traceback_lines(frame_info.lines, Colors, self.has_colors, lvals))
  File "/Users/jayme/Library/Caches/pypoetry/virtualenvs/jambot-B_1U8XU9-py3.9/lib/python3.9/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/Users/jayme/Library/Caches/pypoetry/virtualenvs/jambot-B_1U8XU9-py3.9/lib/python3.9/site-packages/stack_data/core.py", line 699, in lines
    pieces = self.included_pieces
  File "/Users/jayme/Library/Caches/pypoetry/virtualenvs/jambot-B_1U8XU9-py3.9/lib/python3.9/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/Users/jayme/Library/Caches/pypoetry/virtualenvs/jambot-B_1U8XU9-py3.9/lib/python3.9/site-packages/stack_data/core.py", line 647, in included_pieces
    pos = scope_pieces.index(self.executing_piece)
  File "/Users/jayme/Library/Caches/pypoetry/virtualenvs/jambot-B_1U8XU9-py3.9/lib/python3.9/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/Users/jayme/Library/Caches/pypoetry/virtualenvs/jambot-B_1U8XU9-py3.9/lib/python3.9/site-packages/stack_data/core.py", line 626, in executing_piece
    return only(
  File "/Users/jayme/Library/Caches/pypoetry/virtualenvs/jambot-B_1U8XU9-py3.9/lib/python3.9/site-packages/executing/executing.py", line 164, in only
    raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0

@alexmojaki
Copy link
Contributor

Thanks for the report @jaymegordo. It's interesting that you're still experiencing with the combination of the latest executing and autoreload, which isn't supposed to happen. That suggests that either there's another MetaPathFinder getting in the way as @yonatanp feared, or there's a bug in mine.

In any case, I have a plan for a completely general solution, which wouldn't rely on a MetaPathFinder and would include situations that don't involve reloading modules like @ChrisBarker-NOAA repeatedly running files. I should have it done this weekend.

@alexmojaki
Copy link
Contributor

New fix is released in executing 1.0.0!

@alexmojaki
Copy link
Contributor

Based on the reactions above and the lack of further comments, I think this and #13586 can be closed?

@pestrela
Copy link

pestrela commented Jan 22, 2023

@alexmojaki I want to report that this bugfix fixed my significant autoreload problems. Many thanks!

The pattern was this infamous message:

ERROR! Session/line number was not unique in database

PS: Please also note this trivial autoreload bugfix for python3 required kwargs: #14145

@quswadress
Copy link

Greetings all! I'm just a random developer that ran into this bug and @alexmojaki said that the fix was in executing 1.0.0, however even having executing version 1.2.0 the bug remains.

$ ipython 
Python 3.10.9 | packaged by conda-forge | (main, Feb  2 2023, 20:20:04) [GCC 11.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.11.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import executing
   ...: executing.__version__
Out[1]: '1.2.0'

In [2]: def enhanced_foo(*args, **kwargs):
   ...:     pass
   ...:     pass
   ...:     pass
   ...:     raise ValueError()
   ...: 
   ...: 
   ...: def enhance_method(function):
   ...:     f = enhanced_foo
   ...:     c0 = function.__code__
   ...:     f.__code__ = f.__code__.replace(co_filename=c0.co_filename, co_firstlineno=c0.co_firstlineno)
   ...:     return f
   ...: 
   ...: 
   ...: @enhance_method
   ...: def foo():
   ...:     pass
   ...:     pass
   ...: 
   ...: 
   ...: foo()
Unexpected exception formatting exception. Falling back to standard exception
Traceback (most recent call last):
  File "/opt/conda/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3460, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-c6b7ce91db6a>", line 21, in <module>
    foo()
  File "<ipython-input-2-c6b7ce91db6a>", line 19, in enhanced_foo
ValueError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/conda/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 2057, in showtraceback
    stb = self.InteractiveTB.structured_traceback(
  File "/opt/conda/lib/python3.10/site-packages/IPython/core/ultratb.py", line 1288, in structured_traceback
    return FormattedTB.structured_traceback(
  File "/opt/conda/lib/python3.10/site-packages/IPython/core/ultratb.py", line 1177, in structured_traceback
    return VerboseTB.structured_traceback(
  File "/opt/conda/lib/python3.10/site-packages/IPython/core/ultratb.py", line 1030, in structured_traceback
    formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
  File "/opt/conda/lib/python3.10/site-packages/IPython/core/ultratb.py", line 960, in format_exception_as_a_whole
    frames.append(self.format_record(record))
  File "/opt/conda/lib/python3.10/site-packages/IPython/core/ultratb.py", line 870, in format_record
    frame_info.lines, Colors, self.has_colors, lvals
  File "/opt/conda/lib/python3.10/site-packages/IPython/core/ultratb.py", line 704, in lines
    return self._sd.lines
  File "/opt/conda/lib/python3.10/site-packages/stack_data/utils.py", line 144, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/opt/conda/lib/python3.10/site-packages/stack_data/core.py", line 734, in lines
    pieces = self.included_pieces
  File "/opt/conda/lib/python3.10/site-packages/stack_data/utils.py", line 144, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/opt/conda/lib/python3.10/site-packages/stack_data/core.py", line 681, in included_pieces
    pos = scope_pieces.index(self.executing_piece)
  File "/opt/conda/lib/python3.10/site-packages/stack_data/utils.py", line 144, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
  File "/opt/conda/lib/python3.10/site-packages/stack_data/core.py", line 660, in executing_piece
    return only(
  File "/opt/conda/lib/python3.10/site-packages/executing/executing.py", line 190, in only
    raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0

@alexmojaki
Copy link
Contributor

The @enhance_method reproduction is a bit too contrived. Does the error still manifest in a practical setting?

@quswadress
Copy link

quswadress commented Apr 20, 2023

Um, yeah? It was because I encountered this error in my notebook that I googled this issue.

However, I have already restarted linux and this error has disappeared 😆 so I cannot send a screenshot of this error.
So I think it's rare enough to fix it)

@rawlins
Copy link
Contributor

rawlins commented Oct 1, 2023

Just encountered this in fairly current IPython, this env has:

>>> import IPython, executing
>>> IPython.__version__, executing.__version__
('8.15.0', '1.2.0')

Went away on restart kernel + clear all outputs, sorry to not have any more concrete repro info. (Edit: to clarify, the context was fairly chaotic, was actually debugging repr code for an exception class that in my code is triggered via a magic -- so lots of restarting + raising in a non-recoverable sequence. Exactly the sort of scenario where you'd expect a bug like this to pop up if it can, but not helpful for repro.)

@6502
Copy link

6502 commented Nov 3, 2023

Just bumped into the same error using IPython directly from terminal and pressing ctrl-c to stop a loop printing solutions to an equation. 8/10 the loop terminates normally (KeyboardInterrupt), but 2/10 I get the following instead:

^CUnexpected exception formatting exception. Falling back to standard exception
Traceback (most recent call last):
  File "/usr/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3526, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-15-594683a05ef4>", line -1, in <module>
KeyboardInterrupt

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 2120, in showtraceback
    stb = self.InteractiveTB.structured_traceback(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/IPython/core/ultratb.py", line 1435, in structured_traceback
    return FormattedTB.structured_traceback(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/IPython/core/ultratb.py", line 1326, in structured_traceback
    return VerboseTB.structured_traceback(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/IPython/core/ultratb.py", line 1173, in structured_traceback
    formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/IPython/core/ultratb.py", line 1088, in format_exception_as_a_whole
    frames.append(self.format_record(record))
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/IPython/core/ultratb.py", line 970, in format_record
    frame_info.lines, Colors, self.has_colors, lvals
    ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/IPython/core/ultratb.py", line 792, in lines
    return self._sd.lines
           ^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
                                               ^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/stack_data/core.py", line 734, in lines
    pieces = self.included_pieces
             ^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
                                               ^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/stack_data/core.py", line 681, in included_pieces
    pos = scope_pieces.index(self.executing_piece)
                             ^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/stack_data/utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
                                               ^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/stack_data/core.py", line 660, in executing_piece
    return only(
           ^^^^^
  File "/usr/lib/python3.11/site-packages/executing/executing.py", line 116, in only
    raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0

I am using Arch ...

Python 3.11.5 (main, Sep  2 2023, 14:16:33) [GCC 13.2.1 20230801]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.16.0 -- An enhanced Interactive Python. Type '?' for help.

@janhavi-sathe
Copy link

Adding a comment here as I also got this error. Restarting the kernel seems to be the only way out.

Unexpected exception formatting exception. Falling back to standard exception
Traceback (most recent call last):
  File "/Users/janhavisathe/Library/Python/3.9/lib/python/site-packages/IPython/core/interactiveshell.py", line 3526, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "/var/folders/nq/9ll7c1x53s9627s701s9_8840000gn/T/ipykernel_79729/288129965.py", line 12, in <module>
    img = image_from_url(url)
  File "/Users/janhavisathe/Documents/.../image_utils.py", line 64, in image_from_url
    img = imread(fname)
  File "/Users/janhavisathe/Library/Python/3.9/lib/python/site-packages/imageio/__init__.py", line 97, in imread
    return imread_v2(uri, format=format, **kwargs)
  File "/Users/janhavisathe/Library/Python/3.9/lib/python/site-packages/imageio/v2.py", line 359, in imread
    with imopen(uri, "ri", **imopen_args) as file:
  File "/Users/janhavisathe/Library/Python/3.9/lib/python/site-packages/imageio/core/imopen.py", line 237, in imopen
    plugin_instance = config.plugin_class(request, **kwargs)
  File "/Users/janhavisathe/Library/Python/3.9/lib/python/site-packages/imageio/plugins/pillow.py", line 91, in __init__
    with Image.open(request.get_file()):
  File "/Users/janhavisathe/Library/Python/3.9/lib/python/site-packages/PIL/Image.py", line 3284, in open
    # Image processing.
  File "/Users/janhavisathe/Library/Python/3.9/lib/python/site-packages/PIL/Image.py", line 3270, in _open_core
  File "/Users/janhavisathe/Library/Python/3.9/lib/python/site-packages/PIL/JpegImagePlugin.py", line 821, in jpeg_factory
    im = JpegImageFile(fp, filename)
  File "/Users/janhavisathe/Library/Python/3.9/lib/python/site-packages/PIL/ImageFile.py", line 117, in __init__
    self._open()
  File "/Users/janhavisathe/Library/Python/3.9/lib/python/site-packages/PIL/JpegImagePlugin.py", line 382, in _open
    handler(self, i)
  File "/Users/janhavisathe/Library/Python/3.9/lib/python/site-packages/PIL/JpegImagePlugin.py", line 213, in SOF
...
    return only(
  File "/Users/janhavisathe/Library/Python/3.9/lib/python/site-packages/executing/executing.py", line 190, in only
    raise NotOneValueFound('Expected one value, found 0')
executing.executing.NotOneValueFound: Expected one value, found 0

@h-sellak
Copy link

h-sellak commented Jan 1, 2024

Faced the same issue with:
import IPython, executing
IPython.version, executing.version
('8.15.0', '0.8.3')

I still don't understand the source of the problem though. Will update this comment as I find new evidence.

@alexmojaki
Copy link
Contributor

That's not the latest version of executing. I think conda or something weirdly uses an old version of executing when installing IPython.

Carreau added a commit to Carreau/ipython that referenced this issue Jan 8, 2024
This is a workaround for ipython#13598,

It does not fix the root cause, but should prevent IPython crash.

Worst case it should just not show the relevant frame
Carreau added a commit that referenced this issue Jan 9, 2024
This is a workaround for #13598,

It does not fix the root cause, but should prevent IPython crash.

Worst case it should just not show the relevant frame
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests