@@ -1134,25 +1134,34 @@ def exceptHook(*args):
11341134 '''
11351135 import IPython .core
11361136 savehook = sys .excepthook # save the exception hook
1137- # show the error
1138- # TODO: define a config var that allows selection between the following:
1139- #tb_formatter = IPython.core.ultratb.VerboseTB()
1137+ # show the error location
11401138 tb_formatter = IPython .core .ultratb .FormattedTB ()
1141- #tb_formatter = IPython.core.ultratb.ListTB()
1142- print (tb_formatter .text (* args ))
1139+ #tb_formatter = IPython.core.ultratb.ListTB() # better for windows?
1140+ print () # blank line
1141+ print (tb_formatter .text (* args ,- 1 ),end = '' ) # show only last routine
11431142 # get the Ipython shell routine
11441143 if IPython .core .getipython .get_ipython () is None :
1145- ipshell = IPython .terminal .embed .InteractiveShellEmbed .instance ()
1146- else :
1144+ ipshell = IPython .terminal .embed .InteractiveShellEmbed .instance (banner1 = '' )
1145+ else : # older IPython (still needed?)
11471146 ipshell = IPython .terminal .embed .InteractiveShellEmbed ()
1148- # get to the right frame
1149- try :
1147+ # traceback display routines
1148+ def TB (): print (IPython .core .ultratb .FormattedTB ().text (* args ))
1149+ def vTB (): print (IPython .core .ultratb .VerboseTB ().text (* args ))
1150+ def bwTB (): print (IPython .core .ultratb .ListTB ().text (* args )) # uncolored
1151+ try : # get to the right frame
11501152 import inspect
11511153 frame = inspect .getinnerframes (args [2 ])[- 1 ][0 ]
1152- msg = f'Entering IPython console at { frame .f_code .co_filename } at line { frame .f_lineno } \n '
1153- ipshell (msg ,local_ns = frame .f_locals ,global_ns = frame .f_globals ) # newest (IPython >= 8)
1154+ import copy
1155+ locals = frame .f_locals # add traceback commands to shell namespace
1156+ locals ['TB' ] = TB
1157+ locals ['vTB' ] = vTB
1158+ locals ['bwTB' ] = bwTB
1159+ msg = f'IPython console: { frame .f_code .co_filename } , line { frame .f_lineno } '
1160+ msg += '\n [TB(), vTB() & bwTB() for tracebacks]'
1161+ ipshell (msg ,local_ns = locals ,global_ns = frame .f_globals )
11541162 except :
1155- msg = 'Entering IPython console (no contex)'
1163+ msg = 'Entering IPython console (not in error contex)'
1164+ msg += '\n [TB(), vTB() & bwTB() for tracebacks]'
11561165 ipshell (msg )
11571166 sys .excepthook = savehook # reset IPython's change to the exception hook
11581167
0 commit comments