1

I am debugging a huge legacy codebase, actually porting it from python2 to python3. Unfortunately there are some exceptions that seem to be handled and printed but the previous developer was not wise enough to print a stack trace that could tell me where exactly the exception was triggered.

Specifically, I am also porting the code from GTK+2 to GTK+3 and I get many of these output lines:

TypeError: expose() missing 1 required positional argument: 'event'

At this point, it seems that the only option left is to globally hijack the print() function to at least display in which module and which line it was called.

Do you know if that's possible in python3 ? (I am a veteran of python2 but a newbie of python3)

Any other suggestions on how could I solve this problem?

fstab
  • 4,801
  • 8
  • 34
  • 66

1 Answers1

3

You could remap sys.stdout and sys.stderr to an overridden filelike object and whenever something writes to it, check to see if you're currently inside an exception frame and if you are, re-raise the exception.

import io
import sys


class Stream(io.StringIO):

    def write(*args, **kwargs):
        if sys.exc_info()[0] is not None:
            raise
        super().write(*args, **kwargs)

stream = Stream()
sys.stdout = stream
sys.stderr = stream
Brendan Abel
  • 35,343
  • 14
  • 88
  • 118