Search code examples
python-3.xsentry

Recreate traceback from file


I have a stacktrace created by the faulthandler after a fatal interpreter crash. Its content looks like below:

File "/path/to/file.py", line <line-number> in <function-name>
File "/path/to/file.py", line <line-number> in <function-name>

I want to create a traceback object from this file, similar to the one from sys.exc_info() to upload it to sentry. Is there any module that will make it easier?

I will not have the scope variables, but it should be possible to capture the code object with content of the files from traceback.

For now the only solution I can think of is to create a class that will behave similar to the traceback object, but this seems like a lot of work (especially if I want the code).


Solution

  • In the end I have prepared my own class that behaves as a traceback object (using duck-typing). The only thing that was important to set valid f_code.co_filename and f_code.co_name and sentry client will extract the source code.