Skip to content

Commit

Permalink
os_utils.py: refactor rmtree and get rid of deprecation warning on 3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Oct 23, 2023
1 parent d0fc312 commit 3172b8c
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions pyglossary/os_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import shutil
import sys
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, Union

Expand Down Expand Up @@ -124,15 +125,22 @@ def _rmtreeError(
log.error(exc_val)


def _rmtree(direc: str) -> None:
# in Python 3.12, onexc is added and onerror is deprecated
# https://github.com/python/cpython/blob/main/Lib/shutil.py
if sys.version_info < (3, 12):
shutil.rmtree(direc, onerror=_rmtreeError)
return
shutil.rmtree(direc, onexc=_rmtreeError)


def rmtree(direc: str) -> None:
from os.path import isdir
try:
for _ in range(2):
if isdir(direc):
shutil.rmtree(
direc,
onerror=_rmtreeError,
)
if not isdir(direc):
break
_rmtree(direc)
except Exception:
log.exception(f"error removing directory: {direc}")

Expand Down

0 comments on commit 3172b8c

Please sign in to comment.