summaryrefslogtreecommitdiffstats
path: root/Lib/tempfile.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-12-07 16:35:50 (GMT)
committerGitHub <noreply@github.com>2023-12-07 16:35:50 (GMT)
commit6ceb8aeda504b079fef7a57b8d81472f15cdd9a5 (patch)
tree4f502dfdc3aa123a1fb403af302b2763119872e4 /Lib/tempfile.py
parent8f1c9128dd5f0976f9635cb43691d7914438903d (diff)
downloadcpython-6ceb8aeda504b079fef7a57b8d81472f15cdd9a5.zip
cpython-6ceb8aeda504b079fef7a57b8d81472f15cdd9a5.tar.gz
cpython-6ceb8aeda504b079fef7a57b8d81472f15cdd9a5.tar.bz2
[3.12] gh-91133: tempfile.TemporaryDirectory: fix symlink bug in cleanup (GH-99930) (GH-112838)
(cherry picked from commit 81c16cd94ec38d61aa478b9a452436dc3b1b524d) Co-authored-by: Søren Løvborg <sorenl@unity3d.com> 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)