https://github.com/python/cpython/commit/aa17c688d2c160969f2c8963f4454cdd139808e7
commit: aa17c688d2c160969f2c8963f4454cdd139808e7
branch: main
author: Petr Viktorin <[email protected]>
committer: encukou <[email protected]>
date: 2026-11-18T12:45:32Z
summary:

gh-157579: Fix TemporaryDirectory cleanup fallbacks with bytes paths (GH-162116)


Co-authored-by: Stan Ulbrych <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-11-16-16-10-42.gh-issue-157579.6choQ_.rst
M Lib/tempfile.py
M Lib/test/test_shutil.py
M Lib/test/test_tempfile.py

diff --git a/Lib/tempfile.py b/Lib/tempfile.py
index 82c1ee353117e9..53fea2873d2bae 100644
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -987,10 +987,13 @@ def __init__(self, suffix=None, prefix=None, dir=None,
     @classmethod
     def _rmtree(cls, name, ignore_errors=False, repeated=False, dir_fd=None,
                 fullname=None):
-        if fullname is None:
-            fullname = name
+
+        # work with str paths throughout
+        name = _os.fsdecode(name)
+        fullname = name if fullname is None else _os.fsdecode(fullname)
 
         def onexc(func, path, exc, direntry=None, dir_fd=None):
+            path = _os.fsdecode(path)
             # On DragonFly BSD, UF_NOUNLINK removal fails with EISDIR, not 
EPERM.
             if isinstance(exc, (PermissionError, IsADirectoryError)):
                 if repeated and path == name:
diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py
index 1104552215a44f..47221d1bf46fa1 100644
--- a/Lib/test/test_shutil.py
+++ b/Lib/test/test_shutil.py
@@ -566,6 +566,63 @@ def onexc(*args):
         self.assertTrue(onexc_called)
         self.assertFalse(onerror_called)
 
+    @unittest.skipUnless(shutil.rmtree.avoids_symlink_attacks,
+                         'requires the fd based implementation of rmtree()')
+    def test_onexc_kwargs_dir_fd(self):
+        os.mkdir(TESTFN)
+        self.addCleanup(os_helper.rmtree, TESTFN)
+        top_name = os.path.join(TESTFN, 'top')
+        child_name = os.path.join(TESTFN, 'top', 'child')
+        os.mkdir(top_name)
+        os.mkdir(child_name)
+        parent_fd = os.open(TESTFN, os.O_RDONLY)
+        self.addCleanup(os.close, parent_fd)
+
+        errors = []
+        def onexc(func, path, err, direntry=None, dir_fd=None):
+            errors.append((func, path.replace(os.path.sep, '/')))
+            name = path if direntry is None else direntry.name
+            self.assertEqual(name, os.path.basename(path))
+            self.assertTrue(os.path.samestat(
+                os.stat(os.path.join(TESTFN, path)),
+                os.stat(name, dir_fd=dir_fd)))
+
+        def failing_rmdir(path, *, dir_fd=None):
+            raise PermissionError(errno.EACCES, 'simulated')
+        with unittest.mock.patch('os.rmdir', failing_rmdir):
+            shutil.rmtree('top', dir_fd=parent_fd, onexc=onexc,
+                          _onexc_kwargs=True)
+        self.assertEqual(
+            errors,
+            [(failing_rmdir, 'top/child'), (failing_rmdir, 'top')])
+
+        child_stat = os.stat(child_name)
+        scandir = os.scandir
+
+        # Fail for the top-level directory
+        errors.clear()
+        def failing_scandir(fd):
+            raise PermissionError(errno.EACCES, 'simulated')
+        with unittest.mock.patch('os.scandir', failing_scandir):
+            shutil.rmtree('top', dir_fd=parent_fd, onexc=onexc,
+                          _onexc_kwargs=True)
+        self.assertEqual(
+            errors,
+            [(failing_scandir, 'top'), (os.rmdir, 'top')])
+        self.assertTrue(os.path.exists(top_name))
+
+        # Fail for the child (which is empty, so the rmdir succeeds)
+        errors.clear()
+        def failing_scandir_2(fd):
+            if not os.path.samestat(os.fstat(fd), child_stat):
+                return scandir(fd)
+            raise PermissionError(errno.EACCES, 'simulated')
+        with unittest.mock.patch('os.scandir', failing_scandir_2):
+            shutil.rmtree('top', dir_fd=parent_fd, onexc=onexc,
+                          _onexc_kwargs=True)
+        self.assertEqual(errors, [(failing_scandir_2, 'top/child')])
+        self.assertFalse(os.path.exists(top_name))
+
     def test_rmtree_does_not_choke_on_failing_lstat(self):
         try:
             orig_lstat = os.lstat
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py
index 2f7b8798daa8c9..3e87ccfc436d12 100644
--- a/Lib/test/test_tempfile.py
+++ b/Lib/test/test_tempfile.py
@@ -1653,10 +1653,14 @@ def __exit__(self, *exc_info):
 class TestTemporaryDirectory(BaseTestCase):
     """Test TemporaryDirectory()."""
 
-    def do_create(self, dir=None, pre="", suf="", recurse=1, dirs=1, files=1,
+    def do_create(self, dir=None, pre=None, suf=None, recurse=1, dirs=1, 
files=1,
                   ignore_cleanup_errors=False):
         if dir is None:
             dir = tempfile.gettempdir()
+        if pre is None:
+            pre = type(dir)()
+        if suf is None:
+            suf = type(dir)()
         tmp = tempfile.TemporaryDirectory(
             dir=dir, prefix=pre, suffix=suf,
             ignore_cleanup_errors=ignore_cleanup_errors)
@@ -1666,13 +1670,17 @@ def do_create(self, dir=None, pre="", suf="", 
recurse=1, dirs=1, files=1,
 
     def do_create2(self, path, recurse=1, dirs=1, files=1):
         # Create subdirectories and some files
+        def join(name):
+            if isinstance(path, bytes):
+                name = os.fsencode(name)
+            return os.path.join(path, name)
         if recurse:
             for i in range(dirs):
-                name = os.path.join(path, "dir%d" % i)
+                name = join("dir%d" % i)
                 os.mkdir(name)
                 self.do_create2(name, recurse-1, dirs, files)
         for i in range(files):
-            with open(os.path.join(path, "test%d.txt" % i), "wb") as f:
+            with open(join("test%d.txt" % i), "wb") as f:
                 f.write(b"Hello world!")
 
     def test_mkdtemp_failure(self):
@@ -1698,6 +1706,15 @@ def test_explicit_cleanup(self):
         finally:
             os.rmdir(dir)
 
+    def test_explicit_cleanup_bytes(self):
+        # A TemporaryDirectory with a bytes name is deleted when cleaned up
+        with tempfile.TemporaryDirectory() as working_dir:
+            d = self.do_create(dir=os.fsencode(working_dir), recurse=2)
+            self.assertIsInstance(d.name, bytes)
+            self.assertTrue(os.path.exists(d.name))
+            d.cleanup()
+            self.assertFalse(os.path.exists(d.name))
+
     def test_explicit_cleanup_ignore_errors(self):
         """Test that cleanup doesn't return an error when ignoring them."""
         with tempfile.TemporaryDirectory() as working_dir:
@@ -1910,6 +1927,87 @@ def hook(path, *, dir_fd=None):
             self.assertTrue(os.path.exists(target_file))
             self.assertEqual(os.stat(target_file).st_mode, target_mode)
 
+    def _make_unopenable_subdir(self):
+        # Create the following structure:
+        # work/          - top-level
+        #   <d>/         - tempdir to clean up (random name)
+        #     sub/       - an unopenable dir
+        #        subsub/
+        #          file
+        # Return:
+        # - `work` name (str)
+        # - `d` (TemporaryDirectory)
+        # - `subsub` stat result
+        work = self.enterContext(os_helper.temp_dir())
+        d = tempfile.TemporaryDirectory(dir=work)
+        sub = os.path.join(d.name, 'sub')
+        subsub = os.path.join(sub, 'subsub')
+        os.mkdir(sub)
+        os.mkdir(subsub)
+        subsub_stat = os.lstat(subsub)
+        with open(os.path.join(subsub, 'file'), 'w') as f:
+            f.write("rm me pls")
+        os.chmod(sub, 0)
+        # If the test fails, make sure everything can still be removed.
+        self.addCleanup(d.cleanup)
+        @self.addCleanup
+        def _clean():
+            if os.path.isdir(sub):
+                os.chmod(sub, 0o700)
+        return work, d, subsub_stat
+
+    @os_helper.skip_unless_working_chmod
+    @support.requires_non_root_user
+    @unittest.skipUnless(tempfile._rmtree_use_dir_fd
+                         and shutil.rmtree.avoids_symlink_attacks,
+                         'requires the fd based cleanup')
+    def test_cleanup_nested_toplevel_error(self):
+        # rmtree() reports errors relative to the correct parent directory
+        work, d, subsub_stat = self._make_unopenable_subdir()
+        # An unrelated directory with the same name; should not be touched
+        unrelated = os.path.join(work, 'sub')
+        os.mkdir(unrelated)
+
+        rmdir = os.rmdir
+        failed_path = None
+        def failing_rmdir(path, *, dir_fd=None):
+            nonlocal failed_path
+            if path == 'sub' and dir_fd is not None and failed_path is None:
+                failed_path = path
+                raise PermissionError(errno.EPERM, 'simulated', path)
+            return rmdir(path, dir_fd=dir_fd)
+        with os_helper.change_cwd(work), mock.patch('os.rmdir', failing_rmdir):
+            d.cleanup()
+        self.assertEqual(failed_path, 'sub')
+        self.assertFalse(os.path.exists(d.name))
+        self.assertTrue(os.path.exists(unrelated))
+
+    @os_helper.skip_unless_working_chmod
+    @support.requires_non_root_user
+    @unittest.skipUnless(tempfile._rmtree_use_dir_fd
+                         and shutil.rmtree.avoids_symlink_attacks,
+                         'requires the fd based cleanup')
+    def test_cleanup_nested_scandir_error(self):
+        # A scandir() error is reported relative to the parent directory.
+        work, d, subsub_stat = self._make_unopenable_subdir()
+
+        scandir = os.scandir
+        failed = False
+        def failing_scandir(path='.'):
+            nonlocal failed
+            if (
+                not failed
+                and isinstance(path, int)
+                and os.path.samestat(os.fstat(path), subsub_stat)
+            ):
+                failed = True
+                raise PermissionError(errno.EACCES, 'simulated')
+            return scandir(path)
+        with os_helper.change_cwd(work), mock.patch('os.scandir', 
failing_scandir):
+            d.cleanup()
+        self.assertTrue(failed)
+        self.assertFalse(os.path.exists(d.name))
+
     @support.cpython_only
     def test_del_on_collection(self):
         # A TemporaryDirectory is deleted when garbage collected
@@ -2068,11 +2166,17 @@ def test_context_manager(self):
             self.assertEqual(name, d.name)
         self.assertFalse(os.path.exists(name))
 
-    def test_modes(self):
+    @support.subTests('bytes_name', (False, True))
+    def test_modes(self, bytes_name):
         for mode in range(8):
             mode <<= 6
             with self.subTest(mode=format(mode, '03o')):
-                d = self.do_create(recurse=3, dirs=2, files=2)
+                if bytes_name:
+                    d = self.do_create(recurse=3, dirs=2, files=2,
+                                       dir=tempfile.gettempdirb())
+                else:
+                    d = self.do_create(recurse=3, dirs=2, files=2)
+                self.assertIsInstance(d.name, bytes if bytes_name else str)
                 with d:
                     # Change files and directories mode recursively.
                     for root, dirs, files in os.walk(d.name, topdown=False):
@@ -2083,13 +2187,17 @@ def test_modes(self):
                 self.assertFalse(os.path.exists(d.name))
 
     @support.subTests('ignore_errors', (True, False))
-    def test_parent_mode_preserved(self, ignore_errors):
+    @support.subTests('bytes_name', (False, True))
+    def test_parent_mode_preserved(self, ignore_errors, bytes_name):
         # Test that cleanup does not touch the parent directory,
         # even if that prevents removal.
         for mode in range(8):
             mode <<= 6
             with self.subTest(mode=format(mode, '03o')):
-                outer = self.do_create()
+                if bytes_name:
+                    outer = self.do_create(dir=tempfile.gettempdirb())
+                else:
+                    outer = self.do_create()
                 with outer:
                     d = self.do_create(dir=outer.name, dirs=2, files=2,
                                        ignore_cleanup_errors=ignore_errors)
diff --git 
a/Misc/NEWS.d/next/Library/2026-11-16-16-10-42.gh-issue-157579.6choQ_.rst 
b/Misc/NEWS.d/next/Library/2026-11-16-16-10-42.gh-issue-157579.6choQ_.rst
new file mode 100644
index 00000000000000..1bd89a77b5995c
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-11-16-16-10-42.gh-issue-157579.6choQ_.rst
@@ -0,0 +1,3 @@
+The error handler :class:`~tempfile.TemporaryDirectory` uses to clean up
+unreadable and inaccessible files no longer fails for temporary directories
+created using :class:`bytes` paths.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to