I've got a class which uses the context management protocol to have a silent stderr stream for a while (mainly used for py2exe deployments, where the app writing anything to stderr causes ugly dialogs when the app is closed, and I'm doing something that I know will have some stderr output)
import sys
import os
from contextlib import contextmanager
@contextmanager
def noStderr():
stderr = sys.stderr
sys.stderr = open(os.devnull, "w")
yield
sys.stderr = stderr
My question is what would be more pythonic, the reasonably clean solution of opening the system's bit bucket and writing to that, or skipping allocation of the fd and write operations, and creating a new class ala:
class nullWriter(object):
def write(self, string):
pass
and then replacing the above code with
from contextlib import contextmanager
@contextmanager
def noStderr():
stderr = sys.stderr
sys.stderr = nullWriter()
yield
sys.stderr = stderr