@contextmanager<EOL>def _open(filename=None, mode='<STR_LIT:r>'):
if not filename or filename == '<STR_LIT:->':<EOL><INDENT>if not mode or '<STR_LIT:r>' in mode:<EOL><INDENT>file = sys.stdin<EOL><DEDENT>elif '<STR_LIT:w>' in mode:<EOL><INDENT>file = sys.stdout<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(mode))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>file = open(filename, mode)<EOL><DEDENT>try:<EOL><INDENT>yield file<EOL><DEDENT>finally:<EOL><INDENT>if file not in (sys.stdin, sys.stdout):<EOL><INDENT>file.close()<EOL><DEDENT><DEDENT>
Open a file or ``sys.stdout`` depending on the provided filename.

    Args:
        filename (str): The path to the file that should be opened. If
            ``None`` or ``'-'``, ``sys.stdout`` or ``sys.stdin`` is
            returned depending on the desired mode. Defaults to ``None``.
        mode (str): The mode that should be used to open the file.

    Yields:
        A file handle.