I have a program depending on a large code base that prints a lot of irrelevant and annoying messages. I would like to clean them up a bit, but since their content is dynamically generated, I can’t just grep for them.
Is there a way to place a hook on the print statement? (I use python 2.4, but I would be interested in results for any version). Is there another way to find from which “print” statement the output comes?
For CPython2.5 or older:
yields
For CPython2.6+ we can import the print function with
and then redirect it as we wish:
Note that
inspect.currentframeusessys._getframewhich is not part of all implementations of Python. So the solution above may only work for CPython.