@contextmanager
def swap_stdin_and_argv(stdin_data='', argv_data=tuple()):
 real_stdin = sys.stdin
 if PY3:
  sys.stdin = StringIO(stdin_data)
  sys.stdin.buffer = BytesIO(to_bytes(stdin_data))
 else:
  sys.stdin = BytesIO(to_bytes(stdin_data))
 real_argv = sys.argv
 sys.argv = argv_data
 (yield)
 sys.stdin = real_stdin
 sys.argv = real_argv

@ contextmanager def swap_stdin_and_argv ( stdin_data = '' , argv_data = tuple ( ) ) : real_stdin = sys . stdin if PY3 : sys . stdin = StringIO ( stdin_data ) sys . stdin . buffer = BytesIO ( to_bytes ( stdin_data ) ) else : sys . stdin = BytesIO ( to_bytes ( stdin_data ) ) real_argv = sys . argv sys . argv = argv_data ( yield ) sys . stdin = real_stdin sys . argv = real_argv
context manager that temporarily masks the test runners values for stdin and argv .