2424from pprint import pformat
2525from pathlib import Path
2626
27+ import builtins as builtin_mod
28+
2729from IPython .core import ultratb
30+ from IPython .core .application import Application
2831from IPython .core .release import author_email
2932from IPython .utils .sysinfo import sys_info
30- from IPython .utils .py3compat import input
3133
3234from IPython .core .release import __version__ as version
3335
34- from typing import Optional
36+ from typing import Optional , Dict
37+ import types
3538
3639#-----------------------------------------------------------------------------
3740# Code
8487"""
8588
8689
87- class CrashHandler ( object ) :
90+ class CrashHandler :
8891 """Customizable crash handlers for IPython applications.
8992
9093 Instances of this class provide a :meth:`__call__` method which can be
@@ -95,10 +98,11 @@ def __call__(self, etype, evalue, etb)
9598
9699 message_template = _default_message_template
97100 section_sep = '\n \n ' + '*' * 75 + '\n \n '
101+ info : Dict [str , Optional [str ]]
98102
99103 def __init__ (
100104 self ,
101- app ,
105+ app : Application ,
102106 contact_name : Optional [str ] = None ,
103107 contact_email : Optional [str ] = None ,
104108 bug_tracker : Optional [str ] = None ,
@@ -142,10 +146,14 @@ def __init__(
142146 bug_tracker = bug_tracker ,
143147 crash_report_fname = self .crash_report_fname )
144148
145-
146- def __call__ (self , etype , evalue , etb ):
149+ def __call__ (
150+ self ,
151+ etype : type [BaseException ],
152+ evalue : BaseException ,
153+ etb : types .TracebackType ,
154+ ) -> None :
147155 """Handle an exception, call for compatible with sys.excepthook"""
148-
156+
149157 # do not allow the crash handler to be called twice without reinstalling it
150158 # this prevents unlikely errors in the crash handling from entering an
151159 # infinite loop.
@@ -155,21 +163,23 @@ def __call__(self, etype, evalue, etb):
155163 color_scheme = 'NoColor'
156164
157165 # Use this ONLY for developer debugging (keep commented out for release)
158- #color_scheme = 'Linux' # dbg
159- try :
160- rptdir = self .app .ipython_dir
161- except :
166+ # color_scheme = 'Linux' # dbg
167+ ipython_dir = getattr (self .app , "ipython_dir" , None )
168+ if ipython_dir is not None :
169+ assert isinstance (ipython_dir , str )
170+ rptdir = Path (ipython_dir )
171+ else :
162172 rptdir = Path .cwd ()
163- if rptdir is None or not Path .is_dir (rptdir ):
173+ if not rptdir .is_dir ():
164174 rptdir = Path .cwd ()
165175 report_name = rptdir / self .crash_report_fname
166176 # write the report filename into the instance dict so it can get
167177 # properly expanded out in the user message template
168- self .crash_report_fname = report_name
169- self .info [' crash_report_fname' ] = report_name
178+ self .crash_report_fname = str ( report_name )
179+ self .info [" crash_report_fname" ] = str ( report_name )
170180 TBhandler = ultratb .VerboseTB (
171181 color_scheme = color_scheme ,
172- long_header = 1 ,
182+ long_header = True ,
173183 call_pdb = self .call_pdb ,
174184 )
175185 if self .call_pdb :
@@ -195,11 +205,11 @@ def __call__(self, etype, evalue, etb):
195205 print (self .message_template .format (** self .info ), file = sys .stderr )
196206
197207 # Construct report on disk
198- report .write (self .make_report (traceback ))
208+ report .write (self .make_report (str ( traceback ) ))
199209
200- input ("Hit <Enter> to quit (your terminal may close):" )
210+ builtin_mod . input ("Hit <Enter> to quit (your terminal may close):" )
201211
202- def make_report (self ,traceback ) :
212+ def make_report (self , traceback : str ) -> str :
203213 """Return a string containing a crash report."""
204214
205215 sec_sep = self .section_sep
@@ -211,8 +221,8 @@ def make_report(self,traceback):
211221 try :
212222 config = pformat (self .app .config )
213223 rpt_add (sec_sep )
214- rpt_add (' Application name: %s\n \n ' % self .app_name )
215- rpt_add (' Current user configuration structure:\n \n ' )
224+ rpt_add (" Application name: %s\n \n " % self .app . name )
225+ rpt_add (" Current user configuration structure:\n \n " )
216226 rpt_add (config )
217227 except :
218228 pass
@@ -221,7 +231,9 @@ def make_report(self,traceback):
221231 return '' .join (report )
222232
223233
224- def crash_handler_lite (etype , evalue , tb ):
234+ def crash_handler_lite (
235+ etype : type [BaseException ], evalue : BaseException , tb : types .TracebackType
236+ ) -> None :
225237 """a light excepthook, adding a small message to the usual traceback"""
226238 traceback .print_exception (etype , evalue , tb )
227239
0 commit comments