Skip to content

Python 3.10/3.8: shutil.rmtree(None, ignore_errors=True) behaves differently between Windows and *nix platforms #94692

Closed

Description

@RamseyK

Bug report

Expected behavior: Passing None to shutil.rmtree's path argument should not yield an exception when ignore_errors=True.

Behavior on MacOS Python 3.10 (MacPorts) - Correct behavior:

>>> import shutil
>>> shutil.rmtree(None, ignore_errors=True)
>>> shutil.rmtree(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/shutil.py", line 712, in rmtree
    onerror(os.lstat, path, sys.exc_info())
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/shutil.py", line 710, in rmtree
    orig_st = os.lstat(path)
TypeError: lstat: path should be string, bytes or os.PathLike, not NoneType

The above occurs on RedHat Linux 8 Python 3.8 and Python 3.10 as well.

Behavior on Windows (Incorrect/differs from MacOS/Linux):

>>> import shutil
>>> shutil.rmtree(None, ignore_errors=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python310\lib\shutil.py", line 733, in rmtree
    onerror(os.lstat, path, sys.exc_info())
  File "C:\Python310\lib\shutil.py", line 577, in rmtree
    orig_st = os.lstat(path)
TypeError: lstat: path should be string, bytes or os.PathLike, not NoneType
>>> shutil.rmtree(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python310\lib\shutil.py", line 733, in rmtree
    onerror(os.lstat, path, sys.exc_info())
  File "C:\Python310\lib\shutil.py", line 577, in rmtree
    orig_st = os.lstat(path)
TypeError: lstat: path should be string, bytes or os.PathLike, not NoneType

Environment

  • CPython Versions Tested: 3.8.x, 3.10.x
  • Operating Systems: Windows 10 21H2, MacOS, RedHat Linux 8 (x86-64)

Linked PRs

Activity

  1. added
    type-bug
    on Jul 8, 2022
  2. changed the title [-]shutil.rmtree(None) with ignore_errors behaves differently [/-] [+]shutil.rmtree(None) with ignore_errors behaves differently between platforms[/+] on Jul 8, 2022
  3. changed the title [-]shutil.rmtree(None) with ignore_errors behaves differently between platforms[/-] [+]shutil.rmtree(None) with ignore_errors behaves differently between Windows and *nix platforms[/+] on Jul 8, 2022
  4. changed the title [-]shutil.rmtree(None) with ignore_errors behaves differently between Windows and *nix platforms[/-] [+]shutil.rmtree(None, ignore_errors=True) behaves differently between Windows and *nix platforms[/+] on Jul 8, 2022
  5. changed the title [-]shutil.rmtree(None, ignore_errors=True) behaves differently between Windows and *nix platforms[/-] [+]Python 3.10: shutil.rmtree(None, ignore_errors=True) behaves differently between Windows and *nix platforms[/+] on Jul 8, 2022
  6. changed the title [-]Python 3.10: shutil.rmtree(None, ignore_errors=True) behaves differently between Windows and *nix platforms[/-] [+]Python 3.10/3.8: shutil.rmtree(None, ignore_errors=True) behaves differently between Windows and *nix platforms[/+] on Jul 8, 2022
  7. ericvsmith commented on Jul 8, 2022

    @ericvsmith
    Member

    Expected behavior: Passing None to shutil.rmtree's path argument should not yield an exception when ignore_errors=True.

    I disagree with this. The intent isn't to hide all errors, but rather filesystem errors.

    The only change here might be to not ignore the TypeError on macOS.

  8. hauntsaninja commented on Jul 8, 2022

    @hauntsaninja
    Contributor

    +1 to Eric. It's conceivable that these except Exception should be made into except OSError:

    cpython/Lib/shutil.py

    Lines 721 to 727 in 4bed0db

    except Exception:
    onerror(os.lstat, path, sys.exc_info())
    return
    try:
    fd = os.open(path, os.O_RDONLY, dir_fd=dir_fd)
    fd_closed = False
    except Exception:
    (as is the case in several of the rmtree helpers). It looks like those lines date pretty far back, so not clear that except Exception was intentional.

  9. ericvsmith commented on Jul 8, 2022

    @ericvsmith
    Member

    Yeah, it's unclear if maybe macOS raises some other non-OSError exception. It would take a lot of research, and more knowledge of macOS than I'll ever have. So I suspect the best we could do is document this and leave it be. Unless @RamseyK has some specific case where this is causing a real problem, I don't think we'll make a change here.

  10. eryksun commented on Jul 10, 2022

    @eryksun
    Contributor

    Functions in the os module such as os.lstat() and os.open() are supposed to raise OSError for errors due to system calls. The exact errno (or winerror) values may vary across platforms. A Python runtime error such as TypeError, ValueError (e.g. embedded null in a string), or ArithmeticError (e.g. fd value exceeds INT_MAX) is a script logic error, not a "failed removal" at the filesystem level.

  11. RamseyK commented on Jul 10, 2022

    @RamseyK
    Author

    Thanks for the clarification @ericvsmith @eryksun . I think documenting specifically that ignore_errors does not trap all exceptions as I (and others on my team) had naively assumed a different behavior of ignore_errors. Perhaps except Exception @hauntsaninja pointed out should be except OSError so the behavior is a bit more consistent across platforms.

  12. added
    stdlib
    docs
    on Nov 25, 2023
  13. added a commit that references this issue on Dec 5, 2023
  14. serhiy-storchaka commented on Dec 5, 2023

    @serhiy-storchaka
    Member

    I am wondering whether this change should be considered a bug fix (and backported) or a new feature (and only applied to main).

  15. ambv commented on Dec 5, 2023

    @ambv
    Contributor

    Since this isn't a new regression, it is properly worked around by people who noticed, and by those who didn't, it's a behavioral change. Let's treat as feature.

  16. added a commit that references this issue on Dec 5, 2023
  17. serhiy-storchaka commented on Dec 5, 2023

    @serhiy-storchaka
    Member

    Then perhaps add versionchanged and tweak the vague documentation?

  18. added 2 commits that reference this issue on Feb 11, 2024
  19. added 2 commits that reference this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsstdlibtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions