MCPcopy Index your code
hub / github.com/ipython/ipython / _format_exception_only

Method _format_exception_only

IPython/core/ultratb.py:290–403  ·  view source on GitHub ↗

Format the exception part of a traceback. The arguments are the exception type and value such as given by sys.exc_info()[:2]. The return value is a list of strings, each ending in a newline. Normally, the list contains a single string; however, for SyntaxError excep

(
        self, etype: type[BaseException], value: BaseException | None
    )

Source from the content-addressed store, hash-verified

288 return output_list
289
290 def _format_exception_only(
291 self, etype: type[BaseException], value: BaseException | None
292 ) -> list[str]:
293 """Format the exception part of a traceback.
294
295 The arguments are the exception type and value such as given by
296 sys.exc_info()[:2]. The return value is a list of strings, each ending
297 in a newline. Normally, the list contains a single string; however,
298 for SyntaxError exceptions, it contains several lines that (when
299 printed) display detailed information about where the syntax error
300 occurred. The message indicating which exception occurred is the
301 always last string in the list.
302
303 Also lifted nearly verbatim from traceback.py
304 """
305 have_filedata = False
306 output_list = []
307 stype_tokens = [(Token.ExcName, etype.__name__)]
308 stype: str = theme_table[self._theme_name].format(stype_tokens)
309 if value is None:
310 # Not sure if this can still happen in Python 2.6 and above
311 output_list.append(stype + "\n")
312 else:
313 if issubclass(etype, SyntaxError):
314 assert hasattr(value, "filename")
315 assert hasattr(value, "lineno")
316 assert hasattr(value, "text")
317 assert hasattr(value, "offset")
318 assert hasattr(value, "msg")
319 have_filedata = True
320 if not value.filename:
321 value.filename = "<string>"
322 if value.lineno:
323 lineno = value.lineno
324 textline = linecache.getline(value.filename, value.lineno)
325 else:
326 lineno = "unknown"
327 textline = ""
328 output_list.append(
329 theme_table[self._theme_name].format(
330 [(Token, " ")]
331 + _tokens_filename(
332 True,
333 value.filename,
334 lineno=(None if lineno == "unknown" else lineno),
335 )
336 + [(Token, "\n")]
337 )
338 )
339 if textline == "":
340 # sep 2025:
341 # textline = py3compat.cast_unicode(value.text, "utf-8")
342 if value.text is None:
343 textline = ""
344 else:
345 assert isinstance(value.text, str)
346 textline = value.text
347

Callers 2

structured_tracebackMethod · 0.95

Calls 4

_some_strMethod · 0.95
get_ipythonFunction · 0.90
_tokens_filenameFunction · 0.85
formatMethod · 0.45

Tested by

no test coverage detected