Open the stdout output source

I am using a third party library that prints a \n

couple of hundred times before stdout

- it seems to be a bug in their registration.

What's the best way to find out which line of code produces this output (I hope it's python and not an external library)?

Some ideas:

  • How do I change the display of the symbol \n

    to something that is recognizable, such as A

    so that if I go through the debugger I can see when the exit occurs?

  • Can I defuse the low-level function to cause some output or debug break every time characters are sent to std-out?

+3


source to share


1 answer


You can try to overtake stdout:

class StdoutFilter:
    def __init__(self, realStdout):
        self.realStdout = realStdout

    def write(self, text):
        if text == '\n':   # or some more complicated condition
            raise Exception("Newline alert!")

        self.realStdout.write(text)            

import sys
sys.stdout = StdoutFilter(sys.stdout)

# import the 3rd party library and use it

      



In this filter class, you can add a counter and throw an exception only after more than N subsequent newlines have been printed. The stack trace of the exception will reveal the source of the malicious print.

Or you can put a breakpoint there or change \n

to something else.

+1


source







All Articles