Created
July 24, 2015 13:20
-
-
Save bmbouter/8315e40a64108fbe4455 to your computer and use it in GitHub Desktop.
monkey patch stderr and stdout to show call stack when a specific print statement occurs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| class fake_out: | |
| def __init__(self, stream): | |
| self._stream = stream | |
| for name in dir(stream): | |
| if name != "write" and not name.startswith("_"): | |
| setattr(self, name, getattr(stream, name)) | |
| def write(self, s): | |
| self._stream.write(s) | |
| if "enter your password" in s.lower(): | |
| raise ValueError("woah %r" % s) | |
| sys.stdout = fake_out(sys.stdout) | |
| sys.stdout = fake_out(sys.stderr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment