import sys

class make_env(object):
    def __init__(self, argv=None, stdout=None):
        self.argv = argv
        self.stdout = stdout

    def __enter__(self):
        self.old_argv = sys.argv
        if self.argv:
            sys.argv = self.argv

        self.old_stdout = sys.stdout
        self.old_stderr = sys.stderr
        if self.stdout:
            sys.stdout = self.stdout
            sys.stderr = self.stdout

    def __exit__(self, *args):
        if self.old_argv:
            sys.argv = self.old_argv

        if self.old_stdout:
            sys.stdout = self.old_stdout
        if self.old_stderr:
            sys.stderr = self.old_stderr