codekingpro/portable-devtools
114k
1from __future__ import annotations2 3import shutil4import sys5from typing import TYPE_CHECKING, Any, Callable6 7if TYPE_CHECKING:8 from _typeshed import ExcInfo, StrOrBytesPath9 from typing_extensions import TypeAlias10 11# Same as shutil._OnExcCallback from typeshed12_OnExcCallback: TypeAlias = Callable[[Callable[..., Any], str, BaseException], object]13 14 15def shutil_rmtree(16 path: StrOrBytesPath,17 ignore_errors: bool = False,18 onexc: _OnExcCallback | None = None,19) -> None:20 if sys.version_info >= (3, 12):21 return shutil.rmtree(path, ignore_errors, onexc=onexc)22 23 def _handler(fn: Callable[..., Any], path: str, excinfo: ExcInfo) -> None:24 if onexc:25 onexc(fn, path, excinfo[1])26 27 return shutil.rmtree(path, ignore_errors, onerror=_handler)28 