summaryrefslogtreecommitdiffstats
path: root/Lib/tempfile.py
diff options
context:
space:
mode:
authorSøren Løvborg <sorenl@unity3d.com>2023-12-07 16:04:06 (GMT)
committerGitHub <noreply@github.com>2023-12-07 16:04:06 (GMT)
commit81c16cd94ec38d61aa478b9a452436dc3b1b524d (patch)
tree27c5411a6bf8067699416ecd5f6afb41f0ce7bc9 /Lib/tempfile.py
parent21221c398f6d89b2d9295895d8a2fd71d28138fa (diff)
downloadcpython-81c16cd94ec38d61aa478b9a452436dc3b1b524d.zip
cpython-81c16cd94ec38d61aa478b9a452436dc3b1b524d.tar.gz
cpython-81c16cd94ec38d61aa478b9a452436dc3b1b524d.tar.bz2
gh-91133: tempfile.TemporaryDirectory: fix symlink bug in cleanup (GH-99930)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Lib/tempfile.py')
-rw-r--r--Lib/tempfile.py27
1 files changed, 18 insertions, 9 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py
index 55403ad..9a5e7d0 100644
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -270,6 +270,22 @@ def _mkstemp_inner(dir, pre, suf, flags, output_type):
raise FileExistsError(_errno.EEXIST,
"No usable temporary file name found")
+def _dont_follow_symlinks(func, path, *args):
+ # Pass follow_symlinks=False, unless not supported on this platform.
+ if func in _os.supports_follow_symlinks:
+ func(path, *args, follow_symlinks=False)
+ elif _os.name == 'nt' or not _os.path.islink(path):
+ func(path, *args)
+
+def _resetperms(path):
+ try:
+ chflags = _os.chflags
+ except AttributeError:
+ pass
+ else:
+ _dont_follow_symlinks(chflags, path, 0)
+ _dont_follow_symlinks(_os.chmod, path, 0o700)
+
# User visible interfaces.
@@ -876,17 +892,10 @@ class TemporaryDirectory:
def _rmtree(cls, name, ignore_errors=False):
def onexc(func, path, exc):
if isinstance(exc, PermissionError):
- def resetperms(path):
- try:
- _os.chflags(path, 0)
- except AttributeError:
- pass
- _os.chmod(path, 0o700)
-
try:
if path != name:
- resetperms(_os.path.dirname(path))
- resetperms(path)
+ _resetperms(_os.path.dirname(path))
+ _resetperms(path)
try:
_os.unlink(path)