def opened_w_error(filename, mode="r"):
    """Context manager based on https://www.python.org/dev/peps/pep-0343/"""
    try:
        f = open(filename, mode)
    except EnvironmentError as err:
        yield None, err
    else:
        try:
            yield f, None
        finally:
            f.close()
Context manager based on https://www.python.org/dev/peps/pep-0343/
lisa/core/file_handling.py