Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions IPython/core/ultratb.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def structured_traceback(
)
out_list.extend(self._format_list(elist))
# The exception info should be a single entry in the list.
lines = "".join(self._format_exception_only(etype, evalue))
lines = "".join(self._format_exception_only(etype, evalue, etb))
out_list.append(lines)

# Find chained exceptions if we have a traceback (not for exception-only mode)
Expand Down Expand Up @@ -288,7 +288,7 @@ def _format_list(self, extracted_list: list[Any]) -> list[str]:
return output_list

def _format_exception_only(
self, etype: type[BaseException], value: BaseException | None
self, etype: type[BaseException], value: BaseException | None, etb: TracebackType | None
) -> list[str]:
"""Format the exception part of a traceback.

Expand Down Expand Up @@ -372,7 +372,14 @@ def _format_exception_only(
)
s = value.msg
else:
s = self._some_str(value)
if isinstance(etb, TracebackType):
last_line = traceback.format_exception(etype, value, etb)[-1]
if ": " in last_line:
s = last_line.split(": ", 1)[1].rstrip()
else:
s = last_line.rstrip()
else:
s = self._some_str(value)
if s:
output_list.append(
theme_table[self._theme_name].format(
Expand Down Expand Up @@ -748,10 +755,22 @@ def prepare_header(self, etype: str, long_version: bool = False) -> str:

return head

def format_exception(self, etype, evalue):
def format_exception(self, etype, evalue, etb=None):
# Get (safely) a string form of the exception info
try:
etype_str, evalue_str = map(str, (etype, evalue))
etype_str = str(etype)

if isinstance(etb, TracebackType):
last_line = traceback.format_exception(
etype, evalue, etb
)[-1]

if ": " in last_line:
evalue_str = last_line.split(": ", 1)[1].rstrip()
else:
evalue_str = last_line.rstrip()
else:
evalue_str = str(evalue)
except:
# User exception is improperly defined.
etype, evalue = str, sys.exc_info()[:2]
Expand Down Expand Up @@ -840,7 +859,7 @@ def format_exception_as_a_whole(
)
)

formatted_exception = self.format_exception(etype, evalue)
formatted_exception = self.format_exception(etype, evalue, etb)
if records:
frame_info = records[-1]
ipinst = get_ipython()
Expand Down
19 changes: 19 additions & 0 deletions tests/test_ultratb.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,25 @@ def test_jsondecodeerror_message(self):
ip.run_cell(cell)
ip.run_cell("%xmode context")

class NameErrorSuggestionTest(unittest.TestCase):
def test_verbose_nameerror_suggestion(self):
ip.run_cell("%xmode Verbose")
with tt.AssertPrints(
["NameError", "Did you mean", "print"],
suppress=False,
):
ip.run_cell("printn('hello')")

def test_plain_nameerror_suggestion(self):
ip.run_cell("%xmode Plain")
try:
with tt.AssertPrints(
["NameError", "Did you mean", "print"],
suppress=False,
):
ip.run_cell("printn('hello')")
finally:
ip.run_cell("%xmode Verbose")

# ----------------------------------------------------------------------------

Expand Down
Loading