File: collector.py

package info (click to toggle)
python-catcher 0.1.7%2Bgit20140530-1
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 116 kB
  • ctags: 37
  • sloc: python: 257; makefile: 47
file content (41 lines) | stat: -rw-r--r-- 913 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from collections import namedtuple
import sys
import time
import inspect

Report = namedtuple('Report', ['timestamp', 'exception', 'traceback'])
Frame = namedtuple('Frame', ['file', 'line', 'code', 'locals'])


def __collect_frame(frame):
    return Frame(
        file=inspect.getfile(frame),
        line=frame.f_lineno,
        locals=frame.f_locals,
        code=inspect.getsourcelines(frame),
    )


def backup(exception):
    exception.traceback_backup = sys.exc_info()[2]


def collect(exception):
    traceback = []

    if hasattr(exception, 'traceback_backup'):
        tb = exception.traceback_backup
    else:
        exc_info = sys.exc_info()
        tb = exc_info[2]

    while tb:
        frame = tb.tb_frame
        traceback.append(__collect_frame(frame))
        tb = tb.tb_next

    return Report(
        timestamp=time.time(),
        exception=exception,
        traceback=traceback,
    )