Skip to content

Instantly share code, notes, and snippets.

@bmbouter
Created July 24, 2015 13:20
Show Gist options
  • Select an option

  • Save bmbouter/8315e40a64108fbe4455 to your computer and use it in GitHub Desktop.

Select an option

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.
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