Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address deprecation warning in shutil.rmtree(onerror=...) #3079

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ jobs:
main-linux:
uses: asottile/workflows/.github/workflows/tox.yml@v1.6.0
with:
env: '["py39", "py310", "py311"]'
env: '["py39", "py310", "py311", "py312"]'
os: ubuntu-latest
46 changes: 29 additions & 17 deletions pre_commit/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,24 +202,36 @@ def cmd_output_p(
cmd_output_p = cmd_output_b


def rmtree(path: str) -> None:
"""On windows, rmtree fails for readonly dirs."""
def handle_remove_readonly(
func: Callable[..., Any],
path: str,
exc: tuple[type[OSError], OSError, TracebackType],
def _handle_readonly(
func: Callable[[str], object],
path: str,
exc: OSError,
) -> None:
if (
func in (os.rmdir, os.remove, os.unlink) and
exc.errno in {errno.EACCES, errno.EPERM}
):
for p in (path, os.path.dirname(path)):
os.chmod(p, os.stat(p).st_mode | stat.S_IWUSR)
func(path)
else:
raise


if sys.version_info < (3, 12): # pragma: <3.12 cover
def _handle_readonly_old(
func: Callable[[str], object],
path: str,
excinfo: tuple[type[OSError], OSError, TracebackType],
) -> None:
excvalue = exc[1]
if (
func in (os.rmdir, os.remove, os.unlink) and
excvalue.errno in {errno.EACCES, errno.EPERM}
):
for p in (path, os.path.dirname(path)):
os.chmod(p, os.stat(p).st_mode | stat.S_IWUSR)
func(path)
else:
raise
shutil.rmtree(path, ignore_errors=False, onerror=handle_remove_readonly)
return _handle_readonly(func, path, excinfo[1])

def rmtree(path: str) -> None:
shutil.rmtree(path, ignore_errors=False, onerror=_handle_readonly_old)
else: # pragma: >=3.12 cover
def rmtree(path: str) -> None:
"""On windows, rmtree fails for readonly dirs."""
shutil.rmtree(path, ignore_errors=False, onexc=_handle_readonly)


def win_exe(s: str) -> str:
Expand Down