def METHOD_NAME(tmp_path):
    class CustomExc(Exception):
        pass
    f1 = tmp_path / "penguin"
    f1.write_text("Before")
    if os.name != "nt":
        os.chmod(str(f1), 0o701)
        orig_mode = stat.S_IMODE(os.stat(str(f1)).st_mode)
    f2 = tmp_path / "flamingo"
    try:
        os.symlink(str(f1), str(f2))
        have_symlink = True
    except (AttributeError, NotImplementedError, OSError):
        # AttributeError: Python doesn't support it
        # NotImplementedError: The system doesn't support it
        # OSError: The user lacks the privilege (Windows)
        have_symlink = False
    with pytest.raises(CustomExc), atomic_writing(str(f1)) as f:
        f.write("Failing write")
        raise CustomExc
    with open(str(f1)) as f:
        assert f.read() == "Before"
    with atomic_writing(str(f1)) as f:
        f.write("Overwritten")
    with open(str(f1)) as f:
        assert f.read() == "Overwritten"
    if os.name != "nt":
        mode = stat.S_IMODE(os.stat(str(f1)).st_mode)
        assert mode == orig_mode
    if have_symlink:
        # Check that writing over a file preserves a symlink
        with atomic_writing(str(f2)) as f:
            f.write("written from symlink")
        with open(str(f1)) as f:
            assert f.read() == "written from symlink"