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

Method set_custom_exc

IPython/core/interactiveshell.py:1987–2088  ·  view source on GitHub ↗

set_custom_exc(exc_tuple, handler) Set a custom exception handler, which will be called if any of the exceptions in exc_tuple occur in the mainloop (specifically, in the run_code() method). Parameters ---------- exc_tuple : tuple of exception classes

(self, exc_tuple, handler)

Source from the content-addressed store, hash-verified

1985 self.InteractiveTB.set_mode(mode=self.xmode)
1986
1987 def set_custom_exc(self, exc_tuple, handler):
1988 """set_custom_exc(exc_tuple, handler)
1989
1990 Set a custom exception handler, which will be called if any of the
1991 exceptions in exc_tuple occur in the mainloop (specifically, in the
1992 run_code() method).
1993
1994 Parameters
1995 ----------
1996 exc_tuple : tuple of exception classes
1997 A *tuple* of exception classes, for which to call the defined
1998 handler. It is very important that you use a tuple, and NOT A
1999 LIST here, because of the way Python's except statement works. If
2000 you only want to trap a single exception, use a singleton tuple::
2001
2002 exc_tuple == (MyCustomException,)
2003
2004 handler : callable
2005 handler must have the following signature::
2006
2007 def my_handler(self, etype, value, tb, tb_offset=None):
2008 ...
2009 return structured_traceback
2010
2011 Your handler must return a structured traceback (a list of strings),
2012 or None.
2013
2014 This will be made into an instance method (via types.MethodType)
2015 of IPython itself, and it will be called if any of the exceptions
2016 listed in the exc_tuple are caught. If the handler is None, an
2017 internal basic one is used, which just prints basic info.
2018
2019 To protect IPython from crashes, if your handler ever raises an
2020 exception or returns an invalid result, it will be immediately
2021 disabled.
2022
2023 Notes
2024 -----
2025 WARNING: by putting in your own exception handler into IPython's main
2026 execution loop, you run a very good chance of nasty crashes. This
2027 facility should only be used if you really know what you are doing.
2028 """
2029
2030 if not isinstance(exc_tuple, tuple):
2031 raise TypeError("The custom exceptions must be given as a tuple.")
2032
2033 def dummy_handler(self, etype, value, tb, tb_offset=None):
2034 print('*** Simple custom exception handler ***')
2035 print('Exception type :', etype)
2036 print('Exception value:', value)
2037 print('Traceback :', tb)
2038
2039 def validate_stb(stb):
2040 """validate structured traceback return type
2041
2042 return type of CustomTB *should* be a list of strings, but allow
2043 single strings or None, which are harmless.
2044

Callers 7

wrappedMethod · 0.95
test_bad_custom_tbMethod · 0.80
test_custom_exceptionMethod · 0.80
test_custom_exc_countFunction · 0.80

Calls

no outgoing calls

Tested by 5

test_bad_custom_tbMethod · 0.64
test_custom_exceptionMethod · 0.64
test_custom_exc_countFunction · 0.64