Skip to content

Commit

Permalink
Use onexc callback where supported
Browse files Browse the repository at this point in the history
The shutil.rmtree callback defined as a local function in
git.util.rmtree was already capable of being used as both the old
onerror parameter and the new onexc parameter--introduced in Python
3.12, which also deprecates onerror--because they differ only in
the meaning of their third parameter (excinfo), which the callback
defined in git.util.rmtree does not use.

This modifies git.util.rmtree to pass it as onexc on 3.12 and
later, while still passing it as onerror on 3.11 and earlier.

Because the default value of ignore_errors is False, this makes the
varying logic clearer by omitting that argument and using a keyword
argument both when passing onexc (which is keyword-only) and when
passing onerror (which is not keyword-only but can only be passed
positionally if ignore_errors is passed explicitly).
  • Loading branch information
EliahKagan committed Oct 9, 2023
1 parent ccbb273 commit 0b88012
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions git/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
# the BSD License: https://opensource.org/license/bsd-3-clause/

from abc import abstractmethod
import os.path as osp
from .compat import is_win
import contextlib
from functools import wraps
import getpass
import logging
import os
import os.path as osp
import pathlib
import platform
import subprocess
import re
import shutil
import stat
from sys import maxsize
import subprocess
import sys
import time
from urllib.parse import urlsplit, urlunsplit
import warnings

# from git.objects.util import Traversable
from .compat import is_win

# typing ---------------------------------------------------------

Expand All @@ -42,22 +42,17 @@
Tuple,
TypeVar,
Union,
cast,
TYPE_CHECKING,
cast,
overload,
)

import pathlib

if TYPE_CHECKING:
from git.remote import Remote
from git.repo.base import Repo
from git.config import GitConfigParser, SectionConstraint
from git import Git

# from git.objects.base import IndexObject


from .types import (
Literal,
SupportsIndex,
Expand All @@ -75,7 +70,6 @@

# ---------------------------------------------------------------------


from gitdb.util import ( # NOQA @IgnorePep8
make_sha,
LockedFD, # @UnusedImport
Expand All @@ -88,7 +82,6 @@
hex_to_bin, # @UnusedImport
)


# NOTE: Some of the unused imports might be used/imported by others.
# Handle once test-cases are back up and running.
# Most of these are unused here, but are for use by git-python modules so these
Expand Down Expand Up @@ -182,7 +175,7 @@ def rmtree(path: PathLike) -> None:
:note: we use shutil rmtree but adjust its behaviour to see whether files that
couldn't be deleted are read-only. Windows will not remove them in that case"""

def onerror(function: Callable, path: PathLike, _excinfo: Any) -> None:
def handler(function: Callable, path: PathLike, _excinfo: Any) -> None:
# Is the error an access error ?
os.chmod(path, stat.S_IWUSR)

Expand All @@ -195,7 +188,10 @@ def onerror(function: Callable, path: PathLike, _excinfo: Any) -> None:
raise SkipTest(f"FIXME: fails with: PermissionError\n {ex}") from ex
raise

shutil.rmtree(path, False, onerror)
if sys.version_info >= (3, 12):
shutil.rmtree(path, onexc=handler)
else:
shutil.rmtree(path, onerror=handler)


def rmfile(path: PathLike) -> None:
Expand Down Expand Up @@ -995,7 +991,7 @@ def __init__(
self,
file_path: PathLike,
check_interval_s: float = 0.3,
max_block_time_s: int = maxsize,
max_block_time_s: int = sys.maxsize,
) -> None:
"""Configure the instance
Expand Down

0 comments on commit 0b88012

Please sign in to comment.