Handle an exception, call for compatible with sys.excepthook
(self, etype, evalue, etb)
| 136 | |
| 137 | |
| 138 | def __call__(self, etype, evalue, etb): |
| 139 | """Handle an exception, call for compatible with sys.excepthook""" |
| 140 | |
| 141 | # do not allow the crash handler to be called twice without reinstalling it |
| 142 | # this prevents unlikely errors in the crash handling from entering an |
| 143 | # infinite loop. |
| 144 | sys.excepthook = sys.__excepthook__ |
| 145 | |
| 146 | # Report tracebacks shouldn't use color in general (safer for users) |
| 147 | color_scheme = 'NoColor' |
| 148 | |
| 149 | # Use this ONLY for developer debugging (keep commented out for release) |
| 150 | #color_scheme = 'Linux' # dbg |
| 151 | try: |
| 152 | rptdir = self.app.ipython_dir |
| 153 | except: |
| 154 | rptdir = os.getcwd() |
| 155 | if rptdir is None or not os.path.isdir(rptdir): |
| 156 | rptdir = os.getcwd() |
| 157 | report_name = os.path.join(rptdir,self.crash_report_fname) |
| 158 | # write the report filename into the instance dict so it can get |
| 159 | # properly expanded out in the user message template |
| 160 | self.crash_report_fname = report_name |
| 161 | self.info['crash_report_fname'] = report_name |
| 162 | TBhandler = ultratb.VerboseTB( |
| 163 | color_scheme=color_scheme, |
| 164 | long_header=1, |
| 165 | call_pdb=self.call_pdb, |
| 166 | ) |
| 167 | if self.call_pdb: |
| 168 | TBhandler(etype,evalue,etb) |
| 169 | return |
| 170 | else: |
| 171 | traceback = TBhandler.text(etype,evalue,etb,context=31) |
| 172 | |
| 173 | # print traceback to screen |
| 174 | if self.show_crash_traceback: |
| 175 | print(traceback, file=sys.stderr) |
| 176 | |
| 177 | # and generate a complete report on disk |
| 178 | try: |
| 179 | report = open(report_name,'w') |
| 180 | except: |
| 181 | print('Could not create crash report on disk.', file=sys.stderr) |
| 182 | return |
| 183 | |
| 184 | with report: |
| 185 | # Inform user on stderr of what happened |
| 186 | print('\n'+'*'*70+'\n', file=sys.stderr) |
| 187 | print(self.message_template.format(**self.info), file=sys.stderr) |
| 188 | |
| 189 | # Construct report on disk |
| 190 | report.write(self.make_report(traceback)) |
| 191 | |
| 192 | input("Hit <Enter> to quit (your terminal may close):") |
| 193 | |
| 194 | def make_report(self,traceback): |
| 195 | """Return a string containing a crash report.""" |
nothing calls this directly
no test coverage detected