Skip to content

Commit

Permalink
Merge pull request #102 from HansBug/dev/tempfile
Browse files Browse the repository at this point in the history
dev(hansbug): add backport support for tempfile in Python3.10
  • Loading branch information
HansBug authored Oct 27, 2023
2 parents aaf9278 + bc627a5 commit 0af0bf2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
39 changes: 26 additions & 13 deletions hbutils/system/filesystem/tempfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,28 @@
import warnings
import weakref

try:
from types import GenericAlias
except (ImportError, ModuleNotFoundError):
GenericAlias = None

__all__ = [
'TemporaryDirectory',
]

_python_version_tuple = tuple(map(int, platform.python_version_tuple()))

if _python_version_tuple >= (3, 8):
if _python_version_tuple >= (3, 10):
from tempfile import TemporaryDirectory

else:
class TemporaryDirectory(object):
class TemporaryDirectory:
"""
.. note::
**This class is copied from python3.8's tempfile library.**
**This class is copied from python3.10's tempfile library.**
Because PermissionError will be raised when use native TemporaryDirectory on **Windows python3.7**.
This class should be removed when python3.7 is no longer supported.
This class should be removed when python3.9 is end of life.
See `tempfile.TemporaryDirectory <https://docs.python.org/3/library/tempfile.html#tempfile.TemporaryDirectory>`_
for more details.
Expand All @@ -40,14 +46,17 @@ class TemporaryDirectory(object):
in it are removed.
"""

def __init__(self, suffix=None, prefix=None, dir=None):
def __init__(self, suffix=None, prefix=None, dir=None,
ignore_cleanup_errors=False):
self.name = tempfile.mkdtemp(suffix, prefix, dir)
self._ignore_cleanup_errors = ignore_cleanup_errors
self._finalizer = weakref.finalize(
self, self._cleanup, self.name,
warn_message="Implicitly cleaning up {!r}".format(self))
warn_message="Implicitly cleaning up {!r}".format(self),
ignore_errors=self._ignore_cleanup_errors)

@classmethod
def _rmtree(cls, name):
def _rmtree(cls, name, ignore_errors=False):
def onerror(func, path, exc_info):
if issubclass(exc_info[0], PermissionError):
def resetperms(path):
Expand All @@ -66,19 +75,20 @@ def resetperms(path):
os.unlink(path)
# PermissionError is raised on FreeBSD for directories
except (IsADirectoryError, PermissionError):
cls._rmtree(path)
cls._rmtree(path, ignore_errors=ignore_errors)
except FileNotFoundError:
pass
elif issubclass(exc_info[0], FileNotFoundError):
pass
else:
raise
if not ignore_errors:
raise

shutil.rmtree(name, onerror=onerror)

@classmethod
def _cleanup(cls, name, warn_message):
cls._rmtree(name)
def _cleanup(cls, name, warn_message, ignore_errors=False):
cls._rmtree(name, ignore_errors=ignore_errors)
warnings.warn(warn_message, ResourceWarning)

def __repr__(self):
Expand All @@ -91,5 +101,8 @@ def __exit__(self, exc, value, tb):
self.cleanup()

def cleanup(self):
if self._finalizer.detach():
self._rmtree(self.name)
if self._finalizer.detach() or os.path.exists(self.name):
self._rmtree(self.name, ignore_errors=self._ignore_cleanup_errors)

if GenericAlias is not None:
__class_getitem__ = classmethod(GenericAlias)
2 changes: 1 addition & 1 deletion requirements-doc.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Jinja2~=3.0.0
sphinx~=3.2.0
sphinx_rtd_theme~=0.4.3
enum_tools
enum_tools~=0.9.0
sphinx-toolbox
plantumlcli>=0.0.2
packaging
Expand Down

0 comments on commit 0af0bf2

Please sign in to comment.