This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eryksun
Recipients eryksun, lilydjwg, serhiy.storchaka, vidartf, xtreak
Date 2020-09-23.04:29:35
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1600835376.04.0.285275597974.issue35144@roundup.psfhosted.org>
In-reply-to
Content
It seems to me that if `path == name`, then resetperms(path) and possibly a recursive call are only needed on the first call. In subsequent calls, if `path == name`, then we know that resetperms(path) was already called, so it shouldn't handle PermissionError. If resetperms was ineffective (e.g. in Windows, a sharing violation or custom discretionary/mandatory permissions), or if something else changed the permissions in the mean time, just give up instead of risking a RecursionError or stack overflow. For example:


    @classmethod
    def _rmtree(cls, name, first_call=True):
        resetperms_funcs = (_os.unlink, _os.rmdir, _os.scandir, _os.open)

        def resetperms(path):
            try:
                _os.chflags(path, 0)
            except AttributeError:
                pass
            _os.chmod(path, 0o700)

        def onerror(func, path, exc_info):
            if (issubclass(exc_info[0], PermissionError) and
                  func in resetperms_funcs and (first_call or path != name)):
                try:
                    if path != name:
                        resetperms(_os.path.dirname(path))
                    resetperms(path)
                    try:
                        _os.unlink(path)
                    # PermissionError is raised on FreeBSD for directories
                    except (IsADirectoryError, PermissionError):
                        cls._rmtree(path, first_call=False)
                except FileNotFoundError:
                    pass
            elif issubclass(exc_info[0], FileNotFoundError):
                pass
            else:
                raise

        _shutil.rmtree(name, onerror=onerror)
History
Date User Action Args
2020-09-23 04:29:36eryksunsetrecipients: + eryksun, lilydjwg, serhiy.storchaka, vidartf, xtreak
2020-09-23 04:29:36eryksunsetmessageid: <1600835376.04.0.285275597974.issue35144@roundup.psfhosted.org>
2020-09-23 04:29:36eryksunlinkissue35144 messages
2020-09-23 04:29:35eryksuncreate