In this short article I will demonstrate a nifty trick to get your python tracebacks annotated with git blame information, specifically who last modif

Git Blame in your Python Tracebacks

submited by
Style Pass
2021-06-16 00:30:07

In this short article I will demonstrate a nifty trick to get your python tracebacks annotated with git blame information, specifically who last modified each line and when. This is quite useful in the process of routing CI failures to their appropriate owner.

Ignore the code itself, or why it failed — we’re just using code from the real-easypy python package for the purpose of this demo.

Normally (that is — in CPython), when python renders a traceback, it uses an internal implementation, which we can’t really tinker with. So the first thing we must do is replace the default python exception hook with a pure-python implementation. Luckily, such a function is readily available in the built-in traceback module:

Now — it turns out that whenever the traceback module wants to show you a line of code, it uses the built-inlinecache module, kinda like this:

Let’s now do something useful with our newfound super power. Let’s see how we get git blame annotations and inject them into the linecache:

Leave a Comment