| 281 | self.handle((etype, evalue, etb)) |
| 282 | |
| 283 | def handle(self, info=None): |
| 284 | info = info or sys.exc_info() |
| 285 | if self.format == "html": |
| 286 | self.file.write(reset()) |
| 287 | |
| 288 | formatter = (self.format=="html") and html or text |
| 289 | plain = False |
| 290 | try: |
| 291 | doc = formatter(info, self.context) |
| 292 | except: # just in case something goes wrong |
| 293 | doc = ''.join(traceback.format_exception(*info)) |
| 294 | plain = True |
| 295 | |
| 296 | if self.display: |
| 297 | if plain: |
| 298 | doc = pydoc.html.escape(doc) |
| 299 | self.file.write('<pre>' + doc + '</pre>\n') |
| 300 | else: |
| 301 | self.file.write(doc + '\n') |
| 302 | else: |
| 303 | self.file.write('<p>A problem occurred in a Python script.\n') |
| 304 | |
| 305 | if self.logdir is not None: |
| 306 | suffix = ['.txt', '.html'][self.format=="html"] |
| 307 | (fd, path) = tempfile.mkstemp(suffix=suffix, dir=self.logdir) |
| 308 | |
| 309 | try: |
| 310 | with os.fdopen(fd, 'w') as file: |
| 311 | file.write(doc) |
| 312 | msg = '%s contains the description of this error.' % path |
| 313 | except: |
| 314 | msg = 'Tried to save traceback to %s, but failed.' % path |
| 315 | |
| 316 | if self.format == 'html': |
| 317 | self.file.write('<p>%s</p>\n' % msg) |
| 318 | else: |
| 319 | self.file.write(msg + '\n') |
| 320 | try: |
| 321 | self.file.flush() |
| 322 | except: pass |
| 323 | |
| 324 | handler = Hook().handle |
| 325 | def enable(display=1, logdir=None, context=5, format="html"): |